Пример #1
0
def forExpr(parser):
    toplevel = Tree.For(parser)

    parser.currentNode.addNode(toplevel)
    parser.currentNode = toplevel

    while not (Parser.isEnd(parser) or parser.thisToken().token == "do"):
        token = parser.nextToken()
        if token.token == "do":
            break
        Parser.callToken(parser)

    ExprParser.endExpr(parser)

    parser.nodeBookmark.append(len(parser.currentNode.nodes))

    if parser.thisToken().token != "do":
        Error.parseError(parser, "Expecting do")

    if len(toplevel.nodes) != 1: #or not type(toplevel.nodes[0]) is Tree.CreateAssign:
        Error.parseError(parser, "Expecting single node")


    count = 0
    while not (Parser.isEnd(parser) and count > 0): #avoid breaking on do keyword
        count += 1
        token = parser.nextToken()
        Parser.callToken(parser)

    parser.nodeBookmark.pop()

    parser.currentNode = toplevel.owner
Пример #2
0
def funcBody(parser, name, names, types, header, returnType, do):
    body = Tree.FuncBody(parser)
    body.name = name
    body.returnType = returnType
    body.package = parser.package
    body.do = do

    parser.currentNode.addNode(body)
    parser.currentNode = body

    for i in range(len(names)):
        n = Tree.InitArg(names[i], body)
        n.package = parser.package
        n.varType = types[i]
        n.imutable = not Scope.isMutable(parser, parser.package, names[i])
        body.addNode(n)

    parser.nextToken()
    Parser.callToken(parser) #incase next case is newline

    while not Parser.isEnd(parser):
        parser.nextToken()
        t = parser.thisToken().token
        Parser.callToken(parser)

    ExprParser.endExpr(parser)

    parser.currentNode = body.owner

    Scope.decrScope(parser)
Пример #3
0
def elseExpr(parser):
    toplevel = Tree.Else(parser)

    try:
        inside = parser.currentNode.nodes[-1].nodes[-2]
    except IndexError:
        Error.parseError(parser, "unexpected else")

    if not type(inside) is Tree.IfCondition:
        Error.parseError(parser, "unexpected else")

    parser.currentNode.nodes[-1].addNode(toplevel)
    parser.currentNode = toplevel

    block = Tree.Block(parser)
    parser.currentNode.owner.addNode(block)
    parser.currentNode = block

    opening = None
    single = 0

    while not Parser.isEnd(parser):
        token = parser.nextToken()

        Parser.callToken(parser)

    ExprParser.endExpr(parser)
    parser.currentNode = toplevel.owner.owner
Пример #4
0
def whileExpr(parser):
    toplevel = Tree.While(parser)

    parser.currentNode.addNode(toplevel)
    parser.currentNode = toplevel

    n = Tree.WhilePreCondition(parser)
    cond = Tree.WhileCondition(parser)

    parser.currentNode.addNode(n)
    parser.currentNode.addNode(cond)
    parser.currentNode = cond

    while not Parser.isEnd(parser):
        token = parser.nextToken()

        iter = parser.iter
        if token.token == "do":
            ExprParser.endExpr(parser)

            block = Tree.WhileBlock(parser)
            cond.owner.addNode(block)
            parser.currentNode = block

            continue

        Parser.callToken(parser)

    ExprParser.endExpr(parser)

    parser.currentNode = n.owner.owner
Пример #5
0
def funcBody(parser, name, names, types, brace, returnType, do):
    body = Tree.FuncBody(parser)
    body.name = name
    body.returnType = returnType
    body.package = parser.package
    body.do = do

    brace.body = body

    parser.currentNode.addNode(body)
    parser.currentNode = body

    for i in range(len(names)):
        n = Tree.InitArg(names[i], body)
        n.package = parser.package
        n.varType = types[i]
        n.imutable = not Scope.isMutable(parser, parser.package, names[i])
        body.addNode(n)

    parser.nextToken()
    Parser.callToken(parser)  #incase next case is newline

    while not Parser.isEnd(parser):
        parser.nextToken()
        t = parser.thisToken().token
        Parser.callToken(parser)

    ExprParser.endExpr(parser)

    parser.currentNode = body.owner

    Scope.decrScope(parser)
    return body
