def testRightOver(self): source = """ class simple Point field public x: double field public y: double constructor autoinit x = 0 autoinit y = 0 operator const overload right *: Point altName rightMul param left: double return Point(left * x, left * y) operator const overload *: Point altName leftMul param right: double return Point(x * right, y * right) var const a: Point = 1.23 * Point(11, 22) var const b: Point = Point(1.1, 2.2) * 3.3 """ module = WppCore.createMemModule(source, 'right.wpp') a = module.findItem('a') decl = a.getValueTaxon().getDeclaration() self.assertEqual(TaxonAltName.getAltName(decl), 'rightMul') b = module.findItem('b') decl = b.getValueTaxon().getDeclaration() self.assertEqual(TaxonAltName.getAltName(decl), 'leftMul') ctx = OutContextMemoryStream() module.export(ctx) self.assertEqual(str(ctx), module.strPack(source))
def testFunc(self): source = """ func public func1 altName first return func public func2: int altName second param a: int return a """ module = WppCore.createMemModule(source, 'func.wpp') func1 = module.findItem('func1') self.assertEqual(func1.type, 'func') self.assertEqual(func1.getName(), 'func1') self.assertEqual(TaxonAltName.getAltName(func1), 'first') func2 = module.findItem('func2') self.assertEqual(func2.type, 'func') self.assertEqual(func2.getName(), 'func2') self.assertEqual(TaxonAltName.getAltName(func2), 'second') ctx = OutContextMemoryStream() module.export(ctx) self.assertEqual(str(ctx), WppCore.strPack(source))
def transformName(self, srcName): name = srcName if 'useAltName' in self.attrs: from core.TaxonAltName import TaxonAltName name = TaxonAltName.getAltName(self) if not name: name = self.name return self.core.getSafeName(name)
def getName(self): from core.operators import opcodeMap from core.TaxonAltName import TaxonAltName name = TaxonAltName.getAltName(self) if not name: opcode = self.name or self.owner.name # Если оператор перегружен, то имя у него пусто. Тогда используем имя владельца descr = opcodeMap[opcode] name = descr[1] if 'right' in self.attrs: name = 'r' + name return name
def onInit(self): for taxon in self.items: altName = TaxonAltName.getAltName(taxon) if not altName: # Если перегружаются конструкторы, то altName не требуется для конструктора без параметров if taxon.type == 'constructor' and len( taxon.getParamsList()) == 0: continue # У операторов возможно наличие разных имен за счет использования атрибута right if taxon.type == 'operator' and self.getCountOfName( taxon.getName()) == 1: continue taxon.throwError('altName is required for overloaded %s' % (taxon.getDebugStr())) taxon.attrs.add('useAltName')
def exportLexems(self, level, lexems, style): target = self.getCaller().getTarget() constr = target.findConstructor() newLex = [Lex.className(target.getName())] if constr and constr.type == 'overload': if not self.overloadKey: pass else: realConstr = constr.getImplementationByKey(self.overloadKey) if realConstr.isStatic(): newLex += [ Lex.dot, Lex.funcName(TaxonAltName.getAltName(realConstr)) ] lexems += newLex lexems.append(Lex.bracketBegin) args = self.getArguments() if len(args): for arg in args: arg.exportLexems(level, lexems, style) lexems.append(Lex.paramDiv) lexems[-1] = Lex.paramDivLast lexems.append(Lex.bracketEnd)
def makeStaticConstructor(conImpl): """ conImpl - Имплементация перегруженного конструктора. type = constructor """ # Check altName name = TaxonAltName.getAltName(conImpl, 'overloaded constructor') conImpl.attrs.add('useAltName') conImpl.attrs.add('static') cls = conImpl.findOwnerByType('class') # Set result type t = conImpl.addItem(conImpl.creator('@typeExprName')(cls.getName())) t.setType(cls) body = conImpl.getBody() instName = '_inst' # создание экземпляра класса при помощи конструктора с пустыми параметрами. inst = body.addItem(conImpl.creator('var')(instName), 0) inst.attrs.add('const') t = inst.addItem(conImpl.creator('@typeExprName')()) t.setType(cls) txNew = inst.addItem(conImpl.creator('new')()) caller = txNew.addItem(conImpl.creator('named')(cls.getName())) caller.setTarget(cls) # Замена всех this на обращение к _inst def changeThis(taxon): if taxon.type == 'this': taxon.replaceTaxon( conImpl.creator('named')(instName)).setTarget(inst) body.walk(changeThis) # return of created instance ret = body.addItem(conImpl.creator('return')()) retExpr = ret.addItem(conImpl.creator('named')(instName)) retExpr.setTarget(inst) inst.initAll()
def getAltName(taxon): return TaxonAltName.getAltName(taxon)