-
Notifications
You must be signed in to change notification settings - Fork 0
/
toplevel.ml
83 lines (72 loc) · 3.17 KB
/
toplevel.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(*
toplevel.ml
@authors Aidan Barg
Abby Larson
Claudia Aranda Barrios
Steven Oh
*)
type action = Ast | Sast | LLVM_IR | Compile
let () =
let action = ref Compile in
let set_action a () = action := a in
let speclist = [
("-a", Arg.Unit (set_action Ast), "Print the AST");
("-s", Arg.Unit (set_action Sast), "Print the SAST");
("-l", Arg.Unit (set_action LLVM_IR), "Print the generated LLVM IR");
("-c", Arg.Unit (set_action Compile),
"Check and print the generated LLVM IR (default)");
] in
let usage_msg = "usage: ./toplevel.native [-a|-s|-l|-c] [file.gp]" in
let channel = ref stdin in
Arg.parse speclist (fun filename -> channel := open_in filename) usage_msg;
let lexbuf = Lexing.from_channel !channel in
let ast = Parser.program Scanner.token lexbuf in
match !action with
Ast -> print_string (Ast.string_of_program ast)
| _ -> let sast = Semant.check ast in
match !action with
Ast -> ()
| Sast -> print_string (Sast.string_of_sprogram sast)
| LLVM_IR -> print_string (Llvm.string_of_llmodule (Codegen.translate sast))
| Compile -> let m = Codegen.translate sast in
Llvm_analysis.assert_valid_module m;
print_string (Llvm.string_of_llmodule m)
(* open Ast
let () =
let lex_buf = Lexing.from_channel stdin in
let ast = Parser.program Scanner.token lex_buf in
(*** print_string (Ast.string_of_program parsed_expr) ***)
let sast = Semant.check ast in
print_string (Llvm.string_of_llmodule (Codegen.translate sast))
(* print_string (Sast.string_of_program sast) *)
(*** code from MicroC for reference
(* Top-level of the MicroC compiler: scan & parse the input,
check the resulting AST and generate an SAST from it, generate LLVM IR,
and dump the module *)
type action = Ast | Sast | LLVM_IR | Compile
let () =
let action = ref Compile in
let set_action a () = action := a in
let speclist = [
("-a", Arg.Unit (set_action Ast), "Print the AST");
("-s", Arg.Unit (set_action Sast), "Print the SAST");
("-l", Arg.Unit (set_action LLVM_IR), "Print the generated LLVM IR");
("-c", Arg.Unit (set_action Compile),
"Check and print the generated LLVM IR (default)");
] in
let usage_msg = "usage: ./microc.native [-a|-s|-l|-c] [file.mc]" in
let channel = ref stdin in
Arg.parse speclist (fun filename -> channel := open_in filename) usage_msg;
let lexbuf = Lexing.from_channel !channel in
let ast = Parser.program Scanner.token lexbuf in
match !action with
Ast -> print_string (Ast.string_of_program ast)
| _ -> let sast = Semant.check ast in
match !action with
Ast -> ()
| Sast -> print_string (Sast.string_of_sprogram sast)
| LLVM_IR -> print_string (Llvm.string_of_llmodule (Codegen.translate sast))
| Compile -> let m = Codegen.translate sast in
Llvm_analysis.assert_valid_module m;
print_string (Llvm.string_of_llmodule m)
***) *)