-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhile_ast.sml
52 lines (47 loc) · 1.3 KB
/
while_ast.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
(* while_ast.sml *)
structure My=
struct
exception MyError;
fun compile (fileName) =
let
val inStream = TextIO.openIn fileName;
val grab : int -> string = fn
n => if TextIO.endOfStream inStream
then ""
else TextIO.inputN (inStream,n);
val printError : string * int * int -> unit = fn
(msg,line,col) =>
print (fileName^"["^Int.toString line^":"
^Int.toString col^"] "^msg^"\n");
val (tree,rem) = MyParser.parse
(15,
(MyParser.makeLexer grab ),
printError,
())
handle MyParser.ParseError => raise MyError ;
(* Close the source program file *)
val _ = TextIO.closeIn inStream;
in
tree
end
fun writeListToFile (filename : string, lst : string list) =
let
val outStream = TextIO.openOut filename
fun writeList (lst : string list) =
case lst of
[] => ()
| x::xs => (TextIO.output (outStream, ( x ^ "\n")); writeList xs)
in
writeList lst;
TextIO.closeOut outStream
end
fun interpret(filename,fileout)=
let
open DataTypes
val parsed_ast = compile(filename)
val stringop = letsgo(parsed_ast)
val _ = writeListToFile(fileout,!stringop)
in
()
end
end;