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_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_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_while_loop(self): self.assertEqual( self.parse( self.function_wrap( "while (string[i] != NULL) { putc(string[i++]); }")), self.function_wrap_node( For(condition=BinaryOperation( operator="!=", left=ArrayDereference(array=Variable(name="string"), index=Variable("i")), right=Null()), body=Block([ Call(name="putc", args=[ ArrayDereference( array=Variable("string"), index=PostOperation( operator="++", variable=Variable(name="i"))) ]) ]))))
def primary_expression(self, p): if isinstance(p[0], Node): # const return p[0] elif p[0].gettokentype() == "IDENTIFIER": return Variable(name=p[0].getstr()) elif p[0].gettokentype() == "STRING_LITERAL": vals = [] for v in p[0].getstr().strip('"'): vals.append(Char(value=v)) vals.append(Char(value=chr(0))) return Array(value=vals) else: return p[1]
def array_variable(self, p): return Variable(name=p[0].getstr())
def assign(self, p): return Assignment(left=Variable(p[0].getstr()), right=p[2])
def test_function_call_with_arguments(self): self.assertEqual( self.parse(self.function_wrap("putc(string);")), self.function_wrap_node( Call(name="putc", args=[Variable(name="string")])))
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))))
def test_floating_point_assignment(self): self.assertEqual( self.parse(self.function_wrap("i = 0.0;")), self.function_wrap_node( Assignment(left=Variable(name="i"), right=Double(value=0.0))))
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_predecrement(self): self.assertEqual( self.parse(self.function_wrap("--i;")), self.function_wrap_node( PreOperation(operator="--", variable=Variable(name="i"))))
def test_postincrement(self): self.assertEqual( self.parse(self.function_wrap("i++;")), self.function_wrap_node( PostOperation(operator="++", variable=Variable(name="i"))))