annotate tools/ncmain.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 (* ncmain.ml *)
mike@0 2 (* Copyright (c) 2017 J. M. Spivey *)
mike@0 3
mike@0 4 (* The 'nodexp' program is a preprocessor that adds one new notation to
mike@0 5 OCaml. An expression like <LOAD addr_size, <LOCAL stat_link>> is shorthand
mike@0 6 for Node (LOAD addr_size, [Node (LOCAL stat_link, [])]) -- a bit easier to
mike@0 7 read, but a lot easier to type. You can also write, e.g.,
mike@0 8 <SEQ, @(List.map gen_stmt ss)> as shorthand for the expression
mike@0 9 Node (SEQ, List.map gen_stmt ss), which is no longer, but at least gives
mike@0 10 a consistent look to the program.
mike@0 11
mike@0 12 A program that's written in this notation ought to contain the type
mike@0 13 declaration
mike@0 14
mike@0 15 type optree = Node of inst * optree list
mike@0 16
mike@0 17 to make the expressions properly typed. The <...> expressions can be
mike@0 18 used both as patterns and as proper expressions. Ambiguity with the use of
mike@0 19 < and > as comparison operators is removed by the rule that the construct
mike@0 20 must begin <ident with no space. *)
mike@0 21
mike@0 22 open Print
mike@0 23
mike@0 24 let qflag = ref false
mike@0 25
mike@0 26 let spec = Arg.align ["-q", Arg.Unit (fun () -> qflag := true),
mike@0 27 " disable #line directives"]
mike@0 28
mike@0 29 let main () =
mike@0 30 let fname = ref "standard input" in
mike@0 31 let chan = ref stdin in
mike@0 32 let seen = ref false in
mike@0 33 let open_it fn =
mike@0 34 if not !seen then begin
mike@0 35 fname := fn; chan := open_in fn; seen := true
mike@0 36 end in
mike@0 37 Arg.parse spec open_it "Usage: nodexp [-q] file";
mike@0 38 let lexbuf = Lexing.from_channel !chan in
mike@0 39 begin
mike@0 40 if not !qflag then printf "# 1 \"$\"\n" [fStr !fname];
mike@0 41 try Ncparse.text Nclex.token lexbuf with
mike@0 42 Parsing.Parse_error ->
mike@0 43 fprintf stderr "\"$\", line $: syntax error\n"
mike@0 44 [fStr !fname; fNum !Nclex.line_no];
mike@0 45 exit 1
mike@0 46 end;
mike@0 47 exit 0
mike@0 48
mike@0 49 let nodexp = main ()