mirror of
https://github.com/MorizzG/MLox.git
synced 2025-12-06 04:22:41 +00:00
implemented return by rewriting interpreter result
This commit is contained in:
parent
4496875ba3
commit
dee73dea20
16 changed files with 191 additions and 239 deletions
23
lib/value.ml
23
lib/value.ml
|
|
@ -1,6 +1,7 @@
|
|||
type lox_function = {
|
||||
name : string;
|
||||
arity : int;
|
||||
(* env : Environment.environment; *)
|
||||
arg_names : string list;
|
||||
body : Stmt.stmt_node; [@printer fun fmt _ -> fprintf fmt "<body>"]
|
||||
}
|
||||
|
|
@ -13,16 +14,19 @@ type native_function = {
|
|||
}
|
||||
[@@deriving show { with_path = false }]
|
||||
|
||||
and function_ = NativeFunction of native_function | LoxFunction of lox_function
|
||||
[@@deriving show { with_path = false }]
|
||||
|
||||
and lox_value = Function of function_ | String of string | Number of float | Bool of bool | Nil
|
||||
and lox_value =
|
||||
| Function of lox_function
|
||||
| NativeFunction of native_function
|
||||
| String of string
|
||||
| Number of float
|
||||
| Bool of bool
|
||||
| Nil
|
||||
[@@deriving show { with_path = false }]
|
||||
|
||||
let string_of_lox_value lox_value =
|
||||
match lox_value with
|
||||
| Function (NativeFunction { name; arity; _ }) -> Printf.sprintf "<native fn %s/%d>" name arity
|
||||
| Function (LoxFunction { name; arity; _ }) -> Printf.sprintf "<fn %s/%d>" name arity
|
||||
| Function { name; arity; _ } -> Printf.sprintf "<fn %s/%d>" name arity
|
||||
| NativeFunction { name; arity; _ } -> Printf.sprintf "<native fn %s/%d>" name arity
|
||||
| String s -> s
|
||||
| Number x -> if Float.is_integer x then string_of_int (Int.of_float x) else string_of_float x
|
||||
| Bool b -> string_of_bool b
|
||||
|
|
@ -30,8 +34,8 @@ let string_of_lox_value lox_value =
|
|||
|
||||
let type_string_of_lox_value lox_value =
|
||||
match lox_value with
|
||||
| Function (NativeFunction _) -> "NativeFunction"
|
||||
| Function (LoxFunction _) -> "Function"
|
||||
| Function _ -> "Function"
|
||||
| NativeFunction _ -> "NativeFunction"
|
||||
| String _ -> "String"
|
||||
| Number _ -> "Number"
|
||||
| Bool _ -> "Bool"
|
||||
|
|
@ -42,5 +46,4 @@ let lox_value_to_bool lox_value = match lox_value with Bool b -> b | Nil -> fals
|
|||
let make_lox_function (name : string) (arg_names : string list) (body : Stmt.stmt_node) : lox_value
|
||||
=
|
||||
let arity = List.length arg_names in
|
||||
let fn = LoxFunction { name; arity; arg_names; body } in
|
||||
Function fn
|
||||
Function { name; arity; arg_names; body }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue