mirror of
https://github.com/MorizzG/MLox.git
synced 2025-12-06 04:22:41 +00:00
init commit
This commit is contained in:
commit
d33023f435
13 changed files with 325 additions and 0 deletions
26
lib/error.ml
Normal file
26
lib/error.ml
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
type code_pos = { line : int; col : int }
|
||||
type lexer_error = { pos : code_pos; msg : string }
|
||||
|
||||
module LexerError = struct
|
||||
type t = lexer_error
|
||||
|
||||
let make (pos : code_pos) (msg : string) : lexer_error =
|
||||
(* let pos = { line; col } in *)
|
||||
{ pos; msg }
|
||||
|
||||
let print (e : lexer_error) =
|
||||
Printf.printf "LexerError at line %d, column %d: %s\n" e.pos.line e.pos.col e.msg
|
||||
end
|
||||
|
||||
type lox_error = LexerError of lexer_error list
|
||||
|
||||
let print_error (e : lox_error) =
|
||||
match e with
|
||||
| LexerError es ->
|
||||
let num_errors = List.length es in
|
||||
assert (num_errors != 0);
|
||||
Printf.printf "found %d %s:\n" num_errors
|
||||
(if num_errors = 1 then "LexerError" else "LexerErrors");
|
||||
List.iter LexerError.print es
|
||||
|
||||
let of_lexer_error e = Result.map_error (fun e -> LexerError e) e
|
||||
Loading…
Add table
Add a link
Reference in a new issue