test.ml (895B)
1 open Lexing 2 open Ast 3 open Ast.IR2 4 5 let err msg pos = 6 Printf.eprintf "Error on line %d col %d: %s.\n" 7 pos.pos_lnum (pos.pos_cnum - pos.pos_bol) msg ; 8 exit 1 9 10 let () = 11 if (Array.length Sys.argv) != 2 then begin 12 Printf.eprintf "Usage: %s <file>\n" Sys.argv.(0) ; 13 exit 1 14 end; 15 let f = open_in Sys.argv.(1) in 16 let buf = Lexing.from_channel f in 17 try 18 let parsed = Parser.prog Lexer.token buf in 19 close_in f ; 20 let ast = Semantics.analyze parsed in 21 (* print_endline (IR.string_of_ir ast); *) 22 let simplified = Simplifier.simplify ast in 23 let compiled = Compiler.compile simplified in 24 Mips.print_asm Stdlib.stdout compiled 25 with 26 | Lexer.Error c -> 27 err (Printf.sprintf "unrecognized char '%c'" c) (Lexing.lexeme_start_p buf) 28 | Parser.Error -> 29 err "syntax error" (Lexing.lexeme_start_p buf) 30 | Semantics.Error (msg, pos) -> 31 err msg pos