コード例 #1
0
	def testExport(self):
		inCtx = Context.createFromMemory([''])
		txif = WppIf()
		txif.addItem(WppExpression.parse('first', inCtx))
		txBody = txif.addItem(WppBody())
		txRet = txBody.addItem(WppReturn())
		txRet.addItem(WppExpression.parse('1', inCtx))
		txif.addItem(WppExpression.parse('second', inCtx))
		txBody = txif.addItem(WppBody())
		txRet = txBody.addItem(WppReturn())
		txRet.addItem(WppExpression.parse('2', inCtx))
		txBody = txif.addItem(WppBody())
		txRet = txBody.addItem(WppReturn())
		txRet.addItem(WppExpression.parse('0', inCtx))
		outCtx = OutContextMemoryStream()
		txif.export(outCtx)
		expected = """
if first
	return 1
elif second
	return 2
else
	return 0
"""
		self.assertEqual(str(outCtx), expected.strip())
コード例 #2
0
	def testParse(self):
		source = 'super(1)'
		ctx = Context.createFromMemory(source, 'parseSuper.wpp')
		expr = WppExpression.parse(source, ctx)
		self.assertEqual(expr.type, 'call')
		caller = expr.getCaller()
		self.assertEqual(caller.type, 'super')
コード例 #3
0
    def readBody(self, context):
        from Wpp.WppVar import WppVar
        from Wpp.WppReturn import WppReturn
        from Wpp.WppExpression import WppExpression
        from Wpp.WppIf import WppIf
        from Wpp.WppForeach import WppForeach

        word = context.getFirstWord()
        if word == 'var':
            return WppVar()
        if word == 'return':
            return WppReturn()
        if word == 'if':
            return WppIf()
        if word == 'elif' or word == 'else':
            cmd = self.items[-1] if self.items else None
            if cmd and cmd.type == 'If' and cmd.canAdd():
                return cmd
            context.throwError('Invalid statement "' + word + '" without "if"')
        if word == 'foreach':
            return WppForeach()

        # Когда все стандартные инструкции закончились, возможен вариант вычисления выражения
        # Последнее выражение функции воспринимается как return
        return WppExpression.create(context.currentLine.strip(), context)
コード例 #4
0
 def commonVarInit(self, context, name, attrs, typeExpr, initialValue=None):
     self.name = name
     self.attrs |= attrs
     txTypeExpr = WppTypeExpr.parse(typeExpr, context)
     self.addItem(txTypeExpr)
     if initialValue != None:
         txValue = WppExpression.parse(initialValue, context)
         self.addItem(txValue)
コード例 #5
0
	def testConst(self):
		ctx = Context.createFromMemory('', 'fake')
		taxon = WppExpression.create('2019', ctx)
		self.assertEqual(taxon.type, 'Const')
		self.assertEqual(taxon.constType, 'int')
		self.assertEqual(taxon.value, '2019')
		self.assertEqual(taxon.exportString(), '2019')

		taxon = WppExpression.create('-3.14', ctx)
		self.assertEqual(taxon.constType, 'fixed')
		self.assertEqual(taxon.value, '-3.14')
		self.assertEqual(taxon.exportString(), '-3.14')

		taxon = WppExpression.create('1.111e-06', ctx)
		self.assertEqual(taxon.constType, 'float')
		self.assertEqual(taxon.exportString(), '1.111E-06')

		taxon = WppExpression.create('"Hello!"', ctx)
		self.assertEqual(taxon.constType, 'string')
		self.assertEqual(taxon.exportString(), '"Hello!"')

		taxon = WppExpression.create('"First\\nSecond"', ctx)
		self.assertEqual(taxon.constType, 'string')
		self.assertEqual(taxon.value, 'First\nSecond')
		self.assertEqual(taxon.exportString(), '"First\\nSecond"')

		with self.assertRaises(ErrorTaxon) as ex:
			taxon = WppExpression.create('"Not closed', ctx)
		with self.assertRaises(ErrorTaxon) as ex:
			taxon = WppExpression.create('"Invalid slash\\"', ctx)
			
コード例 #6
0
 def readHead(self, context):
     """ autoinit name [= value] """
     chunks = context.currentLine.strip().split('=', 1)
     words = chunks[0].split()
     if len(words) < 2:
         context.throwError('Expected name of autoinit')
     # Здесь пока нельзя искать поле в классе, т.к оно еще может быть не считано
     self.name = words[-1]
     self.attrs = set(words[1:-1])
     if len(chunks) == 2:
         self.addItem(WppExpression.parse(chunks[1], context))
