예제 #1
0
    def test_c_parse():
        src1 = """\
        int a, b = 4;
        float c, d = 2.4;
        """
        expr1.convert_to_expr(src1, "c")
        ls = expr1.return_expr()

        assert ls[0] == Declaration(
            Variable(Symbol("a"),
                     type=IntBaseType(String("integer")),
                     value=Integer(0)))
        assert ls[1] == Declaration(
            Variable(Symbol("b"),
                     type=IntBaseType(String("integer")),
                     value=Integer(4)))
        assert ls[2] == Declaration(
            Variable(
                Symbol("c"),
                type=FloatBaseType(String("real")),
                value=Float("0.0", precision=53),
            ))
        assert ls[3] == Declaration(
            Variable(
                Symbol("d"),
                type=FloatBaseType(String("real")),
                value=Float("2.3999999999999999", precision=53),
            ))
예제 #2
0
 def test_var():
     expr1.convert_to_expr(src, "f")
     ls = expr1.return_expr()
     for iter in expr1.return_expr():
         assert isinstance(iter, Declaration)
     assert ls[0] == Declaration(
         Variable(Symbol("a"), type=IntBaseType(String("integer")), value=Integer(0))
     )
     assert ls[1] == Declaration(
         Variable(Symbol("b"), type=IntBaseType(String("integer")), value=Integer(0))
     )
     assert ls[2] == Declaration(
         Variable(Symbol("c"), type=IntBaseType(String("integer")), value=Integer(0))
     )
     assert ls[3] == Declaration(
         Variable(Symbol("d"), type=IntBaseType(String("integer")), value=Integer(0))
     )
     assert ls[4] == Declaration(
         Variable(Symbol("p"), type=FloatBaseType(String("real")), value=Float(0.0))
     )
     assert ls[5] == Declaration(
         Variable(Symbol("q"), type=FloatBaseType(String("real")), value=Float(0.0))
     )
     assert ls[6] == Declaration(
         Variable(Symbol("r"), type=FloatBaseType(String("real")), value=Float(0.0))
     )
     assert ls[7] == Declaration(
         Variable(Symbol("s"), type=FloatBaseType(String("real")), value=Float(0.0))
     )
예제 #3
0
    def test_function():
        c_src1 = "void fun1()" + "\n" + "{" + "\n" + "int a;" + "\n" + "}"
        c_src2 = ("int fun2()" + "\n" + "{" + "\n" + "int a;" + "\n" +
                  "return a;" + "\n" + "}")
        c_src3 = ("float fun3()" + "\n" + "{" + "\n" + "float b;" + "\n" +
                  "return b;" + "\n" + "}")
        c_src4 = "float fun4()" + "\n" + "{}"

        res1 = SymPyExpression(c_src1, "c").return_expr()
        res2 = SymPyExpression(c_src2, "c").return_expr()
        res3 = SymPyExpression(c_src3, "c").return_expr()
        res4 = SymPyExpression(c_src4, "c").return_expr()

        assert res1[0] == FunctionDefinition(
            NoneToken(),
            name=String("fun1"),
            parameters=(),
            body=CodeBlock(
                Declaration(
                    Variable(
                        Symbol("a"),
                        type=IntBaseType(String("integer")),
                        value=Integer(0),
                    ))),
        )

        assert res2[0] == FunctionDefinition(
            IntBaseType(String("integer")),
            name=String("fun2"),
            parameters=(),
            body=CodeBlock(
                Declaration(
                    Variable(
                        Symbol("a"),
                        type=IntBaseType(String("integer")),
                        value=Integer(0),
                    )),
                Return("a"),
            ),
        )

        assert res3[0] == FunctionDefinition(
            FloatBaseType(String("real")),
            name=String("fun3"),
            parameters=(),
            body=CodeBlock(
                Declaration(
                    Variable(
                        Symbol("b"),
                        type=FloatBaseType(String("real")),
                        value=Float("0.0", precision=53),
                    )),
                Return("b"),
            ),
        )

        assert res4[0] == FunctionPrototype(FloatBaseType(String("real")),
                                            name=String("fun4"),
                                            parameters=())
예제 #4
0
    def test_parameters():
        c_src1 = ('void fun1( int a)' + '\n' + '{' + '\n' + 'int i;' + '\n' +
                  '}')
        c_src2 = ('int fun2(float x, float y)' + '\n' + '{' + '\n' + 'int a;' +
                  '\n' + 'return a;' + '\n' + '}')
        c_src3 = ('float fun3(int p, float q, int r)' + '\n' + '{' + '\n' +
                  'float b;' + '\n' + 'return b;' + '\n' + '}')

        res1 = SymPyExpression(c_src1, 'c').return_expr()
        res2 = SymPyExpression(c_src2, 'c').return_expr()
        res3 = SymPyExpression(c_src3, 'c').return_expr()

        assert res1[0] == FunctionDefinition(
            NoneToken(),
            name=String('fun1'),
            parameters=(Variable(Symbol('a'),
                                 type=IntBaseType(String('integer')),
                                 value=Integer(0)), ),
            body=CodeBlock(
                Declaration(
                    Variable(Symbol('i'),
                             type=IntBaseType(String('integer')),
                             value=Integer(0)))))

        assert res2[0] == FunctionDefinition(
            IntBaseType(String('integer')),
            name=String('fun2'),
            parameters=(Variable(Symbol('x'),
                                 type=FloatBaseType(String('real')),
                                 value=Float('0.0', precision=53)),
                        Variable(Symbol('y'),
                                 type=FloatBaseType(String('real')),
                                 value=Float('0.0', precision=53))),
            body=CodeBlock(
                Declaration(
                    Variable(Symbol('a'),
                             type=IntBaseType(String('integer')),
                             value=Integer(0))), Return('a')))

        assert res3[0] == FunctionDefinition(
            FloatBaseType(String('real')),
            name=String('fun3'),
            parameters=(Variable(Symbol('p'),
                                 type=IntBaseType(String('integer')),
                                 value=Integer(0)),
                        Variable(Symbol('q'),
                                 type=FloatBaseType(String('real')),
                                 value=Float('0.0', precision=53)),
                        Variable(Symbol('r'),
                                 type=IntBaseType(String('integer')),
                                 value=Integer(0))),
            body=CodeBlock(
                Declaration(
                    Variable(Symbol('b'),
                             type=FloatBaseType(String('real')),
                             value=Float('0.0', precision=53))), Return('b')))
