comparison lab1/main.ml @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bfdcc3820b32
1 (* lab1/main.ml *)
2 (* Copyright (c) 2017 J. M. Spivey *)
3
4 open Print
5
6 (* |main| -- main program *)
7 let main () =
8 let dflag = ref 0 in
9 let fns = ref [] in
10 let usage = "Usage: ppc [-d] file.p" in
11 Arg.parse
12 [("-d", Arg.Unit (fun () -> incr dflag), " Print the tree for debugging");
13 ("-O", Arg.Unit (fun () -> Kgen.optflag := true), " Peephole optimiser")]
14 (function s -> fns := !fns @ [s]) usage;
15 if List.length !fns <> 1 then begin
16 fprintf stderr "$\n" [fStr usage]; exit 2
17 end;
18
19 let in_file = List.hd !fns in
20 let in_chan = open_in in_file in
21 Source.init in_file in_chan;
22 ignore (Parsing.set_trace (!dflag > 1));
23 let lexbuf = Lexing.from_channel in_chan in
24 let prog = try Parser.program Lexer.token lexbuf with
25 Parsing.Parse_error ->
26 let tok = Lexing.lexeme lexbuf in
27 Source.err_message "syntax error at token '$'"
28 [fStr tok] !Lexer.lineno;
29 exit 1 in
30
31 if !dflag > 0 then Tree.print_tree stdout prog;
32
33 printf "MODULE Main 0 0\n" [];
34 printf "IMPORT Lib 0\n" [];
35 printf "ENDHDR\n\n" [];
36
37 printf "PROC MAIN 0 0 0\n" [];
38 Kgen.translate prog;
39 printf "RETURN\n" [];
40 printf "END\n\n" [];
41
42 List.iter
43 (fun x -> printf "GLOVAR _$ 4\n" [fStr x])
44 (Lexer.get_vars ());
45
46 exit 0
47
48 let ppc = main ()