コード例 #1
0
 def testInvalidLexems(self):
     ctx = Context.createFromMemory('', 'fake')
     # (x
     lexems = [('(', 'const', None), ('x', 'id', None),
               ('end', 'cmd', None)]
     with self.assertRaises(ErrorTaxon) as ex:
         node, pos = scanLexems(lexems, 0, {'end'}, ctx)
     # 22)
     lexems = [('22', 'const', 'int'), (')', 'cmd', None),
               ('end', 'cmd', None)]
     with self.assertRaises(ErrorTaxon) as ex:
         node, pos = scanLexems(lexems, 0, {'end'}, ctx)
     self.assertEqual(ex.exception.args[0], 'Invalid binary operation ")"')
コード例 #2
0
 def testMinusChange(self):
     ctx = Context.createFromMemory('', 'fake')
     lexems = [('-', 'cmd', None), ('3.14', 'const', 'float'),
               ('end', 'cmd', None)]
     node, pos = scanLexems(lexems, 0, {'end'}, ctx)
     self.assertEqual(node.type, 'arg')
     self.assertEqual(node.value, '-3.14')
コード例 #3
0
 def testArrayIndex(self):
     ctx = Context.createFromMemory('', 'fake')
     lexems = [('vector', 'id', None), ('[', 'cmd', None),
               ('N', 'id', None), ('-', 'cmd', None), ('1', 'const', 'int'),
               (']', 'cmd', None), ('end', 'cmd', None)]
     node, pos = scanLexems(lexems, 0, {'end'}, ctx)
     self.assertEqual(node.type, 'index')
     self.assertEqual(node.export(), 'vector[N - 1]')
コード例 #4
0
 def testArrayIndexDouble(self):
     """ 2D Array access - table[i][j] """
     ctx = Context.createFromMemory('', 'fake')
     lexems = [('table', 'id', None), ('[', 'cmd', None), ('i', 'id', None),
               (']', 'cmd', None), ('[', 'cmd', None), ('j', 'id', None),
               (']', 'cmd', None), ('end', 'cmd', None)]
     node, pos = scanLexems(lexems, 0, {'end'}, ctx)
     self.assertEqual(node.type, 'index')
     self.assertEqual(node.export(), 'table[i][j]')
コード例 #5
0
    def testArithmetic(self):
        ctx = Context.createFromMemory('', 'fake')
        # 2 + 3 * 5
        lexems = [('2', 'const', 'int'), ('+', 'cmd', None),
                  ('3', 'const', 'int'), ('*', 'cmd', None),
                  ('5', 'const', 'int'), ('end', 'cmd', None)]
        node, pos = scanLexems(lexems, 0, {'end'}, ctx)
        self.assertEqual(node.type, 'binop')
        self.assertEqual(node.export(), '2 + 3 * 5')

        # (2 + 3) * 5
        lexems = [
            ('(', 'cmd', None), ('2', 'const', 'int'), ('+', 'cmd', None),
            ('3', 'const', 'int'), (')', 'cmd', None), ('*', 'cmd', None),
            ('5', 'const', 'int'), (',', 'cmd', None)
        ]
        node, pos = scanLexems(lexems, 0, {','}, ctx)
        self.assertEqual(node.export(), '(2 + 3) * 5')
コード例 #6
0
 def testTernaryOp(self):
     ctx = Context.createFromMemory('', 'fake')
     # a == -1 ? str(n + 42) : "Hello!"
     lexems = [('a', 'id', None), ('==', 'cmd', None), ('-', 'cmd', None),
               ('1', 'const', 'int'), ('?', 'cmd', None),
               ('str', 'id', None), ('(', 'cmd', None), ('n', 'id', None),
               ('+', 'cmd', None), ('42', 'const', 'int'),
               (')', 'cmd', None), (':', 'cmd', None),
               ('Hello!', 'const', 'string'), ('end', 'cmd', None)]
     node, pos = scanLexems(lexems, 0, {'end'}, ctx)
     self.assertEqual(node.export(), 'a == -1 ? str(n + 42) : "Hello!"')
コード例 #7
0
    def readHead(self, context):
        self.addItem(WppBlock())
        pair = context.currentLine.split(' ', 1)
        lexems = parseExpr(pair[1], context)
        node, pos = scanLexems(lexems, 0, {'=>'}, context)
        collection = node.makeTaxon()
        self.addItem(collection)

        # value
        pos += 1
        value, lexemType, constType = lexems[pos]
        if value == 'var' and lexemType == 'id':
            # Для значения создается новая переменная
            pos += 1
            value, lexemType, constType = lexems[pos]
            if lexemType != 'id':
                context.throwError('Expected variable name for value')
            self.attrs.add('localValue')
            varValue = WppVar()
            varValue.name = value
            self.addItem(varValue)
            self.setRef('value', varValue)
        else:
            if lexemType != 'id':
                context.throwError('Expected variable name for value')
            self.valueId = value

        # index (optional)
        pos += 1
        value, lexemType, constType = lexems[pos]
        if lexemType == 'cmd' and value == '=>':
            pos += 1
            value, lexemType, constType = lexems[pos]
            if value == 'var' and lexemType == 'id':
                pos += 1
                value, lexemType, constType = lexems[pos]
                if lexemType != 'id':
                    context.throwError('Expected variable name for index')
                self.attrs.add('localIndex')
                varIndex = WppVar()
                varIndex.name = value
                self.addItem(varIndex)
                self.setRef('index', varIndex)
            else:
                if lexemType != 'id':
                    context.throwError('Expected variable name for index')
                self.indexId = value
コード例 #8
0
    def testParse(self):
        ctx = Context.createFromMemory('', 'fake')
        lexems = [('[', 'cmd', None),
                  ('1', 'const', 'int'), (',', 'cmd', None),
                  ('2', 'const', 'int'), (']', 'cmd', None),
                  ('end', 'cmd', None)]
        node, pos = createArray(lexems, 1, ctx)
        v, t, x = lexems[pos]
        self.assertEqual(t, 'cmd')
        self.assertEqual(v, 'end')
        self.assertEqual(node.export(), '[1, 2]')

        stack = [node]
        optimizeStack(stack, 100, ctx)
        self.assertEqual(len(stack), 1)

        node, pos = scanLexems(lexems, 0, {'end'}, ctx)
        self.assertEqual(node.export(), '[1, 2]')
コード例 #9
0
	def create(string, context):
		lexems = parseExpr(string, context)
		node, pos = scanLexems(lexems, 0, {'end'}, context)
		node.update()
		return node.makeTaxon()