예제 #5
0
        def transform_floating_literal(self, node):
            """Transformation function for floating literal

            Used to get the value and type of the given floating literal.

            Returns
            =======

            val : list
                List with two arguments type and Value
                type contains the type of float
                value contains the value stored in the variable

            Notes
            =====

            Only Base Float type supported for now

            """
            type = FloatBaseType(String('real'))
            try:
                value = next(node.get_tokens()).spelling
            except (StopIteration, ValueError):
                # No tokens
                value = Float(node.literal)
            val = [type, value]
            return val
예제 #6
0
        def visit_Variable(self, node):
            """Visitor Function for Variable Declaration

            Visits each variable declaration present in the ASR and creates a
            Symbol declaration for each variable

            Notes
            =====

            The functions currently only support declaration of integer and
            real variables. Other data types are still under development.

            Raises
            ======

            NotImplementedError() when called for unsupported data types

            """
            if isinstance(node.type, asr.Integer):
                var_type = IntBaseType(String('integer'))
                value = Integer(0)
            elif isinstance(node.type, asr.Real):
                var_type = FloatBaseType(String('real'))
                value = Float(0.0)
            else:
                raise NotImplementedError("Data type not supported")

            if not (node.intent == 'in'):
                new_node = Variable(node.name).as_Declaration(type=var_type,
                                                              value=value)
                self._py_ast.append(new_node)
예제 #7
0
    def test_function():
        c_src1 = ('void fun1()' + '\n' + '{' + '\n' + 'int a;' + '\n' + '}')
        c_src2 = ('int fun2()' + '\n' + '{' + '\n' + 'int a;' + '\n' +
                  'return a;' + '\n' + '}')
        c_src3 = ('float fun3()' + '\n' + '{' + '\n' + 'float b;' + '\n' +
                  'return b;' + '\n' + '}')
        c_src4 = ('float fun4()' + '\n' + '{}')

        res1 = SymPyExpression(c_src1, 'c').return_expr()
        res2 = SymPyExpression(c_src2, 'c').return_expr()
        res3 = SymPyExpression(c_src3, 'c').return_expr()
        res4 = SymPyExpression(c_src4, 'c').return_expr()

        assert res1[0] == FunctionDefinition(
            NoneToken(),
            name=String('fun1'),
            parameters=(),
            body=CodeBlock(
                Declaration(
                    Variable(Symbol('a'),
                             type=IntBaseType(String('integer')),
                             value=Integer(0)))))

        assert res2[0] == FunctionDefinition(
            IntBaseType(String('integer')),
            name=String('fun2'),
            parameters=(),
            body=CodeBlock(
                Declaration(
                    Variable(Symbol('a'),
                             type=IntBaseType(String('integer')),
                             value=Integer(0))), Return('a')))

        assert res3[0] == FunctionDefinition(
            FloatBaseType(String('real')),
            name=String('fun3'),
            parameters=(),
            body=CodeBlock(
                Declaration(
                    Variable(Symbol('b'),
                             type=FloatBaseType(String('real')),
                             value=Float('0.0', precision=53))), Return('b')))

        assert res4[0] == FunctionPrototype(FloatBaseType(String('real')),
                                            name=String('fun4'),
                                            parameters=())
예제 #8
0
def test_sym_expr():
    src1 = (src + """\
        d = a + b -c
        """)
    expr3 = SymPyExpression(src, 'f')
    expr4 = SymPyExpression(src1, 'f')
    ls1 = expr3.return_expr()
    ls2 = expr4.return_expr()
    for i in range(0, 7):
        assert isinstance(ls1[i], Declaration)
        assert isinstance(ls2[i], Declaration)
    assert isinstance(ls2[8], Assignment)
    assert ls1[0] == Declaration(
        Variable(Symbol('a'),
                 type=IntBaseType(String('integer')),
                 value=Integer(0)))
    assert ls1[1] == Declaration(
        Variable(Symbol('b'),
                 type=IntBaseType(String('integer')),
                 value=Integer(0)))
    assert ls1[2] == Declaration(
        Variable(Symbol('c'),
                 type=IntBaseType(String('integer')),
                 value=Integer(0)))
    assert ls1[3] == Declaration(
        Variable(Symbol('d'),
                 type=IntBaseType(String('integer')),
                 value=Integer(0)))
    assert ls1[4] == Declaration(
        Variable(Symbol('p'),
                 type=FloatBaseType(String('real')),
                 value=Float(0.0)))
    assert ls1[5] == Declaration(
        Variable(Symbol('q'),
                 type=FloatBaseType(String('real')),
                 value=Float(0.0)))
    assert ls1[6] == Declaration(
        Variable(Symbol('r'),
                 type=FloatBaseType(String('real')),
                 value=Float(0.0)))
    assert ls1[7] == Declaration(
        Variable(Symbol('s'),
                 type=FloatBaseType(String('real')),
                 value=Float(0.0)))
    assert ls2[8] == Assignment(Variable(Symbol('d')),
                                Symbol('a') + Symbol('b') - Symbol('c'))
