KaiquanMah commited on
Commit
16c3393
·
verified ·
1 Parent(s): d56525e

[FIX] split JUMP into parts, match condition, jump_to_locationname

Browse files
Week 7 Libraries and More Python/{21. Own programming language, Part 4: Condition clause → [FIXED]21. Own programming language, Part 4: Condition clause} RENAMED
@@ -7,7 +7,7 @@ in addition to the properties of the previous functions:
7
 
8
  JUMP place IF variable==value
9
  Code execution will continue from the named location after the command if the value of the given variable satisfies the condition.
10
- You can assume that the value is a constant value (not another variable).
11
  Also, the program does not need to understand anything other than the equality operator.
12
 
13
 
@@ -16,10 +16,10 @@ Example programs for testing instructions:
16
 
17
  Program:
18
  LET a=13
19
- JUMP end IF a==13
20
- INC a 10
21
- INC a 15
22
- LABEL end
23
  PRINT a
24
 
25
  Execution of the program returns:
@@ -31,11 +31,14 @@ Execution of the program returns:
31
 
32
  Program:
33
  LET a=27
34
- LABEL start
 
35
  PRINT a
36
  INC a 1
37
- JUMP end IF a==32
38
- GOTO start
 
 
39
  LABEL end
40
 
41
  Program execution returns:
@@ -52,11 +55,14 @@ Program execution returns:
52
  Programme:
53
  LET c=12
54
  LET d=1
 
55
  LABEL loop
56
  PRINT c
57
  DEC c d
58
- JUMP continue IF c==1
59
- GOTO loop
 
 
60
  LABEL continue
61
 
62
  Program execution returns:
@@ -86,9 +92,6 @@ def execute(code: str) -> str:
86
  variables = {'a': 0, 'b': 0, 'c': 0, 'd': 0}
87
  output = []
88
 
89
- ###################
90
- # Refactored for this exercise to keep track of line number for LABEL
91
- ###################
92
  # Split the code into lines and strip whitespace
93
  lines = [line.strip() for line in code.split('\n')]
94
 
@@ -108,7 +111,6 @@ def execute(code: str) -> str:
108
  if not line:
109
  i += 1
110
  continue
111
- ###################
112
 
113
  if line.startswith('LET'):
114
  _, assignment = line.split(' ', 1)
@@ -165,6 +167,7 @@ def execute(code: str) -> str:
165
  else:
166
  raise ValueError(f"Invalid command format: {line}")
167
  ##########################################
 
168
  ##########################################
169
  # 20. part 3
170
  ##########################################
@@ -180,6 +183,35 @@ def execute(code: str) -> str:
180
  raise ValueError(f"Undefined label: {location}")
181
  ##########################################
182
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  else:
184
  raise ValueError(f"Invalid command: {line}")
185
 
@@ -200,6 +232,11 @@ INC a 15
200
  LABEL end
201
  PRINT a'''
202
 
 
 
 
 
 
203
 
204
 
205
 
@@ -212,6 +249,16 @@ JUMP end IF a==32
212
  GOTO start
213
  LABEL end'''
214
 
 
 
 
 
 
 
 
 
 
 
215
 
216
 
217
 
@@ -225,6 +272,87 @@ JUMP continue IF c==1
225
  GOTO loop
