diff --git a/a.out b/a.out deleted file mode 100755 index 6d37925..0000000 Binary files a/a.out and /dev/null differ diff --git a/builtin/builtin.c b/builtin/builtin.c index 8557fb2..bd882e5 100644 --- a/builtin/builtin.c +++ b/builtin/builtin.c @@ -4,7 +4,7 @@ void _printf(char *msg) { - printf(msg); + printf("%s", msg); } /* END builtins */ diff --git a/examples/fib.sb b/examples/fib.sb index 9a28d27..7b48c54 100644 --- a/examples/fib.sb +++ b/examples/fib.sb @@ -1,5 +1,5 @@ fn main() { - let num = 10 + let num: int = 10 println(fib(num)) } diff --git a/examples/playground.sb b/examples/playground.sb index b88ca71..caabd24 100644 --- a/examples/playground.sb +++ b/examples/playground.sb @@ -1,11 +1,4 @@ -fn main(n: int) { - _printf("Hello World!\n") - - if n > 2 { - _printf("Larger than 2\n") - } else if n == 2 { - _printf("Is 2\n") - } else { - _printf("Less than 2\n") - } +fn main() { + let x: string = "Hello World\n" + _printf(x) } \ No newline at end of file diff --git a/src/generator/c.rs b/src/generator/c.rs index 9f846da..e5417d2 100644 --- a/src/generator/c.rs +++ b/src/generator/c.rs @@ -27,6 +27,11 @@ impl Generator for CGenerator { crate::Builtins::get("builtin.c").expect("Could not locate builtin functions"); code += std::str::from_utf8(raw_builtins.as_ref()) .expect("Unable to interpret builtin functions"); + + for func in &prog.func { + code += &format!("{};\n", &generate_function_signature(func.clone())); + } + let funcs: String = prog .func .into_iter() @@ -63,6 +68,14 @@ pub(super) fn generate_type(t: Either>) -> String { } fn generate_function(func: Function) -> String { + let mut buf = String::new(); + buf += &format!("{} ", &generate_function_signature(func.clone())); + buf += &generate_block(func.body); + + buf +} + +fn generate_function_signature(func: Function) -> String { let arguments: String = func .arguments .into_iter() @@ -70,11 +83,7 @@ fn generate_function(func: Function) -> String { .collect::>() .join(", "); let t = generate_type(Either::Right(func.ret_type)); - let mut raw = format!("{T} {N}({A})", T = t, N = func.name, A = arguments); - - raw += &generate_block(func.body); - - raw + format!("{T} {N}({A})", T = t, N = func.name, A = arguments) } fn generate_block(block: Statement) -> String { diff --git a/src/parser/node_type.rs b/src/parser/node_type.rs index 4548ad2..ac0855e 100644 --- a/src/parser/node_type.rs +++ b/src/parser/node_type.rs @@ -29,7 +29,7 @@ impl Program { } } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Function { pub name: String, pub arguments: Vec, @@ -63,7 +63,7 @@ impl TryFrom for Type { } } -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, Clone)] pub enum Statement { Block(Vec), Declare(Variable, Option), @@ -74,7 +74,7 @@ pub enum Statement { Exp(Expression), } -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, Clone)] pub enum Expression { Int(u32), Str(String), @@ -111,7 +111,7 @@ impl TryFrom for Expression { } } -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, Clone)] pub enum BinOp { Addition, Subtraction,