예제 #9
0
        def transform_parm_decl(self, node):
            """Transformation function for Parameter Declaration

            Used to create parameter nodes for the required functions for the
            respective nodes in the clang AST

            Returns
            =======

            param : Codegen AST Node
                Variable node with the value nad type of the variable

            Raises
            ======

            ValueError if multiple children encountered in the parameter node

            """
            if (node.type.kind == cin.TypeKind.INT):
                type = IntBaseType(String('integer'))
                value = Integer(0)
            elif (node.type.kind == cin.TypeKind.FLOAT):
                type = FloatBaseType(String('real'))
                value = Float(0.0)
            try:
                children = node.get_children()
                child = next(children)

                # Any namespace nodes can be ignored
                while child.kind in [cin.CursorKind.NAMESPACE_REF,
                                     cin.CursorKind.TYPE_REF,
                                     cin.CursorKind.TEMPLATE_REF]:
                    child = next(children)

                # If there is a child, it is the default value of the parameter.
                args = self.transform(child)
                param = Variable(
                    node.spelling
                ).as_Declaration(
                    type = args[0],
                    value = args[1]
                )
            except StopIteration:
                param = Variable(
                    node.spelling
                ).as_Declaration(
                        type = type,
                        value = value
                )

            try:
                value = self.transform(next(children))
                raise ValueError("Can't handle multiple children on parameter")
            except StopIteration:
                pass

            return param
예제 #10
0
    def test_fortran_parse():
        expr = SymPyExpression(src, "f")
        ls = expr.return_expr()

        assert ls[0] == Declaration(
            Variable(Symbol("a"),
                     type=IntBaseType(String("integer")),
                     value=Integer(0)))
        assert ls[1] == Declaration(
            Variable(Symbol("b"),
                     type=IntBaseType(String("integer")),
                     value=Integer(0)))
        assert ls[2] == Declaration(
            Variable(Symbol("c"),
                     type=IntBaseType(String("integer")),
                     value=Integer(0)))
        assert ls[3] == Declaration(
            Variable(Symbol("d"),
                     type=IntBaseType(String("integer")),
                     value=Integer(0)))
        assert ls[4] == Declaration(
            Variable(
                Symbol("p"),
                type=FloatBaseType(String("real")),
                value=Float("0.0", precision=53),
            ))
        assert ls[5] == Declaration(
            Variable(
                Symbol("q"),
                type=FloatBaseType(String("real")),
                value=Float("0.0", precision=53),
            ))
        assert ls[6] == Declaration(
            Variable(
                Symbol("r"),
                type=FloatBaseType(String("real")),
                value=Float("0.0", precision=53),
            ))
        assert ls[7] == Declaration(
            Variable(
                Symbol("s"),
                type=FloatBaseType(String("real")),
                value=Float("0.0", precision=53),
            ))
예제 #11
0
    def test_float():
        c_src1 = "float a = 1.0;"
        c_src2 = "float a = 1.25;" + "\n" + "float b = 2.39;" + "\n"
        c_src3 = "float x = 1, y = 2;"
        c_src4 = "float p = 5, e = 7.89;"

        res1 = SymPyExpression(c_src1, "c").return_expr()
        res2 = SymPyExpression(c_src2, "c").return_expr()
        res3 = SymPyExpression(c_src3, "c").return_expr()
        res4 = SymPyExpression(c_src4, "c").return_expr()

        assert res1[0] == Declaration(
            Variable(
                Symbol("a"),
                type=FloatBaseType(String("real")),
                value=Float("1.0", precision=53),
            ))

        assert res2[0] == Declaration(
            Variable(
                Symbol("a"),
                type=FloatBaseType(String("real")),
                value=Float("1.25", precision=53),
            ))

        assert res2[1] == Declaration(
            Variable(
                Symbol("b"),
                type=FloatBaseType(String("real")),
                value=Float("2.3900000000000001", precision=53),
            ))

        assert res3[0] == Declaration(
            Variable(
                Symbol("x"),
                type=FloatBaseType(String("real")),
                value=Float("1.0", precision=53),
            ))

        assert res3[1] == Declaration(
            Variable(
                Symbol("y"),
                type=FloatBaseType(String("real")),
                value=Float("2.0", precision=53),
            ))

        assert res4[0] == Declaration(
            Variable(
                Symbol("p"),
                type=FloatBaseType(String("real")),
                value=Float("5.0", precision=53),
            ))

        assert res4[1] == Declaration(
            Variable(
                Symbol("e"),
                type=FloatBaseType(String("real")),
                value=Float("7.89", precision=53),
            ))
예제 #12
0
 def test_sym_expr():
     src1 = (
         src
         + """\
         d = a + b -c
         """
     )
     expr3 = SymPyExpression(src, "f")
     expr4 = SymPyExpression(src1, "f")
     ls1 = expr3.return_expr()
     ls2 = expr4.return_expr()
     for i in range(0, 7):
         assert isinstance(ls1[i], Declaration)
         assert isinstance(ls2[i], Declaration)
     assert isinstance(ls2[8], Assignment)
     assert ls1[0] == Declaration(
         Variable(Symbol("a"), type=IntBaseType(String("integer")), value=Integer(0))
     )
     assert ls1[1] == Declaration(
         Variable(Symbol("b"), type=IntBaseType(String("integer")), value=Integer(0))
     )
     assert ls1[2] == Declaration(
         Variable(Symbol("c"), type=IntBaseType(String("integer")), value=Integer(0))
     )
     assert ls1[3] == Declaration(
         Variable(Symbol("d"), type=IntBaseType(String("integer")), value=Integer(0))
     )
     assert ls1[4] == Declaration(
         Variable(Symbol("p"), type=FloatBaseType(String("real")), value=Float(0.0))
     )
     assert ls1[5] == Declaration(
         Variable(Symbol("q"), type=FloatBaseType(String("real")), value=Float(0.0))
     )
     assert ls1[6] == Declaration(
         Variable(Symbol("r"), type=FloatBaseType(String("real")), value=Float(0.0))
     )
     assert ls1[7] == Declaration(
         Variable(Symbol("s"), type=FloatBaseType(String("real")), value=Float(0.0))
     )
     assert ls2[8] == Assignment(
         Variable(Symbol("d")), Symbol("a") + Symbol("b") - Symbol("c")
     )