Пример #6
0
def elseExpr(parser):
    toplevel = Tree.Else(parser)

    try:
        inside = parser.currentNode.nodes[-1].nodes[-2]
    except IndexError:
        Error.parseError(parser, "unexpected else")

    if not type(inside) is Tree.IfCondition:
        Error.parseError(parser, "unexpected else")

    parser.currentNode.nodes[-1].addNode(toplevel)
    parser.currentNode = toplevel

    block = Tree.Block(parser)
    parser.currentNode.owner.addNode(block)
    parser.currentNode = block

    opening = None
    single = 0

    while not Parser.isEnd(parser):
        token = parser.nextToken()

        Parser.callToken(parser)

    ExprParser.endExpr(parser)
    parser.currentNode = toplevel.owner.owner
Пример #7
0
def funcCallBody(parser, paren, onlyOneArg):
    parser.nodeBookmark.append(1)

    def notParen():
        t = parser.thisToken()
        if t.token in ["then", "with", "do", ":="]:
            return False
        return not Parser.isEnd(parser)

    if paren:
        notEnd = lambda: parser.paren > parser.parenBookmark[-1]
    else:
        notEnd = notParen

    while not onlyOneArg and notEnd():
        t = parser.nextToken()

        if t.token == ",":
            ExprParser.endExpr(parser)
            parser.nodeBookmark[-1] = len(parser.currentNode.nodes)
            continue
        if t.token in ["then", "with", "do", ":="]:
            break
        Parser.callToken(parser)

    ExprParser.endExpr(parser)

    parser.nodeBookmark.pop()
Пример #8
0
def whileExpr(parser):
    toplevel = Tree.While(parser)

    parser.currentNode.addNode(toplevel)
    parser.currentNode = toplevel

    n = Tree.WhilePreCondition(parser)
    cond = Tree.WhileCondition(parser)

    parser.currentNode.addNode(n)
    parser.currentNode.addNode(cond)
    parser.currentNode = cond

    while not Parser.isEnd(parser):
        token = parser.nextToken()

        iter = parser.iter
        if token.token == "do" :
            ExprParser.endExpr(parser)

            block = Tree.WhileBlock(parser)
            cond.owner.addNode(block)
            parser.currentNode = block

            continue

        Parser.callToken(parser)

    ExprParser.endExpr(parser)

    parser.currentNode = n.owner.owner
Пример #9
0
def arrayRead(parser):
    numB = parser.bracket - 1
    parser.nextToken()

    Struct.popAt(parser)

    parser.nodeBookmark.append(1)
    try:
        arr = parser.currentNode.nodes[-1]
    except IndexError:
        Error.parseError(parser, "unexpected array read")

    del parser.currentNode.nodes[-1]

    arrRead = Tree.ArrRead(parser)
    arrRead.addNode(arr)

    parser.currentNode.addNode(arrRead)
    parser.currentNode = arrRead

    while parser.thisToken().token != "]":
        Parser.callToken(parser)

        if parser.thisToken().token == "]" and parser.bracket <= numB:
            break

        parser.nextToken()
    else:
        closeBracket(parser)

    ExprParser.endExpr(parser)

    parser.currentNode = arrRead.owner
    parser.nodeBookmark.pop()
Пример #10
0
def elseExpr(parser, canHaveElse=False):
    toplevel = Tree.Else(parser)
    ifexpr = False

    if not canHaveElse:
        try:
            inside = parser.currentNode.nodes[-1].nodes[-2]
        except IndexError:
            Error.parseError(parser, "unexpected else")

        if not type(inside) is Tree.IfCondition:
            ifexpr = IfExpr.ifPatternMatch(parser)

            if not ifexpr:
                Error.parseError(parser, "unexpected else")

    if not ifexpr:
        parser.currentNode.nodes[-1].addNode(toplevel)
        parser.currentNode = toplevel

        block = Tree.Block(parser)
        parser.currentNode.owner.addNode(block)
        parser.currentNode = block
    else:
        parser.currentNode = parser.currentNode.nodes[-1].nodes[-1]
        while len(parser.currentNode.nodes) > 0:
            parser.currentNode = parser.currentNode.nodes[-1]

        add_block = len(parser.currentNode.nodes) > 0
        if add_block:
            block = Tree.Block(parser)

            parser.currentNode.nodes[-1].addNode(toplevel)
            parser.currentNode.nodes[-1].addNode(block)
            parser.currentNode = parser.currentNode.nodes[-1]

            parser.currentNode = block

    opening = None
    single = 0

    while not Parser.isEnd(parser):
        token = parser.nextToken()

        Parser.callToken(parser)

    ExprParser.endExpr(parser)

    if ifexpr:
        parser.currentNode = ifexpr
    else:
        parser.currentNode = toplevel.owner.owner
