|
| 1 | +# SPDX-FileCopyrightText: 2023 The meson-python developers |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import ast |
| 8 | +import functools |
| 9 | +import operator |
| 10 | + |
| 11 | +from typing import Any, Dict |
| 12 | + |
| 13 | + |
| 14 | +class Interpreter: |
| 15 | + |
| 16 | + _operators = { |
| 17 | + ast.Add: operator.add, |
| 18 | + ast.Sub: operator.sub, |
| 19 | + ast.Mult: operator.mul, |
| 20 | + ast.Div: operator.truediv, |
| 21 | + } |
| 22 | + |
| 23 | + def __init__(self, variables: Dict[str, Any]): |
| 24 | + self._variables = variables |
| 25 | + |
| 26 | + def eval(self, string: str) -> Any: |
| 27 | + expr = ast.parse(string, mode='eval') |
| 28 | + return self._eval(expr) |
| 29 | + |
| 30 | + __getitem__ = eval |
| 31 | + |
| 32 | + @functools.singledispatchmethod |
| 33 | + def _eval(self, node: ast.Node) -> Any: |
| 34 | + print(node, type(node)) |
| 35 | + raise ValueError |
| 36 | + |
| 37 | + @_eval.register |
| 38 | + def _expression(self, node: ast.Expression) -> Any: |
| 39 | + return self._eval(node.body) |
| 40 | + |
| 41 | + @_eval.register |
| 42 | + def _binop(self, node: ast.BinOp) -> Any: |
| 43 | + func = self._operators.get(type(node.op)) |
| 44 | + if func is None: |
| 45 | + raise ValueError(node.op) |
| 46 | + return func(self._eval(node.left), self._eval(node.right)) |
| 47 | + |
| 48 | + @_eval.register |
| 49 | + def _constant(self, node: ast.Constant) -> Any: |
| 50 | + return node.value |
| 51 | + |
| 52 | + @_eval.register |
| 53 | + def _variable(self, node: ast.Name) -> Any: |
| 54 | + value = self._variables.get(node.id) |
| 55 | + if value is None: |
| 56 | + raise ValueError |
| 57 | + return value |
| 58 | + |
| 59 | + |
| 60 | +def interpolate(string, **variables): |
| 61 | + return string % Interpreter(variables) |
0 commit comments