예제 #13
0
    def test_variable():
        c_src1 = ('int a;' + '\n' + 'int b;' + '\n')
        c_src2 = ('float a;' + '\n' + 'float b;' + '\n')
        c_src3 = ('int a;' + '\n' + 'float b;' + '\n' + 'int c;')

        res1 = SymPyExpression(c_src1, 'c').return_expr()
        res2 = SymPyExpression(c_src2, 'c').return_expr()
        res3 = SymPyExpression(c_src3, 'c').return_expr()

        assert res1[0] == Declaration(
            Variable(Symbol('a'),
                     type=IntBaseType(String('integer')),
                     value=Integer(0)))
        assert res1[1] == Declaration(
            Variable(Symbol('b'),
                     type=IntBaseType(String('integer')),
                     value=Integer(0)))

        assert res2[0] == Declaration(
            Variable(Symbol('a'),
                     type=FloatBaseType(String('real')),
                     value=Float('0.0', precision=53)))
        assert res2[1] == Declaration(
            Variable(Symbol('b'),
                     type=FloatBaseType(String('real')),
                     value=Float('0.0', precision=53)))

        assert res3[0] == Declaration(
            Variable(Symbol('a'),
                     type=IntBaseType(String('integer')),
                     value=Integer(0)))

        assert res3[1] == Declaration(
            Variable(Symbol('b'),
                     type=FloatBaseType(String('real')),
                     value=Float('0.0', precision=53)))

        assert res3[2] == Declaration(
            Variable(Symbol('c'),
                     type=IntBaseType(String('integer')),
                     value=Integer(0)))
예제 #14
0
    def test_float():
        c_src1 = 'float a = 1.0;'
        c_src2 = ('float a = 1.25;' + '\n' + 'float b = 2.39;' + '\n')

        res1 = SymPyExpression(c_src1, 'c').return_expr()
        res2 = SymPyExpression(c_src2, 'c').return_expr()

        assert res1[0] == Declaration(
            Variable(Symbol('a'),
                     type=FloatBaseType(String('real')),
                     value=Float('1.0', precision=53)))

        assert res2[0] == Declaration(
            Variable(Symbol('a'),
                     type=FloatBaseType(String('real')),
                     value=Float('1.25', precision=53)))

        assert res2[1] == Declaration(
            Variable(Symbol('b'),
                     type=FloatBaseType(String('real')),
                     value=Float('2.3900000000000001', precision=53)))
예제 #15
0
def test_var():
    expr1.convert_to_expr(src, 'f')
    ls = expr1.return_expr()
    for iter in expr1.return_expr():
        assert isinstance(iter, Declaration)
    assert ls[0] == Declaration(
        Variable(Symbol('a'),
                 type=IntBaseType(String('integer')),
                 value=Integer(0)))
    assert ls[1] == Declaration(
        Variable(Symbol('b'),
                 type=IntBaseType(String('integer')),
                 value=Integer(0)))
    assert ls[2] == Declaration(
        Variable(Symbol('c'),
                 type=IntBaseType(String('integer')),
                 value=Integer(0)))
    assert ls[3] == Declaration(
        Variable(Symbol('d'),
                 type=IntBaseType(String('integer')),
                 value=Integer(0)))
    assert ls[4] == Declaration(
        Variable(Symbol('p'),
                 type=FloatBaseType(String('real')),
                 value=Float(0.0)))
    assert ls[5] == Declaration(
        Variable(Symbol('q'),
                 type=FloatBaseType(String('real')),
                 value=Float(0.0)))
    assert ls[6] == Declaration(
        Variable(Symbol('r'),
                 type=FloatBaseType(String('real')),
                 value=Float(0.0)))
    assert ls[7] == Declaration(
        Variable(Symbol('s'),
                 type=FloatBaseType(String('real')),
                 value=Float(0.0)))
예제 #16
0
    def test_fortran_parse():
        expr = SymPyExpression(src, 'f')
        ls = expr.return_expr()

        assert ls[0] == Declaration(
            Variable(Symbol('a'),
                     type=IntBaseType(String('integer')),
                     value=Integer(0)))
        assert ls[1] == Declaration(
            Variable(Symbol('b'),
                     type=IntBaseType(String('integer')),
                     value=Integer(0)))
        assert ls[2] == Declaration(
            Variable(Symbol('c'),
                     type=IntBaseType(String('integer')),
                     value=Integer(0)))
        assert ls[3] == Declaration(
            Variable(Symbol('d'),
                     type=IntBaseType(String('integer')),
                     value=Integer(0)))
        assert ls[4] == Declaration(
            Variable(Symbol('p'),
                     type=FloatBaseType(String('real')),
                     value=Float('0.0', precision=53)))
        assert ls[5] == Declaration(
            Variable(Symbol('q'),
                     type=FloatBaseType(String('real')),
                     value=Float('0.0', precision=53)))
        assert ls[6] == Declaration(
            Variable(Symbol('r'),
                     type=FloatBaseType(String('real')),
                     value=Float('0.0', precision=53)))
        assert ls[7] == Declaration(
            Variable(Symbol('s'),
                     type=FloatBaseType(String('real')),
                     value=Float('0.0', precision=53)))
예제 #17
0
    def test_c_parse():
        src1 = """\
        int a, b = 4;
        float c, d = 2.4;
        """
        expr1.convert_to_expr(src1, 'c')
        ls = expr1.return_expr()

        assert ls[0] == Declaration(
            Variable(Symbol('a'),
                     type=IntBaseType(String('integer')),
                     value=Integer(0)))
        assert ls[1] == Declaration(
            Variable(Symbol('b'),
                     type=IntBaseType(String('integer')),
                     value=Integer(4)))
        assert ls[2] == Declaration(
            Variable(Symbol('c'),
                     type=FloatBaseType(String('real')),
                     value=Float('0.0', precision=53)))
        assert ls[3] == Declaration(
            Variable(Symbol('d'),
                     type=FloatBaseType(String('real')),
                     value=Float('2.3999999999999999', precision=53)))