Пример #11
0
def assignParser(parser, name="", init=False, package=""):
    if not init:
        ExprParser.endExpr(parser, -2)
        i = parser.currentNode.nodes[-1]

        if type(i) is Tree.Create:
            createAndAssignParser(parser, False)
            return

        del parser.currentNode.nodes[-1]

    if package == "": package = parser.package

    if name == "":
        node = Tree.Assign("", parser=parser)
        node.addNode(i)
    else:
        node = Tree.Assign(name, parser=parser)

    parser.nodeBookmark.append(0)

    node.package = package
    node.init = init

    parser.currentNode.addNode(node)
    parser.currentNode = node

    curr = parser.thisToken()

    while not Parser.isEnd(parser):
        parser.nextToken()
        if parser.thisToken().token == "do":
            break
        Parser.callToken(parser)

    ExprParser.endExpr(parser)

    parser.currentNode = node.owner
    parser.nodeBookmark.pop()

    self = node

    if self.init:
        if len(node.nodes) > 1 or len(node.nodes) == 0:
            self.error("expecting single expression, not " +
                       str(len(node.nodes)))
    else:
        if len(node.nodes) > 2 or len(node.nodes) == 1:
            self.error("expecting single expression, not " +
                       str(len(node.nodes) - 1))
Пример #12
0
def initStruct(parser, package= ""):
    if ExprParser.isUnary(parser, parser.lookBehind()):
        Error.parseError(parser, "unexpected {")

    if package == "": package = parser.package

    if len(parser.currentNode.nodes) == 0:
        Error.parseError(parser, "unexpected {")
    if not type(parser.currentNode.nodes[-1]) in [Tree.ReadVar, Tree.Field]:
        Error.parseError(parser, "unexpected {")

    readVar = type(parser.currentNode.nodes[-1]) is Tree.ReadVar

    name = parser.currentNode.nodes[-1].name if readVar else parser.currentNode.nodes[-1].field

    init = Tree.InitStruct(parser)

    if not readVar:
        package = parser.currentNode.nodes[-1].nodes[0].name
        t = (parser.currentNode.nodes[-1].nodes[0])
        if not package in parser.imports:
            t.error("no package called " + package)
        elif not type(t) is Tree.ReadVar:
            init.error("unexpected {")

    init.package = package

    del parser.currentNode.nodes[-1]
    s = parser.structs[package][name]

    init.paramNames = offsetsToList(parser.structs[package][name].offsets)
    init.s = s
    init.mutable = False

    parser.currentNode.addNode(init)
    parser.currentNode = init

    parser.nextToken()

    while parser.thisToken().token != "}":
        if parser.thisToken().token == ",":
            ExprParser.endExpr(parser)
        else: Parser.callToken(parser)
        parser.nextToken()
        t = parser.thisToken().token
        continue

    ExprParser.endExpr(parser)

    parser.currentNode = init.owner
