Fix recursive function

This commit is contained in:
백현웅 2022-02-18 16:49:20 +09:00
parent 96d0c882b3
commit b6e00cd862

22
eval.ml
View file

@ -7,7 +7,8 @@ type value =
| Bool of bool
| String of string
| Symbol of string
| Function of string * expr * env
(* (name), arg, expression, name *)
| Function of string option * string * expr * env
| External of string
| Nop (* return of system operations (will be deprecated) *)
@ -255,7 +256,7 @@ let rec eval env ast =
| Nbool b -> Bool b
| Nstring s -> String s
| Nsymbol s -> Symbol s
| Nfunction (arg, e) -> Function (arg, e, env)
| Nfunction (arg, e) -> Function (None, arg, e, env)
| Nexternal f -> External f
| Var v -> begin match Env.get_opt env v with
@ -282,11 +283,10 @@ let rec eval env ast =
| Apply (v, args) ->
begin match aux v with
| Function (var, e, env) as f ->
| Function (itself, var, e, env) as f ->
begin match args with
| [] -> f
| a::args ->
let itself = match v with Var v -> Some v | _ -> None in
let env =
(* binding itself into env for recursion *)
Option.fold
@ -327,9 +327,11 @@ let rec eval env ast =
aux ast
let eval_top env_ref ast =
match ast with
| Let (var, e) ->
let v = eval !env_ref e in
env_ref := Env.bind (var, v) !env_ref;
var, v
| ast -> "-", eval !env_ref ast
let var, v = match ast with
| Let (var, Nfunction (arg, e)) ->
var, Function (Some var, arg, e, !env_ref)
| Let (var, e) -> var, eval !env_ref e
| ast -> "-", eval !env_ref ast
in
if var <> "-" then env_ref := Env.bind (var, v) !env_ref;
var, v