def transform_call_expr(self, node): """Transformation function for a call expression Used to create function call nodes for the function calls present in the C code Returns ======= FunctionCall : Codegen AST Node FunctionCall node with parameters if any parameters are present """ param = [] children = node.get_children() child = next(children) while child.kind == cin.CursorKind.NAMESPACE_REF: child = next(children) while child.kind == cin.CursorKind.TYPE_REF: child = next(children) first_child = self.transform(child) try: for child in children: arg = self.transform(child) if (child.kind == cin.CursorKind.INTEGER_LITERAL): param.append(Integer(arg)) elif (child.kind == cin.CursorKind.FLOATING_LITERAL): param.append(Float(arg)) else: param.append(arg) return FunctionCall(first_child, param) except StopIteration: return FunctionCall(first_child)
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 and type of the variable Raises ====== ValueError if multiple children encountered in the parameter node """ if node.type.kind in self._data_types["int"]: type = self._data_types["int"][node.type.kind] elif node.type.kind in self._data_types["float"]: type = self._data_types["float"][node.type.kind] elif node.type.kind in self._data_types["bool"]: type = self._data_types["bool"][node.type.kind] else: raise NotImplementedError("Only bool, int " "and float are supported") 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. lit = self.transform(child) if node.type.kind in self._data_types["int"]: val = Integer(lit) elif node.type.kind in self._data_types["float"]: val = Float(lit) elif node.type.kind in self._data_types["bool"]: val = sympify(bool(lit)) else: raise NotImplementedError("Only bool, int " "and float are supported") param = Variable( node.spelling ).as_Declaration( type = type, value = val ) except StopIteration: param = Variable( node.spelling ).as_Declaration( type = type ) try: self.transform(next(children)) raise ValueError("Can't handle multiple children on parameter") except StopIteration: pass return param
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 initial value if given Raises ====== NotImplementedError : if called for data types not currently implemented Notes ===== The function currently supports following data types: Boolean: bool, _Bool Integer: 8-bit: signed char and unsigned char 16-bit: short, short int, signed short, signed short int, unsigned short, unsigned short int 32-bit: int, signed int, unsigned int 64-bit: long, long int, signed long, signed long int, unsigned long, unsigned long int Floating point: Single Precision: float Double Precision: double Extended Precision: long double """ if node.type.kind in self._data_types["int"]: type = self._data_types["int"][node.type.kind] elif node.type.kind in self._data_types["float"]: type = self._data_types["float"][node.type.kind] elif node.type.kind in self._data_types["bool"]: type = self._data_types["bool"][node.type.kind] else: raise NotImplementedError("Only bool, int " "and float are supported") 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) supported_rhs = [ cin.CursorKind.INTEGER_LITERAL, cin.CursorKind.FLOATING_LITERAL, cin.CursorKind.UNEXPOSED_EXPR, cin.CursorKind.BINARY_OPERATOR, cin.CursorKind.PAREN_EXPR, cin.CursorKind.UNARY_OPERATOR, cin.CursorKind.CXX_BOOL_LITERAL_EXPR ] if child.kind in supported_rhs: if isinstance(val, str): value = Symbol(val) elif isinstance(val, bool): if node.type.kind in self._data_types["int"]: value = Integer(0) if val == False else Integer(1) elif node.type.kind in self._data_types["float"]: value = Float(0.0) if val == False else Float(1.0) elif node.type.kind in self._data_types["bool"]: value = sympify(val) elif isinstance(val, (Integer, int, Float, float)): if node.type.kind in self._data_types["int"]: value = Integer(val) elif node.type.kind in self._data_types["float"]: value = Float(val) elif node.type.kind in self._data_types["bool"]: value = sympify(bool(val)) else: value = val 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("Given " "variable declaration \"{}\" " "is not possible to parse yet!" .format(" ".join( t.spelling for t in node.get_tokens() ) )) except StopIteration: return Variable( node.spelling ).as_Declaration( type = type )
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)
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. lit = self.transform(child) if (node.type.kind == cin.TypeKind.INT): val = Integer(lit) elif (node.type.kind == cin.TypeKind.FLOAT): val = Float(lit) param = Variable( node.spelling ).as_Declaration( type = type, value = val ) 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
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=())))
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 )
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=())))
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)))
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"), ), )
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), ))
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') ) )
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=() )
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) ) )