semantic: add the concept of Types
This commit is contained in:
parent
7bb14210a7
commit
98f0465238
1 changed files with 21 additions and 3 deletions
|
|
@ -1,13 +1,17 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import abc
|
||||||
|
from typing import Literal
|
||||||
|
|
||||||
from . import nodes
|
from . import nodes
|
||||||
from .logger import Logger
|
from .logger import Logger
|
||||||
|
from .typechecking import typecheck
|
||||||
|
|
||||||
logger = Logger(__name__)
|
logger = Logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Variable:
|
class SymbolABC(abc.ABC):
|
||||||
def __init__(self, context: Context, name: str, value: nodes.Value | None = None):
|
def __init__(self, context: Context, name: str, value: nodes.Value | None | Literal["builtin"] = None):
|
||||||
self.context = context
|
self.context = context
|
||||||
self.name = name
|
self.name = name
|
||||||
self.definitions = [value]
|
self.definitions = [value]
|
||||||
|
|
@ -23,11 +27,25 @@ class Variable:
|
||||||
if self._repr_guard:
|
if self._repr_guard:
|
||||||
return str(self)
|
return str(self)
|
||||||
self._repr_guard = True
|
self._repr_guard = True
|
||||||
definitions = [str(d.location().begin) for d in self.definitions]
|
definitions = [str(d.location().begin) for d in self.definitions if d is not None]
|
||||||
self._repr_guard = False
|
self._repr_guard = False
|
||||||
return f"{str(self)} [definitions: {', '.join(definitions)}]"
|
return f"{str(self)} [definitions: {', '.join(definitions)}]"
|
||||||
|
|
||||||
|
|
||||||
|
class Type(SymbolABC):
|
||||||
|
def __init__(self, context: Context, name: str, value: nodes.Value | None | Literal["builtin"] = None):
|
||||||
|
super().__init__(context, name, value)
|
||||||
|
|
||||||
|
|
||||||
|
class Variable(SymbolABC):
|
||||||
|
@typecheck
|
||||||
|
def __init__(self, context: Context, name: str, value: nodes.Value | None = None):
|
||||||
|
super().__init__(context, name, value)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"{self.__class__.__name__}({self.name})"
|
||||||
|
|
||||||
|
|
||||||
class Context:
|
class Context:
|
||||||
_id_sequence = 0
|
_id_sequence = 0
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue