From 0dc19b4fdfa4fb22cd9ea14a69c22a549ea4da30 Mon Sep 17 00:00:00 2001 From: Moritz Gmeiner Date: Wed, 28 Aug 2024 17:28:07 +0200 Subject: [PATCH] automatically include loxstd in default env --- lib/lox.ml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/lox.ml b/lib/lox.ml index 14e776a..5d88c74 100644 --- a/lib/lox.ml +++ b/lib/lox.ml @@ -4,14 +4,20 @@ module Error = Error module Expr = Expr module Interpreter = Interpreter module Lexer = Lexer +module Loxstd = Loxstd module Parser = Parser module Stmt = Stmt type lox_error = Error.lox_error +let make_env_with_std () = + let env = Environment.Env.make () in + Loxstd.init_std env; + env + let run ?(env : Environment.environment option) ?(debug = false) (source : string) : (unit, lox_error) result = - let env = Option.value env ~default:(Environment.Env.make ()) in + let env = Option.value env ~default:(make_env_with_std ()) in let* tokens = Error.of_lexer_error (Lexer.tokenize source) in let () = if debug then @@ -50,12 +56,12 @@ let run ?(env : Environment.environment option) ?(debug = false) (source : strin interpret_stmts stmts |> Error.of_runtimer_error let runRepl ?(debug = false) () : unit = - let env = Environment.Env.make () in + let env = make_env_with_std () in try while true do print_string "> "; let line = read_line () in let result = run ~env ~debug line in - Result.iter_error Error.print_error result + Result.map_error Error.print_error result |> ignore done with End_of_file -> ()