Expression evaluation for TVMScript parser. Parameters ---------- parser : Parser The parser bound with the evaluator. value_table : Dict[str, Any] The value table for expression evaluation. node : doc.AST The root node o
(parser: "Parser", value_table: dict[str, Any], node: doc.AST)
| 99 | |
| 100 | @staticmethod |
| 101 | def eval(parser: "Parser", value_table: dict[str, Any], node: doc.AST) -> Any: |
| 102 | """Expression evaluation for TVMScript parser. |
| 103 | |
| 104 | Parameters |
| 105 | ---------- |
| 106 | parser : Parser |
| 107 | The parser bound with the evaluator. |
| 108 | |
| 109 | value_table : Dict[str, Any] |
| 110 | The value table for expression evaluation. |
| 111 | |
| 112 | node : doc.AST |
| 113 | The root node of AST tree node of expression to evaluate. |
| 114 | |
| 115 | Returns |
| 116 | ------- |
| 117 | res : Any |
| 118 | The evaluation result. |
| 119 | """ |
| 120 | self = ExprEvaluator(parser, value_table) |
| 121 | result = self._visit(node) # pylint: disable=protected-access |
| 122 | if isinstance(result, doc.Name): |
| 123 | if result.id in self.value_table: |
| 124 | return self.value_table[result.id] |
| 125 | else: |
| 126 | builtin = _get_builtin_or_none(result.id) |
| 127 | if builtin: |
| 128 | return builtin |
| 129 | raise ParserError(result, f"Undefined variable: {result.id}") |
| 130 | if isinstance(result, doc.Constant): |
| 131 | return result.value |
| 132 | raise TypeError(f"Unexpected result type: {type(result)}") |
| 133 | |
| 134 | def _add_intermediate_result(self, value: Any) -> doc.Name: |
| 135 | """Add intermediate result during evaluation into value table. |