Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Compiler
- open Syntax
- type PrimValue =
- | BoolVal of bool
- | IntVal of int
- type BinaryOps =
- | ADD
- | MIN
- | MUL
- | DIV
- let pushNum i =
- match i with
- | IntVal x -> "ldc.i4.s " + string x
- | _ -> failwith "Syntax error"
- let pushOperation op =
- match op with
- | ADD -> "add"
- | MIN -> "sub"
- | MUL -> "mul"
- | DIV -> "div"
- let rec eval expr lst =
- match expr with
- | ConstInt i -> (pushNum (IntVal i)) :: lst
- | PrimOp ("+", lOper, rOper) -> evalNumericExpr ADD lOper rOper lst
- | PrimOp ("-", lOper, rOper) -> evalNumericExpr MIN lOper rOper lst
- | PrimOp ("*", lOper, rOper) -> evalNumericExpr MUL lOper rOper lst
- | PrimOp ("/", lOper, rOper) -> evalNumericExpr DIV lOper rOper lst
- | _ -> failwith "Syntax error"
- and evalNumericExpr op lOper rOper lst =
- let left = eval lOper lst
- let right = eval rOper lst
- let operation = pushOperation op
- lst @ left @ right @ [operation]
- let compile expr =
- eval expr []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement