Seperate eval and eval_top

This commit is contained in:
백현웅 2022-02-10 01:37:01 +09:00
parent d5ac54365d
commit b2251d26d5
2 changed files with 10 additions and 8 deletions

16
eval.ml
View file

@ -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

View file

@ -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 -> ()
| _ ->