Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# LANGUAGE GADTs, EmptyDataDecls #-}
- module Expr where
- type Identifier = String
- data ContextRecord = VariableRecord Identifier Double
- | FunctionRecord Identifier (ExprAST Function)
- type EvalContext = [ContextRecord]
- data BinaryOp a where
- AddOp :: BinaryOp Double
- SubtractOp :: BinaryOp Double
- MultiplyOp :: BinaryOp Double
- DivideOp :: BinaryOp Double
- data Function
- data ExprAST a where
- NumberExprAST :: Double -> ExprAST Double
- VariableExprAST :: Identifier -> ExprAST a
- BinaryExprAST :: BinaryOp Double -> ExprAST Double -> ExprAST Double -> ExprAST Double
- CallExprAST :: Identifier -> ExprAST ()
- FunctionAST :: Identifier -> [ExprAST a] -> ExprAST Function
- {- Test eval -}
- resolveOp :: BinaryOp a -> (a -> a -> a)
- resolveOp AddOp = (+)
- resolveOp SubtractOp = (-)
- resolveOp MultiplyOp = (*)
- resolveOp DivideOp = (/)
- eval :: EvalContext -> ExprAST a -> a
- eval ctx (NumberExprAST n) = n
- eval ctx (BinaryExprAST op e1 e2) = (resolveOp op) (eval ctx e1) (eval ctx e2)
Add Comment
Please, Sign In to add comment