From fcc13c9d64a8663ad0a2b640c6f7d5f4b6c5417a Mon Sep 17 00:00:00 2001 From: Garrit Franke Date: Wed, 16 Dec 2020 19:52:08 +0100 Subject: [PATCH] Test conditionals in C backend --- a.out | Bin 0 -> 49464 bytes builtin/builtin.c | 3 ++- examples/playground.sb | 8 ++++++++ src/generator/c.rs | 4 ++-- src/parser/tests.rs | 15 +++++++++++++++ 5 files changed, 27 insertions(+), 3 deletions(-) create mode 100755 a.out diff --git a/a.out b/a.out new file mode 100755 index 0000000000000000000000000000000000000000..6d37925fd2f17959207057f48d752308cf7ab635 GIT binary patch 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 void _printf(char *msg) { printf(msg); } -/* END builtins */ \ No newline at end of file +/* END builtins */ diff --git a/examples/playground.sb b/examples/playground.sb index 584e33e..b88ca71 100644 --- a/examples/playground.sb +++ b/examples/playground.sb @@ -1,3 +1,11 @@ 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") + } } \ No newline at end of file diff --git a/src/generator/c.rs b/src/generator/c.rs index 04ea85c..9f846da 100644 --- a/src/generator/c.rs +++ b/src/generator/c.rs @@ -228,14 +228,14 @@ fn generate_bin_op(left: Expression, op: BinOp, right: Expression) -> String { BinOp::Addition => "+", BinOp::And => "&&", BinOp::Division => "/", - BinOp::Equal => "===", + BinOp::Equal => "==", BinOp::GreaterThan => ">", BinOp::GreaterThanOrEqual => ">=", BinOp::LessThan => "<", BinOp::LessThanOrEqual => "<=", BinOp::Modulus => "%", BinOp::Multiplication => "*", - BinOp::NotEqual => "!==", + BinOp::NotEqual => "!=", BinOp::Or => "||", BinOp::Subtraction => "-", }; diff --git a/src/parser/tests.rs b/src/parser/tests.rs index 46b31ed..60e6713 100644 --- a/src/parser/tests.rs +++ b/src/parser/tests.rs @@ -605,3 +605,18 @@ fn test_function_with_return_type() { assert!(tree.is_ok()); assert_eq!(tree.unwrap().func[0].ret_type, Some(Type::Int)); } + +#[test] +#[ignore] +fn test_booleans_in_function_call() { + let raw = " + if n > 2 { + _printf(true) + } else { + _printf(true) + } + "; + let tokens = tokenize(raw); + let tree = parse(tokens, Some(raw.to_string())); + assert!(tree.is_ok()); +}