Chronaxis用户手册English
本页目录

chronaxis.core.lookup_compiler

源码基线:8904c2f744951857659abd40aaa3535d57faf53c

1"""将 v4 lookup 的固定签名和表名引用编译为单输入节点。23输入只接受表达式 AST 和当前有身份的绑定。先校验关键字、单位与类型,4随后准备引用表的只读列;不访问其他配置或数据来源。5"""67import ast8from dataclasses import replace9from .expression import _node10from .types import SignalDType as D11from .unit_registry import output_label, resolve_unit121314def compile_lookup(compiler, node):15    """编译查表调用。参数: v4 编译器和调用 AST。返回: 有单位的节点。异常: 签名、表名、类型或单位错。"""16    if len(node.args) != 2:17        compiler._error(node, 'lookup需要两个位置参数:数值和表名字面量')18    name = node.args[1]19    if not isinstance(name, ast.Constant) or type(name.value) is not str:20        compiler._error(name, 'lookup表名须为字符串字面量')21    table = next((item for item in compiler.bindings.lookups if item.name == name.value), None)22    if table is None:23        compiler._error(name, f'未声明的lookup表:{name.value}')24    options = {}25    for keyword in node.keywords:26        if keyword.arg not in ('mode', 'bounds') or keyword.arg in options:27            compiler._error(keyword.value, 'lookup仅接受不重复的mode/bounds关键字')28        if not isinstance(keyword.value, ast.Constant) or type(keyword.value.value) is not str:29            compiler._error(keyword.value, 'lookup选项须为字符串字面量')30        options[keyword.arg] = keyword.value.value31    child = compiler.visit(node.args[0])32    if child.dtype not in (D.INT64, D.UINT64, D.FLOAT64):33        compiler._error(node.args[0], 'lookup仅接受数值输入')34    # 唯一尺度锚点为表轴。裸数按该声明单位解释,quantity 常量保留其原单位再换算。35    if child.constant and child.unit == '1' and not child.unit_explicit and not child.typed_constant:36        child = replace(child, unit=table.input_unit)37    child = compiler._convert(child, table.input_unit, node)38    from .expression_math_v4 import check_integer_leaves39    check_integer_leaves(compiler, (child,), node)40    try:41        prepared = table.prepare(options.get('mode', 'linear'), options.get('bounds', 'invalid'))42    except ValueError as error:43        compiler._error(node, str(error))44    label = output_label(resolve_unit(table.unit), table.unit)45    # 即便输入为裸常量,结果也有表声明的独立单位,不得在外围算术中重新解释。46    result = compiler._known_node(_node('lookup', prepared.dtype, label, (child,), value=prepared))47    return replace(result, constant=False, typed_constant=child.constant or child.typed_constant, unit_explicit=True)48
Chronaxis 用户手册 · 离线阅读