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