semantic: add the concept of Types

This commit is contained in:
Antoine Viallon 2023-05-23 00:45:02 +02:00
parent 7bb14210a7
commit 98f0465238
Signed by: aviallon
GPG key ID: D126B13AB555E16F

View file

@ -1,13 +1,17 @@
from __future__ import annotations
import abc
from typing import Literal
from . import nodes
from .logger import Logger
from .typechecking import typecheck
logger = Logger(__name__)
class Variable:
def __init__(self, context: Context, name: str, value: nodes.Value | None = None):
class SymbolABC(abc.ABC):
def __init__(self, context: Context, name: str, value: nodes.Value | None | Literal["builtin"] = None):
self.context = context
self.name = name
self.definitions = [value]
@ -23,11 +27,25 @@ class Variable:
if self._repr_guard:
return str(self)
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
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:
_id_sequence = 0