-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#19 [program] added MetaTable and used it
- Loading branch information
Showing
10 changed files
with
74 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .error import SleepyError | ||
from .meta_table import UID, Identifiable, MetaTable | ||
from .source_location import SourceLocation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from abc import ABC, abstractproperty | ||
from typing import Generic, NewType, TypeVar | ||
|
||
UID = NewType("UID", int) | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
class Identifiable(ABC): | ||
@abstractproperty | ||
def uid(self) -> UID: | ||
raise NotImplementedError | ||
|
||
|
||
class MetaTable(Generic[T]): | ||
def __init__(self) -> None: | ||
self.entries: dict[UID, T] = {} | ||
|
||
def __getitem__(self, key: Identifiable) -> T: | ||
try: | ||
return self.entries[key.uid] | ||
except KeyError as e: | ||
e.add_note(f"key: {key!r}") | ||
raise | ||
|
||
def __setitem__(self, key: Identifiable, value: T) -> None: | ||
if key.uid in self.entries: | ||
message = f"can't assign {value}, to key with id {key.uid}: {key!r}" | ||
raise KeyError(message) | ||
self.entries[key.uid] = value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,6 @@ | |
Program, | ||
ProgramNode, | ||
Symbol, | ||
SymbolId, | ||
) | ||
from .unit import ProgramUnit | ||
from .visitor import Visitor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from sleepy.core import SleepyError | ||
|
||
|
||
class SemanticError(SleepyError): | ||
pass | ||
|
||
|
||
class NamespaceError(SleepyError): | ||
pass | ||
|
||
|
||
class DefinitionError(NamespaceError): | ||
def __init__(self, name: str, reason: str) -> None: | ||
super().__init__(f"failed to define a name {name}: {reason}") | ||
|
||
|
||
class NameNotFoundError(NamespaceError): | ||
def __init__(self, name: str) -> None: | ||
super().__init__(f"failed to resolve a name {name}: not found") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters