This repository has been archived on 2022-12-27. You can view files and clone it, but cannot push or open issues or pull requests.
compilateurMIPS/main.ml

32 lines
806 B
OCaml

open Lexing
open Ast
let err msg pos =
Printf.eprintf
"Error on line %d col %d: %s.\n"
pos.pos_lnum
(pos.pos_cnum - pos.pos_bol)
msg;
exit 1
;;
let () =
if Array.length Sys.argv != 2
then (
Printf.eprintf "Usage: %s <file>\n" Sys.argv.(0);
exit 1);
let f = open_in Sys.argv.(1) in
let buf = Lexing.from_channel f in
try
let parsed = Parser.prog Lexer.token buf in
close_in f;
let ast = Semantics.analyze parsed in
(* Semantics.emit Stdlib.stderr ast; *)
let asm = Compiler.compile ast in
Mips.emit Stdlib.stdout asm
with
| Lexer.Error c ->
err (Printf.sprintf "Unrecognized char \"%c\"" c) (Lexing.lexeme_start_p buf)
| Parser.Error -> err "Syntax error" (Lexing.lexeme_start_p buf)
| Semantics.Error (msg, pos) -> err msg pos
;;