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 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 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 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 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 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 typeParser(parser, decl= False): name = parser.nextToken() Scope.incrScope(parser) if name.type != "identifier": Error.parseError(parser, "type name must be an identifier") name = name.token if name[0].lower() == name[0]: Error.parseError(parser, "struct name must be upper case") import collections as coll gen = coll.OrderedDict() if parser.nextToken().token == "[": gen = FuncParser.generics(parser, name) if parser.thisToken().token != "=": if parser.thisToken().token == "with": tmp = parser.currentNode parser.currentNode = Tree.PlaceHolder(parser) Interface.traitParser(parser, name, decl, gen) parser.currentNode = tmp return Error.parseError(parser, "expecting =") tmp = parser.currentNode typ = Tree.Type(parser.package, name, parser) typ.package = parser.package typ.normalName = name tmp.addNode(typ) parser.currentNode = typ while not Parser.isEnd(parser): parser.nextToken() Parser.declareOnly(parser, noVar=True) args = [i.varType for i in parser.currentNode] fields = parser.currentNode.nodes typ.fields = [i.name for i in typ] typ.nodes = [] parser.currentNode = tmp if decl: meth = parser.structs[parser.package][name].methods parser.structs[parser.package][name] = Struct(name, args, fields, gen) parser.structs[parser.package][name].methods = meth parser.structs[parser.package][name].package = parser.package Scope.decrScope(parser)
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 asterix(parser): lastToken = parser.lookBehind() op = Tree.Operator("*", parser) if isUnary(parser, lastToken): Parser.precidences["*"] = (100, True) Parser.Opcode(parser, "*", lambda: operatorPop(parser, op, 1, unary=True)) Parser.precidences["*"] = (40, True) op.unary = True else: Parser.Opcode(parser, "*", lambda: operatorPop(parser, op, 2))
def send(parser): lastToken = parser.lookBehind() op = Tree.Operator("<-", parser) if not isUnary(parser, lastToken): Parser.precidences["<-"] = (2, True) Parser.Opcode(parser, "<-", lambda: operatorPop(parser, op, 2)) Parser.precidences["<-"] = (100, True) else: # newOperator("set", (2, True), 2, func=set, token=False) op.unary = True Parser.Opcode(parser, "<-", lambda: operatorPop(parser, op, 1, unary=True))
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 traitParser(parser, name, decl, generic): interface = parser.interfaces[parser.package][name] meth = {} while not Parser.isEnd(parser): parser.nextToken() t = parser.thisToken() if t.token == "def": currentNode = parser.currentNode p = PlaceHolder(parser) parser.currentNode = p (methodName, names, types, brace, returnType, do) = FuncParser.funcHead(parser, decl, dontAdd=True, interfaceMethod=True) Scope.decrScope(parser) if methodName in meth: Error.parseError(parser, "Method " + methodName + ", already defined") meth[methodName] = brace.ftype parser.currentNode = currentNode parser.nextToken() else: Parser.declareOnly(parser, noVar=True) if len(parser.currentNode.nodes) > 0 and type( parser.currentNode.nodes[0]) is Tree.Create: Error.parseError( parser, "Interfaces are abstract interfaces which is why only methods are supported" ) names = {i.name: i.varType for i in parser.currentNode} #args = [i.varType for i in parser.currentNode] #fields = parser.currentNode.nodes if decl: i = interface.fromObj( Types.Interface(False, names, generic, parser.package + "." + name if parser.package != "_global" else name, methods=meth)) parser.interfaces[parser.package][name] = i Scope.decrScope(parser)
def plus(parser): lastToken = parser.lookBehind() node = Tree.Operator("+", parser) if isUnary(parser, lastToken): # unary+ node.unary = True Parser.precidences["+"] = (100, True) Parser.Opcode(parser, "+", lambda: operatorPop(parser, node, 1, unary= True)) Parser.precidences["+"] = (20, True) return Parser.Opcode(parser, "+", lambda: operatorPop(parser, node, 2))
def minus(parser): lastToken = parser.lookBehind() op = Tree.Operator("-", parser) if isUnary(parser, lastToken): # unary- Parser.precidences["-"] = (100, True) # give higher power Parser.Opcode(parser, "-", lambda: operatorPop(parser, op, 1, unary= True)) Parser.precidences["-"] = (20, True) op.unary = True else: Parser.Opcode(parser, "-", lambda: operatorPop(parser, op, 2))
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 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 usingParser(parser): parser.nextToken() len_of_nodes = len(parser.currentNode.nodes) while not Parser.isEnd(parser): parser.nextToken() Parser.declareOnly(parser, noVar=True) if len_of_nodes + 1 != len(parser.currentNode.nodes): parser.error("Expecting single expression") u = Tree.Using(parser.currentNode.nodes[-1]) parser.currentNode.nodes[-1] = u u.owner = parser.currentNode
def init(): global indent global parser global parenThing global indent global tokens tokens = [[]] parenThing = 0 text = "" indent = 0 if __name__ == "__main__": parser = Parser.Parser(tokens, [("main", "_")]) parser.compiled = {} parser.global_target = "client" parser.opt = 0 parser.externFuncs = {"main": []} parser.repl = True parser.hotswap = False parser._tokens = parser.tokens parser._filename = parser.filename parser.filenames = {} PackageParser.packDec(parser, "main", pack=True)
def aliasParser(parser, name, decl, generic): parser.nextToken() typ = False while not Parser.isEnd(parser): if parser.thisToken().token != "\n" and parser.thisToken().type != "indent": if typ: Error.parseError(parser, "Unexpected token " + parser.thisToken().token) typ = Types.parseType(parser) parser.nextToken() if decl: alias = parser.interfaces[parser.package][name] tmp = Types.Alias(parser.package, name, typ, generic) alias.name = tmp.name alias.normalName = tmp.normalName alias.typ = tmp.typ alias.generic = tmp.generic alias.remainingGen = tmp.remainingGen alias.types = tmp.types alias.methods = tmp.methods if decl: parser.interfaces[parser.package][name] = alias Scope.decrScope(parser)
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 callFunc(parser,paren): tail = Tree.FuncCall(parser) tail.addNode(parser.currentNode.nodes[-1]) tail.owner = parser.currentNode parser.currentNode.nodes[-1] = tail parser.currentNode = tail if not paren: Parser.selectExpr(parser, parser.thisToken()) funcCallBody(parser, paren) parser.currentNode = tail.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 callFunc(parser, paren): tail = Tree.FuncCall(parser) tail.addNode(parser.currentNode.nodes[-1]) tail.owner = parser.currentNode parser.currentNode.nodes[-1] = tail parser.currentNode = tail if not paren: Parser.selectExpr(parser, parser.thisToken()) funcCallBody(parser, paren) parser.currentNode = tail.owner
def callFunc(parser, paren, onlyOneArg): if len(parser.currentNode.nodes) == 0: Error.parseError(parser, "Expecting identifier") tail = Tree.FuncCall(parser) tail.addNode(parser.currentNode.nodes[-1]) tail.owner = parser.currentNode parser.currentNode.nodes[-1] = tail parser.currentNode = tail if not paren: Parser.selectExpr(parser, parser.thisToken()) funcCallBody(parser, paren, onlyOneArg) parser.currentNode = tail.owner
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 asOperator(parser): lastToken = parser.lookBehind() if not isUnary(parser, lastToken): op = Tree.Operator("as", parser) parser.nextToken() op.type = Types.parseType(parser) Parser.Opcode(parser, "as", lambda: operatorPop(parser, op, 1, unary=True)) else: Error.parseError(parser, "unexpected as operator ")
def traitParser(parser, name, decl, generic): meth = {} while not Parser.isEnd(parser): parser.nextToken() t = parser.thisToken() Parser.declareOnly(parser, noVar=True) names = {i.name: i.varType for i in parser.currentNode} args = [i.varType for i in parser.currentNode] fields = parser.currentNode.nodes if decl: del parser.structs[parser.package][name]# = Struct.Struct(name, args, fields, coll.OrderedDict()) i = Types.Interface(False, names, generic, parser.package+"."+name) parser.interfaces[parser.package][name] = i Scope.decrScope(parser)
def traitParser(parser, name, decl, generic): meth = {} while not Parser.isEnd(parser): parser.nextToken() t = parser.thisToken() Parser.declareOnly(parser, noVar=True) names = {i.name: i.varType for i in parser.currentNode} args = [i.varType for i in parser.currentNode] fields = parser.currentNode.nodes if decl: del parser.structs[parser.package][name] # = Struct.Struct(name, args, fields, coll.OrderedDict()) i = Types.Interface(False, names, generic) parser.interfaces[parser.package][name] = i Scope.decrScope(parser)
def f(parser): op = Tree.Operator(kind, parser) actuallyUnary = isUnary(parser, parser.lookBehind()) if len(parser.currentNode.nodes) != 0: if not unary and actuallyUnary: Error.parseError(parser, "unexpected "+kind) elif unary and not actuallyUnary: Error.parseError(parser, "unexpected "+kind) #parser.nodeBookmark.append(len(parser.currentNode.nodes)-1) Parser.Opcode(parser, kind, lambda: operatorPop(parser, op, takesIn, actuallyUnary))
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 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 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 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 main(): tokens = [[]] parser = Parser.Parser(tokens, [("main", "anonymous")]) parser.compiled = {} parser.global_target = "node" parser.opt = 0 parser.externFuncs = {"main": []} parser.repl = True PackageParser.packDec(parser, "main", pack=True) js = js2py.EvalJs() js.eval(CodeGen.getRuntimeNode()) text = "" while True: line = input("> ") text = line + "\n" topc.filenames_sources = {"main": {"anonymous": text}} try: tokens[0] = Lexer.tokenize(line, "anonymous") #ResolveSymbols.insert(parser, parser, only= True) parser.package = "main" parser.opackage = "main" t = parser.tokens f = parser.filename for i in range(3): ResolveSymbols._resolve(parser, tokens[0], "anonymous", i) parser.currentNode = Tree.Root() parser.tokens = t parser.filename = f parsed = parser.parse() compiled = (parsed, {"main": []}) code = CodeGen.CodeGen("main", parsed, { "main": [] }, "node").toEval() #print(code) print(parsed.nodes[-1].type, ":", js.eval(code)) tokens[0] = [] parser.currentNode = Tree.Root() except EOFError as e: print(e)
def f(parser): op = Tree.Operator(kind, parser) if kind == "|>": print(kind) if not kind in ["|>"] and len(parser.currentNode.nodes) != 0: if not unary and isUnary(parser, parser.lookBehind()): Error.parseError(parser, "unexpected " + kind) elif unary and not isUnary(parser, parser.lookBehind()): Error.parseError(parser, "unexpected " + kind) #parser.nodeBookmark.append(len(parser.currentNode.nodes)-1) Parser.Opcode(parser, kind, lambda: operatorPop(parser, op, takesIn, unary))
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 isUnary(parser, lastToken): return lastToken.type in ["operator", "indent", "keyword"] or lastToken.token in ["(", "{", "[", ",", ":", "..", "=", "\n"] or Parser.selectStmt(parser, lastToken) != None
def _resolve(self, tokens, filename, passN= 0 ): self.filename = filename self.iter = 0 self.tokens = tokens while self.iter < len(tokens) - 1 : b = self.thisToken().token if passN == 2: if b == "import": ImportParser.importParser(self, True) elif b == "def" : if self.indentLevel == 0: nex = self.lookInfront() Parser.addBookmark(self) funcHead(self) Parser.returnBookmark(self) elif passN == 1: if b == "import": ImportParser.importParser(self, True) elif b == "type": Parser.addBookmark(self) Struct.typeParser(self, decl= True) Parser.returnBookmark(self) elif passN == 0: if b == "type": Scope.addVar(Tree.Node(self), self, self.nextToken().token, Scope.Type(True, Types.StructInit(self.thisToken().token))) self.structs[self.package][self.thisToken().token] = Struct.Struct(self.thisToken().token, [],[], {}) self.structs[self.package][self.thisToken().token].methods = {} if b == "\n": Parser.addBookmark(self) Parser.newLine(self) Parser.returnBookmark(self) self.nextToken() for i in self.imports: if not i in self.allImports[self.package]: self.allImports[self.package].append(i) self.imports = [] self.lineNumber = 1 self.normalIndent = 0
def funcHead(parser, decl= False, dontAdd= False, method= False, attachTyp = False): Scope.incrScope(parser) if parser.tokens[parser.iter+2].token == ".": if attachTyp: Error.parseError(parser, "unexpected .") parser.nextToken() name = parser.thisToken().token parser.nextToken() try: attachTyp = Types.Struct(False, name, parser.structs[parser.package][name].types, parser.package, {}) except KeyError: Error.parseError(parser, "no attachable data structure found, called "+name) return funcHead(parser, decl, dontAdd, True, attachTyp) name = parser.nextToken() if name.type != "identifier": Error.parseError(parser, "function name must be of type identifier, not "+name.type) parser.nextToken() name = name.token g = {} if parser.thisToken().token != "(": if parser.thisToken().token == "[": g = generics(parser, (str(attachTyp)+"." if method else "")+name) if parser.thisToken().token == ".": if attachTyp: Error.parseError(parser, "unexpected .") if not Scope.varExists(parser, parser.package, name): Error.parseError(parser, "cannot attach method to unknown type main."+name) try: attachTyp = Types.Struct(False, name, parser.structs[parser.package][name].types, parser.package, parserMethodGen(parser, g, parser.structs[parser.package][name])) except KeyError: Error.parseError(parser, "no attachable data structure found, called " + name) return funcHead(parser, decl, dontAdd, True, attachTyp) if parser.thisToken().token != "(": Error.parseError(parser, "expecting (") header = Tree.FuncStart(name, Types.Null(), parser) header.package = parser.package parser.currentNode.addNode(header) brace = Tree.FuncBraceOpen(parser) brace.name = name brace.package = parser.package parser.currentNode.addNode(brace) parser.currentNode = brace if method: if not type(parser.currentNode.owner) is Tree.Root and not decl: Error.parseError(parser, "method extension must be in out-most scope") typ = attachTyp self = parser.nextToken() if self.type != "identifier": Error.parseError(parser, "binding name must be identifier not "+self.type) self = self.token selfNode = Tree.Create(self, typ, parser) selfNode.package = parser.package selfNode.imutable = True parser.currentNode.addNode(selfNode) if not parser.lookInfront().token in [")", ","]: Error.parseError(parser, "expecting comma not "+parser.thisToken().token) if name[0].lower() != name[0]: Error.parseError(parser, "function name must be lower case") returnType = Types.Null() parser.paren += 1 parser.nextToken() while parser.paren != parser.parenBookmark[-1] : b = parser.thisToken().token if b == ",": parser.nextToken() continue elif b == ")": parser.paren -= 1 parser.nextToken() continue elif b == "(": Error.parseError(parser, "unexpected (") Parser.declareOnly(parser) parser.nextToken() t = parser.thisToken() do = False if t.token != "=" and t.token != "do": returnType = Types.parseType(parser) t = parser.nextToken() if t.token != "=" and t.token != "do": Error.parseError(parser, "expecting = or do") if t.token == "do": do = True parser.currentNode = brace.owner names = [i.name for i in brace.nodes] types = [i.varType for i in brace.nodes] func = Types.FuncPointer( types, returnType, g, do ) if method: Scope.decrScope(parser) header.method = True header.types = types[1:] header.attachTyp = attachTyp header.normalName = name header.name = attachTyp.normalName+"_"+header.normalName MethodParser.checkIfOperator(parser, attachTyp, name, func) if decl: MethodParser.addMethod(brace, parser, attachTyp, name, func) return attachTyp.normalName+"_"+name, names, types, header, returnType, do parser.func[parser.package][name] = func header.ftype = Types.FuncPointer(types, returnType, g) if decl: if not dontAdd: Scope.addFunc(header, parser, name, Types.FuncPointer(types, returnType, g, do)) return name, names, types, header, returnType, do
def notParen(): return not Parser.isEnd(parser)