Ejemplo n.º 1
0
	def testConstructor(self):
		source = """
class First
	field primary: int
	constructor
		autoinit primary
class Second
	extends First
	field secondary: double
	constructor
		param primary: int
		autoinit secondary
		super(primary)
"""
		expected = """
class First:
	__slots__ = ('primary')
	def __init__(self, primary):
		self.primary = primary
class Second(First):
	__slots__ = ('secondary')
	def __init__(self, primary, secondary):
		super().__init__(primary)
		self.secondary = secondary
"""
		module = PyCore.createModuleFromWpp(source, 'superInCon.wpp')
		out = OutContextMemoryStream()
		module.exportContext(out, style)
		self.assertEqual(str(out), PyCore.strPack(expected))
Ejemplo n.º 2
0
	def testStatic(self):
		source = """
class public static Hello
	field static public first: int = 123
	field static private second: bool
"""
		expected = """
class Hello:
	first = 123
	second = False
"""
		module = PyCore.createModuleFromWpp(source, 'ststic.wpp')
		out = OutContextMemoryStream()
		module.exportContext(out, style)
		self.assertEqual(str(out), PyCore.strPack(expected))
Ejemplo n.º 3
0
    def testSingle(self):
        source = """
class simple Point
	field public x: double
	field public y: double
	constructor
		param x0: double
		param y0: double
		x = x0
		y = y0
var const pt: Point = Point(22, 44)
var const x: double = pt.x
"""
        expect = """
class Point:
	__slots__ = ('x', 'y')
	def __init__(self, x0, y0):
		self.x = x0
		self.y = y0
pt = Point(22, 44)
x = pt.x
"""
        module = PyCore.createModuleFromWpp(source, 'empty.wpp')
        ctx = OutContextMemoryStream()
        module.exportContext(ctx, style)
        self.assertEqual(str(ctx), module.strPack(expect))
Ejemplo n.º 4
0
    def testDefaultParams(self):
        source = """
class simple Point
	field public x: double
	field public y: double
	constructor
		autoinit x = 0
		autoinit y = 0
var const pt0: Point = Point()
var const pt1: Point = Point(1)
var const pt2: Point = Point(1, 2)
"""
        expect = """
class Point:
	__slots__ = ('x', 'y')
	def __init__(self, x = 0, y = 0):
		self.x = x
		self.y = y
pt0 = Point()
pt1 = Point(1)
pt2 = Point(1, 2)
"""
        module = PyCore.createModuleFromWpp(source, 'defaultParams.wpp')
        ctx = OutContextMemoryStream()
        module.exportContext(ctx, style)
        self.assertEqual(str(ctx), module.strPack(expect))
Ejemplo n.º 5
0
    def testUnary(self):
        source = """
class public Unary
	field public first: int
	field public second: int
	method public init
		param value: int
		first = -value * -10
		second = -first
	method public cloneNeg
		param src: Unary
		first = -src.first + 1
		second = -(src.second + 1)
		"""
        expected0 = ''
        expected = """
class Unary:
	__slots__ = ('first', 'second')
	def __init__(self):
		self.first = 0
		self.second = 0
	def init(self, value):
		self.first = -value * -10
		self.second = -self.first
	def cloneNeg(self, src):
		self.first = -src.first + 1
		self.second = -(src.second + 1)
		"""
        srcModule = WppCore.createMemModule(source, 'unary.fake')
        dstModule = srcModule.cloneRoot(PyCore())
        outStream = OutContextMemoryStream()
        dstModule.export(outStream)
        self.assertEqual(str(outStream), WppCore.strPack(expected))
Ejemplo n.º 6
0
    def testSuper(self):
        source = """
class public A
	field public index: int
	constructor public
		param init index
class public B
	extends A
	field public mass: double
	constructor public
		param index: int
		param init mass
		super(index)
		"""
        srcModule = WppCore.createMemModule(source, 'binOp.fake')
        dstModule = srcModule.cloneRoot(PyCore())
        outStream = OutContextMemoryStream()
        dstModule.export(outStream)
        expected = """
class A:
	__slots__ = ('index')
	def __init__(self, index):
		self.index = index

class B(A):
	__slots__ = ('mass')
	def __init__(self, index, mass):
		super().__init__(index)
		self.mass = mass
		"""
        self.assertEqual(str(outStream), WppCore.strPack(expected))