226
  LABEL continue'''
227
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228
 
 
 
 
 
 
 
 
 
 
229
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
 
 
 
7
 
8
  JUMP place IF variable==value
9
  Code execution will continue from the named location after the command if the value of the given variable satisfies the condition.
10
+ You can assume that the value is a CONSTANT VALUE (not another variable).
11
  Also, the program does not need to understand anything other than the equality operator.
12
 
13
 
 
16
 
17
  Program:
18
  LET a=13
19
+ JUMP end IF a==13 # if variable matches the condition, JUMP to the 'end' LABEL
20
+ INC a 10 # SKIP THIS LINE if JUMP. It is in between the JUMP and LABEL end lines
21
+ INC a 15 # SKIP THIS LINE if JUMP. It is in between the JUMP and LABEL end lines
22
+ LABEL end # JUMP here
23
  PRINT a
24
 
25
  Execution of the program returns:
 
31
 
32
  Program:
33
  LET a=27
34
+
35
+ LABEL start # from below
36
  PRINT a
37
  INC a 1
38
+ JUMP end IF a==32 # if variable matches the condition, JUMP to the 'end' LABEL
39
+
40
+ GOTO start # SKIP THIS LINE if JUMP. It is in between the JUMP and LABEL end lines. If not JUMPing, go up to 'start' LABEL
41
+
42
  LABEL end
43
 
44
  Program execution returns:
 
55
  Programme:
56
  LET c=12
57
  LET d=1
58
+
59
  LABEL loop
60
  PRINT c
61
  DEC c d
62
+ JUMP continue IF c==1 # if variable matches the condition, JUMP to the 'continue' LABEL
63
+
64
+ GOTO loop # SKIP THIS LINE if JUMP. It is in between the JUMP and LABEL end lines. If not JUMPing, go up to 'loop' LABEL
65
+
66
  LABEL continue
67
 
68
  Program execution returns:
 
92
  variables = {'a': 0, 'b': 0, 'c': 0, 'd': 0}
93
  output = []
94
 
 
 
 
95
  # Split the code into lines and strip whitespace
96
  lines = [line.strip() for line in code.split('\n')]
97
 
 
111
  if not line:
112
  i += 1
113
  continue
 
114
 
115
  if line.startswith('LET'):
116
  _, assignment = line.split(' ', 1)
 
167
  else:
168
  raise ValueError(f"Invalid command format: {line}")
169
  ##########################################
170
+
171
  ##########################################
172
  # 20. part 3
173
  ##########################################
 
183
  raise ValueError(f"Undefined label: {location}")
184
  ##########################################
185
 
186
+ ##########################################
187
+ # 21. part 4
188
+ ##########################################
189
+ elif line.startswith('JUMP'):
190
+ parts = line.split()
191
+
192
+ # eg JUMP end IF a==13 --becomes--> ['JUMP','end','IF','a==13']
193
+ # Format: JUMP jump_to_locationname IF variable==value
194
+ jump_to_locationname = parts[1]
195
+ condition = parts[3]
196
+ var, value = condition.split('==')
197
+
198
+ # Check if the variable exists
199
+ if var not in variables:
200
+ raise ValueError(f"Undefined variable: {var}")
201
+
202
+ # Check the condition
203
+ if variables[var] == int(value):
204
+ # Jump to the specified label
205
+ if jump_to_locationname in labels:
206
+ i = labels[jump_to_locationname]
207
+ else:
208
+ raise ValueError(f"Undefined label: {jump_to_locationname}")
209
+ else:
210
+ # Continue to the next line
211
+ i += 1
212
+
213
+ ##########################################
214
+
215
  else:
216
  raise ValueError(f"Invalid command: {line}")
217
 
 
232
  LABEL end
233
  PRINT a'''
234
 
235
+ print(execute(eg1))
236
+ 13
237
+
238
+
239
+
240
 
241
 
242
 
 
249
  GOTO start
250
  LABEL end'''
251
 
252
+ print(execute(eg2))
253
+ 27
254
+ 28
255
+ 29
256
+ 30
257
+ 31
258
+
259
+
260
+
261
+
262
 
263
 
264
 
 
272
  GOTO loop
273
  LABEL continue'''
274
 
275
+ print(execute(eg3))
276
+ 12
277
+ 11
278
+ 10
279
+ 9
280
+ 8
281
+ 7
282
+ 6
283
+ 5
284
+ 4
285
+ 3
286
+ 2
287
+
288
+
289
+
290
+
291
+
292
+
293
+
294
+ '''Autograder actual, expected output (53 lines)
295
+
296
+ Testing code:
297
+
298
+ LET a=25
299
+ JUMP end IF a==25
300
+ INC a 10
301
+ INC a 15
302
+ LABEL end
303
+ PRINT a
304
+
305
+ Program outputs:
306
+ 25
307
+
308
+
309
+
310
+
311
+
312
 
313
+ Testing code:
314
+
315
+ LET a=62
316
+ LABEL start
317
+ PRINT a
318
+ INC a 1
319
+ JUMP end IF a==67
320
+ GOTO start
321
+ LABEL end
322
 
323
+ Program outputs:
324
+ 62
325
+ 63
326
+ 64
327
+ 65
328
+ 66
329
+
330
+
331
+
332
+
333
+
334
+ Testing code:
335
+ LET c=13
336
+ LET d=1
337
+ LABEL loop
338
+ PRINT c
339
+ DEC c d
340
+ JUMP continue IF c==1
341
+ GOTO loop
342
+ LABEL continue
343
+
344
+ Program outputs:
345
+ 13
346
+ 12
347
+ 11
348
+ 10
349
+ 9
350
+ 8
351
+ 7
352
+ 6
353
+ 5
354
+ 4
355
+ 3
356
+ 2
357
 
358
+ '''