19 lines
454 B
OCaml
19 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)
|