-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.sml
183 lines (169 loc) · 6.79 KB
/
main.sml
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
(* -*- mode: sml; mode: font-lock; tab-width: 4; insert-tabs-mode: nil; indent-tabs-mode: nil -*- *)
(*
* This is the main entry point for the ES4 reference evaluator.
*)
structure Main = struct
fun findTraceOption (tname:string)
: (bool ref) option =
case tname of
"lex" => SOME (Lexer.UserDeclarations.doTrace)
| "parse" => SOME (Parser.doTrace)
| "name" => SOME (Multiname.doTrace)
| "defn" => SOME (Defn.doTrace)
| "verify" => SOME (Verify.doTrace)
| "eval" => SOME (Eval.doTrace)
| "mach" => SOME (Mach.doTrace)
| "decimal" => SOME (Decimal.doTrace)
| "native" => SOME (Native.doTrace)
| "boot" => SOME (Boot.doTrace)
| "stack" => SOME (Eval.traceStack)
| _ => NONE
exception quitException
fun repl doPrompt =
let
val doParse = ref true
val doDefn = ref true
val doEval = ref true
fun toggleRef (n:string) (r:bool ref) =
(r := not (!r);
print ("set " ^ n ^ " = " ^ (Bool.toString (!r)) ^ "\n"))
fun doLine _ =
let
val _ = if doPrompt then print ">> " else ()
val line = case TextIO.inputLine TextIO.stdIn of
NONE => ""
| SOME s => s
val toks = String.tokens Char.isSpace line
fun help _ = (List.app print
[
":quit - quit repl\n",
":trace <T> - toggle tracing of <T>\n",
":help - this message\n",
":reboot - reload the boot environment\n",
":parse - toggle parse stage\n",
":defn - toggle defn stage\n",
":eval - toggle evaluation stage\n"
];
doLine())
in
case toks of
[":quit"] => raise quitException
| [":q"] => raise quitException
| [":h"] => help ()
| [":help"] => help ()
| [":?"] => help ()
| ["?"] => help ()
| [":reboot"] => (Boot.boot(); doLine ())
| [":parse"] => toggleRef "parse" doParse
| [":defn"] => toggleRef "defn" doDefn
| [":eval"] => toggleRef "eval" doEval
| [":trace", t] =>
((case findTraceOption t of
NONE =>
(print ("unknown trace option " ^ t ^ "\n"))
| SOME r => toggleRef ("trace option " ^ t) r);
doLine())
| [] => doLine ()
| _ =>
if (!doParse)
then
let
val p = Parser.parseLines [line]
in
if (!doDefn)
then
let
val d = Defn.defProgram p
in
if (!doEval)
then
let
val res = Eval.evalProgram d
in
if res = Mach.Undef
then ()
else print ((Eval.toString res) ^ "\n");
doLine ()
end
else
doLine ()
end
else
doLine ()
end
else
doLine ()
end
fun runUntilQuit _ =
((doLine ()
handle
(LogErr.LexError | LogErr.ParseError | LogErr.NameError
| LogErr.DefnError | LogErr.EvalError | LogErr.MachError
| LogErr.HostError | LogErr.UnimplError) =>
Eval.resetStack (); ());
runUntilQuit ())
in
Boot.boot();
runUntilQuit ()
handle quitException => print "bye\n"
end
fun testTC argvRest =
let
val _ = TextIO.print "booting ... \n";
val _ = Boot.boot ();
val asts = List.map Parser.parseFile argvRest
val _ = TextIO.print "defining ... \n";
val dps = map Defn.defProgram asts
val _ = TextIO.print "type checking ... \n";
val _ = List.map Verify.verifyProgram dps;
val _ = TextIO.print "type checked! \n"
in
()
end
fun testEV argvRest =
let
(* 10 seconds to run, then we get SIGALRM. *)
val _ = Posix.Process.alarm (Time.fromReal 100.0)
val _ = TextIO.print "booting ... \n";
val _ = Boot.boot ();
val _ = TextIO.print "parsing ... \n";
val asts = List.map Parser.parseFile argvRest
val _ = TextIO.print "defining ... \n";
val dps = map Defn.defProgram asts
val _ = TextIO.print "type checking ... \n";
val _ = List.map Verify.verifyProgram dps;
val _ = TextIO.print "evaluating ... \n";
val _ = map Eval.evalProgram dps
val _ = TextIO.print "evaluated! \n"
in
()
end
fun testDefn argvRest =
let
val _ = TextIO.print "booting ... \n";
val _ = Boot.boot ();
val _ = TextIO.print "parsing ... \n";
val asts = List.map Parser.parseFile argvRest
val _ = TextIO.print "defining ... \n";
val dps = map Defn.defProgram asts
in
()
end
fun consumeTraceOption (opt:string) : bool =
case explode opt of
(#"-" :: #"T" :: rest) =>
(case findTraceOption (String.implode rest) of
SOME r => (r := true; false)
| NONE => true)
| _ => true
fun main (argv0:string, argvRest:string list) =
BackTrace.monitor (fn () =>
(
(case List.filter consumeTraceOption argvRest of
("-r"::argvRest) => repl true
| ("-rq"::argvRest) => repl false
| ("-tc"::argvRest) => testTC argvRest
| ("-ev"::argvRest) => testEV argvRest
| _ => testDefn argvRest);
0))
end