diff options
| author | mryouse | 2022-06-10 04:02:10 +0000 |
|---|---|---|
| committer | mryouse | 2022-06-10 04:04:06 +0000 |
| commit | 05f68ac90f83df0c7d1c994cb11cd6e69ab7611f (patch) | |
| tree | c94b536e1aa08b8d2e6e5169b0e3d2297310a4c6 /structs.py | |
| parent | 2821c14272c4296a64d94532fa8665ed53f5a0ef (diff) | |
initial commit of more thorough type checking
Diffstat (limited to 'structs.py')
| -rw-r--r-- | structs.py | 68 |
1 files changed, 61 insertions, 7 deletions
@@ -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")) |
