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 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 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 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 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 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 in ["with", "either", "is"]: tmp = parser.currentNode if parser.thisToken().token == "either": Enum.enumParser(parser, name, decl, gen) elif parser.thisToken().token == "is": Alias.aliasParser(parser, name, decl, gen) else: 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 _types = parser.structs[parser.package][name]._types parser.structs[parser.package][name] = Struct(name, args, fields, gen, typ, parser.package) tmp = parser.structs[parser.package][name]._types parser.structs[parser.package][name].methods = meth parser.structs[parser.package][name].package = parser.package parser.structs[parser.package][name]._types = _types _types.update(tmp) Scope.changeType(parser, name, parser.structs[parser.package][name] ) typ.struct = parser.structs[parser.package][name] Scope.decrScope(parser)
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 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 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 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: try: attachTyp = parser.interfaces[parser.package][name] if not type(attachTyp) is Types.Enum: Error.parseError( parser, "no attachable data structure found, called " + name) 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, (attachTyp.normalName + "." 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: try: tmp = parser.interfaces[parser.package][name] if not type(tmp) is Types.Enum: raise KeyError attachTyp = Types.replaceT( tmp, parserMethodGen(parser, g, tmp)) 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 (") if method and not type(parser.currentNode) is Tree.Root and not decl: Error.parseError(parser, "method extension must be in out-most scope") 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: 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) header.do = 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, brace, returnType, do #parser.func[parser.package][name] = func header.ftype = func if decl: if not dontAdd: Scope.addFunc(header, parser, name, func) return name, names, types, brace, returnType, do
def typeParser(parser, decl=False): name = parser.nextToken() Scope.incrScope(parser) if name.type != "identifier" and name.token != "ext": Error.parseError(parser, "type name must be an identifier") externalStruct = False if name.token == "ext": externalStruct = True name = parser.nextToken() if name.type != "identifier" and name.token != "ext": Error.parseError(parser, "type name must be an identifier") name = name.token if name[0].lower() == name[0] and not externalStruct: Error.parseError(parser, "struct name must be upper case") import collections as coll gen = coll.OrderedDict() if parser.nextToken().token == "[": if externalStruct: Error.parseError(parser, "External structs cannot be generic") gen = FuncParser.generics(parser, name) if parser.thisToken().token != "=": if parser.thisToken().token in ["with", "either", "is"]: if externalStruct: Error.parseError( parser, "Can only define external structs not interfaces, enums or aliases" ) tmp = parser.currentNode if parser.thisToken().token == "either": Enum.enumParser(parser, name, decl, gen) elif parser.thisToken().token == "is": Alias.aliasParser(parser, name, decl, gen) else: 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 typ.externalStruct = externalStruct 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 if not decl: using = parser.structs[parser.package][name].using for i in parser.currentNode: if type(i) is Tree.Using: using.append(i.name) typ.fields = [i.name for i in typ] typ.args = args typ.generics = gen typ.nodes = [] parser.currentNode = tmp if decl: meth = parser.structs[parser.package][name].methods _types = parser.structs[parser.package][name]._types #if name == "Channel": # print("Channel make") # print(id(_types)) parser.structs[parser.package][name] = Struct(name, args, fields, gen, typ, parser.package) tmp = parser.structs[parser.package][name]._types parser.structs[parser.package][name].methods = meth parser.structs[parser.package][name].package = parser.package parser.structs[parser.package][name]._types = _types parser.structs[parser.package][name].externalStruct = externalStruct _types.update(tmp) Scope.changeType(parser, name, parser.structs[parser.package][name]) typ.struct = parser.structs[parser.package][name] Scope.decrScope(parser)