Ejemplo n.º 1
0
    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])
Ejemplo n.º 2
0
    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()),
        )
Ejemplo n.º 3
0
 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())
Ejemplo n.º 4
0
    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])
Ejemplo n.º 5
0
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_)
Ejemplo n.º 6
0
 def regular_expression(self, tree):
     """
     Compiles a regexp object from a regular_expression tree
     """
     assert tree.data == 'regular_expression'
     return base_symbol(RegExpType.instance())
Ejemplo n.º 7
0
        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 test_none_assign():
    assert not NoneType.instance().can_be_assigned(IntType.instance())
Ejemplo n.º 8
0
    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={}),
        ),
        (
            ObjectType({
Ejemplo n.º 9
0
    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_none_eq():
    assert NoneType.instance() == NoneType.instance()
    assert NoneType.instance() != IntType.instance()
Ejemplo n.º 10
0
def test_type_to_tree_regex(magic):
    tree = magic()
    bt = RegExpType.instance()
    se = SymbolExpressionVisitor.type_to_tree(tree, bt)
    assert se.data == "base_type"
    tree.create_token.assert_called_with("REGEXP_TYPE", "regex")