2024-08-02 00:10:48 +02:00
|
|
|
let ( let* ) = Result.bind
|
|
|
|
|
|
|
|
|
|
module Error = Error
|
2024-08-12 16:31:28 +02:00
|
|
|
module Expr = Expr
|
2024-08-17 13:32:59 +02:00
|
|
|
module Interpreter = Interpreter
|
2024-08-12 16:31:28 +02:00
|
|
|
module Lexer = Lexer
|
|
|
|
|
module Parser = Parser
|
2024-08-02 00:10:48 +02:00
|
|
|
|
|
|
|
|
type lox_error = Error.lox_error
|
|
|
|
|
|
|
|
|
|
let run (source : string) : (unit, lox_error) result =
|
|
|
|
|
let* tokens = Error.of_lexer_error (Lexer.tokenize source) in
|
2024-08-17 13:32:59 +02:00
|
|
|
(* let f token = Printf.printf "%s " (Lexer.show_token token) in
|
|
|
|
|
Printf.printf "Got %d tokens\n" (List.length tokens);
|
|
|
|
|
List.iter f tokens;
|
|
|
|
|
print_newline (); *)
|
|
|
|
|
let* ast = Error.of_parser_error (Parser.parse tokens) in
|
|
|
|
|
(* Printf.printf "%s\n" (Expr.show_expr expr); *)
|
|
|
|
|
let* value = Error.of_interpreter_error (Interpreter.interpret_expr ast) in
|
|
|
|
|
print_endline (Value.string_of_lox_value value);
|
2024-08-02 00:10:48 +02:00
|
|
|
Ok ()
|
|
|
|
|
|
|
|
|
|
let runRepl () : unit =
|
|
|
|
|
try
|
|
|
|
|
while true do
|
|
|
|
|
print_string "> ";
|
|
|
|
|
let line = read_line () in
|
|
|
|
|
let result = run line in
|
|
|
|
|
Result.iter_error Error.print_error result
|
|
|
|
|
done
|
|
|
|
|
with End_of_file -> ()
|