Skip to content

Commit

Permalink
If and boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Jun 18, 2024
1 parent a671c3e commit 7fb49e0
Show file tree
Hide file tree
Showing 23 changed files with 409 additions and 179 deletions.
60 changes: 49 additions & 11 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use std::fs;
use std::path::PathBuf;

fn main() {
if PathBuf::from("types.toml").is_file() {
fs::remove_file(PathBuf::from("types.toml")).unwrap();
}
}
98 changes: 45 additions & 53 deletions build/out.asm
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,54 @@ section .text
main:
push rbp
mov rbp, rsp
mov qword [rbp-8], 0
mov qword [rbp-16], 1
mov rax, qword [rbp-16]
mov qword [rbp-40], rax
mov qword [rbp-8], 3
mov qword [rbp-16], 4
mov byte [rbp-17], 1
mov al, byte [rbp-17]
mov byte [rbp-18], al
mov rdi, __6_fstr
mov rsi, 0
mov sil, [rbp-18]
mov al, 0
sub rsp, 18
extern printf
call printf
add rsp, 18
mov al, byte [rbp-17]
mov byte [rbp-19], al
cmp byte [rbp-19], 0
jz main_0
mov rax, qword [rbp-8]
mov qword [rbp-48], rax
sub rsp, 48
call _1
add rsp, 48
mov qword [rbp-40], 255
mov rax, qword [rbp-40]
leave
ret

_1:
push rbp
mov rbp, rsp
mov rax, qword [rbp+16]
mov qword [rbp-8], rax

mov rdi, __10_fstr
mov rsi, [rbp-8]
mov qword [rbp-27], rax
mov rdi, __4_fstr
mov rsi, [rbp-27]
mov al, 0
sub rsp, 8
sub rsp, 27
extern printf
call printf
add rsp, 8
mov rax, qword [rbp+16]
mov qword [rbp-24], rax
mov rax, qword [rbp+24]
mov qword [rbp-32], rax
mov rax, qword [rbp-24]
add rax, qword [rbp-32]
mov qword [rbp-16], rax
mov rax, qword [rbp+24]
mov qword [rbp-40], rax
mov qword [rbp-48], 12
mov rax, 60
mov rdi, [rbp-48]
syscall
mov rax, qword [rbp-40]
mov qword [rbp-56], rax
add rsp, 27
mov qword [rbp-35], 13
mov rax, qword [rbp-35]
leave
ret
jmp main_1
main_0:
mov rax, qword [rbp-16]
mov qword [rbp-64], rax
mov rax, qword [rbp-64]
mov qword [rbp-88], rax
mov rax, qword [rbp-56]
mov qword [rbp-96], rax
sub rsp, 96
call _1
add rsp, 96
leave
ret
mov qword [rbp-27], rax
mov rdi, __4_fstr
mov rsi, [rbp-27]
mov al, 0
sub rsp, 27
extern printf
call printf
add rsp, 27
mov qword [rbp-35], 12
mov rax, qword [rbp-35]
leave
ret
main_1:


section .data
__10_fstr db `Integer: %d\n`,0
section .data_readonly
__6_fstr db `Boolean: %d\n`,0
__4_fstr db `Integer: %d\n`,0
Binary file modified build/out.o
Binary file not shown.
Binary file modified build/out.out
Binary file not shown.
23 changes: 13 additions & 10 deletions main.why
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
fn fib(a: int, b: int) {
printi(a);
let d: int = a + b;
let c: int = b;
exit(12);
fib(c, d);
}

fn main() -> int {
fib(0, 1);
return 255;
let i: int = 3;
let j: int = 4;
let x: bool = true;
printb(x);

if (x) {
printi(i);
return 13;
}
else {
printi(j);
return 12;
}
}
29 changes: 29 additions & 0 deletions src/root/assembler/assembly_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

pub struct AssemblyBuilder {
inner: String
}

impl AssemblyBuilder {
pub fn new() -> AssemblyBuilder {
AssemblyBuilder { inner: String::new() }
}

pub fn line(&mut self, line: &str) {
self.inner += " ";
self.inner += line;
self.inner.push('\n');
}

pub fn toplevel(&mut self, line: &str) {
self.inner += line;
self.inner.push('\n');
}

pub fn other(&mut self, other: &str) {
self.inner += other;
}

pub fn finish(self) -> String {
self.inner
}
}
1 change: 1 addition & 0 deletions src/root/assembler/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod assembly_builder;
2 changes: 2 additions & 0 deletions src/root/builtin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod types;
pub mod functions;

use crate::root::builtin::functions::register_functions;
use crate::root::builtin::types::bool::register_bool;
use crate::root::builtin::types::int::register_int;
use crate::root::compiler::global_tracker::GlobalTracker;
use crate::root::errors::WErr;
Expand All @@ -13,6 +14,7 @@ use crate::root::shared::types::Type;
pub fn register_builtin(global_table: &mut GlobalDefinitionTable) {
register_functions(global_table);
register_int(global_table);
register_bool(global_table);
}

pub type InlineFunctionGenerator = fn(&[LocalAddress], Option<LocalAddress>, &mut GlobalTracker, ByteSize) -> String;
Expand Down
59 changes: 59 additions & 0 deletions src/root/builtin/types/bool/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
mod printb;

use b_box::b;
use unique_type_id::UniqueTypeId;
use crate::root::builtin::t_id;
use crate::root::builtin::types::bool::printb::PrintB;
use crate::root::errors::WErr;
use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable;
use crate::root::parser::parse_function::parse_literal::{LiteralToken, LiteralTokens};
use crate::root::shared::common::{ByteSize, LocalAddress, TypeID};
use crate::root::shared::types::Type;

pub fn register_bool(global_table: &mut GlobalDefinitionTable) {
global_table.register_builtin_type(b!(BoolType));
global_table.register_inline_function(&PrintB);
}

#[derive(UniqueTypeId)]
#[UniqueTypeIdType = "u16"]
pub struct BoolType;

impl BoolType {
pub const fn id() -> TypeID {
t_id(BoolType::unique_type_id().0)
}
}

impl Type for BoolType {
fn id(&self) -> TypeID { Self::id() }

fn size(&self) -> ByteSize {
ByteSize(1)
}

fn name(&self) -> &str {
"bool"
}

fn instantiate_from_literal(&self, location: &LocalAddress, literal: &LiteralToken) -> Result<String, WErr> {
Ok(match literal.literal() {
LiteralTokens::Bool(value) => {
if *value {
format!(" mov byte {location}, 1\n")
}
else {
format!(" mov byte {location}, 0\n")
}
}
LiteralTokens::Int(value) => {
if *value == 0 {
format!(" mov byte {location}, 0\n")
}
else {
format!(" mov byte {location}, 1\n")
}
}
})
}
}
Loading

0 comments on commit 7fb49e0

Please sign in to comment.