Spaces:
No application file
No application file
Create 21. Own programming language, Part 4: Condition clause
Browse files
Week 7 Libraries and More Python/21. Own programming language, Part 4: Condition clause
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'''Condition clause
|
| 2 |
+
|
| 3 |
+
Implement the function
|
| 4 |
+
execute(code: str) -> str
|
| 5 |
+
in addition to the properties of the previous functions:
|
| 6 |
+
|
| 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 |
+
|
| 14 |
+
|
| 15 |
+
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:
|
| 26 |
+
13
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 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:
|
| 42 |
+
27
|
| 43 |
+
28
|
| 44 |
+
29
|
| 45 |
+
30
|
| 46 |
+
31
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 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:
|
| 63 |
+
12
|
| 64 |
+
11
|
| 65 |
+
10
|
| 66 |
+
9
|
| 67 |
+
8
|
| 68 |
+
7
|
| 69 |
+
6
|
| 70 |
+
5
|
| 71 |
+
4
|
| 72 |
+
3
|
| 73 |
+
2
|
| 74 |
+
'''
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
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 |
+
|
| 95 |
+
# Track labels and their corresponding line numbers
|
| 96 |
+
labels = {}
|
| 97 |
+
for i, line in enumerate(lines):
|
| 98 |
+
if line.startswith('LABEL'):
|
| 99 |
+
_, location = line.split()
|
| 100 |
+
labels[location] = i # Store the line number of the label
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
i = 0
|
| 104 |
+
while i < len(lines):
|
| 105 |
+
line = lines[i]
|
| 106 |
+
|
| 107 |
+
# Skip empty lines
|
| 108 |
+
if not line:
|
| 109 |
+
i += 1
|
| 110 |
+
continue
|
| 111 |
+
###################
|
| 112 |
+
|
| 113 |
+
if line.startswith('LET'):
|
| 114 |
+
_, assignment = line.split(' ', 1)
|
| 115 |
+
var, value = assignment.split('=')
|
| 116 |
+
if var in variables:
|
| 117 |
+
variables[var] = int(value)
|
| 118 |
+
i += 1
|
| 119 |
+
else:
|
| 120 |
+
raise ValueError(f"Invalid variable name: {var}")
|
| 121 |
+
elif line.startswith('PRINT'):
|
| 122 |
+
_, var = line.split()
|
| 123 |
+
if var in variables:
|
| 124 |
+
output.append(str(variables[var]))
|
| 125 |
+
i += 1
|
| 126 |
+
else:
|
| 127 |
+
raise ValueError(f"Undefined variable: {var}")
|
| 128 |
+
##########################################
|
| 129 |
+
# 19. part 2
|
| 130 |
+
##########################################
|
| 131 |
+
elif line.startswith('INC') or line.startswith('DEC'):
|
| 132 |
+
# Handle INC and DEC commands
|
| 133 |
+
parts = line.split()
|
| 134 |
+
command = parts[0]
|
| 135 |
+
var1 = parts[1]
|
| 136 |
+
|
| 137 |
+
if var1 not in variables:
|
| 138 |
+
raise ValueError(f"Invalid variable name: {var1}")
|
| 139 |
+
if len(parts) == 3:
|
| 140 |
+
# Check if parts[2] is an integer
|
| 141 |
+
try:
|
| 142 |
+
value = int(parts[2]) # Try to convert parts[2] to an integer
|
| 143 |
+
|
| 144 |
+
# If conversion succeeds
|
| 145 |
+
# INC/DEC variable number
|
| 146 |
+
if command == 'INC':
|
| 147 |
+
variables[var1] += value
|
| 148 |
+
elif command == 'DEC':
|
| 149 |
+
variables[var1] -= value
|
| 150 |
+
i += 1
|
| 151 |
+
|
| 152 |
+
# parts[2] is NOT an integer
|
| 153 |
+
except ValueError:
|
| 154 |
+
# INC/DEC variable1 variable2
|
| 155 |
+
var2 = parts[2]
|
| 156 |
+
if var2 not in variables:
|
| 157 |
+
raise ValueError(f"Invalid variable name: {var2}")
|
| 158 |
+
|
| 159 |
+
if command == 'INC':
|
| 160 |
+
variables[var1] += variables[var2]
|
| 161 |
+
elif command == 'DEC':
|
| 162 |
+
variables[var1] -= variables[var2]
|
| 163 |
+
i += 1
|
| 164 |
+
|
| 165 |
+
else:
|
| 166 |
+
raise ValueError(f"Invalid command format: {line}")
|
| 167 |
+
##########################################
|
| 168 |
+
##########################################
|
| 169 |
+
# 20. part 3
|
| 170 |
+
##########################################
|
| 171 |
+
elif line.startswith('LABEL'):
|
| 172 |
+
# Labels are already processed, so just 'skip' the line
|
| 173 |
+
i += 1
|
| 174 |
+
|
| 175 |
+
elif line.startswith('GOTO'):
|
| 176 |
+
_, location = line.split()
|
| 177 |
+
if location in labels:
|
| 178 |
+
i = labels[location] # Jump to the line number of the label
|
| 179 |
+
else:
|
| 180 |
+
raise ValueError(f"Undefined label: {location}")
|
| 181 |
+
##########################################
|
| 182 |
+
|
| 183 |
+
else:
|
| 184 |
+
raise ValueError(f"Invalid command: {line}")
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
joined_output = '\n'.join(output)
|
| 188 |
+
joined_output+='\n'
|
| 189 |
+
return joined_output
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
eg1='''LET a=13
|
| 197 |
+
JUMP end IF a==13
|
| 198 |
+
INC a 10
|
| 199 |
+
INC a 15
|
| 200 |
+
LABEL end
|
| 201 |
+
PRINT a'''
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
eg2='''LET a=27
|
| 208 |
+
LABEL start
|
| 209 |
+
PRINT a
|
| 210 |
+
INC a 1
|
| 211 |
+
JUMP end IF a==32
|
| 212 |
+
GOTO start
|
| 213 |
+
LABEL end'''
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
eg3='''LET c=12
|
| 220 |
+
LET d=1
|
| 221 |
+
LABEL loop
|
| 222 |
+
PRINT c
|
| 223 |
+
DEC c d
|
| 224 |
+
JUMP continue IF c==1
|
| 225 |
+
GOTO loop
|
| 226 |
+
LABEL continue'''
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
|