ce/env.ml

71 lines
1.4 KiB
OCaml
Raw Normal View History

2022-02-01 02:13:14 +09:00
type t = {
2022-02-07 23:12:11 +09:00
vars : (string, value) Hashtbl.t;
2022-02-01 02:13:14 +09:00
parent : t option;
}
2022-02-07 23:12:11 +09:00
and value =
| Int of int
| Float of float
| String of string
| Function of string list * expr
| External of string
| Nop (* return of system operations (will be deprecated) *)
and expr = Ast.t
module Value = struct
module Type = Ast.Type
type t = value
let to_string = function
| Int n -> string_of_int n
| Float n -> string_of_float n
| String s -> "\"" ^ s ^ "\""
| Function (vars, _) ->
Printf.sprintf "function with %d arguments" @@ List.length vars
| External f -> "external " ^ f
| Nop -> "nop"
let typeof = function
| Int _ -> Type.Int
| Float _ -> Type.Float
| String _ -> Type.String
| Function _ -> Type.Function
| External _ -> Type.External
| Nop -> failwith "Value.typeof"
let promote = function
| Int n -> Float (float n)
| _ -> failwith "Value.promote"
end
2022-02-01 02:13:14 +09:00
let init_global () = {
vars = Hashtbl.create 100;
parent = None;
}
let make parent = {
vars = Hashtbl.create 100;
parent = Some parent;
}
exception Not_found
let rec get e name =
match Hashtbl.find_opt e.vars name with
| None -> begin match e.parent with
| None -> raise Not_found
| Some p -> get p name
end
| Some value -> value
let get_opt e name =
try Some (get e name)
with Not_found -> None
let set e name value =
Hashtbl.replace e.vars name value
2022-02-01 21:38:00 +09:00
let add_seq e seq =
Hashtbl.add_seq e.vars seq