2022-01-10 01:31:47 +09:00
|
|
|
open Ast
|
2022-02-08 00:46:43 +09:00
|
|
|
|
2022-02-08 16:05:33 +09:00
|
|
|
(* resulting value of eval *)
|
2022-02-08 00:46:43 +09:00
|
|
|
type value =
|
|
|
|
| Int of int
|
|
|
|
| Float of float
|
2022-02-08 16:05:33 +09:00
|
|
|
| Bool of bool
|
2022-02-08 00:46:43 +09:00
|
|
|
| String of string
|
|
|
|
| Symbol of string
|
2022-02-22 17:01:39 +09:00
|
|
|
(* (name), bound variables, expression, environment *)
|
|
|
|
| Function of string option * string list * expr * env
|
2022-02-08 00:46:43 +09:00
|
|
|
| External of string
|
|
|
|
| Nop (* return of system operations (will be deprecated) *)
|
|
|
|
|
|
|
|
and expr = Ast.t
|
|
|
|
|
2022-02-08 16:05:33 +09:00
|
|
|
(* environment for eval *)
|
2022-02-21 02:15:51 +09:00
|
|
|
and env = Env of (string * value) list
|
2022-02-08 00:46:43 +09:00
|
|
|
|
2022-02-08 01:00:31 +09:00
|
|
|
exception No_operation
|
|
|
|
exception Too_many_arguments
|
|
|
|
|
2022-02-19 20:39:50 +09:00
|
|
|
(* TODO: add proper type system *)
|
2022-02-08 00:46:43 +09:00
|
|
|
module Type = struct
|
|
|
|
type t =
|
|
|
|
| Int
|
|
|
|
| Float
|
2022-02-08 16:05:33 +09:00
|
|
|
| Bool
|
2022-02-08 00:46:43 +09:00
|
|
|
| String
|
|
|
|
| Symbol
|
|
|
|
| Function
|
|
|
|
| External
|
|
|
|
|
2022-02-08 01:00:31 +09:00
|
|
|
exception Invalid of t
|
2022-02-08 22:02:00 +09:00
|
|
|
exception Expected of t
|
2022-02-08 01:00:31 +09:00
|
|
|
|
2022-02-08 00:46:43 +09:00
|
|
|
let to_string = function
|
|
|
|
| Int -> "int"
|
|
|
|
| Float -> "float"
|
2022-02-08 16:05:33 +09:00
|
|
|
| Bool -> "bool"
|
2022-02-08 00:46:43 +09:00
|
|
|
| String -> "string"
|
|
|
|
| Symbol -> "symbol"
|
|
|
|
| Function -> "fun"
|
|
|
|
| External -> "external"
|
|
|
|
|
|
|
|
let supertype = function
|
|
|
|
| Int -> Some Float
|
|
|
|
| _ -> None
|
|
|
|
end
|
|
|
|
|
|
|
|
module Value = struct
|
|
|
|
type t = value
|
|
|
|
|
|
|
|
let to_string = function
|
|
|
|
| Int n -> string_of_int n
|
|
|
|
| Float n -> string_of_float n
|
2022-02-08 16:05:33 +09:00
|
|
|
| Bool b -> string_of_bool b
|
2022-02-08 00:46:43 +09:00
|
|
|
| String s -> "\"" ^ s ^ "\""
|
2022-02-21 02:18:42 +09:00
|
|
|
| Symbol s -> "#" ^ s
|
2022-02-11 02:54:21 +09:00
|
|
|
| Function _ -> "<fun>"
|
2022-02-08 00:46:43 +09:00
|
|
|
| External f -> "external " ^ f
|
|
|
|
| Nop -> "nop"
|
|
|
|
|
|
|
|
let typeof = function
|
|
|
|
| Int _ -> Type.Int
|
|
|
|
| Float _ -> Type.Float
|
2022-02-08 16:05:33 +09:00
|
|
|
| Bool _ -> Type.Bool
|
2022-02-08 00:46:43 +09:00
|
|
|
| String _ -> Type.String
|
|
|
|
| Symbol _ -> Type.Symbol
|
|
|
|
| Function _ -> Type.Function
|
|
|
|
| External _ -> Type.External
|
|
|
|
| Nop -> failwith "Value.typeof"
|
|
|
|
|
|
|
|
let promote = function
|
|
|
|
| Int n -> Float (float n)
|
|
|
|
| _ -> failwith "Value.promote"
|
|
|
|
end
|
|
|
|
|
|
|
|
module Env = struct
|
|
|
|
type t = env
|
|
|
|
|
2022-02-21 02:15:51 +09:00
|
|
|
let empty = Env []
|
2022-02-08 00:46:43 +09:00
|
|
|
|
2022-02-21 02:15:51 +09:00
|
|
|
let get_opt (Env e) name =
|
2022-02-12 03:18:00 +09:00
|
|
|
List.assoc_opt name e
|
2022-02-08 00:46:43 +09:00
|
|
|
|
2022-02-21 02:15:51 +09:00
|
|
|
let bind v (Env e) =
|
|
|
|
Env (v::e)
|
2022-02-08 00:46:43 +09:00
|
|
|
|
2022-02-21 02:15:51 +09:00
|
|
|
let bind_seq seq (Env e) =
|
|
|
|
Env (List.of_seq seq @ e)
|
2022-02-08 00:46:43 +09:00
|
|
|
end
|
2022-01-10 01:31:47 +09:00
|
|
|
|
2022-02-07 23:12:11 +09:00
|
|
|
(* operators *)
|
|
|
|
module Operator = struct
|
|
|
|
type t = Ast.operator
|
|
|
|
|
|
|
|
exception Unavailable of t
|
|
|
|
|
|
|
|
let to_string = Ast.op_to_string
|
|
|
|
|
|
|
|
let negate = function
|
2022-02-19 21:46:48 +09:00
|
|
|
| [Int n] -> Int ~-n
|
|
|
|
| [Float n] -> Float ~-.n
|
2022-02-07 23:12:11 +09:00
|
|
|
| _ -> failwith "Operator.negate"
|
|
|
|
|
2022-02-19 21:46:48 +09:00
|
|
|
let vi f = function
|
|
|
|
| [Int a; Int b] -> Int (f a b)
|
2022-02-08 01:00:31 +09:00
|
|
|
| _ -> raise @@ Type.Invalid Int
|
2022-02-07 23:12:11 +09:00
|
|
|
|
2022-02-19 21:46:48 +09:00
|
|
|
let vf f = function
|
|
|
|
| [Float a; Float b] -> Float (f a b)
|
2022-02-08 22:02:00 +09:00
|
|
|
| _ -> raise @@ Type.Expected Float
|
2022-02-07 23:12:11 +09:00
|
|
|
|
2022-02-19 21:46:48 +09:00
|
|
|
let compare = function
|
|
|
|
| [Int a; Int b] -> Int.compare a b
|
|
|
|
| [Float a; Float b] -> Float.compare a b
|
|
|
|
| [Bool a; Bool b] -> Bool.compare a b
|
|
|
|
| [String a; String b] -> String.compare a b
|
|
|
|
| [Symbol a; Symbol b] -> String.compare a b
|
2022-02-08 22:02:00 +09:00
|
|
|
| _ -> invalid_arg "Operator.compare"
|
|
|
|
|
2022-02-19 21:46:48 +09:00
|
|
|
let eq vs = Bool (compare vs = 0)
|
|
|
|
let neq vs = Bool (compare vs <> 0)
|
|
|
|
let ge vs = Bool (compare vs >= 0)
|
|
|
|
let le vs = Bool (compare vs <= 0)
|
|
|
|
let gt vs = Bool (compare vs > 0)
|
|
|
|
let lt vs = Bool (compare vs < 0)
|
2022-02-08 22:02:00 +09:00
|
|
|
|
|
|
|
(* operator table *)
|
2022-02-07 23:12:11 +09:00
|
|
|
let operators =
|
|
|
|
let open Type in
|
2022-02-19 21:46:48 +09:00
|
|
|
let ip = [Int; Int] and fp = [Float; Float] in
|
2022-02-08 22:02:00 +09:00
|
|
|
let any f = [ip, f; fp, f] in
|
2022-02-07 23:12:11 +09:00
|
|
|
[
|
|
|
|
Add, [ip, vi Int.add; fp, vf Float.add];
|
|
|
|
Sub, [ip, vi Int.sub; fp, vf Float.sub];
|
|
|
|
Mul, [ip, vi Int.mul; fp, vf Float.mul];
|
|
|
|
Div, [ip, vi Int.div; fp, vf Float.div];
|
|
|
|
Mod, [ip, vi Int.rem; fp, vf Float.rem];
|
|
|
|
Exp, [fp, vf Float.pow];
|
2022-02-08 22:02:00 +09:00
|
|
|
Eq, any eq;
|
|
|
|
Neq, any neq;
|
|
|
|
GE, any ge;
|
|
|
|
LE, any le;
|
|
|
|
GT, any gt;
|
|
|
|
LT, any lt;
|
2022-02-19 21:46:48 +09:00
|
|
|
Negate, [[Int], negate; [Float], negate];
|
2022-02-07 23:12:11 +09:00
|
|
|
]
|
|
|
|
|> List.to_seq
|
|
|
|
|> Hashtbl.of_seq
|
|
|
|
|
2022-02-19 21:46:48 +09:00
|
|
|
let get op =
|
2022-02-07 23:12:11 +09:00
|
|
|
Hashtbl.find operators op
|
|
|
|
end
|
|
|
|
|
2022-02-08 00:46:43 +09:00
|
|
|
module External = struct
|
2022-02-08 01:00:31 +09:00
|
|
|
exception Invalid of string
|
|
|
|
|
2022-02-08 00:46:43 +09:00
|
|
|
let rad r =
|
|
|
|
r *. 180. /. Float.pi
|
|
|
|
|
|
|
|
let deg d =
|
|
|
|
d /. 180. *. Float.pi
|
|
|
|
|
|
|
|
let floatfun f = function
|
2022-02-19 21:46:48 +09:00
|
|
|
| [Float n] -> Float (f n)
|
|
|
|
| [v] -> raise @@ Type.Invalid (Value.typeof v)
|
|
|
|
| _ -> invalid_arg "External.floatfun"
|
2022-02-08 00:46:43 +09:00
|
|
|
|
2022-02-20 03:35:11 +09:00
|
|
|
let symbol_to_op op =
|
|
|
|
op
|
|
|
|
|> String.to_seqi
|
|
|
|
|> Lex.find_token
|
|
|
|
|> Option.get
|
|
|
|
|> fst
|
|
|
|
|> Parser.token_to_op
|
|
|
|
|
|
|
|
let set_op_pre = function
|
|
|
|
| [Symbol op; Int l] ->
|
|
|
|
let op = symbol_to_op op in
|
|
|
|
Hashtbl.replace Parser.precedence op l;
|
|
|
|
Nop
|
|
|
|
| _ -> failwith "set_op_pre"
|
|
|
|
|
|
|
|
let get_op_pre = function
|
|
|
|
| [Symbol op] ->
|
|
|
|
let op = symbol_to_op op in
|
|
|
|
Int (Hashtbl.find Parser.precedence op)
|
|
|
|
| _ -> failwith "get_op_pre"
|
|
|
|
|
|
|
|
let set_op_assoc = function
|
|
|
|
| [Symbol op; String a] ->
|
|
|
|
let op = symbol_to_op op in
|
|
|
|
Hashtbl.replace Parser.oper_assoc op @@ Parser.assoc_of_string a;
|
|
|
|
Nop
|
|
|
|
| _ -> failwith "set_op_assoc"
|
|
|
|
|
|
|
|
let get_op_assoc = function
|
|
|
|
| [Symbol op] ->
|
|
|
|
let op = symbol_to_op op in
|
|
|
|
Hashtbl.find_opt Parser.oper_assoc op
|
|
|
|
|> Option.value ~default: Parser.Left_to_right
|
|
|
|
|> (fun a -> String (Parser.assoc_to_string a))
|
|
|
|
| _ -> failwith "get_op_assoc"
|
|
|
|
|
2022-02-21 02:17:29 +09:00
|
|
|
let print args =
|
|
|
|
let to_string = function
|
|
|
|
| Int n -> string_of_int n
|
|
|
|
| Float n -> string_of_float n
|
|
|
|
| Bool b -> string_of_bool b
|
|
|
|
| String s -> s
|
|
|
|
| Symbol s -> s
|
|
|
|
| _ -> failwith "print"
|
|
|
|
in
|
|
|
|
List.map to_string args
|
|
|
|
|> List.iter (Printf.printf "%s");
|
|
|
|
Nop
|
|
|
|
|
|
|
|
let println args =
|
|
|
|
ignore @@ print args;
|
|
|
|
Printf.printf "\n";
|
|
|
|
Nop
|
|
|
|
|
2022-02-08 00:46:43 +09:00
|
|
|
let apply f args =
|
2022-02-19 21:46:48 +09:00
|
|
|
let f = match f with
|
|
|
|
| "sin" -> floatfun Float.sin
|
|
|
|
| "cos" -> floatfun Float.cos
|
|
|
|
| "tan" -> floatfun Float.tan
|
|
|
|
| "deg" -> floatfun deg
|
|
|
|
| "rad" -> floatfun rad
|
2022-02-20 03:35:11 +09:00
|
|
|
| "set_op_pre" -> set_op_pre
|
|
|
|
| "get_op_pre" -> get_op_pre
|
|
|
|
| "set_op_assoc" -> set_op_assoc
|
|
|
|
| "get_op_assoc" -> get_op_assoc
|
2022-02-21 02:17:29 +09:00
|
|
|
| "print" -> print
|
|
|
|
| "println" -> println
|
2022-02-19 21:46:48 +09:00
|
|
|
| _ -> raise @@ Invalid f
|
|
|
|
in
|
|
|
|
f args
|
2022-02-08 00:46:43 +09:00
|
|
|
end
|
|
|
|
|
2022-02-19 21:46:48 +09:00
|
|
|
let find_operator op ts =
|
|
|
|
let filter t =
|
|
|
|
List.filter (fun (ts, _) ->
|
|
|
|
match ts with [] -> false | x::_ -> t=x)
|
2022-01-25 15:28:38 +09:00
|
|
|
in
|
2022-02-19 21:46:48 +09:00
|
|
|
let rec aux ops = function
|
|
|
|
| [] -> List.nth_opt ops 0
|
|
|
|
| t::ts ->
|
|
|
|
(match aux (filter t ops) ts with
|
|
|
|
| None -> Option.bind (Type.supertype t) (fun t -> aux ops (t::ts))
|
|
|
|
| Some _ as x -> x)
|
|
|
|
in
|
|
|
|
aux (Operator.get op) ts
|
2022-01-25 15:28:38 +09:00
|
|
|
|
2022-02-19 21:46:48 +09:00
|
|
|
let promote_values =
|
|
|
|
let rec promote_until t v =
|
|
|
|
if Value.typeof v = t
|
|
|
|
then v
|
|
|
|
else promote_until t @@ Value.promote v
|
2022-01-18 16:52:33 +09:00
|
|
|
in
|
2022-02-19 21:46:48 +09:00
|
|
|
List.map2 promote_until
|
|
|
|
|
|
|
|
let unary op v =
|
|
|
|
match find_operator op [Value.typeof v] with
|
|
|
|
| None -> raise No_operation
|
|
|
|
| Some (ts, f) ->
|
|
|
|
let vs = promote_values ts [v] in
|
|
|
|
f vs
|
|
|
|
|
|
|
|
let binop op l r =
|
|
|
|
let open Value in
|
|
|
|
match find_operator op [typeof l; typeof r] with
|
|
|
|
| None -> raise No_operation
|
|
|
|
| Some (ts, f) ->
|
|
|
|
let vs = promote_values ts [l; r] in
|
|
|
|
f vs
|
2022-01-10 23:11:13 +09:00
|
|
|
|
2022-02-08 01:00:31 +09:00
|
|
|
exception Unbound of string
|
|
|
|
|
2022-02-21 02:15:13 +09:00
|
|
|
let rec eval global env ast =
|
|
|
|
let aux = eval global env in (* eval with current env *)
|
2022-02-19 23:17:10 +09:00
|
|
|
match ast with
|
2022-02-17 02:51:56 +09:00
|
|
|
| Nothing -> Nop
|
2022-02-07 01:09:57 +09:00
|
|
|
| Nint n -> Int n
|
|
|
|
| Nfloat n -> Float n
|
2022-02-08 16:05:33 +09:00
|
|
|
| Nbool b -> Bool b
|
2022-02-07 01:09:57 +09:00
|
|
|
| Nstring s -> String s
|
2022-02-08 00:26:03 +09:00
|
|
|
| Nsymbol s -> Symbol s
|
2022-02-18 16:49:20 +09:00
|
|
|
| Nfunction (arg, e) -> Function (None, arg, e, env)
|
2022-02-07 01:09:57 +09:00
|
|
|
| Nexternal f -> External f
|
2022-02-09 17:27:19 +09:00
|
|
|
|
2022-02-01 02:13:14 +09:00
|
|
|
| Var v -> begin match Env.get_opt env v with
|
2022-02-21 02:15:13 +09:00
|
|
|
| None -> (try Hashtbl.find global v
|
|
|
|
with Not_found -> raise @@ Unbound v)
|
2022-01-20 01:35:18 +09:00
|
|
|
| Some v -> v
|
|
|
|
end
|
2022-02-09 17:27:19 +09:00
|
|
|
| Letin (v, e, f) ->
|
2022-02-12 03:18:00 +09:00
|
|
|
let env = Env.bind (v, aux e) env in
|
2022-02-21 02:15:13 +09:00
|
|
|
eval global env f
|
2022-02-09 17:27:19 +09:00
|
|
|
|
2022-02-19 21:46:48 +09:00
|
|
|
| Unary (op, v) -> unary op (aux v)
|
|
|
|
| Binop (l, op, r) -> binop op (aux l) (aux r)
|
2022-02-19 23:17:10 +09:00
|
|
|
|
2022-02-08 16:05:33 +09:00
|
|
|
| If (co, th, el) ->
|
|
|
|
begin match aux co with
|
|
|
|
| Bool true -> aux th
|
|
|
|
| Bool false -> aux el
|
|
|
|
| v -> raise @@ Type.Invalid (Value.typeof v)
|
|
|
|
end
|
2022-02-22 17:01:39 +09:00
|
|
|
| Apply (v, args) ->
|
|
|
|
let args = List.map (eval global env) args in
|
|
|
|
apply global env v args
|
2022-02-10 01:37:01 +09:00
|
|
|
|
2022-02-08 01:22:49 +09:00
|
|
|
| _ -> failwith "Eval.eval"
|
|
|
|
|
2022-02-19 23:17:10 +09:00
|
|
|
(* apply args to result of expr *)
|
2022-02-21 02:15:13 +09:00
|
|
|
and apply global env expr args =
|
|
|
|
match eval global env expr with
|
2022-02-22 17:01:39 +09:00
|
|
|
| Function (itself, vars, body, local_env) as f ->
|
2022-02-19 23:17:10 +09:00
|
|
|
begin match args with
|
|
|
|
| [] -> f
|
2022-02-22 17:01:39 +09:00
|
|
|
| args ->
|
|
|
|
let rec aux e = function
|
|
|
|
| [], [] -> [], [], e
|
|
|
|
| vars, [] -> vars, [], e
|
|
|
|
| [], args -> [], args, e
|
|
|
|
| v::vars, a::args ->
|
|
|
|
let e = Env.bind (v, a) e in
|
|
|
|
aux e (vars, args)
|
|
|
|
in
|
|
|
|
let vars, args, env = aux local_env (vars, args) in
|
2022-02-19 23:17:10 +09:00
|
|
|
let env = (* binding itself into env for recursion *)
|
2022-02-22 17:01:39 +09:00
|
|
|
itself |> Option.fold
|
|
|
|
~none: env
|
|
|
|
~some: (fun n -> Env.bind (n, f) env)
|
2022-02-19 23:17:10 +09:00
|
|
|
in
|
2022-02-22 17:01:39 +09:00
|
|
|
if vars <> [] then (* partially-applied function *)
|
|
|
|
Function (None, vars, body, env)
|
|
|
|
else if args <> [] then (* reapply *)
|
|
|
|
apply global env body args
|
|
|
|
else (* eval (vars = [], args = []) *)
|
|
|
|
eval global env body
|
2022-02-19 23:17:10 +09:00
|
|
|
end
|
2022-02-22 17:01:39 +09:00
|
|
|
| External f -> External.apply f args
|
2022-02-19 23:17:10 +09:00
|
|
|
| v ->
|
|
|
|
if args = [] then v
|
|
|
|
else raise @@ Type.Invalid (Value.typeof v)
|
|
|
|
|
|
|
|
(* toplevel for global let *)
|
2022-02-21 02:15:13 +09:00
|
|
|
let eval_top global ast =
|
2022-02-18 16:49:20 +09:00
|
|
|
let var, v = match ast with
|
2022-02-19 20:39:50 +09:00
|
|
|
| Let (var, Nfunction (arg, e)) -> (* named function *)
|
2022-02-21 02:15:13 +09:00
|
|
|
var, Function (Some var, arg, e, Env.empty)
|
|
|
|
| Let (var, e) -> var, eval global Env.empty e
|
|
|
|
| ast -> "-", eval global Env.empty ast
|
2022-02-18 16:49:20 +09:00
|
|
|
in
|
2022-02-21 02:15:13 +09:00
|
|
|
if var <> "-" then Hashtbl.replace global var v;
|
2022-02-18 16:49:20 +09:00
|
|
|
var, v
|