예제 #18
0
        def visit_Function(self, node):
            """Visitor Function for function Definitions

            Visits each function definition present in the ASR and creates a
            function definition node in the Python AST with all the elements of the
            given function

            The functions declare all the variables required as SymPy symbols in
            the function before the function definition

            This function also the call_visior_function to parse the contents of
            the function body

            """
            # TODO: Return statement, variable declaration
            fn_args = []
            fn_body = []
            fn_name = node.name
            for arg_iter in node.args:
                fn_args.append(Variable(arg_iter.name))
            for i in node.body:
                fn_ast = call_visitor(i)
            try:
                fn_body_expr = fn_ast
            except UnboundLocalError:
                fn_body_expr = []
            for sym in node.symtab.symbols:
                decl = call_visitor(node.symtab.symbols[sym])
                for symbols in decl:
                    fn_body.append(symbols)
            for elem in fn_body_expr:
                fn_body.append(elem)
            fn_body.append(Return(Variable(node.return_var.name)))
            if isinstance(node.return_var.type, asr.Integer):
                ret_type = IntBaseType(String('integer'))
            elif isinstance(node.return_var.type, asr.Real):
                ret_type = FloatBaseType(String('real'))
            else:
                raise NotImplementedError("Data type not supported")
            new_node = FunctionDefinition(return_type=ret_type,
                                          name=fn_name,
                                          parameters=fn_args,
                                          body=fn_body)
            self._py_ast.append(new_node)
예제 #19
0
    def test_float():
        c_src1 = 'float a = 1.0;'
        c_src2 = ('float a = 1.25;' + '\n' + 'float b = 2.39;' + '\n')
        c_src3 = 'float x = 1, y = 2;'
        c_src4 = 'float p = 5, e = 7.89;'

        res1 = SymPyExpression(c_src1, 'c').return_expr()
        res2 = SymPyExpression(c_src2, 'c').return_expr()
        res3 = SymPyExpression(c_src3, 'c').return_expr()
        res4 = SymPyExpression(c_src4, 'c').return_expr()

        assert res1[0] == Declaration(
            Variable(Symbol('a'),
                     type=FloatBaseType(String('real')),
                     value=Float('1.0', precision=53)))

        assert res2[0] == Declaration(
            Variable(Symbol('a'),
                     type=FloatBaseType(String('real')),
                     value=Float('1.25', precision=53)))

        assert res2[1] == Declaration(
            Variable(Symbol('b'),
                     type=FloatBaseType(String('real')),
                     value=Float('2.3900000000000001', precision=53)))

        assert res3[0] == Declaration(
            Variable(Symbol('x'),
                     type=FloatBaseType(String('real')),
                     value=Float('1.0', precision=53)))

        assert res3[1] == Declaration(
            Variable(Symbol('y'),
                     type=FloatBaseType(String('real')),
                     value=Float('2.0', precision=53)))

        assert res4[0] == Declaration(
            Variable(Symbol('p'),
                     type=FloatBaseType(String('real')),
                     value=Float('5.0', precision=53)))

        assert res4[1] == Declaration(
            Variable(Symbol('e'),
                     type=FloatBaseType(String('real')),
                     value=Float('7.89', precision=53)))
예제 #20
0
    def test_variable():
        c_src1 = "int a;" + "\n" + "int b;" + "\n"
        c_src2 = "float a;" + "\n" + "float b;" + "\n"
        c_src3 = "int a;" + "\n" + "float b;" + "\n" + "int c;"
        c_src4 = "int x = 1, y = 6.78;" + "\n" + "float p = 2, q = 9.67;"

        res1 = SymPyExpression(c_src1, "c").return_expr()
        res2 = SymPyExpression(c_src2, "c").return_expr()
        res3 = SymPyExpression(c_src3, "c").return_expr()
        res4 = SymPyExpression(c_src4, "c").return_expr()

        assert res1[0] == Declaration(
            Variable(Symbol("a"),
                     type=IntBaseType(String("integer")),
                     value=Integer(0)))

        assert res1[1] == Declaration(
            Variable(Symbol("b"),
                     type=IntBaseType(String("integer")),
                     value=Integer(0)))

        assert res2[0] == Declaration(
            Variable(
                Symbol("a"),
                type=FloatBaseType(String("real")),
                value=Float("0.0", precision=53),
            ))
        assert res2[1] == Declaration(
            Variable(
                Symbol("b"),
                type=FloatBaseType(String("real")),
                value=Float("0.0", precision=53),
            ))

        assert res3[0] == Declaration(
            Variable(Symbol("a"),
                     type=IntBaseType(String("integer")),
                     value=Integer(0)))

        assert res3[1] == Declaration(
            Variable(
                Symbol("b"),
                type=FloatBaseType(String("real")),
                value=Float("0.0", precision=53),
            ))

        assert res3[2] == Declaration(
            Variable(Symbol("c"),
                     type=IntBaseType(String("integer")),
                     value=Integer(0)))

        assert res4[0] == Declaration(
            Variable(Symbol("x"),
                     type=IntBaseType(String("integer")),
                     value=Integer(1)))

        assert res4[1] == Declaration(
            Variable(Symbol("y"),
                     type=IntBaseType(String("integer")),
                     value=Integer(6)))

        assert res4[2] == Declaration(
            Variable(
                Symbol("p"),
                type=FloatBaseType(String("real")),
                value=Float("2.0", precision=53),
            ))

        assert res4[3] == Declaration(
            Variable(
                Symbol("q"),
                type=FloatBaseType(String("real")),
                value=Float("9.67", precision=53),
            ))
