From 05f68ac90f83df0c7d1c994cb11cd6e69ab7611f Mon Sep 17 00:00:00 2001 From: mryouse Date: Fri, 10 Jun 2022 04:02:10 +0000 Subject: initial commit of more thorough type checking --- structs.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 7 deletions(-) (limited to 'structs.py') diff --git a/structs.py b/structs.py index 0cd4a1e..d6b0b67 100644 --- a/structs.py +++ b/structs.py @@ -56,6 +56,8 @@ class TokenType(Enum): FLOAT_TYPE = auto() STRING_TYPE = auto() ANY_TYPE = auto() + LIST_TYPE = auto() + @dataclass class Token: @@ -67,26 +69,67 @@ class Token: def __str__(self): return f"{self.type_.name} {self.text} {self.line}" +class T: + def __repr__(self): + return "T" + class Any: + def __repr__(self): + return ":any" + class List(Any): + pass + class Literal(Any): + def __repr__(self): + return ":literal" + class String(Literal): + def __repr__(self): + return ":string" + pass + class Bool(Literal): + def __repr__(self): + return ":bool" + pass + class Number(Literal): + def __repr__(self): + return ":number" + class Int(Number): + def __repr__(self): + return ":int" + class Float(Number): + def __repr__(self): + return ":float" + # Literals -class Literal: - def __init__(self, value): +class Literal(T.Any): + def __init__(self, value, type_=None): self.value = value + if type_ is None: + self.type_ = T.Any + else: + self.type_ = type_ def __str__(self): return f"{self.value}:literal" -class Int(Literal): +class Int(Literal, T.Int): + def __init__(self, value): + super().__init__(value, T.Int) def __str__(self): return f"{self.value}" -class Float(Literal): +class Float(Literal, T.Float): + def __init__(self, value): + super().__init__(value, T.Float) def __str__(self): return f"{self.value}" -class Bool(Literal): +class Bool(Literal, T.Bool): + def __init__(self, value): + super().__init__(value, T.Bool) def __str__(self): return f"#{str(self.value).lower()}" -class String(Literal): +class String(Literal, T.String): + def __init__(self, value): + super().__init__(value, T.String) def __str__(self): return f'"{self.value}"' @@ -103,9 +146,20 @@ class Symbol: def __str__(self): return f"'{self.name}" -class List: +class List(T.List): def __init__(self, args, data=False): self.args = args self.data = data def __str__(self): return "(" + " ".join(f"{arg}" for arg in self.args) + ")" + +class TypedList(List): + def __init__(self, args, data=True, type_=Type("string")): + super().__init__(args, data) + self.type_ = type_ + def __str__(self): + return super().__str__() + f"{self.type_}" + +class Strings(TypedList): + def __init__(self, args, data=True): + super().__init__(args, data, Type(":string")) -- cgit v1.2.3