Esempio n. 1
0
 def build_conditional_body(node):
     node.append(ast.AssignNode(
         ast.IdentifierNode('_rph_foo'), ast.PlaceholderNode('foo')))
     node.append(ast.AssignNode(
         ast.IdentifierNode('_fph123'), ast.FilterNode(
             ast.IdentifierNode('_rph_foo'))))
     node.append(ast.BufferWrite(ast.IdentifierNode('_fph123')))
Esempio n. 2
0
    def test_nested_else(self):
        self.ast_description = """
        file: TestTemplate
        #def test_function
          #if True
            #set $foo = 1
          #else
            #if
              #set $foo = 2
            #else
              #set $foo = 3
            #end if
          #end if
          $foo
        #end def
        """

        ast_root, function_node, if_node = self._build_if_template()
        if_node.append(ast.AssignNode(
            ast.IdentifierNode('foo'), ast.LiteralNode(1)))
        if_node_2 = ast.IfNode(ast.LiteralNode(True))
        if_node_2.append(ast.AssignNode(
            ast.IdentifierNode('foo'), ast.LiteralNode(2)))
        if_node_2.else_.append(ast.AssignNode(
            ast.IdentifierNode('foo'), ast.LiteralNode(3)))
        if_node.else_.append(if_node_2)
        function_node.append(ast.PlaceholderNode('foo'))

        optimization_analyzer = self._get_analyzer(ast_root)

        try:
            optimization_analyzer.visit_ast(ast_root)
        except analyzer.SemanticAnalyzerError:
            self.fail('visit_ast raised SemanticAnalyzerError unexpectedly.')
Esempio n. 3
0
    def test_partial_nested_else_if(self):
        self.ast_description = """
        file: TestTemplate
        #def test_function
          #if True
            #set $foo = 1
          #else
            #if True
              #set $foo = 2
            #end if
          #end if
          $foo
        #end def
        """

        ast_root, function_node, if_node = self._build_if_template()
        if_node.append(ast.AssignNode(
            ast.IdentifierNode('foo'), ast.LiteralNode(1)))
        if_node_2 = ast.IfNode(ast.LiteralNode(True))
        if_node_2.append(ast.AssignNode(
            ast.IdentifierNode('foo'), ast.LiteralNode(2)))
        if_node.else_.append(if_node_2)
        function_node.append(ast.PlaceholderNode('foo'))

        optimization_analyzer = self._get_analyzer(ast_root)
        self.assertRaises(analyzer.SemanticAnalyzerError,
                          optimization_analyzer.visit_ast, ast_root)
Esempio n. 4
0
 def analyzeBlockNode(self, pnode):
     self.analyzeDefNode(pnode, allow_nesting=True)
     function_node = ast.CallFunctionNode(pos=pnode.pos)
     function_node.expression = self.build_ast(
         ast.PlaceholderNode(pnode.name))[0]
     p = ast.PlaceholderSubstitutionNode(function_node, pos=pnode.pos)
     call_block = self.build_ast(p)
     return call_block
Esempio n. 5
0
 def scope_setter(scope):
     scope.local_identifiers.add(ast.IdentifierNode('_rph_foo'))
     scope.aliased_expression_map[ast.PlaceholderNode('foo')] = (
         ast.IdentifierNode('_rph_foo'))
     scope.aliased_expression_map[ast.FilterNode(ast.IdentifierNode(
         '_rph_foo'))] = (ast.IdentifierNode('_fph123'))
     scope.alias_name_set.add('_fph123')
     scope.alias_name_set.add('_rph_foo')
Esempio n. 6
0
    def test_hoists_lhs_only_from_and(self):
        self.ast_description = """
        file: TestTemplate

        #def test_function
          #if $foo or $bar
          #end if
        #end def
        """

        condition = ast.BinOpNode('or', ast.PlaceholderNode('foo'),
                                  ast.PlaceholderNode('bar'))
        ast_root, function_node, if_node = self._build_if_template(condition)

        optimization_analyzer = self._get_analyzer_and_visit(ast_root)
        self.assertEqual(
            optimization_analyzer._placeholdernode_replacement.GetResults(),
            [True, False])
Esempio n. 7
0
    def test_simple_hoist(self):
        self.ast_description = """
        file: TestTemplate

        #def test_function
          $foo
          $foo
        #end def
        """

        ast_root, function_node = self._build_function_template()
        function_node.append(ast.PlaceholderNode('foo'))
        function_node.append(ast.PlaceholderNode('foo'))

        optimization_analyzer = self._get_analyzer_and_visit(ast_root)
        self.assertEqual(
            optimization_analyzer._placeholdernode_replacement.GetResults(),
            [True, True])
Esempio n. 8
0
    def test_hoists_both_from_plus(self):
        self.ast_description = """
        file: TestTemplate

        #global $foo

        #def test_function
          #set $bar = $foo + $foo
        #end def
        """

        ast_root, function_node = self._build_function_template()
        ast_root.global_placeholders.add('foo')
        function_node.append(ast.AssignNode(
            ast.IdentifierNode('bar'), ast.BinOpNode('+', ast.PlaceholderNode(
                'foo'), ast.PlaceholderNode('foo'))))

        optimization_analyzer = self._get_analyzer_and_visit(ast_root)
        self.assertEqual(
            optimization_analyzer._placeholdernode_replacement.GetResults(),
            [True, True])
Esempio n. 9
0
    def _build_for_template(self):
        """ Build a simple template with a function and a for loop.

        file: TestTemplate
        #def test_function
          #for $i in []
          #end for
        #end def
        """
        ast_root, def_node = self._build_function_template()
        target_list = ast.TargetListNode()
        target_list.append(ast.PlaceholderNode('foo'))
        expression_list = ast.ExpressionListNode()
        expression_list.append(ast.LiteralNode([]))
        for_node = ast.ForNode(target_list=target_list,
                               expression_list=expression_list)
        def_node.append(for_node)
        return (ast_root, def_node, for_node)
Esempio n. 10
0
 def build_udn_path_ast(self, path):
     pieces = path.split('.')
     node = ast.PlaceholderNode(pieces[0])
     for piece in pieces[1:]:
         node = ast.GetUDNNode(node, piece)
     return node