Skip to content

facade

Classes:

Functions:

SemanticAnalyzer

SemanticAnalyzer()

Bases: BaseVisitor

Methods:

Source code in src/irx/analysis/facade.py
65
66
67
68
69
def __init__(self) -> None:
    """
    title: Initialize SemanticAnalyzer.
    """
    self.context = SemanticContext()

analyze

analyze(node: AST) -> AST
Source code in src/irx/analysis/facade.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def analyze(self, node: astx.AST) -> astx.AST:
    """
    title: Analyze one AST root.
    parameters:
      node:
        type: astx.AST
    returns:
      type: astx.AST
    """
    if isinstance(node, astx.Module):
        self.visit(node)
    else:
        with self.context.scope("module"):
            self.visit(node)
    self.context.diagnostics.raise_if_errors()
    return node

visit

visit(node: SubscriptExpr) -> None
Source code in src/irx/analysis/facade.py
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
@dispatch
def visit(self, node: astx.SubscriptExpr) -> None:
    """
    title: Visit SubscriptExpr nodes.
    parameters:
      node:
        type: astx.SubscriptExpr
    """
    self.visit(node.value)
    if not isinstance(node.index, astx.LiteralNone):
        self.visit(node.index)
    value_type = self._expr_type(node.value)
    if isinstance(node.value, astx.LiteralDict):
        if not node.value.elements:
            self.context.diagnostics.add(
                "SubscriptExpr: key lookup on empty dict",
                node=node,
            )
        elif not isinstance(
            node.index,
            (
                astx.LiteralInt8,
                astx.LiteralInt16,
                astx.LiteralInt32,
                astx.LiteralInt64,
                astx.LiteralUInt8,
                astx.LiteralUInt16,
                astx.LiteralUInt32,
                astx.LiteralUInt64,
                astx.LiteralFloat32,
                astx.LiteralFloat64,
                astx.Identifier,
            ),
        ):
            self.context.diagnostics.add(
                "SubscriptExpr: only integer and floating-point "
                "dict keys are supported",
                node=node,
            )
    self._set_type(
        node,
        cast(
            astx.DataType | None,
            getattr(value_type, "value_type", None),
        ),
    )

analyze

analyze(node: AST) -> AST
Source code in src/irx/analysis/facade.py
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
@public
def analyze(node: astx.AST) -> astx.AST:
    """
    title: Analyze one AST root and attach node.semantic sidecars.
    parameters:
      node:
        type: astx.AST
    returns:
      type: astx.AST
    """
    return SemanticAnalyzer().analyze(node)

analyze_module

analyze_module(module: Module) -> Module
Source code in src/irx/analysis/facade.py
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
@public
def analyze_module(module: astx.Module) -> astx.Module:
    """
    title: Analyze an AST module.
    parameters:
      module:
        type: astx.Module
    returns:
      type: astx.Module
    """
    return cast(astx.Module, analyze(module))