From a072842248fe7324574bc7733a8c4255af56c855 Mon Sep 17 00:00:00 2001 From: mryouse Date: Thu, 16 Jun 2022 04:09:26 +0000 Subject: refactor: take type hints from user in function args --- structs.py | 37 ++++--------------------------------- 1 file changed, 4 insertions(+), 33 deletions(-) (limited to 'structs.py') diff --git a/structs.py b/structs.py index 72fabf7..adfd0e4 100644 --- a/structs.py +++ b/structs.py @@ -1,6 +1,7 @@ from dataclasses import dataclass from enum import Enum, auto from typing import Any +from typeclass import TypeEnum # tokens and types # NOTE: this can probably be simplified @@ -54,42 +55,12 @@ class TokenType(Enum): # types INT_TYPE = auto() FLOAT_TYPE = auto() + NUMBER_TYPE = auto() STRING_TYPE = auto() ANY_TYPE = auto() LIST_TYPE = auto() - - -class TypeEnum(Enum): - ANY = auto() - STRING = auto() - INT = auto() - FLOAT = auto() - NUMBER = auto() - LIST = auto() - LITERAL = auto() - BOOL = auto() - - def __str__(self): - return f":{self.name.lower()}" - -TYPE_HIERARCHY = { TypeEnum.ANY: None, - TypeEnum.LITERAL: TypeEnum.ANY, - TypeEnum.LIST: TypeEnum.ANY, - TypeEnum.STRING: TypeEnum.LITERAL, - TypeEnum.BOOL: TypeEnum.LITERAL, - TypeEnum.NUMBER: TypeEnum.LITERAL, - TypeEnum.INT: TypeEnum.NUMBER, - TypeEnum.FLOAT: TypeEnum.NUMBER } - -def is_subtype_of(candidate, expected): - if candidate == expected: - return True - parent = TYPE_HIERARCHY[candidate] - while parent is not None: - if parent == expected: - return True - parent = TYPE_HIERARCHY[parent] - return False + LITERAL_TYPE = auto() + BOOL_TYPE = auto() @dataclass class Token: -- cgit v1.2.3