Skip to content

context

Classes:

SemanticContext dataclass

SemanticContext(
    scopes: ScopeStack = ScopeStack(),
    diagnostics: DiagnosticBag = DiagnosticBag(),
    functions: dict[str, SemanticFunction] = dict(),
    structs: dict[str, StructDefStmt] = dict(),
    current_function: SemanticFunction | None = None,
    loop_depth: int = 0,
    _symbol_counter: int = 0,
)

Methods:

in_function

in_function(function: SemanticFunction) -> Iterator[None]
Source code in src/irx/analysis/context.py
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
@contextmanager
def in_function(self, function: SemanticFunction) -> Iterator[None]:
    """
    title: Temporarily set current_function.
    parameters:
      function:
        type: SemanticFunction
    returns:
      type: Iterator[None]
    """
    previous = self.current_function
    self.current_function = function
    try:
        yield
    finally:
        self.current_function = previous

in_loop

in_loop() -> Iterator[None]
Source code in src/irx/analysis/context.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
@contextmanager
def in_loop(self) -> Iterator[None]:
    """
    title: Increase loop depth for loop analysis.
    returns:
      type: Iterator[None]
    """
    self.loop_depth += 1
    try:
        yield
    finally:
        self.loop_depth -= 1

next_symbol_id

next_symbol_id(prefix: str) -> str
Source code in src/irx/analysis/context.py
49
50
51
52
53
54
55
56
57
58
59
def next_symbol_id(self, prefix: str) -> str:
    """
    title: Return a fresh semantic symbol id.
    parameters:
      prefix:
        type: str
    returns:
      type: str
    """
    self._symbol_counter += 1
    return f"{prefix}:{self._symbol_counter}"

scope

scope(kind: str) -> Iterator[None]
Source code in src/irx/analysis/context.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
@contextmanager
def scope(self, kind: str) -> Iterator[None]:
    """
    title: Push/pop a scope around a block of work.
    parameters:
      kind:
        type: str
    returns:
      type: Iterator[None]
    """
    self.scopes.push(kind)
    try:
        yield
    finally:
        self.scopes.pop()