Exemple #1
0
def abstractToConcreteType(abstract_type):
    if abstract_type == 'T':
        return [
            AST.BASICTYPE(type_id=Token(Position(), TOKEN.TYPE_IDENTIFIER, b))
            for b in BASIC_TYPES
        ]
    elif abstract_type in BASIC_TYPES:
        return [
            AST.BASICTYPE(type_id=Token(Position(), TOKEN.TYPE_IDENTIFIER,
                                        abstract_type))
        ]
    elif abstract_type == '[T]':
        return [
            AST.LISTTYPE(type=AST.TYPE(val=AST.BASICTYPE(
                type_id=Token(Position(), TOKEN.TYPE_IDENTIFIER, b))))
            for b in BASIC_TYPES
        ]
    elif abstract_type in ['[' + x + ']' for x in BASIC_TYPES]:
        return [
            AST.LISTTYPE(type=AST.TYPE(val=AST.BASICTYPE(type_id=Token(
                Position(), TOKEN.TYPE_IDENTIFIER, abstract_type[1:-1]))))
        ]
    elif abstract_type in VOID_TYPE:
        return [Token(Position(), TOKEN.TYPE_IDENTIFIER, "Void")]
    else:
        raise Exception(
            "Unknown abstract type encountered in builtin operator table: %s" %
            abstract_type)
Exemple #2
0
def tokenToNode(token):
    if token.typ == TOKEN.INT:
        node = AST.BASICTYPE(type_id=Token(Position(), TOKEN.TYPE_IDENTIFIER, "Int"))
        node._start_pos = Position()
        return node
    elif token.typ == TOKEN.CHAR:
        node = AST.BASICTYPE(type_id=Token(Position(), TOKEN.TYPE_IDENTIFIER, "Char"))
        node._start_pos = Position()
        return node
    elif token.typ == TOKEN.BOOL:
        node = AST.BASICTYPE(type_id=Token(Position(), TOKEN.TYPE_IDENTIFIER, "Bool"))
        node._start_pos = Position()
        return node
    elif token.typ == TOKEN.STRING:
        node = AST.LISTTYPE(type=AST.TYPE(val=AST.BASICTYPE(type_id=Token(Position(), TOKEN.TYPE_IDENTIFIER, "Char"))))
        node._start_pos = Position()
        return node
    elif token.typ == TOKEN.EMPTY_LIST:
        node = AST.BASICTYPE(type_id=Token(Position(), TOKEN.TYPE_IDENTIFIER, "[]"))
        node._start_pos = Position()
        return node
    else:
        raise Exception('Unexpected token type encountered')
Exemple #3
0
def ListType():
    yield ps.token(TOKEN.BRACK_OPEN)
    a = yield Type
    yield ps.token(TOKEN.BRACK_CLOSE)
    return AST.LISTTYPE(type=a)
Exemple #4
0
 ],
 decls=[
     AST.VARDECL(
         type=None,
         id=Token(None, TOKEN.IDENTIFIER, "temp"),
         expr=AST.DEFERREDEXPR(contents=[Token(None, TOKEN.INT, 2)])),
     AST.VARDECL(
         type=AST.TYPE(val=AST.BASICTYPE(
             type_id=Token(None, TOKEN.TYPE_IDENTIFIER, "Char"))
                       ),  # TODO make this work for type node
         id=Token(None, TOKEN.IDENTIFIER, "hey"),
         expr=AST.DEFERREDEXPR(
             contents=[Token(None, TOKEN.CHAR, 'a')])),
     AST.TYPESYN(type_id=Token(None, TOKEN.TYPE_IDENTIFIER, "String"),
                 def_type=AST.TYPE(val=AST.LISTTYPE(type=AST.TYPE(
                     val=AST.BASICTYPE(type_id=Token(
                         None, TOKEN.TYPE_IDENTIFIER, "Char")))))),
     AST.FUNDECL(
         kind=FunKind.INFIXL,
         fixity=Token(None, TOKEN.INT, 4),
         id=Token(None, TOKEN.OP_IDENTIFIER, "%%"),
         params=[
             Token(None, TOKEN.IDENTIFIER, "x"),
             Token(None, TOKEN.IDENTIFIER, "y")
         ],
         type=AST.FUNTYPE(from_types=[
             AST.TYPE(val=AST.BASICTYPE(
                 type_id=Token(None, TOKEN.TYPE_IDENTIFIER, "Char"))),
             AST.TYPE(val=AST.BASICTYPE(
                 type_id=Token(None, TOKEN.TYPE_IDENTIFIER, "Char")))
         ],