def _parse_value(self, value): if value == 'y': return c_ast.CNumber(1) elif len(value) >= 2 and value[0] == '"' and value[-1] == '"': return c_ast.CLiteral(value[1:-1]) else: return c_ast.CNumber(int(value, base=0)) # Throws ValueError.
def test_parse_alignof_with_double_underscores_of_array(self): source = '__alignof__(unsigned int[42])' actual = self.parser.parse(source) expected = c_ast.CFunctionCall( function_name='__alignof__', arguments=[c_ast.CLiteral('unsigned int[42]')], ) self.assertASTEqual(actual, expected)
def test_parse_alignof_with_double_underscores(self): source = '__alignof__(struct s*)' actual = self.parser.parse(source) expected = c_ast.CFunctionCall( function_name='__alignof__', arguments=[c_ast.CLiteral('struct s*')], ) self.assertASTEqual(actual, expected)
def test_parse_sizeof(self): source = 'sizeof(struct s*)' actual = self.parser.parse(source) expected = c_ast.CFunctionCall( function_name='sizeof', arguments=[c_ast.CLiteral('struct s*')], ) self.assertASTEqual(actual, expected)
def test_visit_function_call(self): function_call = c_ast.CFunctionCall( function_name='f1', arguments=[ c_ast.CNumber(24), c_ast.CLiteral('literal'), ], ) actual = self.expression_evaluator.evaluate(function_call) self.assertEqual(actual, 33) self.assertEqual(self.x, [24]) self.assertEqual(self.y, ['literal'])
def test_parse_sizeof_and_binary_plus_operators_and_additional_parentheses( self): source = """ ( sizeof(struct ymmh_struct) + sizeof(struct lwp_struct) + sizeof(struct mpx_struct) ) """ actual = self.parser.parse(source) expected = c_ast.CNestedExpression( opener='(', content=c_ast.CFunctionCall( function_name='+', arguments=[ c_ast.CFunctionCall( function_name='+', arguments=[ c_ast.CFunctionCall( function_name='sizeof', arguments=[ c_ast.CLiteral('struct ymmh_struct') ], ), c_ast.CFunctionCall( function_name='sizeof', arguments=[ c_ast.CLiteral('struct lwp_struct') ], ), ], ), c_ast.CFunctionCall( function_name='sizeof', arguments=[c_ast.CLiteral('struct mpx_struct')], ), ], ), closer=')', ) self.assertASTEqual(actual, expected)
def XXXX_create_cast_expression(self, tok): if tok.typeof_arg: type_expression = self.type_manager.get_type_of( tok.typeof_arg.first) else: type_expression = tok.simple_type # Check that casting makes sense. target = self.type_manager.get_type_of(type_expression) if not target: raise pyparsing.ParseException("%s is not a type" % target) return c_ast.CFunctionCall( function_name='()', arguments=[ c_ast.CLiteral(target), tok.expression, ], )
def _create_sizeof_type(self, sizeof, type_name): return c_ast.CFunctionCall( function_name=sizeof, arguments=[c_ast.CLiteral(type_name)], )
def test_visit_literal(self): literal = c_ast.CLiteral('value') actual = self.expression_evaluator.evaluate(literal) self.assertEqual(actual, 'value')
def test_parse_with_string_flag(self): config = 'FLAG="33"' actual = self.parser.parse(config) expected = {'FLAG': c_ast.CLiteral('33')} self.assertEqual(actual, expected)