mirror of
https://github.com/MorizzG/MLox.git
synced 2025-12-06 04:22:41 +00:00
added closures
This commit is contained in:
parent
34c279ec2e
commit
b425a59db5
5 changed files with 59 additions and 28 deletions
13
lib/value.ml
13
lib/value.ml
|
|
@ -1,12 +1,12 @@
|
|||
type lox_function = {
|
||||
name : string;
|
||||
env : environment;
|
||||
arity : int;
|
||||
(* env : Environment.environment; [@printer fun fmt _ -> fprintf fmt "<env>"] *)
|
||||
arg_names : string list;
|
||||
body : Stmt.stmt_node; [@printer fun fmt _ -> fprintf fmt "<body>"]
|
||||
}
|
||||
|
||||
type native_function = {
|
||||
and native_function = {
|
||||
name : string;
|
||||
arity : int;
|
||||
fn : lox_value list -> (lox_value, string) result;
|
||||
|
|
@ -20,6 +20,9 @@ and lox_value =
|
|||
| Bool of bool
|
||||
| Nil
|
||||
|
||||
and env_table = (string, lox_value) Hashtbl.t
|
||||
and environment = { globals : env_table ref; locals : env_table list }
|
||||
|
||||
let string_of_lox_value lox_value =
|
||||
match lox_value with
|
||||
| Function { name; arity; _ } -> Printf.sprintf "<fn %s/%d>" name arity
|
||||
|
|
@ -40,7 +43,7 @@ let type_string_of_lox_value lox_value =
|
|||
|
||||
let lox_value_to_bool lox_value = match lox_value with Bool b -> b | Nil -> false | _ -> true
|
||||
|
||||
let make_lox_function (name : string) (arg_names : string list) (body : Stmt.stmt_node) : lox_value
|
||||
=
|
||||
let make_lox_function (name : string) (env : environment) (arg_names : string list)
|
||||
(body : Stmt.stmt_node) : lox_value =
|
||||
let arity = List.length arg_names in
|
||||
Function { name; arity; arg_names; body }
|
||||
Function { name; env; arity; arg_names; body }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue