annotate lab1/main.ml @ 1:b5139af1a420 tip basis

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