ce/eval.ml

178 lines
4.4 KiB
OCaml
Raw Normal View History

2022-01-10 01:31:47 +09:00
open Ast
2022-02-07 23:12:11 +09:00
open Env
open Env.Value
2022-01-10 01:31:47 +09:00
2022-01-18 16:52:33 +09:00
exception No_operation
2022-01-19 14:17:04 +09:00
exception No_such_variable of string
2022-01-29 20:01:48 +09:00
exception No_such_function of string
2022-02-01 21:38:00 +09:00
exception Too_many_arguments
2022-02-08 00:26:03 +09:00
exception Invalid_type of Type.t
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)
| _ -> raise @@ Invalid_type Int
let vf f a b =
match a, b with
| Float a, Float b -> Float (f a b)
| _ -> raise @@ Invalid_type Float
let operators =
let open Type in
let ip = Int, Int and fp = Float, Float in
[
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];
]
|> 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-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-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 00:15:09 +09:00
let rad r =
2022-01-29 20:01:48 +09:00
r *. 180. /. Float.pi
2022-02-08 00:15:09 +09:00
let deg d =
2022-01-29 20:01:48 +09:00
d /. 180. *. Float.pi
let floatfun f = function
| Float n -> Float (f n)
| v -> raise @@ Invalid_type (typeof v)
2022-02-01 21:38:00 +09:00
let ex_apply f args =
2022-01-29 20:01:48 +09:00
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
| _ -> raise @@ No_such_function f
2022-02-01 21:38:00 +09:00
let rec eval env ast =
2022-01-19 14:17:04 +09:00
let rec aux = function
| Nint n -> Int n
| Nfloat n -> Float n
| Nstring s -> String s
2022-02-08 00:26:03 +09:00
| Nsymbol s -> Symbol s
| Nfunction (args, e) -> Function (args, e)
| Nexternal f -> External f
2022-02-01 02:13:14 +09:00
| Var v -> begin match Env.get_opt env v with
2022-01-20 01:35:18 +09:00
| None -> raise @@ No_such_variable v
| Some v -> v
end
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-01-21 00:17:01 +09:00
| Let (var, e) ->
let v = aux e in
2022-02-01 21:38:00 +09:00
Env.set env var v; v
2022-01-29 20:01:48 +09:00
| Apply (v, args) ->
2022-02-01 21:38:00 +09:00
begin match aux v with
| Function (vars, e) ->
assert_same_length vars args;
let args = List.map aux args in
let nenv = Env.make env in
List.combine vars args
|> List.iter (fun (v, a) -> Env.set nenv v a);
eval nenv e
| External f ->
let args = List.map aux args in
ex_apply f args
| v -> raise @@ Invalid_type (typeof v)
2022-01-29 20:01:48 +09:00
end
2022-01-19 14:17:04 +09:00
| Set_binop_pre (op, l) ->
let l =
match aux l with
| Int n -> n
| v -> raise @@ Invalid_type (typeof v)
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 ->
match Hashtbl.find_opt Parser.oper_assoc op with
| None -> String "left"
| Some a -> String (Parser.assoc_to_string a)
2022-01-19 14:17:04 +09:00
in
aux ast