Skip to content

scopes

Classes:

Scope dataclass

Scope(
    kind: str, symbols: dict[str, SemanticSymbol] = dict()
)

ScopeStack

ScopeStack()

Methods:

Attributes:

Source code in src/irx/analysis/scopes.py
39
40
41
42
43
def __init__(self) -> None:
    """
    title: Initialize ScopeStack.
    """
    self._stack: list[Scope] = []

current property

current: Scope | None

declare

declare(symbol: SemanticSymbol) -> bool
Source code in src/irx/analysis/scopes.py
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def declare(self, symbol: SemanticSymbol) -> bool:
    """
    title: Declare a symbol in the current scope.
    parameters:
      symbol:
        type: SemanticSymbol
    returns:
      type: bool
    """
    current = self.current
    if current is None:
        raise RuntimeError("No active scope")
    if symbol.name in current.symbols:
        return False
    current.symbols[symbol.name] = symbol
    return True

pop

pop() -> Scope
Source code in src/irx/analysis/scopes.py
69
70
71
72
73
74
75
def pop(self) -> Scope:
    """
    title: Pop the current scope.
    returns:
      type: Scope
    """
    return self._stack.pop()

push

push(kind: str) -> Scope
Source code in src/irx/analysis/scopes.py
56
57
58
59
60
61
62
63
64
65
66
67
def push(self, kind: str) -> Scope:
    """
    title: Push a scope.
    parameters:
      kind:
        type: str
    returns:
      type: Scope
    """
    scope = Scope(kind=kind)
    self._stack.append(scope)
    return scope

resolve

resolve(name: str) -> SemanticSymbol | None
Source code in src/irx/analysis/scopes.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def resolve(self, name: str) -> SemanticSymbol | None:
    """
    title: Resolve a symbol by name.
    parameters:
      name:
        type: str
    returns:
      type: SemanticSymbol | None
    """
    for scope in reversed(self._stack):
        symbol = scope.symbols.get(name)
        if symbol is not None:
            return symbol
    return None