-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
codegen: Object forward declaration tests
- Loading branch information
Showing
2 changed files
with
83 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 80 additions & 33 deletions
113
packages/codegen/test/codegen/object-declaration-forward-declaration.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,84 @@ | ||
obj Person { | ||
mut name: str; | ||
mut age: int; | ||
mut pets: (ref Pet)[]; | ||
} | ||
obj Pet { | ||
mut name: str | ||
mut age: int | ||
mut owner: ref Person | ||
} | ||
|
||
obj Test1 { | ||
t3: Test3 | ||
} | ||
obj Test2 { | ||
a: int | ||
} | ||
obj Test3 { | ||
a: int | ||
} | ||
obj Test4 { | ||
t2: Test2 | ||
} | ||
|
||
obj Test1 { t3: Test3 } | ||
obj Test2 { a: int } | ||
obj Test3 { t4: Test4 } | ||
obj Test4 { t2: Test2 } | ||
main { | ||
mut person := Person{name: "Daniel", age: 28} | ||
dog := Pet{name: "Eric", age: 4, owner: ref person} | ||
cat := Pet{name: "Sam", age: 8, owner: ref person} | ||
|
||
person.pets.push(ref dog) | ||
person.pets.push(ref cat) | ||
|
||
t1 := Test1{t3: Test3{a: 2}} | ||
t2 := Test4{t2: Test2{a: 1}} | ||
t := Test1{t3: Test3{t4: Test4{t2: Test2{a: 1}}}} | ||
} | ||
===== code ===== | ||
#include <d4/number.h> | ||
#include <d4/object.h> | ||
#include <d4/string.h> | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
D4_OBJECT_FORWARD_DECLARE(Test2) | ||
D4_OBJECT_FORWARD_DECLARE(Test4) | ||
D4_OBJECT_FORWARD_DECLARE(Test3) | ||
D4_OBJECT_FORWARD_DECLARE(Test1) | ||
D4_OBJECT_DECLARE(Test2, { | ||
int32_t a; | ||
}, const int32_t a) | ||
D4_OBJECT_DECLARE(Test4, { | ||
d4_obj_Test2_t t2; | ||
}, const d4_obj_Test2_t t2) | ||
D4_OBJECT_DECLARE(Test3, { | ||
d4_obj_Test4_t t4; | ||
}, const d4_obj_Test4_t t4) | ||
D4_OBJECT_DECLARE(Test1, { | ||
d4_obj_Test3_t t3; | ||
}, const d4_obj_Test3_t t3) | ||
D4_OBJECT_DEFINE(Test2, Test2, { | ||
self.a = a; | ||
}, { | ||
result.a = self.a; | ||
}, { | ||
return self.a == rhs.a; | ||
}, { | ||
(void) self; | ||
}, { | ||
result = d4_obj_str_append(result, d4_str_alloc(L"a"), d4_int_str(self.a)); | ||
}, const int32_t a) | ||
D4_OBJECT_DEFINE(Test4, Test4, { | ||
self.t2 = d4_obj_Test2_copy(t2); | ||
}, { | ||
result.t2 = d4_obj_Test2_copy(self.t2); | ||
}, { | ||
return d4_obj_Test2_eq(self.t2, rhs.t2); | ||
}, { | ||
d4_obj_Test2_free(self.t2); | ||
}, { | ||
result = d4_obj_str_append(result, d4_str_alloc(L"t2"), d4_obj_Test2_str(self.t2)); | ||
}, const d4_obj_Test2_t t2) | ||
D4_OBJECT_DEFINE(Test3, Test3, { | ||
self.t4 = d4_obj_Test4_copy(t4); | ||
}, { | ||
result.t4 = d4_obj_Test4_copy(self.t4); | ||
}, { | ||
return d4_obj_Test4_eq(self.t4, rhs.t4); | ||
}, { | ||
d4_obj_Test4_free(self.t4); | ||
}, { | ||
result = d4_obj_str_append(result, d4_str_alloc(L"t4"), d4_obj_Test4_str(self.t4)); | ||
}, const d4_obj_Test4_t t4) | ||
D4_OBJECT_DEFINE(Test1, Test1, { | ||
self.t3 = d4_obj_Test3_copy(t3); | ||
}, { | ||
result.t3 = d4_obj_Test3_copy(self.t3); | ||
}, { | ||
return d4_obj_Test3_eq(self.t3, rhs.t3); | ||
}, { | ||
d4_obj_Test3_free(self.t3); | ||
}, { | ||
result = d4_obj_str_append(result, d4_str_alloc(L"t3"), d4_obj_Test3_str(self.t3)); | ||
}, const d4_obj_Test3_t t3) | ||
int main (void) { | ||
d4_obj_Test2_t __THE_1 = d4_obj_Test2_alloc(0); | ||
d4_obj_Test4_t __THE_2 = d4_obj_Test4_alloc(d4_obj_Test2_alloc(0)); | ||
d4_obj_Test3_t __THE_3 = d4_obj_Test3_alloc(d4_obj_Test4_alloc(d4_obj_Test2_alloc(0))); | ||
d4_obj_Test1_t t_0 = d4_obj_Test1_alloc(__THE_3 = d4_obj_Test3_alloc(__THE_2 = d4_obj_Test4_alloc(__THE_1 = d4_obj_Test2_alloc(1)))); | ||
d4_obj_Test1_free(t_0); | ||
d4_obj_Test3_free(__THE_3); | ||
d4_obj_Test4_free(__THE_2); | ||
d4_obj_Test2_free(__THE_1); | ||
} | ||
===== output ===== |