Ejemplo n.º 7
0
    def testIntDiv(self):
        source = """
var const fres: double = 10.0 / 2.0
var const ires: int = 10 / 2
var const la: long = 10
var const lb: long = 2
var const lres: long = la / lb
var const ua: unsigned int = 10
var const ub: unsigned int = 2
var const ures: unsigned int = ua / ub
"""
        expected = """
fres = 10.0 / 2.0
ires = 10 // 2
la = 10
lb = 2
lres = la // lb
ua = 10
ub = 2
ures = ua // ub
"""
        module = PyCore.createModuleFromWpp(source, 'intDiv.wpp')
        ctx = OutContextMemoryStream()
        module.exportContext(ctx, style)
        self.assertEqual(str(ctx), module.strPack(expected))
Ejemplo n.º 8
0
    def testSimple(self):
        source = """
class simple Point
	field public x: double
	field public y: double
	constructor
		autoinit x = 0
		autoinit y = 0
	operator const +: Point
		param right: const ref Point
		return Point(x + right.x, y + right.y)

var const a: Point = Point(11, 22)
var const b: Point = a + Point(0, -1)
"""
        expected = """
class Point:
	__slots__ = ('x', 'y')
	def __init__(self, x = 0, y = 0):
		self.x = x
		self.y = y
	def __add__(self, right):
		return Point(self.x + right.x, self.y + right.y)
a = Point(11, 22)
b = a + Point(0, -1)
"""
        module = PyCore.createModuleFromWpp(source, 'simple.wpp')
        ctx = OutContextMemoryStream()
        module.exportContext(ctx, style)
        self.assertEqual(str(ctx), module.strPack(expected))
Ejemplo n.º 9
0
    def testParent(self):
        source = """
class public A
class public B
	# Class B
	extends A
		"""
        expected = """
class A:
	pass

class B(A):
	\"\"\" Class B \"\"\"
	pass
		"""
        srcModule = WppCore.createMemModule(source, 'ext.fake')
        pyCore = PyCore()
        dstModule = srcModule.clone(pyCore)
        dstModule.updateRefs()
        classA = dstModule.dictionary['A']
        classB = dstModule.dictionary['B']
        self.assertEqual(classB.type, 'Class')
        self.assertTrue(classB.canBeStatic)
        self.assertEqual(classB.name, 'B')
        self.assertEqual(classB.core, pyCore)
        self.assertEqual(classB.getParent(), classA)
        outContext = OutContextMemoryStream()
        dstModule.export(outContext)
        self.assertEqual(str(outContext).strip(), WppCore.strPack(expected))
Ejemplo n.º 10
0
    def testCases(self):
        source = """
func public getNumName: String
	param value: int
	var result: String
	if value == 0
		result = "Zero"
	elif value == 1
		result = "One"
	elif value == 2
		result = "Two"
	else
		result = "Else"
	result
		"""
        expected = """
def getNumName(value):
	result = ''
	if value == 0:
		result = 'Zero'
	elif value == 1:
		result = 'One'
	elif value == 2:
		result = 'Two'
	else:
		result = 'Else'
	return result
		"""
        srcModule = WppCore.createMemModule(source, 'elseIf.fake')
        dstModule = srcModule.cloneRoot(PyCore())
        outContext = OutContextMemoryStream()
        dstModule.export(outContext)
        self.assertEqual(str(outContext), expected.strip())
Ejemplo n.º 11
0
    def testElse(self):
        source = """
func public getState: String
	param value: int
	var result: String
	if value < 0
		result = "Negative"
	else
		result = "Positive"
	result
		"""
        expected = """
def getState(value):
	result = ''
	if value < 0:
		result = 'Negative'
	else:
		result = 'Positive'
	return result
		"""
        srcModule = WppCore.createMemModule(source, 'elseIf.fake')
        dstModule = srcModule.cloneRoot(PyCore())
        outContext = OutContextMemoryStream()
        dstModule.export(outContext)
        self.assertEqual(str(outContext), expected.strip())
