ce/eval.ml
2022-01-10 23:27:37 +09:00

18 lines
454 B
OCaml

open Ast
let binop_to_func : type a b. (a, b) Ast.binop -> (a -> a -> b) = function
| Add -> Int.add
| Sub -> Int.sub
| Mul -> Int.mul
| Div -> Int.div
| Eq -> (=)
let rec eval : type a. a Ast.t -> a = function
| Value (Int t) -> t
| Value Unit -> ()
| Binop (l, op, r) ->
let f = binop_to_func op in
f (eval l) (eval r)
| Set_binop_pre (op, l) ->
Hashtbl.replace Parser.precedence
(Ast.binop_to_string op) (eval l)