-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcamlc.ml
109 lines (89 loc) · 3.6 KB
/
camlc.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
(*
* Copyright (c) 2015 Stefan Krah. All rights reserved.
*
* This file is distributed under the terms of the Q Public License
* version 1.0.
*)
open Printf
type options =
Indent_parsetree
| Indent_ast
| Indent_typedtree
| Indent_cconv
| Emit_llvm
| No_option
let action = ref No_option
let source = ref None
let f opt () = action := opt
let usage = "camlc: usage: ./camlc [options] file"
let specs =
["-dparsetree", Arg.Unit (f Indent_parsetree), " <file> reindent the parse tree";
"-dast", Arg.Unit (f Indent_ast), " <file> reindent the (annotated) ast";
"-dtypedtree", Arg.Unit (f Indent_typedtree), " <file> reindent the typedtree";
"-dcconv", Arg.Unit (f Indent_cconv), " <file> reindent the closure-converted typedtree";
"-emit-llvm", Arg.Unit (f Emit_llvm), " <file> emit llvm";
]
let _ =
Arg.parse
specs
(fun name -> source := Some name)
usage
let indent_parsetree file =
let tree = Lib.parsetree_from_file file in
Visualize.ParseTreePrinter.indent ([], tree)
let indent_ast file =
let headers = Lib.parsetree_from_file "builtins.ml" in
let tree = Lib.parsetree_from_file file in
let module State = ModuleState.Make (struct let filename = file end) in
let module Symtbl = Symtable.Make(State) in
let module AstPrinter = Visualize.AstPrinter(State) in
let ast = Symtbl.trans_main (headers, tree) in
AstPrinter.indent ([], snd ast)
let indent_typedtree file =
let headers = Lib.parsetree_from_file "builtins.ml" in
let tree = Lib.parsetree_from_file file in
let module State = ModuleState.Make (struct let filename = file end) in
let module Symtbl = Symtable.Make(State) in
let module Typecheck = Typecheck.Make(State) in
let module TypedtreePrinter = Visualize.TypedtreePrinter (State) (struct let closure = false let fullname = false end) in
let ast = Symtbl.trans_main (headers, tree) in
let typedtree = Typecheck.trans_main ast in
TypedtreePrinter.indent ([], snd typedtree)
let indent_cconv file =
let headers = Lib.parsetree_from_file "builtins.ml" in
let tree = Lib.parsetree_from_file file in
let module State = ModuleState.Make (struct let filename = file end) in
let module Symtbl = Symtable.Make(State) in
let module Typecheck = Typecheck.Make(State) in
let module Closureconv = Closureconv.Make(State) in
let module TypedtreePrinter = Visualize.TypedtreePrinter(State) (struct let closure = true let fullname = true end) in
let ast = Symtbl.trans_main (headers, tree) in
let typedtree = Typecheck.trans_main ast in
let cconv = Closureconv.conv typedtree in
TypedtreePrinter.indent ([], snd cconv)
let emit_llvm file =
let headers = Lib.parsetree_from_file "builtins.ml" in
let tree = Lib.parsetree_from_file file in
let module State = ModuleState.Make (struct let filename = file end) in
let module Symtbl = Symtable.Make(State) in
let module Typecheck = Typecheck.Make(State) in
let module Closureconv = Closureconv.Make(State) in
let module Compile = Translate.Make(State) in
let ast = Symtbl.trans_main (headers, tree) in
let typedtree = Typecheck.trans_main ast in
let cconv = Closureconv.conv typedtree in
Compile.trans_main cconv
let main () =
match !source with
| None -> fprintf stderr "%s" (Arg.usage_string specs usage); exit(1)
| Some file ->
match !action with
Indent_parsetree -> indent_parsetree file
| Indent_ast -> indent_ast file
| Indent_typedtree -> indent_typedtree file
| Indent_cconv -> indent_cconv file
| Emit_llvm -> emit_llvm file
| No_option ->
fprintf stderr "%s" (Arg.usage_string specs usage);
exit(1)
let _ = main ()