Browse Source

Test conditionals in C backend

github-actions
Garrit Franke 3 years ago
parent
commit
fcc13c9d64
  1. BIN
      a.out
  2. 1
      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.

1
builtin/builtin.c

@ -1,5 +1,6 @@
/* START builtins */
#include "stdio.h"
#include <stdbool.h>
void _printf(char *msg)
{

8
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")
}
}

4
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 => "-",
};

15
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());
}

Loading…
Cancel
Save