@@ -54,25 +54,25 @@ aBinOpFunction op = case op of
5454
5555
5656
57- aBinaryOp :: Machine -> Int -> (Int -> Int -> Int ) -> Machine
57+ aBinaryOp :: Machine -> Int -> (Int -> Int -> Int ) -> IO Machine
5858aBinaryOp machine opcode op =
5959 let popped = (doublePopValue (stack machine)) in
6060 let newStack = (snd popped) in
6161 let x = (snd (fst popped)) in
6262 let y = (fst (fst popped)) in
6363 let res = ( op (getIntValue (x)) (getIntValue (y)) ) in
6464 let appendedStack = pushValue newStack (IntVal res) in
65- Machine ((pointer machine)+ 1 ) (bytecode machine) appendedStack (heap machine) (heapAddresses machine)
65+ do return $ ( Machine ((pointer machine)+ 1 ) (bytecode machine) appendedStack (heap machine) (heapAddresses machine))
6666
6767
68- aNegOp :: Machine -> Machine
68+ aNegOp :: Machine -> IO Machine
6969aNegOp machine =
7070 let popped = popValue (stack machine) in
7171 let newStack = (snd popped) in
7272 let v = (getIntValue (fst popped)) in
7373 let negV = 0 - v in
7474 let appendedStack = (pushValue newStack (IntVal negV)) in
75- (Machine ((pointer machine ) + 1 ) (bytecode machine) appendedStack (heap machine) (heapAddresses machine))
75+ do return $ (Machine ((pointer machine ) + 1 ) (bytecode machine) appendedStack (heap machine) (heapAddresses machine))
7676
7777{- ********************************
7878
@@ -93,37 +93,37 @@ bBinOpFunction op = case op of
9393
9494
9595
96- bBinaryOp :: Machine -> Int -> (Bool -> Bool -> Bool ) -> Machine
96+ bBinaryOp :: Machine -> Int -> (Bool -> Bool -> Bool ) -> IO Machine
9797bBinaryOp machine opcode op =
9898 let popped = (doublePopValue (stack machine)) in
9999 let newStack = (snd popped) in
100100 let x = (snd (fst popped)) in
101101 let y = (fst (fst popped)) in
102102 let res = ( op (getBoolValue (x)) (getBoolValue (y)) ) in
103103 let appendedStack = pushValue newStack (BoolVal res) in
104- Machine ((pointer machine)+ 1 ) (bytecode machine) appendedStack (heap machine) (heapAddresses machine)
104+ do return $ ( Machine ((pointer machine)+ 1 ) (bytecode machine) appendedStack (heap machine) (heapAddresses machine) )
105105
106106
107107
108- bNotOp :: Machine -> Machine
108+ bNotOp :: Machine -> IO Machine
109109bNotOp machine =
110110 let popped = popValue (stack machine) in
111111 let newStack = (snd popped) in
112112 let v = (getBoolValue (fst popped)) in
113113 let appendedStack = pushValue newStack (BoolVal v) in
114- (Machine ((pointer machine ) + 1 ) (bytecode machine) appendedStack (heap machine) (heapAddresses machine))
114+ do return $ (Machine ((pointer machine ) + 1 ) (bytecode machine) appendedStack (heap machine) (heapAddresses machine))
115115
116116
117117
118- rBinaryOp :: Machine -> Int -> (Int -> Int -> Bool ) -> Machine
118+ rBinaryOp :: Machine -> Int -> (Int -> Int -> Bool ) -> IO Machine
119119rBinaryOp machine opcode op =
120120 let popped = (doublePopValue (stack machine)) in
121121 let newStack = (snd popped) in
122122 let x = (snd (fst popped)) in
123123 let y = (fst (fst popped)) in
124124 let res = ( op (getIntValue (x)) (getIntValue (y)) ) in
125125 let appendedStack = pushValue newStack (BoolVal res) in
126- Machine ((pointer machine)+ 1 ) (bytecode machine) appendedStack (heap machine) (heapAddresses machine)
126+ do return $ ( Machine ((pointer machine)+ 1 ) (bytecode machine) appendedStack (heap machine) (heapAddresses machine))
127127
128128
129129
@@ -135,7 +135,7 @@ rBinaryOp machine opcode op =
135135
136136 ******************************** -}
137137
138- movOp :: Machine -> Machine
138+ movOp :: Machine -> IO Machine
139139
140140movOp machine =
141141 let debug = 1 in
@@ -144,67 +144,71 @@ movOp machine =
144144 let bcaddr = (opCodeRel machine 1 ) in
145145 let newheap = setInHeap (bcaddr) value (heap machine) in
146146 let newStack = (snd popped) in
147- Machine ((pointer machine)+ 2 ) (bytecode machine) newStack newheap (heapAddresses machine)
147+ do return $ ( Machine ((pointer machine)+ 2 ) (bytecode machine) newStack newheap (heapAddresses machine) )
148148
149- pushOp :: Machine -> Machine
149+ pushOp :: Machine -> IO Machine
150150pushOp machine =
151151 let debug = 1 in
152152 let k = ((pointer machine)+ 1 ) in
153153 let addr = ((bytecode machine) !! (k)) in
154154 let value = (heap machine) !! (addr) in
155155 let newStack = pushValue (stack machine) value in
156- (Machine ((pointer machine)+ 2 ) (bytecode machine) newStack (heap machine) (heapAddresses machine))
156+ do return $ (Machine ((pointer machine)+ 2 ) (bytecode machine) newStack (heap machine) (heapAddresses machine))
157157
158- printOp :: Machine -> Machine
158+ printOp :: Machine -> IO Machine
159159printOp machine =
160160 let popped = popValue (stack machine) in
161161 let newStack = (snd popped) in
162162 let value = (fst popped) in
163- trace (" PRINT " ++ (show value)) (Machine ((pointer machine) + 1 ) (bytecode machine) newStack (heap machine) (heapAddresses machine))
163+ do
164+ putStrLn (" PRINT " ++ (show value))
165+ return $ ((Machine ((pointer machine) + 1 ) (bytecode machine) newStack (heap machine) (heapAddresses machine)))
164166
165- noOp :: Machine -> Machine
166- noOp machine = Machine ((pointer machine) + 1 ) (bytecode machine) (stack machine) (heap machine) (heapAddresses machine)
167167
168+ noOp :: Machine -> IO Machine
169+ noOp machine = do return $ (Machine ((pointer machine) + 1 ) (bytecode machine) (stack machine) (heap machine) (heapAddresses machine))
168170
169171
170172
171173
172- unconditionalJump :: Machine -> Machine
174+
175+ unconditionalJump :: Machine -> IO Machine
173176unconditionalJump machine = let dest = opCodeRel machine 1 in
174- Machine dest (bytecode machine) (stack machine) (heap machine) (heapAddresses machine)
177+ do return $ ( Machine dest (bytecode machine) (stack machine) (heap machine) (heapAddresses machine) )
175178
176- jumpIfTrue :: Machine -> Machine
179+ jumpIfTrue :: Machine -> IO Machine
177180jumpIfTrue machine = let dest = opCodeRel machine 1 in
178181 let popped = popValue (stack machine) in
179182 if (fst popped) == (BoolVal True ) then
180- Machine dest (bytecode machine) (stack machine) (heap machine) (heapAddresses machine)
183+ do return $ ( Machine dest (bytecode machine) (stack machine) (heap machine) (heapAddresses machine) )
181184 else
182- Machine ((pointer machine)+ 2 ) (bytecode machine) (stack machine) (heap machine) (heapAddresses machine)
185+ do return $ ( Machine ((pointer machine)+ 2 ) (bytecode machine) (stack machine) (heap machine) (heapAddresses machine) )
183186
184- jumpIfNotTrue :: Machine -> Machine
187+ jumpIfNotTrue :: Machine -> IO Machine
185188jumpIfNotTrue machine = let dest = opCodeRel machine 1 in
186189 let popped = popValue (stack machine) in
187190 if (fst popped) == (BoolVal False ) then
188- Machine dest (bytecode machine) (snd popped) (heap machine) (heapAddresses machine)
191+ do return $ ( Machine dest (bytecode machine) (snd popped) (heap machine) (heapAddresses machine) )
189192 else
190- Machine ((pointer machine)+ 2 ) (bytecode machine) (snd popped) (heap machine) (heapAddresses machine)
193+ do return $ ( Machine ((pointer machine)+ 2 ) (bytecode machine) (snd popped) (heap machine) (heapAddresses machine) )
191194
192195{- ********************************
193196
194197 ENTRY POINT
195198
196199 ******************************** -}
197200
198- runIt :: Machine -> Machine
201+ runIt :: Machine -> IO Machine
199202
200203runIt machine
201- | (pointer machine) >= (length (bytecode machine)) = machine
204+ | (pointer machine) >= (length (bytecode machine)) = do
205+ return machine
202206 | otherwise = runIt' machine
203207
204208
205209
206210
207- genericOpCodeFunctions :: Int -> (Machine -> Machine )
211+ genericOpCodeFunctions :: Int -> (Machine -> IO Machine )
208212genericOpCodeFunctions opcode = case opcode of
209213 1 -> pushOp
210214 3 -> movOp
@@ -219,25 +223,33 @@ genericOpCodeFunctions opcode = case opcode of
219223
220224
221225
222- runIt' :: Machine -> Machine
226+ runIt' :: Machine -> IO Machine
223227runIt' machine
224228 | (opCodeIn machine [1 ,3 ,8 ,9 ,15 ,16 ,17 ,18 ,19 ]) =
225229 let opCodeFunction = (genericOpCodeFunctions (opCode machine)) in
226230 let opIt= opCodeFunction machine in
227- runIt opIt
231+ do
232+ machine <- opIt
233+ runIt machine
228234 | (opCodeIn machine [4 .. 7 ]) =
229235 let opIt = (aBinaryOp machine (opCode machine) (aBinOpFunction (opCode machine))) in
230- runIt opIt
236+ do
237+ machine <- opIt
238+ runIt machine
231239 | (opCodeIn machine [10 ,11 ]) =
232240 let opIt = (bBinaryOp machine (opCode machine) (bBinOpFunction (opCode machine))) in
233- runIt opIt
241+ do
242+ machine <- opIt
243+ runIt machine
234244 | (opCodeIn machine [12 ,14 ]) =
235245 let opIt = (rBinaryOp machine (opCode machine) (rBinOpFunction (opCode machine))) in
236- runIt opIt
237- | otherwise = Machine (- 1 ) [] [] [] []
246+ do
247+ machine <- opIt
248+ runIt machine
249+ | otherwise = do return $ Machine (- 1 ) [] [] [] []
238250
239251
240- runMachine :: Machine -> Machine
252+ runMachine :: Machine -> IO Machine
241253runMachine machine = runIt machine
242254
243255
0 commit comments