-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathex02.py
More file actions
49 lines (40 loc) · 1.57 KB
/
Copy pathex02.py
File metadata and controls
49 lines (40 loc) · 1.57 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import ast
class ExtendedInterpreter(ast.NodeVisitor):
def __init__(self):
self.result = 0
def visit_BinOp(self, node):
left = self.visit(node.left)
right = self.visit(node.right)
if isinstance(node.op, ast.Add):
return left + right
elif isinstance(node.op, ast.Sub):
return left - right
elif isinstance(node.op, ast.Mult):
return left * right
elif isinstance(node.op, ast.Div):
return left / right
elif isinstance(node.op, ast.Pow):
return left ** right
else:
raise ValueError(f"Unsupported operation: {type(node.op)}")
def visit_Num(self, node):
return node.n
def visit_UnaryOp(self, node):
operand = self.visit(node.operand)
if isinstance(node.op, ast.USub):
return -operand
elif isinstance(node.op, ast.UAdd):
return +operand
else:
raise ValueError(f"Unsupported unary operation: {type(node.op)}")
def interpret(self, code):
tree = ast.parse(code)
return self.visit(tree.body[0].value)
if __name__ == '__main__':
# Тестування інтерпретатора
interpreter = ExtendedInterpreter()
print(interpreter.interpret("3 + 5 - 2")) # Виведе: 6
print(interpreter.interpret("3 * 5")) # Виведе: 15
print(interpreter.interpret("10 / 2")) # Виведе: 5.0
print(interpreter.interpret("2 ** 3 + (1 - 3)")) # Виведе: 6
print(interpreter.interpret("-3 + 5")) # Виведе: 2