comparison lab3/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 (* lab3/main.ml *)
2 (* Copyright (c) 2017 J. M. Spivey *)
3
4 open Print
5 open Source
6
7 (* |main| -- main program *)
8 let main () =
9 let dflag = ref false in
10 let fns = ref [] in
11 let usage = "Usage: ppc [-d] file.p" in
12 Arg.parse [("-d", Arg.Set dflag, " Print the tree");
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 let in_file = List.hd !fns in
19 let in_chan = open_in in_file in
20 Source.init in_file in_chan;
21 let lexbuf = Lexing.from_channel in_chan in
22 let prog = try Parser.program Lexer.token lexbuf with
23 Parsing.Parse_error ->
24 let tok = Lexing.lexeme lexbuf in
25 err_message "syntax error at token '$'"
26 [fStr tok] !Lexer.lineno;
27 exit 1 in
28
29 if !dflag then Tree.print_tree stdout prog;
30
31 begin try Check.annotate prog with
32 Check.Semantic_error (fmt, args, line) ->
33 err_message fmt args line;
34 exit 1
35 end;
36
37 printf "MODULE Main 0 0\n" [];
38 printf "IMPORT Lib 0\n" [];
39 printf "ENDHDR\n\n" [];
40
41 Kgen.translate prog;
42 exit 0
43
44 let ppc = main ()