From 949b4dc449bcb2f14e72fd819491dbb5e07ee840 Mon Sep 17 00:00:00 2001 From: Garrit Franke Date: Thu, 17 Dec 2020 14:44:02 +0100 Subject: [PATCH] Generate function prototypes --- a.out | Bin 49464 -> 0 bytes builtin/builtin.c | 2 +- examples/fib.sb | 2 +- examples/playground.sb | 13 +++---------- src/generator/c.rs | 19 ++++++++++++++----- src/parser/node_type.rs | 8 ++++---- 6 files changed, 23 insertions(+), 21 deletions(-) delete mode 100755 a.out diff --git a/a.out b/a.out deleted file mode 100755 index 6d37925fd2f17959207057f48d752308cf7ab635..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 49464 zcmeI*&ubiI7{Kv&w}~!IY&I9Ezv!w&RBTg=lpbu|XoDLJ{b5oq1$o`=PO}S>O_-V0 z1YB8+QG!G&qV&%Yy?9)(2%fz5*h?h^d+Di2F+R`CJL&GEIrrfAfp^|{-uIn%-ucXI zZg2bNKX?9p+?d=xV@zI3QOd<(W7bVc&Ww3VN=1sb=ceABzBK**S#2GR++h09Oy{{F z1FfB(zBoTPB)+~p*k)WVOEGED5!OcDN~C8=WaqniN*+eKzI6SX54rQW8ZzDf)Y>p= z*TS8q?0ogNlKCEUBhoy*py1|9`;+->!}FKDKn7*!^WA(;y9qQ`_np|azRO`0G+PU9 zKz6>b+DA+irc@Y}IYEb!92e2j9o9Za#hI8k%S4NP3c^zSnC~tUWz7 zKV{!JduA?qyXC|!eUI5%zc(%VuJoI;c44XSweC4zLB_^WPOf*EQUCZmat*q!Eh)*m zT%%Uq%(^6xqnuRfy3%L1*00)db+NYO+vOGz*M`2P6(7k9tQJ<%CCcud+#Qj%wsRpmzF^VqUb&A%#r`rW1R zvOItMyqVvR-uHwQ?bG$=-W-)bN4W=Uk@d{2hLN{;@qB-&^x;zA*Pq{SW~;%17nGt# zwN*N{-^_%1ob|$RrzzRj`mA0r(O>HxRo(OchSXX-x1Uwdb?@t_>|HIoU9plT+hgT~ zKmY**5I_I{1Q0*~0R#|0009ILKmY**5I_I{1Q0*~0R#|0009ILKmY**5I_I{1Q0*~ z0R#|0009ILKmY**5I_I{1Q0*~0R#|00D(s*@R|Gn-XGlxxHAF>Abep=;wQIK4Y}LKMoDa-Z6H1R=4w|j#iewTwYlcnU zn&M)$*)qpXez9S_74LF8@@&Ja>V=2h_}B9?exJG7kwf=$z1T=nitAHyJWw#E(3L6W zT!)V3igHZyC*xek%Rucv 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,