refactor errors
This commit is contained in:
parent
c800a14262
commit
3cd480fbee
10
bin/main.ml
10
bin/main.ml
|
@ -1,6 +1,8 @@
|
||||||
let () =
|
let () =
|
||||||
|
Logs.set_reporter (Logs.format_reporter ());
|
||||||
|
Logs.set_level (Some Logs.Debug);
|
||||||
|
|
||||||
try
|
try
|
||||||
let modl = Spice.parse "fun main(x) { val y = x * 2 val z = if(y > x) y else 1 }" in
|
let syn = Spice.parse "val x = 3 val y = x + 1" in
|
||||||
Format.fprintf Format.std_formatter "%a\n" Spice.Syn.pp_modl modl
|
Fmt.pr "%a\n" Spice.Syn.pp_modl syn
|
||||||
with Spice.Error.Error err ->
|
with Spice.Error msg -> Logs.err (fun m -> m "%s" msg)
|
||||||
Format.fprintf Format.err_formatter "error: %a\n" Spice.Error.pp err
|
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
type t = Syntax of string
|
|
||||||
|
|
||||||
exception Error of t
|
|
||||||
|
|
||||||
let raise_syntax_error msg = raise (Error (Syntax msg))
|
|
||||||
|
|
||||||
let pp ppf = function
|
|
||||||
| Syntax why -> Format.fprintf ppf "syntax error: %s" why
|
|
|
@ -1,6 +1,11 @@
|
||||||
{
|
{
|
||||||
open Parser
|
open Parser
|
||||||
|
|
||||||
|
exception Error of string
|
||||||
|
|
||||||
|
let fail msg =
|
||||||
|
raise (Error msg)
|
||||||
|
|
||||||
let keywords = Hashtbl.create 17
|
let keywords = Hashtbl.create 17
|
||||||
let _ = begin
|
let _ = begin
|
||||||
Hashtbl.add keywords "true" Kw_true;
|
Hashtbl.add keywords "true" Kw_true;
|
||||||
|
@ -27,7 +32,7 @@ let parse_integer str =
|
||||||
Buffer.add_char buffer ch)
|
Buffer.add_char buffer ch)
|
||||||
str;
|
str;
|
||||||
if Buffer.length buffer > 18 then
|
if Buffer.length buffer > 18 then
|
||||||
Error.raise_syntax_error "integer literal is too long";
|
fail "integer literal is too long";
|
||||||
Int64.of_string (Buffer.contents buffer)
|
Int64.of_string (Buffer.contents buffer)
|
||||||
|
|
||||||
let identifier str =
|
let identifier str =
|
||||||
|
@ -68,6 +73,6 @@ rule read =
|
||||||
| "!=" { Binop Not_eql }
|
| "!=" { Binop Not_eql }
|
||||||
| "." { Dot }
|
| "." { Dot }
|
||||||
| "," { Com }
|
| "," { Com }
|
||||||
| _ { Error.raise_syntax_error "unrecognized character" }
|
| _ { fail "unrecognized character" }
|
||||||
| eof { EOF }
|
| eof { EOF }
|
||||||
|
|
||||||
|
|
14
lib/spice.ml
14
lib/spice.ml
|
@ -1,7 +1,15 @@
|
||||||
module Error = Error
|
|
||||||
module Syn = Syn
|
module Syn = Syn
|
||||||
|
module Ir = Ir
|
||||||
|
|
||||||
|
exception Error of string
|
||||||
|
|
||||||
|
let failf f = Fmt.kstr (fun s -> raise (Error s)) f
|
||||||
|
|
||||||
let parse input =
|
let parse input =
|
||||||
let lexbuf = Lexing.from_string input ~with_positions:true in
|
let lexbuf = Lexing.from_string input ~with_positions:true in
|
||||||
try Parser.modl Lexer.read lexbuf
|
try Parser.modl Lexer.read lexbuf with
|
||||||
with Parser.Error -> Error.raise_syntax_error "bad syntax"
|
| Parser.Error -> failf "syntax error"
|
||||||
|
| Lexer.Error msg -> failf "syntax error: %s" msg
|
||||||
|
|
||||||
|
let compile syn =
|
||||||
|
try Anf_pass.anf syn with Failure msg -> failf "compilation error: %s" msg
|
||||||
|
|
Loading…
Reference in New Issue