Пример #13
0
def funcBody(parser, name, names, types, brace, returnType, do):
    body = Tree.FuncBody(parser)
    body.name = name
    body.returnType = returnType
    body.package = parser.package
    body.do = do
    body.types = types

    brace.body = body

    parser.currentNode.addNode(body)
    parser.currentNode = body

    if not parser.sc and not SimplifyAst.isGenericFunc(brace):
        body.sc = False

        if parser.nextToken().token == "\n":
            Parser.callToken(parser)
        while not Parser.isEnd(parser):
            if parser.nextToken().token == "\n":
                Parser.callToken(parser)
        parser.currentNode = body.owner
        Scope.decrScope(parser)
        #print("should not compile", name)
        return body

    for i in range(len(names)):
        n = Tree.InitArg(names[i], parser)
        n.package = parser.package
        n.varType = types[i]
        n.imutable = not Scope.isMutable(parser, parser.package, names[i])
        body.addNode(n)

    parser.nextToken()
    Parser.callToken(parser)  #incase next case is newline

    while not Parser.isEnd(parser):
        parser.nextToken()
        t = parser.thisToken().token
        Parser.callToken(parser)

    ExprParser.endExpr(parser)

    parser.currentNode = body.owner

    Scope.decrScope(parser)
    return body
Пример #14
0
def assignParser(parser, name="", init=False, package=""):
    if not init:
        i = parser.currentNode.nodes[-1]
        del parser.currentNode.nodes[-1]

    if package == "": package = parser.package

    if name == "":
        node = Tree.Assign("", parser=parser)
        node.addNode(i)
    else:
        node = Tree.Assign(name, parser=parser)

    parser.nodeBookmark.append(0)

    node.package = package
    node.init = init

    parser.currentNode.addNode(node)
    parser.currentNode = node

    curr = parser.thisToken()

    while not Parser.isEnd(parser):
        parser.nextToken()
        Parser.callToken(parser)

    if name == "_random":
        print()
    ExprParser.endExpr(parser)

    parser.currentNode = node.owner
    parser.nodeBookmark.pop()

    self = node

    if self.init:
        if len(node.nodes) > 1 or len(node.nodes) == 0:
            self.error("expecting single expression, not " +
                       str(len(node.nodes)))
    else:
        if len(node.nodes) > 2 or len(node.nodes) == 1:
            self.error("expecting single expression, not " +
                       str(len(node.nodes) - 1))
Пример #15
0
def ifBody(parser):
    cond = Tree.IfCondition(parser)

    parser.currentNode.addNode(cond)
    parser.currentNode = cond

    single = 0

    parser.nodeBookmark.append(0)

    then = False
    while not (Parser.isEnd(parser) and then) :
        token = parser.nextToken()

        if token.token == "then" and not then:
            then = True
            ExprParser.endExpr(parser)

            parser.nodeBookmark.pop()

            block = Tree.Block(parser)
            cond.owner.addNode(block)
            parser.currentNode = block
            continue

        isEnd = Parser.maybeEnd(parser)

        if (token.token in ["else", "elif"]) and isEnd:
            break

        token = parser.thisToken()

        Parser.callToken(parser)

        next = parser.lookInfront()
        if (next.token == "else" or next.token == "elif") and isEnd:
            break

    ExprParser.endExpr(parser)

    cond.type = Types.Bool()

    parser.currentNode = cond.owner.owner
Пример #16
0
def ifBody(parser):
    cond = Tree.IfCondition(parser)

    parser.currentNode.addNode(cond)
    parser.currentNode = cond

    single = 0

    parser.nodeBookmark.append(0)

    then = False
    while not (Parser.isEnd(parser) and then) :
        token = parser.nextToken()

        if token.token == "then" and not then:
            then = True
            ExprParser.endExpr(parser)

            parser.nodeBookmark.pop()

            block = Tree.Block(parser)
            cond.owner.addNode(block)
            parser.currentNode = block
            continue

        Parser.callToken(parser)

        next = parser.lookInfront()
        if (next.token == "else" or next.token == "elif") and Parser.maybeEnd(parser):
            parser.iter -= 1
            break



    ExprParser.endExpr(parser)

    cond.type = Types.Bool()

    parser.currentNode = cond.owner.owner
Пример #17
0
def funcCallBody(parser, paren):
    parser.nodeBookmark.append(1)

    def notParen():
        return not Parser.isEnd(parser)

    if paren:
        notEnd = lambda: parser.paren > parser.parenBookmark[-1]
    else:
        notEnd = notParen

    while notEnd():
        t = parser.nextToken()
        if t.token == ",":
            ExprParser.endExpr(parser)
            parser.nodeBookmark[-1] = len(parser.currentNode.nodes)
            continue

        Parser.callToken(parser)

    ExprParser.endExpr(parser)

    parser.nodeBookmark.pop()