Ejemplo n.º 12
0
    def testMethod(self):
        source = """
class public Point
	field public x: double
	field public y: double
	operator +=: ref Point
		param pt: const ref Point
		x += pt.x
		y += pt.y
		this
		"""
        expected = """
class Point:
	__slots__ = ('x', 'y')
	def __init__(self):
		self.x = 0.0
		self.y = 0.0
	def __iadd__(self, pt):
		self.x += pt.x
		self.y += pt.y
		return self
		"""
        srcModule = WppCore.createMemModule(source, 'method.fake')
        dstModule = srcModule.cloneRoot(PyCore())
        outContext = OutContextMemoryStream()
        dstModule.export(outContext)
        self.assertEqual(str(outContext), expected.strip())
Ejemplo n.º 13
0
    def testSimple(self):
        source = """
var const public myInt: int = 22
var const public myLong: long = -123456
var public myFloat: float = 1.23
var public myDouble: double = -1.23E+04
var myTrue: bool = true
var myFalse: bool = false
var defInt: int
var defULong: unsigned long
var defFloat: float
var defDouble: double
var defBool: bool
"""
        expected = """
myInt = 22
myLong = -123456
myFloat = 1.23
myDouble = -1.23E+04
myTrue = True
myFalse = False
defInt = 0
defULong = 0
defFloat = 0.0
defDouble = 0.0
defBool = False
"""
        module = PyCore.createModuleFromWpp(source, 'pyVar.wpp')
        ctx = OutContextMemoryStream()
        module.exportContext(ctx, style)
        self.assertEqual(str(ctx), expected.strip())
Ejemplo n.º 14
0
    def testOverload(self):
        source = """
class simple Point
	field x: double
	field y: double
	constructor
		autoinit x
		autoinit y
	operator const overload +: Point
		param p: Point
		return Point(x + p.x, y + p.y)
	operator const overload +: Point
		param k: double
		return Point(x + k, y + k)
	method const overload plus: Point
		altName plusPt
		param p: const ref Point
		return Point(x + p.x, y + p.y)
	method const overload plus: Point
		altName plusN
		param k: double
		return Point(x + k, y + k)
var const a: Point = Point(11, 22) + 3
var const b: Point = a + Point(-1, -2)
var const a1: Point = Point(11, 22).plus(3)
var const b1: Point = a1.plus(Point(-1, -2))
"""
        expected = """
"""
        module = PyCore.createModuleFromWpp(source, 'overload.wpp')
        ctx = OutContextMemoryStream()
        module.exportContext(ctx, style)
        self.assertEqual(str(ctx), module.strPack(expected))
Ejemplo n.º 15
0
    def testUnary(self):
        source = """
class public Point
	field public x: double
	field public y: double
	constructor
		param init x
		param init y

	operator const -: ref Point
		Point(-x, -y)
		"""
        expected = """
class Point:
	__slots__ = ('x', 'y')
	def __init__(self, x, y):
		self.x = x
		self.y = y
	def __neg__(self):
		return Point(-self.x, -self.y)
		"""
        srcModule = WppCore.createMemModule(source, 'method.fake')
        dstModule = srcModule.cloneRoot(PyCore())
        outContext = OutContextMemoryStream()
        dstModule.export(outContext)
        self.assertEqual(str(outContext), expected.strip())
Ejemplo n.º 16
0
    def testString(self):
        source = """
func public main
	var s: String = "Hello!"
	var n: unsigned long = s.length
		"""
        expected = """
def main():
	s = 'Hello!'
	n = len(s)
		"""
        srcModule = WppCore.createMemModule(source, 'string.fake')
        dstModule = srcModule.cloneRoot(PyCore())
        mainOver = dstModule.dictionary['main']
        mainFunc = mainOver.items[0]
        cmd2 = mainFunc.getBody().items[1]
        self.assertEqual(cmd2.type, 'Var')
        expr = cmd2.getValueTaxon()
        self.assertEqual(expr.type, 'Call')
        lenId = expr.getCaller()
        self.assertEqual(lenId.type, 'IdExpr')
        self.assertEqual(lenId.id, 'len')
        lenDecl = lenId.getDeclaration()
        self.assertEqual(lenDecl.type, 'Overloads')
        self.assertEqual(lenDecl.name, 'len')

        outContext = OutContextMemoryStream()
        dstModule.export(outContext)
        self.assertEqual(str(outContext), expected.strip())