コード例 #7
0
ファイル: WppBody.py プロジェクト: Peter-Win/WidePreprocessor
 def readBody(self, context):
     taxonType = context.getFirstWord()
     lastTaxon = self.items[-1] if len(self.items) > 0 else None
     if taxonType in ('elif',
                      'else') and lastTaxon and lastTaxon.type == 'if':
         return lastTaxon
     if self.isValidSubTaxon(taxonType):
         return super().readBody(context)
     try:
         expr = WppExpression.parse(context.currentLine.strip(), context)
         expr.attrs.add('instruction')
         return expr
     except Exception as e:
         return super().readBody(context)
コード例 #8
0
ファイル: WppIf.py プロジェクト: Peter-Win/WidePreprocessor
 def readHead(self, context):
     from Wpp.WppExpression import WppExpression
     from Wpp.WppBlock import WppBlock
     pair = context.currentLine.strip().split(' ', 1)
     self.phase = pair[0]
     if self.phase not in {'if', 'elif', 'else'}:
         context.throwError('Invalid phase of "if" statement: "' +
                            self.phase + '"')
     if self.phase != 'else':
         if len(pair) != 2:
             context.throwError('Expected boolean expression')
         expr = WppExpression.create(pair[1], context)
         self.addItem(expr)
     self.addItem(WppBlock())
コード例 #9
0
 def init(self, words, typeDescr, valueDescr, context):
     # parse main part with name and attrs
     if len(words) < 2:
         context.throwError('Expected name of ' + self.keyWord)
     self.name = words[-1]
     self.attrs |= set(words[1:-1])
     # parse type
     if typeDescr:
         self.addItem(WppLocalType.create(typeDescr, context))
     else:
         self.items.append(None)
     if valueDescr:
         self.addItem(WppExpression.create(valueDescr, context))
     self._location = context.createLocation()
コード例 #10
0
ファイル: WppIf.py プロジェクト: Peter-Win/WidePreprocessor
 def readHead(self, context):
     words = context.currentLine.strip().split(' ', 1)
     cmd = words[0]
     if cmd in ('if', 'elif'):
         if len(words) != 2:
             context.throwError('Expected conditional expression after ' +
                                cmd)
         self.addItem(WppExpression.parse(words[1], context))
         self.addItem(WppBody())
         return
     elif cmd == 'else':
         self.addItem(WppBody())
     else:
         context.throwError(
             'Invalid part of conditional instruction: "%s"' % (cmd))
コード例 #11
0
    def testConstParse(self):
        ctx = Context.createFromMemory('')
        c = WppExpression.parse('25', ctx)
        self.assertEqual(c.type, WppConst.type)
        self.assertEqual(c.constType, 'int')
        self.assertEqual(c.value, 25)
        self.assertTrue(c.isNumber())

        c = WppExpression.parse('-8', ctx)
        self.assertEqual(c.type, WppConst.type)
        self.assertEqual(c.constType, 'int')
        self.assertEqual(c.value, -8)

        c = WppExpression.parse('3.14', ctx)
        self.assertEqual(c.type, WppConst.type)
        self.assertEqual(c.constType, 'fixed')
        self.assertEqual(c.value, 3.14)
        self.assertTrue(c.isNumber())

        c = WppExpression.parse('-1.1E-4', ctx)
        self.assertEqual(c.type, WppConst.type)
        self.assertEqual(c.constType, 'float')
        self.assertEqual(c.value, -0.00011)

        c = WppExpression.parse('true', ctx)
        self.assertEqual(c.type, WppConst.type)
        self.assertEqual(c.constType, 'bool')
        self.assertEqual(c.value, True)
        self.assertFalse(c.isNumber())

        c = WppExpression.parse('false', ctx)
        self.assertEqual(c.type, WppConst.type)
        self.assertEqual(c.constType, 'bool')
        self.assertEqual(c.value, False)
        self.assertFalse(c.isNumber())

        c = WppExpression.parse('null', ctx)
        self.assertEqual(c.type, WppConst.type)
        self.assertEqual(c.constType, 'null')
        self.assertEqual(c.value, None)
        self.assertFalse(c.isNumber())
コード例 #12
0
 def readHead(self, context):
     exprCode = WppReturn.parse(context.currentLine)
     if exprCode:
         exprTaxon = WppExpression.parse(exprCode, context)
         self.setResult(exprTaxon)
コード例 #13
0
 def readHead(self, context):
     pair = context.currentLine.split(' ', 1)
     if len(pair) == 2:
         self.addItem(WppExpression.create(pair[1], context))