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
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
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)
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
def addToContext(parser): previous = parser.currentNode node = Tree.AddToContext(parser) parser.currentNode = node if not type(previous) is Tree.Root: Error.parseError(parser, "Can only add field to context from global scope") if parser.nextToken().type != "identifier": Error.parseError(parser, "Expecting identifier") Parser.callToken(parser) parser.nextToken() iter = parser.iter+1 line = parser.lineNumber Parser.callToken(parser) if not type(node.nodes[0]) is Tree.CreateAssign: parser.iter = iter parser.lineNumber = line Error.parseError(parser, "Expecting :=") createAssign = node.nodes[0] node.name = createAssign.nodes[0].name parser.currentNode = previous previous.addNode(node)
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()
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
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
def enumParser(parser, name, decl, generic): const = parser.interfaces[parser.package][name].const existing_generics = parser.interfaces[parser.package][name].generic existing_generics.update(generic) #const = coll.OrderedDict() enum = Types.Enum(parser.package, name, const, existing_generics) if decl: parser.interfaces[parser.package][name] = enum """if parser.lookInfront().token == "\n": parser.nextToken() Parser.callToken(parser) parser.nextToken()"" \ """ while not Parser.isEnd(parser): t = parser.nextToken() if t.token == "\n" or t.type == "indent": Parser.callToken(parser) continue if t.type != "identifier": Error.parseError(parser, "expecting identifier") varName = t.token if varName[0].upper() != varName[0]: Error.parseError(parser, "constructor type must be capitalized") args = [] nextT = parser.nextToken() #print(varName) #print(nextT) if nextT.token == "(": args = Types.parseType(parser).list const[varName] = args if decl: Scope.addVar(Tree.PlaceHolder(parser), parser, varName, Scope.Type( True, Types.FuncPointer(args, enum, generic=generic) if len(args) > 0 else enum), _global=True) t = parser.thisToken() if t.token == "\n" or t.type == "indent": Parser.callToken(parser) parser.currentNode.addNode(Tree.Enum(const, name, parser, generic)) Scope.decrScope(parser)
def return_some_error(parser, case_name="Some"): prev = parser.currentNode m = Tree.Match(parser) parser.currentNode.addNode(m) parser.currentNode = m while not (Parser.isEnd(parser)): parser.nextToken() Parser.callToken(parser) #ExprParser.endExpr(parser, -2) parser.currentNode = m.owner case = Tree.MatchCase(parser) m.addNode(case) r = Tree.ReadVar("Some", True, parser) r.package = "_global" f = Tree.FuncCall(parser) f.addNode(r) case.addNode(f) r = Tree.ReadVar("_x", False, parser) r.package = parser.package f.addNode(r) block = Tree.Block(parser) m.addNode(block) ret = Tree.Return(parser) block.addNode(ret) f2 = Tree.FuncCall(parser) some = Tree.ReadVar(case_name, True, parser) some.package = "_global" f2.addNode(copy.copy(some)) f2.addNode(copy.copy(r)) ret.addNode(f2) case = Tree.MatchCase(parser) m.addNode(case) case.addNode(Tree.Under(parser)) m.addNode(Tree.Block(parser)) parser.currentNode = prev
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
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))
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
def defer(parser): node = Tree.Defer(parser) previos = parser.currentNode previos.addNode(node) parser.currentNode = node while not Parser.isEnd(parser): parser.nextToken() Parser.callToken(parser) if len(node.nodes) != 1: Error.parseError(parser, "Expecting single expression, not "+str(len(node.nodes))) if not type(node.nodes[0]) is Tree.FuncCall: Error.parseError(parser, "Expecting function call") parser.currentNode = previos
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))
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
def pushContext(parser): parser.nextToken() node = Tree.PushContext(parser) previous = parser.currentNode previous.addNode(node) parser.currentNode = node Parser.callToken(parser) if len(node.nodes) == 0 or not type(node.nodes[0]) is Tree.ReadVar: Error.parseError(parser, "Expecting identifier") if parser.nextToken().token != "do": Error.parseError(parser, "Expecting do") while not Parser.isEnd(parser) or parser.thisToken().token == "do": parser.nextToken() Parser.callToken(parser) parser.currentNode = previous
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
def returnF(parser): previous = parser.currentNode parser.nodeBookmark.append(0) if type(previous) is Tree.Root: Error.parseError(parser, "Can only return from function") returnAST = Tree.Return(parser) previous.addNode(returnAST) parser.currentNode = returnAST while not Parser.isEnd(parser): parser.nextToken() Parser.callToken(parser) if len(returnAST.nodes) > 1: Error.parseError( parser, "Expecting single expression not " + str(len(returnAST.nodes))) parser.currentNode = previous parser.nodeBookmark.pop()
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()
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()
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
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")
def fromParser(parser, decl=False, stage=False): place = Tree.PlaceHolder(parser) importParser(parser, decl) name = parser.imports[-1] if parser.nextToken().token != "import": Error.parseError(parser, "Expecting import keyword") parser.nextToken() names = [] if not decl: parser.currentNode.addNode( Tree.Import(name, parser.package, names, parser)) def getName(token, nameOfVar=""): if nameOfVar == "": nameOfVar = token.token package = parser.package if nameOfVar in parser.structs[name]: if decl: if stage and nameOfVar in parser.structs[package]: Error.parseError(parser, nameOfVar + " is already a struct") parser.structs[package][nameOfVar] = parser.structs[name][ nameOfVar] else: pass #Scope.addVar(place, parser, nameOfVar, parser.scope[name][0][nameOfVar]) names.append((nameOfVar, "full")) elif nameOfVar in parser.interfaces[name]: if not decl: return if stage and nameOfVar in parser.interfaces[package]: Error.parseError(parser, nameOfVar + " is already an interface") parser.interfaces[package][nameOfVar] = parser.interfaces[name][ nameOfVar] elif not decl and nameOfVar in parser.scope[name][0]: #can't set lambda Scope.addVar(place, parser, nameOfVar, parser.scope[name][0][nameOfVar]) names.append((nameOfVar, parser.scope[name][0][nameOfVar].target)) elif not decl: Error.parseError( parser, "Package " + name + " does not have a variable, or type called " + nameOfVar) token = parser.thisToken() if token.token == "all": _names = set() for i in parser.structs[name]: _names.add(i) for i in parser.interfaces[name]: _names.add(i) for i in parser.scope[name][0]: _names.add(i) for i in _names: getName(token, i) return Parser.callToken(parser) elem = parser.currentNode.nodes.pop() def pattern(name): if type(name) in [Tree.Tuple, Tree.PlaceHolder]: for i in name: pattern(i) elif type(name) is Tree.ReadVar: return getName(name.token) elif type(name) is Lexer.Token and name.type == "identifier": return getName(name) else: p = Tree.PlaceHolder(parser) p.token = name p.error("Unexpected token " + name.token) pattern(elem)
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
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
def createAndAssignParser(parser, imutable= True): # let i assignment node = parser.currentNode parser.nextToken() #get current token to position of = checkIt = False attachTyp = False """if parser.lookInfront().token == ".": attachTyp = Types.parseType(parser, _attachTyp= True) parser.nextToken() if not imutable or not type(node) is Tree.Root: Error.parseError(parser, "expecting =, not .") parser.nextToken() """ name = parser.thisToken() typ = None create = False pattern = False isPattern = False t = parser.thisToken() if t.token == "{": Error.parseError(parser, "Expecting space") if t.token in ["("] or t.type == "bracketOpenS": isPattern = True owner = parser.currentNode pattern = Tree.PlaceHolder(parser) parser.currentNode = pattern Parser.callToken(parser) paren = parser.paren while parser.paren > paren: parser.nextToken() Parser.callToken(parser) parser.currentNode = owner name = pattern if parser.nextToken().token == ":": checkIt = True parser.nextToken() typ = Types.parseType(parser) parser.nextToken() if parser.thisToken().token != "=": create = True elif parser.thisToken().token != "=": Error.parseError(parser, "expecting =, not "+parser.thisToken().token) if not create: n = Tree.CreateAssign(parser) parser.currentNode.addNode(n) parser.currentNode = n createParser(parser, name=name, typ=typ, check=checkIt, imutable=imutable, attachTyp=attachTyp) if attachTyp: assignParser(parser, name=parser.package+"_"+attachTyp.normalName+"_"+name.token, package= attachTyp.package, init=True) n.nodes[1].attachName = parser.package+"_"+attachTyp.normalName n.nodes[1].varName = name.token else: c = name if type(name) is Tree.PlaceHolder else name.token assignParser(parser, name= c, init= True) n.nodes[1].isGlobal = n.nodes[0].isGlobal n.nodes[1].createTyp = n.nodes[0].varType parser.currentNode = node else: createParser(parser, name=name, typ=typ, check=checkIt, imutable=imutable, attachTyp=attachTyp)
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
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
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
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
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
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