Ejemplo n.º 17
0
    def testRight(self):
        source = """
class public simple Point
	field public x: double
	field public y: double
	operator const *: Point
		param k: double
		Point(x * k, y * k)
	operator right const *: Point
		# Point = k * Point
		param k: double
		Point(k * x, k * y)
		"""
        expected0 = ''
        expected = """
class Point:
	__slots__ = ('x', 'y')
	def __init__(self):
		self.x = 0.0
		self.y = 0.0
	def __mul__(self, k):
		return Point(self.x * k, self.y * k)
	def __rmul__(self, k):
		\"\"\" Point = k * Point \"\"\"
		return Point(k * self.x, k * self.y)
		"""
        srcModule = WppCore.createMemModule(source, 'right.fake')
        dstModule = srcModule.cloneRoot(PyCore())
        outContext = OutContextMemoryStream()
        dstModule.export(outContext)
        self.assertEqual(str(outContext), expected.strip())
Ejemplo n.º 18
0
    def testStringStr(self):
        source = """
var s: String = String(15)
		"""
        srcModule = WppCore.createMemModule(source, 'toa.fake')
        s = srcModule.dictionary['s']
        v = s.getValueTaxon()
        self.assertEqual(v.type, 'New')
        c = v.getCaller()
        self.assertEqual(c.type, 'IdExpr')
        wString = c.refs['decl']
        self.assertEqual(wString.name + ':' + wString.type, 'String:Class')

        dstModule = srcModule.cloneRoot(PyCore())
        s1 = dstModule.dictionary['s']
        v1 = s1.getValueTaxon()
        self.assertEqual(v1.type, 'New')
        c1 = v1.getCaller()
        self.assertEqual(c1.type, 'IdExpr')
        pString = c1.refs['decl']
        self.assertEqual(pString.name, 'String')

        outContext = OutContextMemoryStream()
        dstModule.export(outContext)
        self.assertEqual(str(outContext), 's = str(15)')
Ejemplo n.º 19
0
	def testSimple(self):
		source = """
class public static Hello
	field public first: int = 123
	field private second: bool
"""
		expected = """
class Hello:
	__slots__ = ('first', 'second')
	def __init__(self):
		self.first = 123
		self.second = False
"""
		module = PyCore.createModuleFromWpp(source, 'simple.wpp')
		out = OutContextMemoryStream()
		module.exportContext(out, style)
		self.assertEqual(str(out), PyCore.strPack(expected))
Ejemplo n.º 20
0
 def testEmptyConstructor(self):
     srcModule = WppCore.createMemModule('class Con', 'con.fake')
     dstModule = srcModule.cloneRoot(PyCore())
     classCon = dstModule.dictionary['Con']
     self.assertEqual(classCon.type, 'Class')
     con = classCon.createEmptyConstructor()
     self.assertEqual(con.type, 'Constructor')
     self.assertEqual(classCon.findConstructor(), con.owner)
Ejemplo n.º 21
0
def exportPy(module):
    from Python.PyCore import PyCore
    from Python.style import style
    print('-- Python')
    pyModule = PyCore.createFromSource(module)
    outCtx = OutContextMemoryStream()
    pyModule.exportContext(outCtx, style)
    printCtx(outCtx)
Ejemplo n.º 22
0
    def testSimple(self):
        source = """
class public static Hello
	method getCount: unsigned int
		return 1234
	method setCount
		param count: unsigned int
		param bUpdate: bool = false
"""
        expected = """
class Hello:
	def getCount(self):
		return 1234
	def setCount(self, count, bUpdate = False):
		pass
"""
        module = PyCore.createModuleFromWpp(source, 'simple.wpp')
        out = OutContextMemoryStream()
        module.exportContext(out, style)
        self.assertEqual(str(out), PyCore.strPack(expected))
