Browse Source

Test conditionals in C backend

github-actions
Garrit Franke 3 years ago
parent
commit
fcc13c9d64
  1. BIN
      a.out
  2. 3
      builtin/builtin.c
  3. 8
      examples/playground.sb
  4. 4
      src/generator/c.rs
  5. 15
      src/parser/tests.rs

BIN
a.out

Binary file not shown.

3
builtin/builtin.c

@ -1,9 +1,10 @@
/* START builtins */ /* START builtins */
#include "stdio.h" #include "stdio.h"
#include <stdbool.h>
void _printf(char *msg) void _printf(char *msg)
{ {
printf(msg); printf(msg);
} }
/* END builtins */ /* END builtins */

8
examples/playground.sb

@ -1,3 +1,11 @@
fn main(n: int) { fn main(n: int) {
_printf("Hello World!\n") _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")
}
} }

4
src/generator/c.rs

@ -228,14 +228,14 @@ fn generate_bin_op(left: Expression, op: BinOp, right: Expression) -> String {
BinOp::Addition => "+", BinOp::Addition => "+",
BinOp::And => "&&", BinOp::And => "&&",
BinOp::Division => "/", BinOp::Division => "/",
BinOp::Equal => "===", BinOp::Equal => "==",
BinOp::GreaterThan => ">", BinOp::GreaterThan => ">",
BinOp::GreaterThanOrEqual => ">=", BinOp::GreaterThanOrEqual => ">=",
BinOp::LessThan => "<", BinOp::LessThan => "<",
BinOp::LessThanOrEqual => "<=", BinOp::LessThanOrEqual => "<=",
BinOp::Modulus => "%", BinOp::Modulus => "%",
BinOp::Multiplication => "*", BinOp::Multiplication => "*",
BinOp::NotEqual => "!==", BinOp::NotEqual => "!=",
BinOp::Or => "||", BinOp::Or => "||",
BinOp::Subtraction => "-", BinOp::Subtraction => "-",
}; };

15
src/parser/tests.rs

@ -605,3 +605,18 @@ fn test_function_with_return_type() {
assert!(tree.is_ok()); assert!(tree.is_ok());
assert_eq!(tree.unwrap().func[0].ret_type, Some(Type::Int)); 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());
}

Loading…
Cancel
Save