Skip to content

Commit

Permalink
change: codegen with type data
Browse files Browse the repository at this point in the history
  • Loading branch information
momeemt committed Aug 13, 2024
1 parent 7343d83 commit e28f48f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
4 changes: 2 additions & 2 deletions app/app.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ let () =
close_in in_channel;
let tokens = tokenize source_code in
let ast = parse tokens in
let _ = tinf ast in
let wat = codegen ast in
let (te, _, _, _) = tinf ast in
let wat = codegen ast te in
let out_channel = open_out wat_file in
output_string out_channel wat;
close_out out_channel;
Expand Down
7 changes: 5 additions & 2 deletions compiler/codegen.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
open Ast
open Builtin
open Inferer
open Runtime.Instructions
open Runtime.Modules
open Runtime.Wasi
Expand All @@ -11,7 +12,7 @@ module Env = Map.Make (String)

type identifierKind = Func | Arg

let codegen ast =
let codegen ast te =
let rec aux func_name funcs env expr addr =
let aux_if cond then_ else_ addr =
let cond_funcs, addr = aux func_name funcs env cond addr in
Expand Down Expand Up @@ -42,7 +43,9 @@ let codegen ast =
{
name = rand_name;
params = List.map (fun param -> (Some param, I32)) params;
results = [ I32 ];
results =
(let ty = lookup name te in
match ty with TUnit -> [] | _ -> [ I32 ]);
body = [];
locals = [];
}
Expand Down
30 changes: 17 additions & 13 deletions test/compiler/e2e.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ let rec find_project_root current_dir =
let exec_code code test_name =
let tokens = tokenize code in
let ast = parse tokens in
let wat = codegen ast in
let _ = tinf ast in
let te, _, _, _ = tinf ast in
let wat = codegen ast te in
let filename =
find_project_root (Sys.getcwd ())
^ "/test/compiler/tmp/" ^ test_name ^ ".wat"
Expand Down Expand Up @@ -110,24 +110,28 @@ let () =
( "sequence_2",
[
test_case_str "sequence_2"
"print_int32 (if 1 = 2 then 3 else 4); \
print_string \", \"; \
print_int32 (let x = 10 in x + 20); \
print_string \", \"; \
"print_int32 (if 1 = 2 then 3 else 4); print_string \", \"; \
print_int32 (let x = 10 in x + 20); print_string \", \"; \
print_int32 (3 * 8)"
"4, 30, 24\n";
] );
("list_1", [ test_case_str "list_1" "print_list [1 2 3 4 5]" "[1, 2, 3, 4, 5]\n" ]);
( "list_1",
[ test_case_str "list_1" "print_list [1 2 3 4 5]" "[1, 2, 3, 4, 5]\n" ]
);
( "list_length_1",
[ test_case "list_length_1" "print_int32 (list_length [10 20 30])" 3 ]
);
( "list_length_2",
[ test_case "list_length_2" "print_int32 (list_length [])" 0 ] );
( "list_cons_1",
[ test_case_str "list_cons_1" "print_list (1 :: [2 3 4 5])" "[1, 2, 3, 4, 5]\n" ] );
[
test_case_str "list_cons_1" "print_list (1 :: [2 3 4 5])"
"[1, 2, 3, 4, 5]\n";
] );
( "list_cons_2",
[ test_case_str "list_cons_2" "print_list (1 :: 2 :: [3])" "[1, 2, 3]\n" ]
);
[
test_case_str "list_cons_2" "print_list (1 :: 2 :: [3])" "[1, 2, 3]\n";
] );
( "list_cons_3",
[
test_case_str "list_cons_3" "print_list (1 :: 2 :: 3 :: [])"
Expand All @@ -141,8 +145,8 @@ let () =
( "string_2",
[
test_case_str "string_2"
"print_string \"# Todo\n\"; print_string \"- File I/O\n\"; \
print_string \"- ref\""
"# Todo\n- File I/O\n- ref\n";
"print_string \"# Todo\n\
\"; print_string \"- File I/O\n\
\"; print_string \"- ref\"" "# Todo\n- File I/O\n- ref\n";
] );
]

0 comments on commit e28f48f

Please sign in to comment.