started implementing expressions

Parser.parse now return list of statements or list of errors.
parsing continues until EOF, even when errors are found; but after the
first error the result can only be Error.
Also implemented Print and Expr statements.
This commit is contained in:
Moritz Gmeiner 2024-08-25 02:12:51 +02:00
commit ea0d7acbee
6 changed files with 119 additions and 57 deletions

View file

@ -21,38 +21,38 @@ module ParserError = struct
Printf.printf "ParserError at line %d, column %d: %s\n" e.pos.line e.pos.col e.msg
end
type interpreter_error = { pos : code_pos; msg : string }
type runtime_error = { pos : code_pos; msg : string }
module InterpreterError = struct
module RuntimeError = struct
type t = parser_error
let make (pos : code_pos) (msg : string) : interpreter_error = { pos; msg }
let make (pos : code_pos) (msg : string) : runtime_error = { pos; msg }
let print (e : interpreter_error) =
Printf.printf "InterpreterError at line %d, column %d: %s\n" e.pos.line e.pos.col e.msg
let print (e : runtime_error) =
Printf.printf "RuntimeError at line %d, column %d: %s\n" e.pos.line e.pos.col e.msg
end
type lox_error =
| LexerError of lexer_error list
| ParserError of parser_error list
| InterpreterError of interpreter_error
| RuntimeError of runtime_error
let print_error (e : lox_error) =
match e with
| LexerError es ->
let num_errors = List.length es in
assert (num_errors != 0);
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
| ParserError es ->
let num_errors = List.length es in
assert (num_errors != 0);
assert (num_errors <> 0);
Printf.printf "found %d %s:\n" num_errors
(if num_errors = 1 then "ParserError" else "ParserErrors");
List.iter ParserError.print es
| InterpreterError e -> InterpreterError.print e
| RuntimeError e -> RuntimeError.print e
let of_lexer_error e = Result.map_error (fun e -> LexerError e) e
let of_parser_error e = Result.map_error (fun e -> ParserError e) e
let of_interpreter_error e = Result.map_error (fun e -> InterpreterError e) e
let of_runtimer_error e = Result.map_error (fun e -> RuntimeError e) e