diff options
| author | 2024-02-28 12:09:56 +0800 | |
|---|---|---|
| committer | 2024-02-28 12:09:56 +0800 | |
| commit | 8793ecea74706aedb4674434f65ef9d05850b769 (patch) | |
| tree | 425fcec75d4f899760a0919fa0604327b20309f9 /nivis_python/interpreter.py | |
| parent | ab026b7e0ba3baa1ffab7603e7f5fa47864427f7 (diff) | |
| download | TRPGNivis-8793ecea74706aedb4674434f65ef9d05850b769.tar.gz TRPGNivis-8793ecea74706aedb4674434f65ef9d05850b769.zip | |
refactor(bones)!: rename nivis-python -> nivis_python
Diffstat (limited to 'nivis_python/interpreter.py')
| -rw-r--r-- | nivis_python/interpreter.py | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/nivis_python/interpreter.py b/nivis_python/interpreter.py new file mode 100644 index 0000000..6322180 --- /dev/null +++ b/nivis_python/interpreter.py @@ -0,0 +1,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 |
