Seperate eval and eval_top
This commit is contained in:
parent
d5ac54365d
commit
b2251d26d5
2 changed files with 10 additions and 8 deletions
16
eval.ml
16
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
|
||||
|
|
2
main.ml
2
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 -> ()
|
||||
| _ ->
|
||||
|
|
Loading…
Add table
Reference in a new issue