def base_type(self, tree): """ Resolves a base type expression to a type """ assert tree.data == 'base_type' tok = tree.first_child() if tok.type == 'BOOLEAN_TYPE': return base_symbol(BooleanType.instance()) elif tok.type == 'INT_TYPE': return base_symbol(IntType.instance()) elif tok.type == 'FLOAT_TYPE': return base_symbol(FloatType.instance()) elif tok.type == 'STRING_TYPE': return base_symbol(StringType.instance()) elif tok.type == 'ANY_TYPE': return base_symbol(AnyType.instance()) elif tok.type == 'OBJECT_TYPE': return base_symbol(ObjectType.instance()) elif tok.type == 'FUNCTION_TYPE': return base_symbol(AnyType.instance()) elif tok.type == 'TIME_TYPE': return base_symbol(TimeType.instance()) else: assert tok.type == 'REGEXP_TYPE' return base_symbol(RegExpType.instance())
def type_to_tree(tree, t): """ Converts a type to its respective AST Tree representation. """ if isinstance(t, ListType): inner = SymbolExpressionVisitor.type_to_tree(tree, t.inner) return Tree('list_type', [ Tree('types', [inner]) ]) if isinstance(t, MapType): key = SymbolExpressionVisitor.type_to_tree(tree, t.key) value = SymbolExpressionVisitor.type_to_tree(tree, t.value) return Tree('map_type', [ key, Tree('types', [value]), ]) if t == BooleanType.instance(): base_type = tree.create_token('BOOLEAN_TYPE', 'boolean') elif t == IntType.instance(): base_type = tree.create_token('INTEGER_TYPE', 'int') elif t == FloatType.instance(): base_type = tree.create_token('FLOAT_TYPE', 'float') elif t == StringType.instance(): base_type = tree.create_token('STRING_TYPE', 'string') elif t == TimeType.instance(): base_type = tree.create_token('TIME_TYPE', 'time') elif t == RegExpType.instance(): base_type = tree.create_token('REGEXP_TYPE', 'regex') else: assert t == AnyType.instance() base_type = tree.create_token('ANY_TYPE', 'any') return Tree('base_type', [base_type])
def type_to_tree(tree, t): """ Converts a type to its respective AST Tree representation. """ if isinstance(t, ListType): inner = SymbolExpressionVisitor.type_to_tree(tree, t.inner) return Tree("list_type", [Tree("types", [inner])]) if isinstance(t, MapType): key = SymbolExpressionVisitor.type_to_tree(tree, t.key) value = SymbolExpressionVisitor.type_to_tree(tree, t.value) return Tree("map_type", [key, Tree("types", [value]),]) if t == BooleanType.instance(): base_type = tree.create_token("BOOLEAN_TYPE", "boolean") elif t == IntType.instance(): base_type = tree.create_token("INTEGER_TYPE", "int") elif t == FloatType.instance(): base_type = tree.create_token("FLOAT_TYPE", "float") elif t == StringType.instance(): base_type = tree.create_token("STRING_TYPE", "string") elif t == TimeType.instance(): base_type = tree.create_token("TIME_TYPE", "time") elif t == RegExpType.instance(): base_type = tree.create_token("REGEXP_TYPE", "regex") else: assert t == AnyType.instance() base_type = tree.create_token("ANY_TYPE", "any") return Tree("base_type", [base_type])
def get_type_instance(cls, ty): """ Maps a type class from the hub SDK to its corresponding TypeClass in the compiler. """ assert isinstance(ty, OutputBase), ty if isinstance(ty, OutputBoolean): return BooleanType.instance() if isinstance(ty, OutputInt): return IntType.instance() if isinstance(ty, OutputFloat): return FloatType.instance() if isinstance(ty, OutputString): return StringType.instance() if isinstance(ty, OutputAny): return AnyType.instance() if isinstance(ty, OutputObject): return ObjectType({ k: cls.get_type_instance(v) for k, v in ty.properties().items() }) if isinstance(ty, OutputList): return ListType(cls.get_type_instance(ty.elements()), ) if isinstance(ty, OutputNone): return NoneType.instance() if isinstance(ty, OutputRegex): return RegExpType.instance() if isinstance(ty, OutputEnum): return StringType.instance() assert isinstance(ty, OutputMap), f"Unknown Hub Type: {ty!r}" return MapType( cls.get_type_instance(ty.keys()), cls.get_type_instance(ty.values()), )
def number(self, tree): """ Compiles a number tree """ assert tree.data == 'number' token = tree.child(0) if token.value[0] == '+': token.value = token.value[1:] if token.type == 'FLOAT': return base_symbol(FloatType.instance()) return base_symbol(IntType.instance())
def parse_type(type_): """ Parses a type string and returns its parsed type which can be a: - BaseType (e.g. `IntType`) - TypeSymbol (e.g. `TypeSymbol(A)`) - GenericType (e.g. `ListGenericType(TypeSymbol(A))`) """ assert len(type_) > 0 type_ = type_.strip() if type_ == "boolean": return BooleanType.instance() if type_ == "int": return IntType.instance() if type_ == "float": return FloatType.instance() if type_ == "string": return StringType.instance() if type_ == "time": return TimeType.instance() if type_ == "none": return NoneType.instance() if type_ == "regexp": return RegExpType.instance() if type_ == "any": return AnyType.instance() if "[" in type_: types = [] for t in parse_type_inner(type_).split(","): t2 = parse_type(t) types.append(t2) if type_.startswith("List["): return ListGenericType(types) else: assert type_.startswith("Map[") return MapGenericType(types) assert "]" not in type_ return TypeSymbol(type_)
def test_fn(): nonlocal c c = c + 1 return c single_fn = singleton(test_fn) assert single_fn() == 1 assert single_fn() == 1 assert c == 1 @mark.parametrize('type_,expected', [ (BooleanType.instance(), 'boolean'), (IntType.instance(), 'int'), (FloatType.instance(), 'float'), (NoneType.instance(), 'none'), (AnyType.instance(), 'any'), (RegExpType.instance(), 'regexp'), (ListType(AnyType.instance()), 'List[any]'), (MapType(IntType.instance(), StringType.instance()), 'Map[int,string]'), ]) def test_boolean_str(type_, expected): assert str(type_) == expected def test_none_eq(): assert NoneType.instance() == NoneType.instance() assert NoneType.instance() != IntType.instance() assert NoneType.instance() != AnyType.instance()
def number(tree): token = tree.child(0) if token.type == 'FLOAT': return FloatType.instance() assert token.type == 'INT' return IntType.instance()
ListType, MapType, NoneType, ObjectType, RegExpType, StringType, ) from storyscript.hub.TypeMappings import TypeMappings @mark.parametrize( "expected,hub", [ (BooleanType.instance(), OutputBoolean(data={})), (IntType.instance(), OutputInt(data={})), (FloatType.instance(), OutputFloat(data={})), (NoneType.instance(), OutputNone(data={})), (AnyType.instance(), OutputAny(data={})), (RegExpType.instance(), OutputRegex(data={})), (StringType.instance(), OutputEnum(data={})), ( ListType(AnyType.instance()), OutputList(OutputAny.create(), data={}), ), ( ListType(IntType.instance()), OutputList(OutputInt(data={}), data={}), ), ( MapType(IntType.instance(), StringType.instance()), OutputMap(OutputInt(data={}), OutputString(data={}), data={}),
('foo[a, b] arg:[]', ['foo[a, b]', 'arg:[]']), ('foo[a, b] arg:[]', ['foo[a, b]', 'arg:[]']), ('foo[a, b] arg: []', ['foo[a, b]', 'arg: []']), ('foo[a, b] arg:list[int] a2:bar', ['foo[a, b]', 'arg:list[int]', 'a2:bar']), ('foo[a, b] arg:foo[bar]', ['foo[a, b]', 'arg:foo[bar]']), ('foo[bar[abc]] arg:foo[bar]', ['foo[bar[abc]]', 'arg:foo[bar]']), ]) def test_split_args(text, expected): assert [*split_type_arguments(text)] == expected @mark.parametrize('text,expected', [ ('any', AnyType.instance()), ('boolean', BooleanType.instance()), ('float', FloatType.instance()), ('int', IntType.instance()), ('none', NoneType.instance()), ('string', StringType.instance()), ('time', TimeType.instance()), ('A', TypeSymbol('A')), ]) def test_parse_type_base(text, expected): t = parse_type(text) assert str(t) == str(expected) @mark.parametrize('text,expected_type,expected_symbols', [ ('List[int]', ListGenericType, [IntType.instance()]), ('List[float]', ListGenericType, [FloatType.instance()]), ('Map[int,string]', MapGenericType,
def number(tree): token = tree.child(0) if token.type == "FLOAT": return FloatType.instance() assert token.type == "INT" return IntType.instance()
nonlocal c c = c + 1 return c single_fn = singleton(test_fn) assert single_fn() == 1 assert single_fn() == 1 assert c == 1 @mark.parametrize( "type_,expected", [ (BooleanType.instance(), "boolean"), (IntType.instance(), "int"), (FloatType.instance(), "float"), (NoneType.instance(), "none"), (AnyType.instance(), "any"), (RegExpType.instance(), "regexp"), (ListType(AnyType.instance()), "List[any]"), ( MapType(IntType.instance(), StringType.instance()), "Map[int,string]", ), (NullType.instance(), "null"), ], ) def test_boolean_str(type_, expected): assert str(type_) == expected
) def test_parse_end(text, expected): assert parse_type_inner(text) == expected def test_parse_end_error(): with raises(AssertionError): parse_type_inner("foo[bar") @mark.parametrize( "text,expected", [ ("any", AnyType.instance()), ("boolean", BooleanType.instance()), ("float", FloatType.instance()), ("int", IntType.instance()), ("none", NoneType.instance()), ("string", StringType.instance()), ("time", TimeType.instance()), ("A", TypeSymbol("A")), ], ) def test_parse_type_base(text, expected): t = parse_type(text) assert str(t) == str(expected) @mark.parametrize( "text,expected_type,expected_symbols", [