Пример #18
0
def funcCallBody(parser, paren):
    parser.nodeBookmark.append(1)

    def notParen():
        return not Parser.isEnd(parser)

    if paren :
        notEnd = lambda: parser.paren > parser.parenBookmark[-1]
    else:
        notEnd = notParen

    while notEnd():
        t = parser.nextToken()
        if t.token == "," :
            ExprParser.endExpr(parser)
            parser.nodeBookmark[-1] = len(parser.currentNode.nodes)
            continue

        Parser.callToken(parser)

    ExprParser.endExpr(parser)

    parser.nodeBookmark.pop()
Пример #19
0
def assignParser(parser, name= "", init= False, package = ""):
    if not init:
        i = parser.currentNode.nodes[-1]
        del parser.currentNode.nodes[-1]

    if package == "": package = parser.package

    if name == "":
        node = Tree.Assign("",  parser= parser)
        node.addNode(i)
    else:
        node = Tree.Assign(name, parser=parser)

    node.package = package
    node.init = init

    parser.currentNode.addNode(node)
    parser.currentNode = node

    curr = parser.thisToken()

    while not Parser.isEnd(parser):
        parser.nextToken()
        Parser.callToken(parser)


    ExprParser.endExpr(parser)
    parser.currentNode = node.owner

    self = node

    if self.init:
        if len(node.nodes) > 1 or len(node.nodes) == 0:
            self.error("expecting single expression")
    else:
        if len(node.nodes) > 2 or len(node.nodes) == 1:
            self.error("expecting single expression")
Пример #20
0
def initStruct(parser, package= "", shouldRead=True):
    numB = parser.curly
    unary = not shouldRead
    if shouldRead and ExprParser.isUnary(parser, parser.lookBehind()):
        unary = True

    parser.curly += 1

    readVar = True

    if not unary:
        if package == "": package = parser.package

        if len(parser.currentNode.nodes) == 0:
            Error.parseError(parser, "unexpected {")
        if not type(parser.currentNode.nodes[-1]) in [Tree.ReadVar, Tree.Field]:
            Error.parseError(parser, "unexpected {")

        readVar = type(parser.currentNode.nodes[-1]) is Tree.ReadVar

        name = parser.currentNode.nodes[-1].name if readVar else parser.currentNode.nodes[-1].field

    init = Tree.InitStruct(parser)

    if not readVar:
        if type(parser.currentNode.nodes[-1].nodes[0]) is Tree.ReadVar:
            package = parser.currentNode.nodes[-1].nodes[0].name
            t = (parser.currentNode.nodes[-1].nodes[0])
            if not package in parser.imports:
                t.error("no package called " + package)

    init.package = package

    if not unary:
        init.constructor = parser.currentNode.nodes[-1]
        init.addNode(parser.currentNode.nodes[-1])

        del parser.currentNode.nodes[-1]

    init.mutable = False
    init.unary = unary

    parser.currentNode.addNode(init)
    parser.currentNode = init

    parser.nextToken()

    while parser.thisToken().token != "}":

        if parser.thisToken().token in [",", "\n"]:
            ExprParser.endExpr(parser)
        if parser.thisToken().token != ",": Parser.callToken(parser)

        t = parser.thisToken().token

        if t == "}" and parser.curly <= numB:
            break
        parser.nextToken()
    else:
        closeCurly(parser)

    ExprParser.endExpr(parser)

    parser.currentNode = init.owner
