blob: 632218023cc9dcae8a48dcf294702f56aaccccd5 (
plain) (
blame)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
from .lexer import Token
__all__ = ["Interpreter"]
class Interpreter:
"""
A class representing an interpreter for Psi code.
Args:
ast: The abstract syntax tree (AST) of the code to be interpreted.
Returns:
None
Example:
```python
interpreter = Interpreter(ast)
interpreter.interpret()
```
"""
def __init__(self, ast):
"""
Initializes an Interpreter object.
Args:
ast: The abstract syntax tree (AST) of the code to be interpreted.
Returns:
None
"""
self.ast = ast
def interpret(self):
"""
Interprets the code represented by the AST.
Returns:
The result of the interpretation.
"""
return self.interpret_expr(self.ast)
def interpret_expr(self, node):
"""
Interprets an expression node in the AST.
Args:
node: The expression node to be interpreted.
Returns:
The result of the interpretation.
"""
if isinstance(node, Token):
return node.value
elif isinstance(node, list):
for expr in node:
result = self.interpret_expr(expr)
if result is not None:
return result
def interpret_condition(self, node):
"""
Interprets a condition node in the AST.
Args:
node: The condition node to be interpreted.
Returns:
The result of the interpretation.
"""
variable = self.interpret_expr(node[0])
value = self.interpret_expr(node[2])
return variable == value
|