-
Notifications
You must be signed in to change notification settings - Fork 2
/
context.py
51 lines (37 loc) · 1.19 KB
/
context.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from typing import NamedTuple
from lark.lexer import Token
from nodes import Binding, VarBinding
class _ContextElem(NamedTuple):
name: Token
binding: Binding
class Context:
def __init__(self) -> None:
self.data: list[_ContextElem] = []
def clone(self):
ctx = Context()
ctx.data = self.data.copy()
return ctx
def add_binding(self, name, binding: Binding):
self.data.append(_ContextElem(name, binding))
def find_binding(self, name):
for i, binding in enumerate(reversed(self.data)):
if binding.name == name:
return i, binding
raise ValueError
def get_binding(self, idx):
return self.data[~idx]
def get_name(self, idx):
return self.get_binding(idx).name
def get_type(self, idx):
match self.get_binding(idx).binding:
case VarBinding(ty):
return ty
raise ValueError(f"Wrong binding for var {self.get_name(idx)} at {idx}")
def pop_binding(self):
self.data.pop()
@property
def top(self):
"""Return most recent binding"""
return self.data[-1]
def __len__(self):
return len(self.data)