예제 #21
0
        def transform_function_decl(self, node):
            """Transformation Function For Function Declaration

            Used to create nodes for function declarations and definitions for
            the respective nodes in the clang AST

            Returns
            =======

            function : Codegen AST node
                - FunctionPrototype node if function body is not present
                - FunctionDefinition node if the function body is present


            """
            token = node.get_tokens()
            c_ret_type = next(token).spelling
            if (c_ret_type == 'void'):
                ret_type = none
            elif (c_ret_type == 'int'):
                ret_type = IntBaseType(String('integer'))
            elif (c_ret_type == 'float'):
                ret_type = FloatBaseType(String('real'))
            else:
                raise NotImplementedError("Variable not yet supported")
            body = []
            param = []
            try:
                children = node.get_children()
                child = next(children)

                # If the node has any children, the first children will be the
                # return type and namespace for the function declaration. These
                # nodes can be ignored.
                while child.kind == cin.CursorKind.NAMESPACE_REF:
                    child = next(children)

                while child.kind == cin.CursorKind.TYPE_REF:
                    child = next(children)

                # Subsequent nodes will be the parameters for the function.
                try:
                    while True:
                        decl = self.transform(child)
                        if (child.kind == cin.CursorKind.PARM_DECL):
                            param.append(decl)
                        elif (child.kind == cin.CursorKind.COMPOUND_STMT):
                            for val in decl:
                                body.append(val)
                        else:
                            body.append(decl)
                        child = next(children)
                except StopIteration:
                    pass
            except StopIteration:
                pass

            if body == []:
                function = FunctionPrototype(return_type=ret_type,
                                             name=node.spelling,
                                             parameters=param)
            else:
                function = FunctionDefinition(return_type=ret_type,
                                              name=node.spelling,
                                              parameters=param,
                                              body=body)
            return function
예제 #22
0
        def transform_var_decl(self, node):
            """Transformation Function for Variable Declaration

            Used to create nodes for variable declarations and assignments with
            values or function call for the respective nodes in the clang AST

            Returns
            =======

            A variable node as Declaration, with the given value or 0 if the
            value is not provided

            Raises
            ======

            NotImplementedError : if called for data types not currently
            implemented

            Notes
            =====

            This function currently only supports basic Integer and Float data
            types

            """
            try:
                children = node.get_children()
                child = next(children)
                #ignoring namespace and type details for the variable
                while child.kind == cin.CursorKind.NAMESPACE_REF:
                    child = next(children)

                while child.kind == cin.CursorKind.TYPE_REF:
                    child = next(children)

                val = self.transform(child)
                # List in case of variable assignment, FunctionCall node in case of a funcion call
                if (child.kind == cin.CursorKind.INTEGER_LITERAL
                        or child.kind == cin.CursorKind.UNEXPOSED_EXPR):
                    if (node.type.kind == cin.TypeKind.INT):
                        type = IntBaseType(String('integer'))
                        value = Integer(val)
                    elif (node.type.kind == cin.TypeKind.FLOAT):
                        type = FloatBaseType(String('real'))
                        value = Float(val)
                    else:
                        raise NotImplementedError()
                    return Variable(node.spelling).as_Declaration(type=type,
                                                                  value=value)
                elif (child.kind == cin.CursorKind.CALL_EXPR):
                    return Variable(node.spelling).as_Declaration(value=val)
                else:
                    raise NotImplementedError()

            except StopIteration:

                if (node.type.kind == cin.TypeKind.INT):
                    type = IntBaseType(String('integer'))
                    value = Integer(0)
                elif (node.type.kind == cin.TypeKind.FLOAT):
                    type = FloatBaseType(String('real'))
                    value = Float(0.0)
                else:
                    raise NotImplementedError()
                return Variable(node.spelling).as_Declaration(type=type,
                                                              value=value)
예제 #23
0
    def test_parameters():
        c_src1 = "void fun1( int a)" + "\n" + "{" + "\n" + "int i;" + "\n" + "}"
        c_src2 = ("int fun2(float x, float y)" + "\n" + "{" + "\n" + "int a;" +
                  "\n" + "return a;" + "\n" + "}")
        c_src3 = ("float fun3(int p, float q, int r)" + "\n" + "{" + "\n" +
                  "float b;" + "\n" + "return b;" + "\n" + "}")

        res1 = SymPyExpression(c_src1, "c").return_expr()
        res2 = SymPyExpression(c_src2, "c").return_expr()
        res3 = SymPyExpression(c_src3, "c").return_expr()

        assert res1[0] == FunctionDefinition(
            NoneToken(),
            name=String("fun1"),
            parameters=(Variable(Symbol("a"),
                                 type=IntBaseType(String("integer")),
                                 value=Integer(0)), ),
            body=CodeBlock(
                Declaration(
                    Variable(
                        Symbol("i"),
                        type=IntBaseType(String("integer")),
                        value=Integer(0),
                    ))),
        )

        assert res2[0] == FunctionDefinition(
            IntBaseType(String("integer")),
            name=String("fun2"),
            parameters=(
                Variable(
                    Symbol("x"),
                    type=FloatBaseType(String("real")),
                    value=Float("0.0", precision=53),
                ),
                Variable(
                    Symbol("y"),
                    type=FloatBaseType(String("real")),
                    value=Float("0.0", precision=53),
                ),
            ),
            body=CodeBlock(
                Declaration(
                    Variable(
                        Symbol("a"),
                        type=IntBaseType(String("integer")),
                        value=Integer(0),
                    )),
                Return("a"),
            ),
        )

        assert res3[0] == FunctionDefinition(
            FloatBaseType(String("real")),
            name=String("fun3"),
            parameters=(
                Variable(Symbol("p"),
                         type=IntBaseType(String("integer")),
                         value=Integer(0)),
                Variable(
                    Symbol("q"),
                    type=FloatBaseType(String("real")),
                    value=Float("0.0", precision=53),
                ),
                Variable(Symbol("r"),
                         type=IntBaseType(String("integer")),
                         value=Integer(0)),
            ),
            body=CodeBlock(
                Declaration(
                    Variable(
                        Symbol("b"),
                        type=FloatBaseType(String("real")),
                        value=Float("0.0", precision=53),
                    )),
                Return("b"),
            ),
        )
