-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalc.hs
76 lines (58 loc) · 1.59 KB
/
Calc.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
{-# LANGUAGE FlexibleInstances #-}
module Calc where
-- Week 5 solutions
import Control.Applicative
import ExprT
import Parser
import StackVM
-- ex 1
-- |Evaluates an expression.
eval :: ExprT -> Integer
eval (ExprT.Lit x) = x
eval (ExprT.Add x y) = eval x + eval y
eval (ExprT.Mul x y) = eval x * eval y
-- ex 2
evalStr :: String -> Maybe Integer
evalStr s = eval <$> parseExp Lit ExprT.Add ExprT.Mul s
-- -- ex 3
class Expr a where
lit :: Integer -> a
add :: a -> a -> a
mul :: a -> a -> a
instance Expr ExprT where
lit x = Lit x
add x y = ExprT.Add x y
mul x y = ExprT.Mul x y
-- ex 4
instance Expr Integer where
lit x = x
add x y = x + y
mul x y = x * y
instance Expr Bool where
lit = (> 0)
add = (||)
mul = (&&)
newtype MinMax = MinMax Integer deriving (Eq, Show)
newtype Mod7 = Mod7 Integer deriving (Eq, Show)
instance Expr MinMax where
lit x = MinMax x
add (MinMax x) (MinMax y) = MinMax (max x y)
mul (MinMax x) (MinMax y) = MinMax (min x y)
instance Expr Mod7 where
lit x = Mod7 (x `mod` 7)
add (Mod7 x) (Mod7 y) = Mod7 $ (x + y) `mod` 7
mul (Mod7 x) (Mod7 y) = Mod7 $ (x * y) `mod` 7
testExp :: Expr a => Maybe a
testExp = parseExp lit add mul "(3 * -4) + 5"
testInteger = testExp :: Maybe Integer
testBool = testExp :: Maybe Bool
testMM = testExp :: Maybe MinMax
testSat = testExp :: Maybe Mod7
-- ex 5
instance Expr Program where
lit x = [PushI x]
add x y = x ++ y ++ [StackVM.Add]
mul x y = x ++ y ++ [StackVM.Mul]
-- |Compile a string into instructions that can be run on the VM in StackVM.
compile :: String -> Maybe Program
compile = parseExp lit add mul