Spaces:
No application file
No application file
Create 20. Own programming language, Part 3: Jump command
Browse files
Week 7 Libraries and More Python/20. Own programming language, Part 3: Jump command
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'''Jump command
|
| 2 |
+
|
| 3 |
+
Implement the function
|
| 4 |
+
execute(code: str) -> str
|
| 5 |
+
in addition to the properties of the previous functions, the following additional commands:
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
LABEL location
|
| 9 |
+
The command adds the named location to the code.
|
| 10 |
+
The command does not by itself cause any changes in the code execution.
|
| 11 |
+
The given location may be any STRING of letters and numbers.
|
| 12 |
+
|
| 13 |
+
GOTO place
|
| 14 |
+
Code execution continues from the given named location.
|
| 15 |
+
The location must be named by the LABEL instruction.
|
| 16 |
+
The location may be in the code before or after the jump instruction.
|
| 17 |
+
The program need not worry about possible eternal loops, this is left to the programmer.
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
Example programs for testing instructions:
|
| 22 |
+
|
| 23 |
+
Program:
|
| 24 |
+
LET a=29
|
| 25 |
+
GOTO end
|
| 26 |
+
INC a 10 # SKIP THIS LINE
|
| 27 |
+
INC a 20 # SKIP THIS LINE
|
| 28 |
+
|
| 29 |
+
LABEL end
|
| 30 |
+
PRINT a
|
| 31 |
+
The program returns the printout:
|
| 32 |
+
29
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
Program:
|
| 41 |
+
LET a=88
|
| 42 |
+
GOTO point1
|
| 43 |
+
INC a 10 # SKIP THIS LINE
|
| 44 |
+
INC a 20 # SKIP THIS LINE
|
| 45 |
+
|
| 46 |
+
LABEL point1
|
| 47 |
+
PRINT a
|
| 48 |
+
|
| 49 |
+
GOTO per2
|
| 50 |
+
DEC a 20 # SKIP THIS LINE
|
| 51 |
+
DEC a 30 # SKIP THIS LINE
|
| 52 |
+
|
| 53 |
+
LABEL per2
|
| 54 |
+
PRINT a
|
| 55 |
+
The program returns the result:
|
| 56 |
+
88
|
| 57 |
+
88
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
Program:
|
| 66 |
+
GOTO point1
|
| 67 |
+
|
| 68 |
+
LABEL point3 # from below
|
| 69 |
+
PRINT a
|
| 70 |
+
GOTO end # go to 'end' on the last line
|
| 71 |
+
|
| 72 |
+
LABEL point2 # from below
|
| 73 |
+
DEC a 10
|
| 74 |
+
GOTO point3 # go up to point3
|
| 75 |
+
|
| 76 |
+
LABEL point1 # From line 1
|
| 77 |
+
LET a=53
|
| 78 |
+
GOTO point2 # them go up to point2
|
| 79 |
+
|
| 80 |
+
LABEL end # from above
|
| 81 |
+
|
| 82 |
+
The program returns the result:
|
| 83 |
+
43
|
| 84 |
+
|
| 85 |
+
'''
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def execute(code: str) -> str:
|
| 94 |
+
variables = {'a': 0, 'b': 0, 'c': 0, 'd': 0}
|
| 95 |
+
output = []
|
| 96 |
+
|
| 97 |
+
###################
|
| 98 |
+
# Refactored for this exercise to keep track of line number for LABEL
|
| 99 |
+
###################
|
| 100 |
+
# Split the code into lines and strip whitespace
|
| 101 |
+
lines = [line.strip() for line in code.split('\n')]
|
| 102 |
+
|
| 103 |
+
# Track labels and their corresponding line numbers
|
| 104 |
+
labels = {}
|
| 105 |
+
for i, line in enumerate(lines):
|
| 106 |
+
if line.startswith('LABEL'):
|
| 107 |
+
_, location = line.split()
|
| 108 |
+
labels[location] = i # Store the line number of the label
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
i = 0
|
| 112 |
+
while i < len(lines):
|
| 113 |
+
line = lines[i]
|
| 114 |
+
|
| 115 |
+
# Skip empty lines
|
| 116 |
+
if not line:
|
| 117 |
+
i += 1
|
| 118 |
+
continue
|
| 119 |
+
###################
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
if line.startswith('LET'):
|
| 123 |
+
_, assignment = line.split(' ', 1)
|
| 124 |
+
var, value = assignment.split('=')
|
| 125 |
+
if var in variables:
|
| 126 |
+
variables[var] = int(value)
|
| 127 |
+
i += 1
|
| 128 |
+
else:
|
| 129 |
+
raise ValueError(f"Invalid variable name: {var}")
|
| 130 |
+
elif line.startswith('PRINT'):
|
| 131 |
+
_, var = line.split()
|
| 132 |
+
if var in variables:
|
| 133 |
+
output.append(str(variables[var]))
|
| 134 |
+
i += 1
|
| 135 |
+
else:
|
| 136 |
+
raise ValueError(f"Undefined variable: {var}")
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
##########################################
|
| 142 |
+
# 19. part 2
|
| 143 |
+
##########################################
|
| 144 |
+
elif line.startswith('INC') or line.startswith('DEC'):
|
| 145 |
+
# Handle INC and DEC commands
|
| 146 |
+
parts = line.split()
|
| 147 |
+
command = parts[0]
|
| 148 |
+
var1 = parts[1]
|
| 149 |
+
|
| 150 |
+
if var1 not in variables:
|
| 151 |
+
raise ValueError(f"Invalid variable name: {var1}")
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
if len(parts) == 3:
|
| 157 |
+
# Check if parts[2] is an integer
|
| 158 |
+
try:
|
| 159 |
+
value = int(parts[2]) # Try to convert parts[2] to an integer
|
| 160 |
+
|
| 161 |
+
# If conversion succeeds
|
| 162 |
+
# INC/DEC variable number
|
| 163 |
+
if command == 'INC':
|
| 164 |
+
variables[var1] += value
|
| 165 |
+
elif command == 'DEC':
|
| 166 |
+
variables[var1] -= value
|
| 167 |
+
i += 1
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
# parts[2] is NOT an integer
|
| 173 |
+
except ValueError:
|
| 174 |
+
# INC/DEC variable1 variable2
|
| 175 |
+
var2 = parts[2]
|
| 176 |
+
if var2 not in variables:
|
| 177 |
+
raise ValueError(f"Invalid variable name: {var2}")
|
| 178 |
+
|
| 179 |
+
if command == 'INC':
|
| 180 |
+
variables[var1] += variables[var2]
|
| 181 |
+
elif command == 'DEC':
|
| 182 |
+
variables[var1] -= variables[var2]
|
| 183 |
+
i += 1
|
| 184 |
+
|
| 185 |
+
else:
|
| 186 |
+
raise ValueError(f"Invalid command format: {line}")
|
| 187 |
+
##########################################
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
##########################################
|
| 193 |
+
# 20. part 3
|
| 194 |
+
##########################################
|
| 195 |
+
elif line.startswith('LABEL'):
|
| 196 |
+
# Labels are already processed, so just 'skip' the line
|
| 197 |
+
i += 1
|
| 198 |
+
|
| 199 |
+
elif line.startswith('GOTO'):
|
| 200 |
+
_, location = line.split()
|
| 201 |
+
if location in labels:
|
| 202 |
+
i = labels[location] # Jump to the line number of the label
|
| 203 |
+
else:
|
| 204 |
+
raise ValueError(f"Undefined label: {location}")
|
| 205 |
+
##########################################
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
else:
|
| 209 |
+
raise ValueError(f"Invalid command: {line}")
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
joined_output = '\n'.join(output)
|
| 215 |
+
joined_output+='\n'
|
| 216 |
+
return joined_output
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
eg1='''LET a=29
|
| 223 |
+
GOTO end
|
| 224 |
+
INC a 10
|
| 225 |
+
INC a 20
|
| 226 |
+
LABEL end
|
| 227 |
+
PRINT a'''
|
| 228 |
+
|
| 229 |
+
print(execute(eg1))
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
eg2='''LET a=88
|
| 237 |
+
GOTO point1
|
| 238 |
+
INC a 10
|
| 239 |
+
INC a 20
|
| 240 |
+
LABEL point1
|
| 241 |
+
PRINT a
|
| 242 |
+
GOTO per2
|
| 243 |
+
DEC a 20
|
| 244 |
+
DEC a 30
|
| 245 |
+
LABEL per2
|
| 246 |
+
PRINT a'''
|
| 247 |
+
|
| 248 |
+
print(execute(eg2))
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
eg3='''GOTO point1
|
| 254 |
+
LABEL point3
|
| 255 |
+
PRINT a
|
| 256 |
+
GOTO end
|
| 257 |
+
LABEL point2
|
| 258 |
+
DEC a 10
|
| 259 |
+
GOTO point3
|
| 260 |
+
LABEL point1
|
| 261 |
+
LET a=53
|
| 262 |
+
GOTO point2
|
| 263 |
+
LABEL end'''
|
| 264 |
+
|
| 265 |
+
print(execute(eg3))
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
|