22 lines
542 B
OCaml
22 lines
542 B
OCaml
open Ast
|
|
|
|
let intop f a b =
|
|
match a, b with
|
|
| Int a, Int b -> Int (f a b)
|
|
| _ -> failwith "typecheck failed"
|
|
|
|
let binop_to_func = function
|
|
| Add -> intop Int.add
|
|
| Sub -> intop Int.sub
|
|
| Mul -> intop Int.mul
|
|
| Div -> intop Int.div
|
|
|
|
let rec eval = function
|
|
| Value v -> v
|
|
| Binop (l, op, r) ->
|
|
let f = binop_to_func op in
|
|
f (eval l) (eval r)
|
|
| Set_binop_pre (op, l) ->
|
|
let l = match eval l with Int n -> n | _ -> failwith "not int" in
|
|
Hashtbl.replace Parser.precedence (Ast.binop_to_string op) l;
|
|
Unit
|