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

33 lines
806 B
OCaml
Raw Normal View History

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