diff --git a/compiler/semantic.py b/compiler/semantic.py index 53f4cc6..0c6c337 100644 --- a/compiler/semantic.py +++ b/compiler/semantic.py @@ -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