From e28f48f511289808c0c73edd5eef9529be209da6 Mon Sep 17 00:00:00 2001 From: Mutsuha Asada Date: Tue, 13 Aug 2024 15:13:23 +0900 Subject: [PATCH] change: codegen with type data --- app/app.ml | 4 ++-- compiler/codegen.ml | 7 +++++-- test/compiler/e2e.ml | 30 +++++++++++++++++------------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/app/app.ml b/app/app.ml index b4e2190..a050f80 100644 --- a/app/app.ml +++ b/app/app.ml @@ -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; diff --git a/compiler/codegen.ml b/compiler/codegen.ml index 509d844..b325ee1 100644 --- a/compiler/codegen.ml +++ b/compiler/codegen.ml @@ -1,5 +1,6 @@ open Ast open Builtin +open Inferer open Runtime.Instructions open Runtime.Modules open Runtime.Wasi @@ -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 @@ -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 = []; } diff --git a/test/compiler/e2e.ml b/test/compiler/e2e.ml index 2820e3b..94e7c42 100644 --- a/test/compiler/e2e.ml +++ b/test/compiler/e2e.ml @@ -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" @@ -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 :: [])" @@ -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"; ] ); ]