Ejemplo n.º 23
0
    def testExtends(self):
        source = """
class Top
class Middle
	extends Top
class Bottom
	extends Middle
"""
        expect = """
class Top:
	pass
class Middle(Top):
	pass
class Bottom(Middle):
	pass
"""
        module = PyCore.createModuleFromWpp(source, 'extends.wpp')
        out = OutContextMemoryStream()
        module.exportContext(out, style)
        self.assertEqual(str(out), PyCore.strPack(expect))
Ejemplo n.º 24
0
    def testConst(self):
        source = """
var public curYear: int = 2019
"""
        srcModule = WppCore.createMemModule(source, 'const.fake')
        dstModule = srcModule.cloneRoot(PyCore())
        outStream = OutContextMemoryStream()
        dstModule.export(outStream)
        expected = """
curYear = 2019
		"""
        self.assertEqual(str(outStream), WppCore.strPack(expected))
Ejemplo n.º 25
0
    def testPI(self):
        source = """var a : double = Math.PI * 0.75"""
        expected = """
import math

a = math.pi * 0.75
		"""
        srcModule = WppCore.createMemModule(source, 'pi.fake')
        dstModule = srcModule.cloneRoot(PyCore())
        outContext = OutContextMemoryStream()
        dstModule.export(outContext)
        self.assertEqual(str(outContext), WppCore.strPack(expected))
Ejemplo n.º 26
0
    def testSimpleExport(self):
        source = """
# First
# Second
# Third
class Simple
		"""
        srcModule = WppCore.createMemModule(source, 'simple.fake')
        dstModule = srcModule.clone(PyCore())
        outContext = OutContextMemoryStream()
        dstModule.export(outContext)
        self.assertEqual(
            str(outContext).strip(),
            '\"\"\" First\n Second\n Third \"\"\"\nclass Simple:\n\tpass')
Ejemplo n.º 27
0
    def testTernary(self):
        source = """
func public isGood: int
	param value: double
	value < 1.0 ? 0 : 1
		"""
        expected = """
def isGood(value):
	return 0 if value < 1.0 else 1
		"""
        srcModule = WppCore.createMemModule(source, 'ternary.fake')
        dstModule = srcModule.cloneRoot(PyCore())
        outStream = OutContextMemoryStream()
        dstModule.export(outStream)
        self.assertEqual(str(outStream), WppCore.strPack(expected))
Ejemplo n.º 28
0
    def testBinOp(self):
        source = """
var first: double = 2.2
var public second: double = (first + 1.1) / 3.3
		"""
        srcModule = WppCore.createMemModule(source, 'binOp.fake')
        dstModule = srcModule.cloneRoot(PyCore())
        outStream = OutContextMemoryStream()
        dstModule.export(outStream)
        expected = """
__first = 2.2

second = (__first + 1.1) / 3.3
		"""
        self.assertEqual(str(outStream), WppCore.strPack(expected))
Ejemplo n.º 29
0
    def testArrayInit(self):
        source = """
func public main
	var vec: Array float = [1.11, 2.22, 3.33]
		"""
        expected = """
def main():
	vec = [1.11, 2.22, 3.33]
		"""
        srcModule = WppCore.createMemModule(source, 'arrayInit.fake')
        dstModule = srcModule.cloneRoot(PyCore())

        outContext = OutContextMemoryStream()
        dstModule.export(outContext)
        self.assertEqual(str(outContext), expected.strip())
Ejemplo n.º 30
0
	def testOverride(self):
		source = """
class First
	method virtual getValue: int
		param id: int
		return id * 2
class Second
	extends First
	method override getValue: int
		param id: int
		return super(id) + 1
"""
		expected = """
class First:
	def getValue(self, id):
		return id * 2
class Second(First):
	def getValue(self, id):
		return super().getValue(id) + 1
"""
		module = PyCore.createModuleFromWpp(source, 'superInOver.wpp')
		out = OutContextMemoryStream()
		module.exportContext(out, style)
		self.assertEqual(str(out), PyCore.strPack(expected))