@@ -65,11 +65,69 @@ run (More fuel) stk (Do c f) =
65
65
run fuel newStk (f res)
66
66
run Dry stk p = pure ()
67
67
68
- data StkInput = Number Integer | Add
68
+ data StkInput = Number Integer | Add | Subtract | Multiply | Negate | Discard | Duplicate
69
69
70
70
strToInput : String -> Maybe StkInput
71
+ strToInput " " = Nothing
72
+ strToInput " add" = Just Add
73
+ strToInput " subtract" = Just Subtract
74
+ strToInput " multiply" = Just Multiply
75
+ strToInput " negate" = Just Negate
76
+ strToInput " discard" = Just Discard
77
+ strToInput " duplicate" = Just Duplicate
78
+ strToInput x = if all isDigit (unpack x) then Just (Number (cast x)) else Nothing
79
+
80
+
71
81
mutual
72
- tryAdd : StackIO height
82
+ duplicate : StackIO height
83
+ duplicate {height = Z } =
84
+ do
85
+ PutStr " Fewer elements than 1"
86
+ stackCalc
87
+ duplicate {height = (S k)} =
88
+ do
89
+ val1 <- Top
90
+ Push val1
91
+ PutStr (" Duplicated " ++ show val1)
92
+ stackCalc
93
+
94
+ discard : StackIO height
95
+ discard {height = Z } =
96
+ do
97
+ PutStr " Fewer elements than 1"
98
+ stackCalc
99
+ discard {height = (S k)} =
100
+ do
101
+ val1 <- Pop
102
+ PutStr (" Discarded " ++ show val1)
103
+ stackCalc
104
+
105
+ negate : StackIO height
106
+ negate {height = Z } =
107
+ do
108
+ PutStr " Fewer elements than 1"
109
+ stackCalc
110
+ negate {height = (S k)} =
111
+ do
112
+ val1 <- Pop
113
+ Push (- val1)
114
+ result <- Top
115
+ PutStr (show result)
116
+ stackCalc
117
+
118
+ tryBinary : (Integer -> Integer -> Integer) -> StackIO height
119
+ tryBinary op {height = (S (S k))} =
120
+ do
121
+ val1 <- Pop
122
+ val2 <- Pop
123
+ Push (val2 `op` val1)
124
+ result <- Top
125
+ PutStr (show result)
126
+ stackCalc
127
+ tryBinary _ =
128
+ do
129
+ PutStr " Fewer elements than 2"
130
+ stackCalc
73
131
74
132
stackCalc : StackIO height
75
133
stackCalc =
@@ -85,7 +143,12 @@ mutual
85
143
do
86
144
Push x
87
145
stackCalc
88
- Just Add => tryAdd
146
+ Just Add => tryBinary (+ )
147
+ Just Subtract => tryBinary (- )
148
+ Just Multiply => tryBinary (* )
149
+ Just Negate => negate
150
+ Just Discard => discard
151
+ Just Duplicate => duplicate
89
152
90
153
partial
91
154
main : IO ()
0 commit comments