-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload-compiler.sml
49 lines (44 loc) · 1.53 KB
/
load-compiler.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
structure compilerLrVals = compilerLrValsFun(structure Token = LrParser.Token)
structure compilerLex = compilerLexFun(structure Tokens = compilerLrVals.Tokens);
structure compilerParser =
Join(structure LrParser = LrParser
structure ParserData = compilerLrVals.ParserData
structure Lex = compilerLex)
fun invoke lexstream =
let
fun print_error (s, linenum:int, col:int) =
TextIO.output(TextIO.stdOut, "Syntax Error:" ^ (Int.toString linenum) ^ ":" ^ (Int.toString col) ^ ":" ^ s ^ "\n")
in
compilerParser.parse(0, lexstream, print_error, ())
end
fun stringToLexer str =
let
val done = ref false
val lexer = compilerParser.makeLexer (fn _ => if (!done) then "" else (done:=true;str))
in
lexer
end
fun parse (lexer) =
let
val dummyEOF = compilerLrVals.Tokens.EOF(0,0)
val (result, lexer) = invoke lexer
val (nextToken, lexer) = compilerParser.Stream.get lexer
in
if compilerParser.sameToken(nextToken, dummyEOF) then result
else (TextIO.output(TextIO.stdOut, "Warning: Unconsumed input \n"); result)
end
val parseString = parse o stringToLexer
fun compile filename =
let
val file = TextIO.openIn filename
val s = TextIO.inputAll file
val _ = TextIO.closeIn file
val i = parseString(s)
in
(TypeCheckEvaluator.printTree(i);
print("\n\n---------------------------\n\nEvaluated Value(s):\n\n");
TypeCheckEvaluator.evalStatements(i, []);
print("---------------------------\n\n"))
end
handle invalidTokenError msg => print("\n"^msg^"\n")
| Fail msg => print("\n"^msg^"\n");