Re-organized a lot of code, changed functions so that functions capture the surrounding environment and execute in that environment

This commit is contained in:
Emin Arslan
2025-10-14 20:21:29 +03:00
committed by Emin Arslan
parent 965804c18d
commit 22e7c3dbb3
3 changed files with 48 additions and 52 deletions

View File

@@ -11,32 +11,36 @@ type lisp_val =
generally, builtin functions should handle their arguments directly,
and eval forms in the environment as necessary. *)
| LBuiltinFunction of string * (environment -> lisp_val -> lisp_val)
(* a function is a name, a parameter list, and function body. *)
| LFunction of string * lisp_val * lisp_val
(* a function is a name, captured environment, a parameter list, and function body. *)
| LFunction of string * environment * lisp_val * lisp_val
(* a macro is exactly the same as a function, with the distinction
that it receives all of its arguments completely unevaluated
in a compiled lisp this would probably make more of a difference *)
| LMacro of string * lisp_val * lisp_val
| LMacro of string * environment * lisp_val * lisp_val
| LQuoted of lisp_val
and environment = (string, lisp_val) Hashtbl.t list
let env_add env s v =
let env_set_local env s v =
match env with
| [] -> ()
| e1 :: _ -> Hashtbl.add e1 s v
| e1 :: _ -> Hashtbl.replace e1 s v
let env_new_lexical env =
let h = Hashtbl.create 16 in
h :: env
let rec env_root env =
let rec env_root (env : environment) =
match env with
| [] -> raise (Invalid_argument "Empty environment passed to env_root!")
| e :: [] -> e
| _ :: t -> env_root t
let env_set_global env s v =
Hashtbl.replace (env_root env) s v
let env_copy env =
List.map Hashtbl.copy env
let rec dbg_print_one v =
let pf = Printf.sprintf in
@@ -48,9 +52,9 @@ let rec dbg_print_one v =
| LCons (a, b) -> pf "(%s . %s)" (dbg_print_one a) (dbg_print_one b)
| LDouble d -> pf "<double: %f>" d
| LBuiltinFunction (name, _) -> pf "<builtin: %s>" name
| LFunction (name, args, _) -> pf "<function: '%s' lambda-list: %s>"
| LFunction (name, _, args, _) -> pf "<function: '%s' lambda-list: %s>"
name (dbg_print_one args)
| LMacro (name, args, _) -> pf "<function '%s' lambda-list: %s>"
| LMacro (name, _, args, _) -> pf "<function '%s' lambda-list: %s>"
name (dbg_print_one args)
| LQuoted v -> pf "<quote: %s>" (dbg_print_one v)
(*| _ -> "<Something else>"*)