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/errors.ml
2022-12-11 03:35:52 +01:00

47 lines
999 B
OCaml

open Ast
open Lexing
exception LexerError of char
exception SemanticsError of string * Lexing.position
exception SyntaxError of string
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 rec string_of_type_t = function
| Magic_t -> "magic"
| Void_t -> "void"
| Int_t -> "int"
| Bool_t -> "bool"
| Str_t -> "str"
| Func_t (r, a) ->
(if List.length a > 1 then "(" else "")
^ String.concat ", " (List.map string_of_type_t a)
^ (if List.length a > 1 then ")" else "")
^ " -> "
^ string_of_type_t r
;;
let errt expected given pos =
raise
(SemanticsError
( Printf.sprintf
"Expected %s but given %s"
(string_of_type_t expected)
(string_of_type_t given)
, pos ))
;;
let warn msg (pos : Lexing.position) =
Printf.eprintf
"Warning on line %d col %d: %s.\n"
pos.pos_lnum
(pos.pos_cnum - pos.pos_bol)
msg
;;