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
|
exception Unbound of string
|
||||||
|
|
||||||
let rec eval env ast : string * value =
|
let rec eval env ast =
|
||||||
let rec aux = function
|
let rec aux = function
|
||||||
| Nint n -> Int n
|
| Nint n -> Int n
|
||||||
| Nfloat n -> Float n
|
| Nfloat n -> Float n
|
||||||
|
@ -280,7 +280,7 @@ let rec eval env ast : string * value =
|
||||||
| Letin (v, e, f) ->
|
| Letin (v, e, f) ->
|
||||||
let nenv = Env.make env in
|
let nenv = Env.make env in
|
||||||
Env.set nenv v (aux e);
|
Env.set nenv v (aux e);
|
||||||
snd @@ eval nenv f
|
eval nenv f
|
||||||
|
|
||||||
| Unary (op, t) ->
|
| Unary (op, t) ->
|
||||||
let t = aux t in
|
let t = aux t in
|
||||||
|
@ -303,12 +303,13 @@ let rec eval env ast : string * value =
|
||||||
let nenv = Env.make env in
|
let nenv = Env.make env in
|
||||||
List.combine vars args
|
List.combine vars args
|
||||||
|> List.iter (fun (v, a) -> Env.set nenv v a);
|
|> List.iter (fun (v, a) -> Env.set nenv v a);
|
||||||
snd @@ eval nenv e
|
eval nenv e
|
||||||
| External f ->
|
| External f ->
|
||||||
let args = List.map aux args in
|
let args = List.map aux args in
|
||||||
External.apply f args
|
External.apply f args
|
||||||
| v -> raise @@ Type.Invalid (Value.typeof v)
|
| v -> raise @@ Type.Invalid (Value.typeof v)
|
||||||
end
|
end
|
||||||
|
|
||||||
| Set_binop_pre (op, l) ->
|
| Set_binop_pre (op, l) ->
|
||||||
let l =
|
let l =
|
||||||
match aux l with
|
match aux l with
|
||||||
|
@ -328,10 +329,11 @@ let rec eval env ast : string * value =
|
||||||
| Some a -> String (Parser.assoc_to_string a))
|
| Some a -> String (Parser.assoc_to_string a))
|
||||||
| _ -> failwith "Eval.eval"
|
| _ -> failwith "Eval.eval"
|
||||||
in
|
in
|
||||||
|
aux ast
|
||||||
|
|
||||||
|
let eval_top env ast =
|
||||||
match ast with
|
match ast with
|
||||||
| Let (var, e) ->
|
| Let (var, e) ->
|
||||||
let v = aux e in
|
let v = eval env e in
|
||||||
Env.set env var v;
|
Env.set env var v; var, v
|
||||||
var, v
|
| ast -> "-", eval env ast
|
||||||
| ast -> "-", aux ast
|
|
||||||
|
|
2
main.ml
2
main.ml
|
@ -39,7 +39,7 @@ let rep env : unit =
|
||||||
if line = "quit" then raise Exit;
|
if line = "quit" then raise Exit;
|
||||||
let ast = line |> Lex.tokenize |> Parser.parse in
|
let ast = line |> Lex.tokenize |> Parser.parse in
|
||||||
if !debug then Ast.print ast;
|
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
|
match v with
|
||||||
| Nop -> ()
|
| Nop -> ()
|
||||||
| _ ->
|
| _ ->
|
||||||
|
|
Loading…
Add table
Reference in a new issue