예제 #24
0
    def test_function_call():
        c_src1 = "x = fun1(2);"
        c_src2 = "y = fun2(2, 3, 4);"
        c_src3 = "int p, q, r;" + "\n" + "z = fun3(p, q, r);"
        c_src4 = "float x, y;" + "\n" + "int z;" + "\n" + "i = fun4(x, y, z)"
        c_src5 = "a = fun()"

        res1 = SymPyExpression(c_src1, "c").return_expr()
        res2 = SymPyExpression(c_src2, "c").return_expr()
        res3 = SymPyExpression(c_src3, "c").return_expr()
        res4 = SymPyExpression(c_src4, "c").return_expr()
        res5 = SymPyExpression(c_src5, "c").return_expr()

        assert res1[0] == Declaration(
            Variable(Symbol("x"),
                     value=FunctionCall(String("fun1"), function_args=([
                         2,
                     ]))))

        assert res2[0] == Declaration(
            Variable(
                Symbol("y"),
                value=FunctionCall(String("fun2"), function_args=([2, 3, 4])),
            ))

        assert res3[0] == Declaration(
            Variable(Symbol("p"),
                     type=IntBaseType(String("integer")),
                     value=Integer(0)))

        assert res3[1] == Declaration(
            Variable(Symbol("q"),
                     type=IntBaseType(String("integer")),
                     value=Integer(0)))

        assert res3[2] == Declaration(
            Variable(Symbol("r"),
                     type=IntBaseType(String("integer")),
                     value=Integer(0)))

        assert res3[3] == Declaration(
            Variable(
                Symbol("z"),
                value=FunctionCall(
                    String("fun3"),
                    function_args=([Symbol("p"),
                                    Symbol("q"),
                                    Symbol("r")]),
                ),
            ))

        assert res4[0] == Declaration(
            Variable(
                Symbol("x"),
                type=FloatBaseType(String("real")),
                value=Float("0.0", precision=53),
            ))

        assert res4[1] == Declaration(
            Variable(
                Symbol("y"),
                type=FloatBaseType(String("real")),
                value=Float("0.0", precision=53),
            ))

        assert res4[2] == Declaration(
            Variable(Symbol("z"),
                     type=IntBaseType(String("integer")),
                     value=Integer(0)))

        assert res4[3] == Declaration(
            Variable(
                Symbol("i"),
                value=FunctionCall(
                    String("fun4"),
                    function_args=([Symbol("x"),
                                    Symbol("y"),
                                    Symbol("z")]),
                ),
            ))
        assert res5[0] == Declaration(
            Variable(Symbol("a"),
                     value=FunctionCall(String("fun"), function_args=())))
예제 #25
0
        def transform_var_decl(self, node):
            """Transformation Function for Variable Declaration

            Used to create nodes for variable declarations and assignments with
            values or function call for the respective nodes in the clang AST

            Returns
            =======

            A variable node as Declaration, with the given value or 0 if the
            value is not provided

            Raises
            ======

            NotImplementedError : if called for data types not currently
            implemented

            Notes
            =====

            This function currently only supports basic Integer and Float data
            types

            """
            try:
                children = node.get_children()
                child = next(children)
                #ignoring namespace and type details for the variable
                while child.kind == cin.CursorKind.NAMESPACE_REF:
                    child = next(children)

                while child.kind == cin.CursorKind.TYPE_REF:
                    child = next(children)

                val = self.transform(child)
                # List in case of variable assignment,
                # FunctionCall node in case of a function call
                if (child.kind == cin.CursorKind.INTEGER_LITERAL
                    or child.kind == cin.CursorKind.UNEXPOSED_EXPR):
                    if (node.type.kind == cin.TypeKind.INT):
                        type = IntBaseType(String('integer'))
                        # when only one decl_ref_expr is assigned
                        # e.g., int b = a;
                        if isinstance(val, str):
                            value = Symbol(val)
                        # e.g., int b = true;
                        elif isinstance(val, bool):
                            value = Integer(0) if val == False else Integer(1)
                        # when val is integer or character literal
                        # e.g., int b = 1; or int b = 'a';
                        elif isinstance(val, (Integer, int, Float, float)):
                            value = Integer(val)
                        # when val is combination of both of the above
                        # but in total only two nodes on rhs
                        # e.g., int b = a * 1;
                        else:
                            value = val

                    elif (node.type.kind == cin.TypeKind.FLOAT):
                        type = FloatBaseType(String('real'))
                        # e.g., float b = a;
                        if isinstance(val, str):
                            value = Symbol(val)
                        # e.g., float b = true;
                        elif isinstance(val, bool):
                            value = Float(0.0) if val == False else Float(1.0)
                        # e.g., float b = 1.0;
                        elif isinstance(val, (Integer, int, Float, float)):
                            value = Float(val)
                        # e.g., float b = a * 1.0;
                        else:
                            value = val

                    elif (node.type.kind == cin.TypeKind.BOOL):
                        type = Type(String('bool'))
                        # e.g., bool b = a;
                        if isinstance(val, str):
                            value = Symbol(val)
                        # e.g., bool b = 1;
                        elif isinstance(val, (Integer, int, Float, float)):
                            value = sympify(bool(val))
                        # e.g., bool b = a * 1;
                        else:
                            value = val

                    else:
                        raise NotImplementedError("Only bool, int " \
                            "and float are supported")

                elif (child.kind == cin.CursorKind.CALL_EXPR):
                    return Variable(
                        node.spelling
                    ).as_Declaration(
                        value = val
                    )

                # when val is combination of more than two expr and
                # integer(or float)
                elif (child.kind == cin.CursorKind.BINARY_OPERATOR):
                    if (node.type.kind == cin.TypeKind.INT):
                        type = IntBaseType(String('integer'))
                    elif (node.type.kind == cin.TypeKind.FLOAT):
                        type = FloatBaseType(String('real'))
                    elif (node.type.kind == cin.TypeKind.BOOL):
                        type = Type(String('bool'))
                    else:
                        raise NotImplementedError("Only bool, int " \
                            "and float are supported")
                    value = val

                elif (child.kind == cin.CursorKind.CXX_BOOL_LITERAL_EXPR):
                    if (node.type.kind == cin.TypeKind.INT):
                        type = IntBaseType(String('integer'))
                        value = Integer(val)
                    elif (node.type.kind == cin.TypeKind.FLOAT):
                        type = FloatBaseType(String('real'))
                        value = Float(val)
                    elif (node.type.kind == cin.TypeKind.BOOL):
                        type = Type(String('bool'))
                        value = sympify(val)
                    else:
                        raise NotImplementedError("Only bool, int " \
                            "and float are supported")
                else:
                    raise NotImplementedError()

            except StopIteration:

                if (node.type.kind == cin.TypeKind.INT):
                    type = IntBaseType(String('integer'))
                    value = Integer(0)
                elif (node.type.kind == cin.TypeKind.FLOAT):
                    type = FloatBaseType(String('real'))
                    value = Float(0.0)
                elif (node.type.kind == cin.TypeKind.BOOL):
                    type = Type(String('bool'))
                    value = false
                else:
                    raise NotImplementedError("Only bool, int " \
                            "and float are supported")

            return Variable(
                node.spelling
            ).as_Declaration(
                type = type,
                value = value
            )
