-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_lang.py
35 lines (27 loc) · 1.02 KB
/
test_lang.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Interpreter:
def __init__(self):
self.variables = {}
def interpret(self, code):
tokens = code.split()
if len(tokens) < 3:
raise SyntaxError("Invalid syntax")
if tokens[1] != "=":
raise SyntaxError("Invalid assignment")
variable = tokens[0]
operator = tokens[2]
value = int(tokens[3])
if operator == "+":
self.variables[variable] = self.variables.get(variable, 0) + value
elif operator == "-":
self.variables[variable] = self.variables.get(variable, 0) - value
elif operator == "*":
self.variables[variable] = self.variables.get(variable, 0) * value
elif operator == "/":
self.variables[variable] = self.variables.get(variable, 0) / value
else:
raise SyntaxError("Invalid operator")
return self.variables[variable]
interpreter = Interpreter()
code = input("Enter code: ")
result = interpreter.interpret(code)
print("Result:", result)