Пример #21
0
def ifBody(parser):
    cond = Tree.IfCondition(parser)

    parser.currentNode.addNode(cond)
    parser.currentNode = cond

    single = 0

    parser.nodeBookmark.append(0)

    then = False
    assign = False

    while not (Parser.isEnd(parser) and then):
        if parser.thisToken().token == ":=":
            assign = True

        token = parser.nextToken()

        if token.token == "then" and not then:
            then = True
            ExprParser.endExpr(parser)
            parser.nodeBookmark.pop()

            block = Tree.Block(parser)
            cond.owner.addNode(block)
            parser.currentNode = block
            continue

        isEnd = Parser.maybeEnd(parser)

        if (token.token in ["else", "elif"]) and isEnd:
            break

        token = parser.thisToken()

        Parser.callToken(parser)

        token = parser.thisToken()

        if token.token == "then" and not then:
            then = True
            ExprParser.endExpr(parser)

            parser.nodeBookmark.pop()

            block = Tree.Block(parser)
            cond.owner.addNode(block)
            parser.currentNode = block
            continue

        next = parser.lookInfront()
        if (next.token == "else" or next.token == "elif") and isEnd:
            break

    ExprParser.endExpr(parser)

    cond.type = Types.Bool()

    parser.currentNode = cond.owner.owner

    if assign:
        m = Tree.Match(cond.owner)
        m.addNode(cond.nodes[1])

        c = Tree.MatchCase(cond)
        c.addNode(cond.nodes[0])

        m.addNode(c)

        m.addNode(cond.owner.nodes[1])

        c = Tree.MatchCase(cond)
        u = Tree.Under(cond)
        c.addNode(u)

        m.addNode(c)

        b = Tree.Block(cond.owner)

        m.addNode(b)

        parser.currentNode.nodes[-1] = m
        m.owner = parser.currentNode

        b.ifexpr = m.owner
Пример #22
0
def func(parser):
    place = Tree.Lambda(parser)
    parser.currentNode.addNode(place)

    brace = Tree.FuncBraceOpen(parser)
    place.addNode(brace)

    parser.currentNode = brace

    b = parser.nextToken()

    count = -1

    while parser.thisToken().token != "|":
        if b.type == "identifier" and parser.lookInfront().token != ":":
            count += 1

            def callback(typ):
                print(u.varName, "changed to", typ)

            u = Types.Unknown(parser, callback)
            u.varName = parser.thisToken().token
            VarParser.createParser(parser, b, typ=u, check=False)
            b = parser.nextToken()
            continue
        elif parser.thisToken().token == ":":
            count += 1
        elif b.token == ",":
            b = parser.nextToken()
            continue

        Parser.declareOnly(parser)
        b = parser.nextToken()

    vars = [i.varType for i in brace]

    typ = False
    do = False

    parser.nextToken()

    place.returnTyp = False
    if parser.thisToken().token == "->":
        parser.nextToken()
        typ = Types.parseType(parser)
        place.returnTyp = True
        parser.nextToken()

    body = Tree.FuncBody(parser)

    place.addNode(body)
    parser.currentNode = body

    if parser.thisToken().token == "do":
        do = True
        parser.nextToken()

    brace.do = do
    brace.body = body
    body.do = do

    first = True

    parser.iter -= 1

    while not Parser.isEnd(parser):
        parser.nextToken()
        Parser.callToken(parser, lam=first)
        first = False

    ExprParser.endExpr(parser)
    place.args = vars
    place.returnTyp = typ

    place.do = do

    parser.currentNode = place.owner
