def test_it_generates(self): inp = [ cgen.FuncCall( "lol", cgen.Val("d", type_=cgen.CTypes.char), cgen.Val("some string", type_=cgen.CTypes.ptr(cgen.CTypes.char))) ] self.check_gen([inp], ("lol('d', \"some string\");", ))
def test_array_elem_by_index(self): inp = [ cgen.Assignment( cgen.ArrayElemByIndex("a", cgen.Val("0", type_=cgen.CTypes.int)), cgen.Val("1", type_=cgen.CTypes.int)) ] self.check_gen([inp], ("a[0] = 1;", ))
def test_auto_size(self): inp = [ cgen.Decl("a", type_=cgen.CTypes.array(cgen.CTypes.int, size="auto"), expr=cgen.Val((cgen.Val("0", type_=cgen.CTypes.int), cgen.Val("1", cgen.CTypes.int)), cgen.CTypes.array(cgen.CTypes.int))) ] self.check_gen([inp], ("int a[2] = {0, 1};", ))
def test_of_addition_expr(self): expr = cgen.Cast(cgen.Expr( cgen.COps.plus, cgen.Cast(cgen.Val("1", type_=cgen.CTypes.int), to=cgen.CTypes.uint_fast8), cgen.Cast(cgen.Val("2", type_=cgen.CTypes.int), to=cgen.CTypes.uint_fast8)), to=cgen.CTypes.size) expected = ("#include <stdint.h>", "(size_t)((uint_fast8_t)(1) + (uint_fast8_t)(2));") self.check_gen([[expr]], expected)
def test_it_generates(self): inp = [ cgen.Expr( cgen.COps.plus, cgen.Val("1", type_=cgen.CTypes.int), cgen.Expr( cgen.COps.star, cgen.Val("3", type_=cgen.CTypes.int), cgen.Expr( cgen.COps.minus, cgen.Val("5", type_=cgen.CTypes.int), cgen.Expr(cgen.COps.slash, cgen.Val("8", type_=cgen.CTypes.int), cgen.Val("4", type_=cgen.CTypes.int))))) ] self.check_gen([inp], ("1 + 3 * 5 - 8 / 4;", ))
def test_use_in_simple_function(self): inp = [ cgen.Func("lol", rettype=cgen.CTypes.ptr(cgen.StructType("MyStruct")), args=(), body=(cgen.Return( cgen.FuncCall( "initMyStruct", cgen.Val("23", type_=cgen.CTypes.int_fast8))), )) ] expected = ("#include <stdint.h>", "struct MyStruct* lol() {", "return initMyStruct(23);", "}") self.check_gen([inp], expected)
def test_use_in_large_example(self): struct_decl = [ cgen.Struct("MyStruct", (cgen.Decl("data", type_=cgen.CTypes.int_fast8), )) ] init_my_struct = [ cgen.Func("initMyStruct", rettype=cgen.CTypes.ptr(cgen.StructType("MyStruct")), args=(cgen.Decl("data", type_=cgen.CTypes.int_fast8), ), body=(cgen.Decl( "self", type_=cgen.CTypes.ptr(cgen.StructType("MyStruct")), expr=malloc_func( cgen.SizeOf(cgen.StructType("MyStruct")))), cgen.Assignment(name=cgen.StructElem( cgen.CTypes.ptr(cgen.Var("self")), cgen.Var("data")), expr=cgen.Var("data")), cgen.Return(cgen.Var("self")))) ] lol = [ cgen.Func("lol", rettype=cgen.CTypes.ptr(cgen.StructType("MyStruct")), args=(), body=(cgen.Return( cgen.FuncCall( "initMyStruct", cgen.Val("23", type_=cgen.CTypes.int_fast8))), )) ] expected = ("#include <stdint.h>", "#include <stdlib.h>", "struct MyStruct {", "int_fast8_t data;", "};", "struct MyStruct* initMyStruct(int_fast8_t data) {", "struct MyStruct* self = malloc(sizeof(struct MyStruct));", "self->data = data;", "return self;", "}", "struct MyStruct* lol() {", "return initMyStruct(23);", "}") self.check_gen([struct_decl, init_my_struct, lol], expected)
def make_val(self, s): return cgen.Val(s, type_=cgen.CTypes.ptr(cgen.CTypes.char))