Guest User

Untitled

a guest
Jul 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. {-# LANGUAGE GADTs, EmptyDataDecls #-}
  2. module Expr where
  3.  
  4. type Identifier = String
  5.  
  6. data ContextRecord = VariableRecord Identifier Double
  7. | FunctionRecord Identifier (ExprAST Function)
  8.  
  9. type EvalContext = [ContextRecord]
  10.  
  11. data BinaryOp a where
  12. AddOp :: BinaryOp Double
  13. SubtractOp :: BinaryOp Double
  14. MultiplyOp :: BinaryOp Double
  15. DivideOp :: BinaryOp Double
  16.  
  17. data Function
  18.  
  19. data ExprAST a where
  20. NumberExprAST :: Double -> ExprAST Double
  21. VariableExprAST :: Identifier -> ExprAST a
  22. BinaryExprAST :: BinaryOp Double -> ExprAST Double -> ExprAST Double -> ExprAST Double
  23. CallExprAST :: Identifier -> ExprAST ()
  24. FunctionAST :: Identifier -> [ExprAST a] -> ExprAST Function
  25.  
  26. {- Test eval -}
  27. resolveOp :: BinaryOp a -> (a -> a -> a)
  28. resolveOp AddOp = (+)
  29. resolveOp SubtractOp = (-)
  30. resolveOp MultiplyOp = (*)
  31. resolveOp DivideOp = (/)
  32.  
  33. eval :: EvalContext -> ExprAST a -> a
  34. eval ctx (NumberExprAST n) = n
  35. eval ctx (BinaryExprAST op e1 e2) = (resolveOp op) (eval ctx e1) (eval ctx e2)
Add Comment
Please, Sign In to add comment