Пример #23
0
def guardExpr(parser):
    ExprParser.endExpr(parser, -1)

    parser.nodeBookmark.append(0)

    place = Tree.PlaceHolder(parser)
    parser.currentNode.addNode(place)

    m = Tree.Match(parser)
    m.guard = True
    m.place = place
    parser.currentNode.addNode(m)
    parser.currentNode = m

    assign = False

    while True:
        parser.nextToken()
        if parser.thisToken().token == ":=":
            assign = True
            ExprParser.endExpr(parser, -1)
        else:

            Parser.callToken(parser)
        b = parser.thisToken()
        if b.token == ":=":
            assign = True

        isEnd = Parser.maybeEnd(parser)

        next = parser.lookInfront()
        if (next.token in ["else", "elif"]) and isEnd:
            break

    if len(m.nodes) != 2:
        Error.parseError(
            parser,
            "Expecting singular expression, not " + str(len(m.nodes) - 1))
    if not assign:
        Error.parseError(parser, "Expecting :=")

    #parser.currentNode.addNode(m)

    ExprParser.endExpr(parser)
    parser.nodeBookmark.pop()

    create = m.nodes[0]
    assign = m.nodes[1]

    m.nodes[0] = assign
    m.nodes[0].owner = m
    del m.nodes[1]

    case = Tree.MatchCase(parser)

    m.addNode(case)
    case.addNode(create)
    m.addNode(Tree.Block(parser))

    case2 = Tree.MatchCase(parser)
    m.addNode(case2)

    if not parser.nextToken().token in ["else", "elif"]:
        Error.parseError(parser, "Expecting else")
    if parser.thisToken().token == "else":
        case2.addNode(Tree.Under(parser))
        m.addNode(Tree.Block(parser))

        ElseExpr.elseExpr(parser, canHaveElse=True)

        m.nodes[4].nodes = m.nodes[4].nodes[1].nodes
        else_block = m.nodes[4]
    else:
        IfExpr.elifExpr(parser, canHaveElse=True)
        conditions = case2.nodes[0].nodes
        block = case2.nodes[1]

        case2.nodes = conditions
        m.addNode(block)

        else_block = block

    if len(else_block) == 0:
        Error.parseError(parser, "Guard block requires exit statement")

    exit_condition = else_block.nodes[-1]
    if not (type(exit_condition) in [Tree.Continue, Tree.Return, Tree.Break] or
            (type(exit_condition) is Tree.FuncCall
             and exit_condition.nodes[0].name == "panic")):
        Error.parseError(parser, "Guard block requires exit statement")

    parser.currentNode = m.owner
Пример #24
0
def parseLens(parser):
    #parser.nextToken()
    Scope.incrScope(parser)
    #lensType = Types.parseType(parser)
    Scope.decrScope(parser)

    place = Tree.Place(parser)

    lens = Tree.Lens(parser)
    lens.place = place

    parser.currentNode.addNode(lens)

    parser.currentNode = lens

    lens.addNode(place)

    #parser.nextToken()

    while not Parser.isEnd(parser):
        parser.nextToken()
        Parser.callToken(parser)

    ExprParser.endExpr(parser)

    parser.currentNode = lens.owner

    B = Types.T("B", Types.All, "Lens")

    def loop(n):
        if type(n) in [Tree.PlaceHolder, Tree.Place]:
            return B
        elif type(n) is Tree.Field:
            inner = loop(n.nodes[0])
            return Types.Interface(False, {n.field: inner})
        elif type(n) is Tree.ArrRead:
            inner = loop(n.nodes[0])
            return Types.Array(False, inner)
        else:
            n.error("unexpected token " + n.token.token)

    lens_typ = loop(lens.nodes[0])

    A = Types.T("A", lens_typ, "Lens")

    self = Types.Interface(False, {
        "query": Types.FuncPointer([A], B),
        "set": Types.FuncPointer([A, B], A),
        "toString": Types.FuncPointer([], Types.String(0)),
    },
                           coll.OrderedDict([("Lens.A", A), ("Lens.B", B)]),
                           name="Lens")

    Lens = Types.Interface(False, {
        "query": Types.FuncPointer([A], B),
        "set": Types.FuncPointer([A, B], A),
        "toString": Types.FuncPointer([], Types.String(0)),
    },
                           coll.OrderedDict([("Lens.A", A), ("Lens.B", B)]),
                           name="Lens")

    #lens.type = Types.Interface(False, {
    #    #   "query": Types.FuncPointer([i.lensType], i.nodes[0].type),
    #    "set": Types.FuncPointer([i.lensType, i.nodes[0].type], i.lensType),
    #})

    lens.type = Lens
