from dataclasses import dataclass from enum import Enum, auto from typing import Any from .typeclass import TypeEnum from .exceptions import NebPanic #from . import Function # tokens and types # NOTE: this can probably be simplified class TokenType(Enum): OPEN_PAREN = auto() CLOSE_PAREN = auto() OPEN_BRACKET = auto() CLOSE_BRACKET = auto() EOF = auto() # literals INT = auto() FLOAT = auto() STRING = auto() TRUE = auto() FALSE = auto() # keywords IF = auto() FOR_COUNT = auto() DEF = auto() LAMBDA = auto() FUNC = auto() # symbols SYMBOL = auto() # types INT_TYPE = auto() FLOAT_TYPE = auto() NUMBER_TYPE = auto() STRING_TYPE = auto() ANY_TYPE = auto() LIST_TYPE = auto() LITERAL_TYPE = auto() BOOL_TYPE = auto() USER_TYPE = auto() MANY = auto() COLON = auto() @dataclass class Token: type_: TokenType text: str value: Any line: int def __str__(self): return f"{self.type_.name} {self.text} {self.line}" class Literal: def __init__(self, value, type_=None): self.value = value if type_ is None: self.type_ = TypeEnum.ANY else: self.type_ = type_ def __str__(self): return f"{self.value}:literal" class Int(Literal): def __init__(self, value): super().__init__(value, TypeEnum.INT) def __str__(self): return f"{self.value}" class Float(Literal): def __init__(self, value): super().__init__(value, TypeEnum.FLOAT) def __str__(self): return f"{self.value}" class Bool(Literal): def __init__(self, value): super().__init__(value, TypeEnum.BOOL) def __str__(self): return f"#{str(self.value).lower()}" class String(Literal): def __init__(self, value): super().__init__(value, TypeEnum.STRING) def __str__(self): return f'"{repr(self.value)[1:-1]}"' class Type: def __init__(self, name): self.name = name def __str__(self): return self.name class Symbol: def __init__(self, name, line): self.name = name self.line = line self.type_ = TypeEnum.ANY # TODO no it's not def __str__(self): return f"{self.name}" class Expr: def __init__(self, args): self.args = args self.type_ = TypeEnum.ANY # TODO no it's not def __str__(self): return "(" + " ".join(f"{arg}" for arg in self.args) + ")" class List: def __init__(self, args): self.args = args self.type_ = TypeEnum.LIST def __str__(self): return "(" + " ".join(f"{arg}" for arg in self.args) + ")" # function things class Arg: def __init__(self, name, type_, *, optional=False, lazy=False): self.name = name self.type_ = type_ self.optional = optional self.lazy = lazy def __str__(self): return f"{self.name} {self.type_}" def string_args(args, many): out = [f"{arg}" for arg in args] if many is not None: many_dup = Arg("&", many.type_) out.append(f"{many_dup}") return " ".join(out).strip() class Environment: def __init__(self, parent=None): self.parent = parent self.environment = {} def register(self, key, value): self.environment[key] = value def reregister(self, key, value): if not self.contains(key): raise NebPanic(f"undefined symbol: '{key}") if key in self.environment: self.register(key, value) else: self.parent.reregister(key, value) def contains(self, key): if key in self.environment: return True elif self.parent is not None: return self.parent.contains(key) else: return False def get(self, key): try: return self.environment[key] except: pass try: return self.parent.get(key) except: raise NebPanic(f"undefined symbol: '{key}") def __str__(self): out = "" for k, v in self.environment.items(): out += f"{k}: {v}, " return out