Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 401eb60

Browse filesBrowse files
committed
Add Interpreter class for code interpretation
1 parent c455233 commit 401eb60
Copy full SHA for 401eb60

File tree

Expand file treeCollapse file tree

1 file changed

+35
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+35
-0
lines changed
Open diff view settings
Collapse file

‎test_lang.py‎

Copy file name to clipboard
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Interpreter:
2+
def __init__(self):
3+
self.variables = {}
4+
5+
def interpret(self, code):
6+
tokens = code.split()
7+
if len(tokens) < 3:
8+
raise SyntaxError("Invalid syntax")
9+
10+
if tokens[1] != "=":
11+
raise SyntaxError("Invalid assignment")
12+
13+
variable = tokens[0]
14+
operator = tokens[2]
15+
value = int(tokens[3])
16+
17+
if operator == "+":
18+
self.variables[variable] = self.variables.get(variable, 0) + value
19+
elif operator == "-":
20+
self.variables[variable] = self.variables.get(variable, 0) - value
21+
elif operator == "*":
22+
self.variables[variable] = self.variables.get(variable, 0) * value
23+
elif operator == "/":
24+
self.variables[variable] = self.variables.get(variable, 0) / value
25+
else:
26+
raise SyntaxError("Invalid operator")
27+
28+
return self.variables[variable]
29+
30+
31+
interpreter = Interpreter()
32+
code = input("Enter code: ")
33+
result = interpreter.interpret(code)
34+
print("Result:", result)
35+

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.