ce/eval.ml

341 lines
8.1 KiB
OCaml
Raw Normal View History

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-18 16:49:20 +09:00
(* (name), arg, expression, name *)
| Function of string option * string * 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-12 03:18:00 +09:00
and env = (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 ^ "\""
| Symbol s -> "symbol " ^ s
| 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-12 03:18:00 +09:00
let empty = []
2022-02-08 00:46:43 +09:00
2022-02-12 03:18:00 +09:00
let get_opt e name =
List.assoc_opt name e
2022-02-08 00:46:43 +09:00
2022-02-12 03:18:00 +09:00
let bind v e =
v::e
2022-02-08 00:46:43 +09:00
2022-02-12 03:18:00 +09:00
let bind_seq seq e =
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
| Int n -> Int ~-n
| Float n -> Float ~-.n
| _ -> failwith "Operator.negate"
let vi f a b =
match a, b with
| 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
let vf f a b =
match a, b with
| 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-08 22:02:00 +09:00
let vb intf floatf a b =
match a, b with
| Int a, Int b -> Bool (intf a b)
| Float a, Float b -> Bool (floatf a b)
| _ -> raise @@ Type.Expected Bool
let vnot = function
| Bool b -> Bool (not b)
| _ -> raise @@ Type.Expected Bool
let map ?intf ?floatf ?boolf v =
let app x f = f x in
match v with
| Int i -> Option.map (app i) intf
| Float f -> Option.map (app f) floatf
| Bool b -> Option.map (app b) boolf
| _ -> invalid_arg "Operator.map"
let eq = vb Int.equal Float.equal
let neq a b = vnot @@ eq a b
let compare a b =
match a, b with
| Int a, Int b -> Int.compare a b
| Float a, Float b -> Float.compare a b
| _ -> invalid_arg "Operator.compare"
let ge a b = Bool (compare a b >= 0)
let le a b = Bool (compare a b <= 0)
let gt a b = Bool (compare a b > 0)
let lt a b = Bool (compare a b < 0)
(* operator table *)
2022-02-19 20:39:50 +09:00
(* TODO: refactor operator finding alg (support type vars) *)
2022-02-07 23:12:11 +09:00
let operators =
let open Type in
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-07 23:12:11 +09:00
]
|> List.to_seq
|> Hashtbl.of_seq
let get_types op =
match Hashtbl.find_opt operators op with
| None -> raise @@ Unavailable op
| Some p -> List.map fst p
let get_unary = function
| Negate -> negate
| op -> raise @@ Unavailable op
let get_binary op typ =
Hashtbl.find operators op
|> List.assoc_opt typ
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
| Float n -> Float (f n)
2022-02-08 01:00:31 +09:00
| v -> raise @@ Type.Invalid (Value.typeof v)
2022-02-08 00:46:43 +09:00
let apply f args =
match f, args with
| "sin", [n] -> floatfun Float.sin n
| "cos", [n] -> floatfun Float.cos n
| "tan", [n] -> floatfun Float.tan n
| "deg", [n] -> floatfun deg n
| "rad", [n] -> floatfun rad n
2022-02-08 01:00:31 +09:00
| _ -> raise @@ Invalid f
2022-02-08 00:46:43 +09:00
end
2022-02-01 21:38:00 +09:00
let assert_same_length vars args =
let vl = List.length vars
and al = List.length args in
if vl > al then
failwith "assert_same_length"
else if vl < al then
raise Too_many_arguments
2022-01-25 15:28:38 +09:00
let resolve_type op tp =
2022-01-28 00:56:24 +09:00
let optypes = Operator.get_types op in
2022-01-25 15:28:38 +09:00
let q = Queue.create () in
let rec aux (t1, t2) =
if List.mem (t1, t2) optypes then
t1, t2
else begin
[ Type.supertype t1 |> Option.map (fun t1 -> t1, t2);
Type.supertype t2 |> Option.map (fun t2 -> t1, t2); ]
|> List.filter_map Fun.id
|> List.iter (Fun.flip Queue.push q);
aux @@ Queue.pop q
end
in
aux tp
2022-01-18 16:52:33 +09:00
let rec binop op l r =
2022-02-08 00:46:43 +09:00
let open Value in
2022-01-25 15:28:38 +09:00
let t1 = typeof l and t2 = typeof r in
let t1, t2 = resolve_type op (t1, t2) in
2022-01-18 16:52:33 +09:00
let rec promote_until t x =
if typeof x = t
then x
else promote_until t (promote x)
in
2022-01-25 15:28:38 +09:00
let l = promote_until t1 l
and r = promote_until t2 r in
2022-01-28 00:56:24 +09:00
match Operator.get_binary op (t1, t2) with
2022-01-18 16:52:33 +09:00
| None -> begin
try binop op (promote l) (promote r)
with _ -> raise No_operation
2022-01-18 15:33:56 +09:00
end
2022-01-18 16:52:33 +09:00
| Some f -> f l r
2022-01-10 23:11:13 +09:00
2022-02-08 01:00:31 +09:00
exception Unbound of string
2022-02-19 20:39:50 +09:00
(* TODO: refactor eval, split function into parts *)
2022-02-10 01:37:01 +09:00
let rec eval env ast =
2022-01-19 14:17:04 +09:00
let rec aux = function
2022-02-17 02:51:56 +09:00
| Nothing -> Nop
| Nint n -> Int n
| Nfloat n -> Float n
2022-02-08 16:05:33 +09:00
| Nbool b -> Bool b
| 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)
| 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-08 01:00:31 +09:00
| None -> 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
eval env f
2022-02-09 17:27:19 +09:00
2022-01-23 01:20:34 +09:00
| Unary (op, t) ->
let t = aux t in
2022-01-28 00:56:24 +09:00
let op = Operator.get_unary op in
2022-01-23 01:20:34 +09:00
op t
2022-01-19 14:17:04 +09:00
| Binop (l, op, r) ->
let l = aux l and r = aux r in
binop op l r
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-01-29 20:01:48 +09:00
| Apply (v, args) ->
2022-02-01 21:38:00 +09:00
begin match aux v with
2022-02-18 16:49:20 +09:00
| Function (itself, var, e, env) as f ->
begin match args with
| [] -> f
| a::args ->
2022-02-13 21:10:25 +09:00
let env =
(* binding itself into env for recursion *)
Option.fold
~none: env ~some: (fun v -> Env.bind (v, f) env)
itself
|> Env.bind (var, aux a)
in
2022-02-12 03:18:00 +09:00
eval env @@ Apply (e, args)
end
2022-02-01 21:38:00 +09:00
| External f ->
let args = List.map aux args in
2022-02-08 00:46:43 +09:00
External.apply f args
| v ->
if args = []
then v
else raise @@ Type.Invalid (Value.typeof v)
2022-01-29 20:01:48 +09:00
end
2022-02-10 01:37:01 +09:00
2022-01-19 14:17:04 +09:00
| Set_binop_pre (op, l) ->
let l =
match aux l with
| Int n -> n
2022-02-08 01:00:31 +09:00
| v -> raise @@ Type.Invalid (Value.typeof v)
2022-01-19 14:17:04 +09:00
in
Hashtbl.replace Parser.precedence op l;
Nop
| Get_binop_pre op ->
Int (Hashtbl.find Parser.precedence op)
2022-01-20 23:36:53 +09:00
| Set_binop_aso (op, a) ->
Hashtbl.replace Parser.oper_assoc op @@ Parser.assoc_of_string a;
Nop
| Get_binop_aso op ->
2022-02-08 01:22:49 +09:00
(match Hashtbl.find_opt Parser.oper_assoc op with
| None -> String "left"
| Some a -> String (Parser.assoc_to_string a))
| _ -> failwith "Eval.eval"
2022-01-19 14:17:04 +09:00
in
2022-02-10 01:37:01 +09:00
aux ast
2022-02-08 01:22:49 +09:00
2022-02-12 03:18:00 +09:00
let eval_top env_ref 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-18 16:49:20 +09:00
var, Function (Some var, arg, e, !env_ref)
| Let (var, e) -> var, eval !env_ref e
| ast -> "-", eval !env_ref ast
in
if var <> "-" then env_ref := Env.bind (var, v) !env_ref;
var, v