Skip to content

Commit

Permalink
Add some tests. Improve Emacs b mode
Browse files Browse the repository at this point in the history
  • Loading branch information
elimirks committed Dec 31, 2021
1 parent 9708e90 commit 182d286
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
3 changes: 2 additions & 1 deletion plugins/b-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

(define-generic-mode 'b-mode
'(("/*" . "*/"))
'("return" "auto" "extrn" "eof" "while" "if" "else" "goto" "switch" "break" "case" "default")
'("return" "auto" "extrn" "eof" "while" "if" "else" "goto" "switch" "break"
"case" "default" "#import")
'(("\\b[0-9]+\\b" . font-lock-constant-face))
'("\\.b$")
'() ;; TODO: Highlight string & char literals here
Expand Down
1 change: 1 addition & 0 deletions run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/sh

# TODO: Replace this with a B program
set -e

for f in test/*; do
cargo -q run --release -- -r $f
Expand Down
40 changes: 32 additions & 8 deletions src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,7 @@ fn gen_statement(
Statement::Break(pos) => {
match c.break_dest_stack.last() {
Some(label) => {
instructions.push(format!("# break"));
instructions.push(format!("jmp {}", label));
Ok(())
},
Expand All @@ -1123,6 +1124,7 @@ fn gen_statement(
Statement::Goto(pos, name) => {
match c.labels.get(name) {
Some(label) => {
instructions.push(format!("# goto {}", label));
instructions.push(format!("jmp {}", label));
Ok(())
},
Expand All @@ -1137,10 +1139,14 @@ fn gen_statement(
Ok(())
},
Statement::Return => {
instructions.push(format!("# return"));
gen_return(instructions);
Ok(())
},
Statement::ReturnExpr(expr) => gen_return_expr(c, instructions, expr),
Statement::ReturnExpr(expr) => {
instructions.push(format!("# return"));
gen_return_expr(c, instructions, expr)
},
Statement::Block(statements) => {
c.new_scope();
for statement in statements {
Expand All @@ -1149,17 +1155,35 @@ fn gen_statement(
c.drop_scope();
Ok(())
},
Statement::Auto(pos, vars) => gen_auto(c, instructions, pos, vars),
Statement::Extern(pos, vars) => gen_extern(c, pos, vars),
Statement::Auto(pos, vars) => {
instructions.push(format!("# auto"));
gen_auto(c, instructions, pos, vars)
},
Statement::Extern(pos, vars) => {
instructions.push(format!("# extrn"));
gen_extern(c, pos, vars)
},
Statement::Expr(expr) => {
instructions.push(format!("# Expression statement"));
gen_expr(c, instructions, expr)?;
Ok(())
},
Statement::If(cond, if_body, None) => gen_if(c, instructions, cond, if_body),
Statement::While(cond, body) => gen_while(c, instructions, cond, body),
Statement::If(cond, if_body, Some(else_body)) =>
gen_if_else(c, instructions, cond, if_body, else_body),
Statement::Switch(cond, body) => gen_switch(c, instructions, cond, body),
Statement::If(cond, if_body, None) => {
instructions.push(format!("# if"));
gen_if(c, instructions, cond, if_body)
},
Statement::If(cond, if_body, Some(else_body)) => {
instructions.push(format!("# if"));
gen_if_else(c, instructions, cond, if_body, else_body)
},
Statement::While(cond, body) => {
instructions.push(format!("# while"));
gen_while(c, instructions, cond, body)
},
Statement::Switch(cond, body) => {
instructions.push(format!("# switch"));
gen_switch(c, instructions, cond, body)
},
}
}

Expand Down
18 changes: 18 additions & 0 deletions test/call.b
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#import "../assets/best.b";

main() {
foo(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
}

foo(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
assert_eq_int(0, a0);
assert_eq_int(1, a1);
assert_eq_int(2, a2);
assert_eq_int(3, a3);
assert_eq_int(4, a4);
assert_eq_int(5, a5);
assert_eq_int(6, a6);
assert_eq_int(7, a7);
assert_eq_int(8, a8);
assert_eq_int(9, a9);
}

0 comments on commit 182d286

Please sign in to comment.