From b2251d26d5f4cb775f08166c5703f5dd31bb4294 Mon Sep 17 00:00:00 2001 From: Hyeonung Baek Date: Thu, 10 Feb 2022 01:37:01 +0900 Subject: [PATCH] Seperate eval and eval_top --- eval.ml | 16 +++++++++------- main.ml | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/eval.ml b/eval.ml index aaea48f..422dead 100644 --- a/eval.ml +++ b/eval.ml @@ -261,7 +261,7 @@ let rec binop op l r = exception Unbound of string -let rec eval env ast : string * value = +let rec eval env ast = let rec aux = function | Nint n -> Int n | Nfloat n -> Float n @@ -280,7 +280,7 @@ let rec eval env ast : string * value = | Letin (v, e, f) -> let nenv = Env.make env in Env.set nenv v (aux e); - snd @@ eval nenv f + eval nenv f | Unary (op, t) -> let t = aux t in @@ -303,12 +303,13 @@ let rec eval env ast : string * value = let nenv = Env.make env in List.combine vars args |> List.iter (fun (v, a) -> Env.set nenv v a); - snd @@ eval nenv e + eval nenv e | External f -> let args = List.map aux args in External.apply f args | v -> raise @@ Type.Invalid (Value.typeof v) end + | Set_binop_pre (op, l) -> let l = match aux l with @@ -328,10 +329,11 @@ let rec eval env ast : string * value = | Some a -> String (Parser.assoc_to_string a)) | _ -> failwith "Eval.eval" in + aux ast +let eval_top env ast = match ast with | Let (var, e) -> - let v = aux e in - Env.set env var v; - var, v - | ast -> "-", aux ast + let v = eval env e in + Env.set env var v; var, v + | ast -> "-", eval env ast diff --git a/main.ml b/main.ml index 56e36a8..77b6a10 100644 --- a/main.ml +++ b/main.ml @@ -39,7 +39,7 @@ let rep env : unit = if line = "quit" then raise Exit; let ast = line |> Lex.tokenize |> Parser.parse in if !debug then Ast.print ast; - let var, v = Eval.eval env ast in + let var, v = Eval.eval_top env ast in match v with | Nop -> () | _ ->