forked from mychrisdangelo/microc-withcomments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
microc.ml
75 lines (72 loc) · 2.55 KB
/
microc.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
type action = Ast | Interpret | Bytecode | Compile
(*
* Complete entry point for the running compiler
*
* First let _ means don't hold onto the value returned
* from the following procedures
*
* if Sys.argv is 0 then by default the action is
* Ast else returns action so that Action is used in
* "match action with" line.
*
* List.assoc will take the argument from Sys.argv
* which is an array dereferenced by (1)
* the key values tuples are stored in an ocaml list
* when the appropriate item is found the value
* of the tuple is returned to be assigned to
* action
*)
let _ =
let action = if Array.length Sys.argv > 1 then
List.assoc Sys.argv.(1) [ ("-a", Ast);
("-i", Interpret);
("-b", Bytecode);
("-c", Compile) ]
else Compile in
(*
* Now action is set either by the if or the else.
* else will assign Action to Compile and then the
* following code will use the variable named
* action
*
* Lexing.from_channel
* Lexing is the run-time library for lexers (aka scanners)
* generated by ocamllex. from_channel will create a lexer
* buffer on a given input channel. stdin is the the predefined
* unix input channel.
*
* Scanner.token
* refers to the token member function created in the
* scanner.mll document sending lexbuf in as the argument
* to the token function.
*)
let lexbuf = Lexing.from_channel stdin in
(*
* program is a tuple representing (vars, funcs)
* vars = a list of the global variables declared
* funcs = a list of the functions declared/defined
* and all the information that goes with it
*)
let program = Parser.program Scanner.token lexbuf in
match action with
(*
* the ast flag is simply a way of reviewing the input program
* after being scanned and parsed. We revisit the global variables
* parsed and the functions delcared. We print them all out
* as we understand them now that they are parsed.
*)
Ast -> let listing = Ast.string_of_program program
in print_string listing
(*
* the ignore funciton will allow its argument to execute
* and return unit = (). This is necessary to keep the
* return types of let _ = consistent. An OCaml requirement
*
* We now head over to the Interpret module to ue the run
* member function
*)
| Interpret -> ignore (Interpret.run program)
| Bytecode -> let listing =
Bytecode.string_of_prog (Compile.translate program)
in print_endline listing
| Compile -> Execute.execute_prog (Compile.translate program)