def test_main_with_multiple_parameters(self): self.assertEqual( self.parse( "int main(int argc, char **argv, char **env) { return 0; }"), Program(units=[ Function(return_type=Type(base='int'), name="main", params=[ VariableDeclaration(name="argc", vtype=Type(base="int")), VariableDeclaration( name="argv", vtype=Type(base="pointer", reference=Type(base="pointer", reference=Type( base="char")))), VariableDeclaration( name="env", vtype=Type(base="pointer", reference=Type(base="pointer", reference=Type( base="char")))), ], body=Block([ReturnStatement(value=Int32(value=0))])) ]))
def function_wrap_node(self, node): return Program([ Function(return_type=Type(base="int"), name="main", params=[], body=Block([node])) ])
def function_with_args(self, p): return Function(return_type=p[0], name=p[1].getstr(), params=[ VariableDeclaration(name=None, vtype=x, value=None) for x in p[3].get_items() ], prototype=True)
def test_main_with_no_parameters(self): self.assertEqual( self.parse("int main(void) { return 0; }"), Program(units=[ Function(return_type=Type(base='int'), name="main", params=[], body=Block([ReturnStatement(value=Int32(value=0))])) ]))
def test_puts_function(self): self.assertEqual( self.parse(""" int puts(const char * string) { int i = 0; while (string[i] != NULL) { putc(string[i++]); } putc('\\n'); return i + 1; } """), Program([ Function( return_type=Type(base='int'), name="puts", params=[ VariableDeclaration(name="string", vtype=Type( base="pointer", const=True, reference=Type(base="char"))) ], body=Block([ VariableDeclaration(name="i", vtype=Type(base="int"), value=Int32(value=0)), For(condition=BinaryOperation( operator="!=", left=ArrayDereference( array=Variable(name="string"), index=Variable(name="i")), right=Null()), body=Block([ Call(name="putc", args=[ ArrayDereference( array=Variable("string"), index=PostOperation( operator="++", variable=Variable(name="i"))) ]) ])), Call(name="putc", args=[Char('\n')]), ReturnStatement( value=BinaryOperation(operator="+", left=Variable(name="i"), right=Int32(value=1))) ])) ]))
def test_auxiliary_function(self): self.assertEqual( self.parse(""" int foo(void) { return 12; } int main(void) { return foo(); } """), Program([ Function(return_type=Type(base='int'), name="foo", params=[], body=Block( [ReturnStatement(value=Int32(value=12))], )), Function(return_type=Type(base='int'), name="main", params=[], body=Block([ ReturnStatement(value=Call(name="foo", args=[]), ), ])), ]))
def test_function_arguments(self): self.assertEqual( self.parse("int puts(const char* string) { return 0; }"), Program([ Function(return_type=Type(base='int'), name="puts", params=[ VariableDeclaration( name="string", vtype=Type(base="pointer", const=True, reference=Type(base="char"))) ], body=Block([ReturnStatement(value=Int32(value=0))])) ]))
def test_main_function(self): self.assertEqual( self.parse(""" int main(void) { return puts("Hello, world!"); } """), Program([ Function(return_type=Type(base='int'), name="main", params=[], body=Block([ ReturnStatement(value=Call( name="puts", args=[String("Hello, world!")])) ])) ]))
def test_prototype_with_named_arguments(self): self.assertEqual( self.parse("int foo(int i, long l, double d, char *cp);"), Program([ Function( return_type=Type(base='int'), name="foo", params=[ VariableDeclaration(name="i", vtype=Type(base="int")), VariableDeclaration(name="l", vtype=Type(base="long")), VariableDeclaration(name="d", vtype=Type(base="double")), VariableDeclaration(name="cp", vtype=Type( base="pointer", reference=Type(base="char"))), ], prototype=True, ) ]))
def function_with_args(self, p): return Function(return_type=p[0], name=p[1].getstr(), params=p[3].get_items(), prototype=True)
def function_void_param(self, p): return Function(return_type=p[0], name=p[1].getstr(), params=[], prototype=True)
def function_void_param(self, p): return Function(return_type=p[0], name=p[1].getstr(), params=[], body=p[5])