From b6e00cd862d80fef042d927d6ce05463e7cd7522 Mon Sep 17 00:00:00 2001 From: Hyeonung Baek Date: Fri, 18 Feb 2022 16:49:20 +0900 Subject: [PATCH] Fix recursive function --- eval.ml | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/eval.ml b/eval.ml index ad2f098..654bd0d 100644 --- a/eval.ml +++ b/eval.ml @@ -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