def test_modulus(self): self.assertEqual( self.parse(self.function_wrap("1 % 2;")), self.function_wrap_node( BinaryOperation(operator="%", left=Int32(value=1), right=Int32(value=2))))
def test_basic_lte(self): self.assertEqual( self.parse(self.function_wrap('2 <= 3;')), self.function_wrap_node( BinaryOperation(operator="<=", left=Int32(value=2), right=Int32(value=3))))
def test_division(self): self.assertEqual( self.parse(self.function_wrap("1 / 2;")), self.function_wrap_node( BinaryOperation(operator="/", left=Int32(value=1), right=Int32(value=2))))
def test_multiplication(self): self.assertEqual( self.parse(self.function_wrap("1 * 2;")), self.function_wrap_node( BinaryOperation(operator="*", left=Int32(value=1), right=Int32(value=2))))
def test_basic_or(self): self.assertEqual( self.parse(self.function_wrap('1 || 0;')), self.function_wrap_node( BinaryOperation(operator="||", left=Int32(value=1), right=Int32(value=0))))
def test_array_variable_declaration(self): self.assertEqual( self.parse(self.function_wrap('int foo[10];')), self.function_wrap_node( VariableDeclaration(name="foo", vtype=Type(base="array", arraylength=10, reference=Type(base="int")), value=None))) var = Type(base="array", arraylength=Int32(value=10), reference=Type(base="int")) self.assertEqual(var.base_type, "pointer") self.assertEqual(var.length, Int32(value=10))
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_for_loop(self): self.assertEqual( self.parse( self.function_wrap("for ( i = 0; i < 10; i++ ) { putc(i); }")), self.function_wrap_node( For( initial=Assignment(left=Variable(name="i"), right=Int32(value=0)), condition=BinaryOperation(operator="<", left=Variable(name="i"), right=Int32(value=10)), increment=PostOperation(operator="++", variable=Variable(name="i")), body=Block([Call(name="putc", args=[Variable(name="i")])]), )))
def test_include_string(self): self.skipTest("Not sure how to handle this one yet.") self.assertEqual( self.parse('#include "foo.h"\n' + self.function_wrap("return 1;"), ), self.function_wrap_node(ReturnStatement(value=Int32(value=1))), )
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 test_variable_declaration_with_assignment(self): self.assertEqual( self.parse(self.function_wrap("int i = 0;")), self.function_wrap_node( VariableDeclaration(name="i", vtype=Type(base="int"), value=Int32(value=0))))
def const(self, p): if p[0].gettokentype() == "INTEGER_LITERAL": return Int32(int(p[0].getstr())) elif p[0].gettokentype() == "FLOAT_LITERAL": return Double(float(p[0].getstr())) elif p[0].gettokentype() == "CHAR_LITERAL": return Char(p[0].getstr().strip("'")) raise AssertionError("Bad token type in const")
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_if_loop(self): self.assertEqual( self.parse(self.function_wrap("if ( i > 10 ) { i++; } ")), self.function_wrap_node( If( condition=BinaryOperation(operator=">", left=Variable(name="i"), right=Int32(value=10)), body=Block([ PostOperation(operator="++", variable=Variable(name="i")) ]), )))
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_braceless_while_loop(self): self.assertEqual( self.parse( self.function_wrap(""" while ( i < 10 ) i++; """)), self.function_wrap_node( For( condition=BinaryOperation(operator="<", left=Variable(name="i"), right=Int32(value=10)), body=Block([ PostOperation(operator="++", variable=Variable(name="i")) ]), )))
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 declare_assign_int(self, p): return VariableDeclaration(name=p[1].getstr(), vtype=p[0], value=Int32(int(p[3].getstr())))
def test_assignment(self): self.assertEqual( self.parse(self.function_wrap("i = 0;")), self.function_wrap_node( Assignment(left=Variable(name="i"), right=Int32(value=0))))
def test_array_dereference(self): self.assertEqual( self.parse(self.function_wrap("array[4];")), self.function_wrap_node( ArrayDereference(array=Variable(name="array"), index=Int32(value=4))))