compile if expressions into conditional branches
This commit is contained in:
parent
58827f230d
commit
22b0ce775d
|
@ -5,7 +5,7 @@ let[@warning "-26"] () =
|
|||
Logs.set_level (Some Logs.Debug);
|
||||
|
||||
try
|
||||
let ast = parse "val ret = 1 + 1 == 2" in
|
||||
let ast = parse "val x = 3 val y = 5 val output = 2 * (if(9 < 8) x else 1 + y)" in
|
||||
let prog = compile ast in
|
||||
let ret = run prog in
|
||||
Fmt.pr "{\"program\":%a,\"output\":%a}" Code.pp_program prog Value.pp ret
|
||||
|
|
|
@ -50,6 +50,23 @@ let compile modl =
|
|||
emit (NOT ret)
|
||||
| _ -> Fmt.failwith "Bcc.compile_exp: TODO: %S" (Ast.string_of_binop op));
|
||||
Reg ret
|
||||
| Ast.If (cnd, e1, e2) ->
|
||||
let l1 = Code.make_basic_block [] in
|
||||
let l2 = Code.make_basic_block [] in
|
||||
let jp = Code.make_basic_block [] in
|
||||
let tmp = !sp in
|
||||
emit (CBR (compile_exp env cnd, l1, l2));
|
||||
sp := tmp;
|
||||
bb := l1;
|
||||
emit_mov tmp (compile_exp env e1);
|
||||
emit (JMP jp);
|
||||
sp := tmp;
|
||||
bb := l2;
|
||||
emit_mov tmp (compile_exp env e2);
|
||||
emit (JMP jp);
|
||||
sp := tmp + 1;
|
||||
bb := jp;
|
||||
Reg tmp
|
||||
| Ast.Obj body -> compile_obj env body
|
||||
| _ -> failwith "Bcc.compile_exp: TODO"
|
||||
and compile_obj env items =
|
||||
|
|
Loading…
Reference in New Issue