Пример #25
0
def match(parser):
    t = parser.thisToken()
    self = Tree.Match(parser)
    parser.currentNode.addNode(self)
    parser.currentNode = self

    while t.token != "with":
        t = parser.nextToken()
        if t.token == "with":
            break
        Parser.callToken(parser)
        t = parser.thisToken()

    ExprParser.endExpr(parser)

    parser.nextToken()
    Parser.callToken(parser)

    while not Parser.isEnd(parser):
        t = parser.nextToken()
        if t.token == "->":
            ExprParser.endExpr(parser)

            #print("entered ->")
            if len(self.nodes) == 0:
                Error.parseError(parser, "unexpected ->")
            previos = self.nodes[-1]
            if type(previos) is [Tree.MatchCase, Tree.Block]:
                Error.parseError(parser, "unexpected ->")

            case = Tree.MatchCase(parser)
            case.addNode(previos)

            self.nodes[-1] = case
            case.owner = self

            body = Tree.Block(parser)
            self.addNode(body)

            parser.currentNode = body

            Parser.addBookmark(parser)

            parser.nextToken()
            Parser.callToken(parser)

            while not Parser.isEnd(parser):
                parser.nextToken()
                """if Parser.isEnd(parser):
                    parser.callToken(parser)
                    #print("break", parser.thisToken())
                break
                """
                Parser.callToken(parser)
            #print("break", parser.thisToken())
            Parser.returnBookmark(parser)

            ExprParser.endExpr(parser)
            parser.currentNode = body.owner

            continue

        Parser.callToken(parser)

        if parser.thisToken().token == "->":
            parser.iter -= 1

    if len(self.nodes) % 2 == 0:
        Error.parseError(parser, "Expecting ->")

    parser.currentNode = self.owner
Пример #26
0
def whileExpr(parser):
    toplevel = Tree.While(parser)

    parser.currentNode.addNode(toplevel)
    parser.currentNode = toplevel

    n = Tree.WhilePreCondition(parser)
    cond = Tree.WhileCondition(parser)

    parser.currentNode.addNode(n)
    parser.currentNode.addNode(cond)
    parser.currentNode = cond

    assign = False
    block = None

    while not (Parser.isEnd(parser) and not parser.thisToken().token in ["!", "do", ":="]):
        token = parser.nextToken()
        if token.token == ":=" and not block:
            assign = True

        iter = parser.iter
        if token.token == "do" :
            ExprParser.endExpr(parser)

            block = Tree.WhileBlock(parser)
            cond.owner.addNode(block)
            parser.currentNode = block

            continue

        Parser.callToken(parser)
        if parser.thisToken().token == ":=":
            assign = True

    ExprParser.endExpr(parser)

    parser.currentNode = n.owner.owner

    if assign:
        m = Tree.Match(cond.owner)
        m.addNode(cond.nodes[1])

        c = Tree.MatchCase(cond)
        c.addNode(cond.nodes[0])

        m.addNode(c)

        new_block = Tree.Block(block)
        new_block.nodes = block.nodes
        for i in new_block.nodes:
            i.owner = new_block

        m.addNode(new_block)

        c = Tree.MatchCase(cond)
        u = Tree.Under(cond)
        c.addNode(u)

        m.addNode(c)

        b = Tree.Block(cond.owner)
        b.addNode(Tree.Break(cond))

        m.addNode(b)

        cond.nodes = [Tree.Bool("true", cond)]
        cond.nodes[0].owner = cond

        block.nodes = [m]
        m.owner = block

        b.ifexpr = m.owner
Пример #27
0
def func(parser):
    place = Tree.Lambda(parser)
    parser.currentNode.addNode(place)

    brace = Tree.FuncBraceOpen(parser)
    place.addNode(brace)

    parser.currentNode = brace

    b = parser.nextToken()

    count = -1

    while parser.thisToken().token != "|":
        count += 1

        if b.type == "identifier":
            VarParser.createParser(parser,
                                   b,
                                   typ=Types.Unknown(count, place),
                                   check=False)
            b = parser.nextToken()
            continue
        elif b.token == ",":
            b = parser.nextToken()
            continue

        Parser.declareOnly(parser)
        b = parser.Parser.nextToken(parser)

    vars = [i.varType for i in brace]

    typ = False
    do = False

    if parser.nextToken().token == "->":
        typ = Types.parseType(parser)

    body = Tree.FuncBody(parser)

    place.addNode(body)
    parser.currentNode = body

    if parser.thisToken().token == "do":
        do = True
        parser.nextToken()

    brace.do = do
    brace.body = body
    body.do = do

    first = True

    while not Parser.isEnd(parser):
        Parser.callToken(parser, lam=first)
        parser.nextToken()
        first = False

    ExprParser.endExpr(parser)

    if typ:
        place.type = Types.Lambda(place, vars, typ=typ, do=do)
    else:
        place.type = Types.Lambda(place, vars, do=do)

    parser.currentNode = place.owner