예제 #26
0
    def test_variable():
        c_src1 = ('int a;' + '\n' + 'int b;' + '\n')
        c_src2 = ('float a;' + '\n' + 'float b;' + '\n')
        c_src3 = ('int a;' + '\n' + 'float b;' + '\n' + 'int c;')
        c_src4 = ('int x = 1, y = 6.78;' + '\n' + 'float p = 2, q = 9.67;')

        res1 = SymPyExpression(c_src1, 'c').return_expr()
        res2 = SymPyExpression(c_src2, 'c').return_expr()
        res3 = SymPyExpression(c_src3, 'c').return_expr()
        res4 = SymPyExpression(c_src4, 'c').return_expr()

        assert res1[0] == Declaration(
            Variable(Symbol('a'),
                     type=IntBaseType(String('integer')),
                     value=Integer(0)))

        assert res1[1] == Declaration(
            Variable(Symbol('b'),
                     type=IntBaseType(String('integer')),
                     value=Integer(0)))

        assert res2[0] == Declaration(
            Variable(Symbol('a'),
                     type=FloatBaseType(String('real')),
                     value=Float('0.0', precision=53)))
        assert res2[1] == Declaration(
            Variable(Symbol('b'),
                     type=FloatBaseType(String('real')),
                     value=Float('0.0', precision=53)))

        assert res3[0] == Declaration(
            Variable(Symbol('a'),
                     type=IntBaseType(String('integer')),
                     value=Integer(0)))

        assert res3[1] == Declaration(
            Variable(Symbol('b'),
                     type=FloatBaseType(String('real')),
                     value=Float('0.0', precision=53)))

        assert res3[2] == Declaration(
            Variable(Symbol('c'),
                     type=IntBaseType(String('integer')),
                     value=Integer(0)))

        assert res4[0] == Declaration(
            Variable(Symbol('x'),
                     type=IntBaseType(String('integer')),
                     value=Integer(1)))

        assert res4[1] == Declaration(
            Variable(Symbol('y'),
                     type=IntBaseType(String('integer')),
                     value=Integer(6)))

        assert res4[2] == Declaration(
            Variable(Symbol('p'),
                     type=FloatBaseType(String('real')),
                     value=Float('2.0', precision=53)))

        assert res4[3] == Declaration(
            Variable(Symbol('q'),
                     type=FloatBaseType(String('real')),
                     value=Float('9.67', precision=53)))
예제 #27
0
    def test_function_call():
        c_src1 = 'x = fun1(2);'
        c_src2 = 'y = fun2(2, 3, 4);'
        c_src3 = ('int p, q, r;' + '\n' + 'z = fun3(p, q, r);')
        c_src4 = ('float x, y;' + '\n' + 'int z;' + '\n' + 'i = fun4(x, y, z)')
        c_src5 = 'a = fun()'

        res1 = SymPyExpression(c_src1, 'c').return_expr()
        res2 = SymPyExpression(c_src2, 'c').return_expr()
        res3 = SymPyExpression(c_src3, 'c').return_expr()
        res4 = SymPyExpression(c_src4, 'c').return_expr()
        res5 = SymPyExpression(c_src5, 'c').return_expr()

        assert res1[0] == Declaration(
            Variable(Symbol('x'),
                     value=FunctionCall(String('fun1'), function_args=([
                         2,
                     ]))))

        assert res2[0] == Declaration(
            Variable(Symbol('y'),
                     value=FunctionCall(String('fun2'),
                                        function_args=([2, 3, 4]))))

        assert res3[0] == Declaration(
            Variable(Symbol('p'),
                     type=IntBaseType(String('integer')),
                     value=Integer(0)))

        assert res3[1] == Declaration(
            Variable(Symbol('q'),
                     type=IntBaseType(String('integer')),
                     value=Integer(0)))

        assert res3[2] == Declaration(
            Variable(Symbol('r'),
                     type=IntBaseType(String('integer')),
                     value=Integer(0)))

        assert res3[3] == Declaration(
            Variable(Symbol('z'),
                     value=FunctionCall(String('fun3'),
                                        function_args=([
                                            Symbol('p'),
                                            Symbol('q'),
                                            Symbol('r')
                                        ]))))

        assert res4[0] == Declaration(
            Variable(Symbol('x'),
                     type=FloatBaseType(String('real')),
                     value=Float('0.0', precision=53)))

        assert res4[1] == Declaration(
            Variable(Symbol('y'),
                     type=FloatBaseType(String('real')),
                     value=Float('0.0', precision=53)))

        assert res4[2] == Declaration(
            Variable(Symbol('z'),
                     type=IntBaseType(String('integer')),
                     value=Integer(0)))

        assert res4[3] == Declaration(
            Variable(Symbol('i'),
                     value=FunctionCall(String('fun4'),
                                        function_args=([
                                            Symbol('x'),
                                            Symbol('y'),
                                            Symbol('z')
                                        ]))))
        assert res5[0] == Declaration(
            Variable(Symbol('a'),
                     value=FunctionCall(String('fun'), function_args=())))