// BEGIN mycpp output #include "gc_heap.h" using gc_heap::Alloc; using gc_heap::kZeroMask; using gc_heap::StackRoots; #include "my_runtime.h" #include "mylib2.h" using gc_heap::NewStr; using gc_heap::NewList; using gc_heap::NewDict; GLOBAL_STR(str0, "foo"); GLOBAL_STR(str1, "spam"); GLOBAL_STR(str2, "eggs"); GLOBAL_STR(str3, "local_list = %d"); GLOBAL_STR(str4, "len(intlist) = %d"); GLOBAL_STR(str5, "i = %d"); GLOBAL_STR(str6, "1? %d"); GLOBAL_STR(str7, "42? %d"); GLOBAL_STR(str8, "len() after del = %d"); GLOBAL_STR(str9, "a"); GLOBAL_STR(str10, "b"); GLOBAL_STR(str11, "len(strlist) = %d"); GLOBAL_STR(str12, "s = %s"); GLOBAL_STR(str13, "a? %d"); GLOBAL_STR(str14, "a"); GLOBAL_STR(str15, "foo? %d"); GLOBAL_STR(str16, "foo"); GLOBAL_STR(str17, "len(strlist) = %d"); GLOBAL_STR(str18, "len(blank) = %d"); GLOBAL_STR(str19, "hello"); GLOBAL_STR(str20, "myint = %d"); GLOBAL_STR(str21, "mystr = %s"); GLOBAL_STR(str22, "yes"); GLOBAL_STR(str23, "no"); GLOBAL_STR(str24, "yes"); GLOBAL_STR(str25, "no"); GLOBAL_STR(str26, "foo"); GLOBAL_STR(str27, "foo"); GLOBAL_STR(str28, "bar"); GLOBAL_STR(str29, "yes"); GLOBAL_STR(str30, "no"); GLOBAL_STR(str31, "glist_int = %d"); GLOBAL_STR(str32, "glist_str = %d"); GLOBAL_STR(str33, "foo"); GLOBAL_STR(str34, ""); GLOBAL_STR(str35, ""); GLOBAL_STR(str36, "foo"); GLOBAL_STR(str37, "Appended %d items to 2 lists"); namespace containers { // forward declare class Point; } // forward declare namespace containers namespace containers { // declare extern Str* gstr; extern List* glist_int; extern List* glist_str; void ListDemo(); class Point : public gc_heap::Obj { public: Point(int x, int y); int x; int y; DISALLOW_COPY_AND_ASSIGN(Point) }; void TupleDemo(); void DictDemo(); void run_tests(); void run_benchmarks(); inline Str* fmt0(int a0) { gBuf.reset(); gBuf.write_const("local_list = ", 13); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt1(int a0) { gBuf.reset(); gBuf.write_const("len(intlist) = ", 15); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt2(int a0) { gBuf.reset(); gBuf.write_const("i = ", 4); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt3(bool a0) { gBuf.reset(); gBuf.write_const("1? ", 3); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt4(bool a0) { gBuf.reset(); gBuf.write_const("42? ", 4); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt5(int a0) { gBuf.reset(); gBuf.write_const("len() after del = ", 18); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt6(int a0) { gBuf.reset(); gBuf.write_const("len(strlist) = ", 15); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt7(Str* a0) { gBuf.reset(); gBuf.write_const("s = ", 4); gBuf.format_s(a0); return gBuf.getvalue(); } inline Str* fmt8(bool a0) { gBuf.reset(); gBuf.write_const("a? ", 3); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt9(bool a0) { gBuf.reset(); gBuf.write_const("foo? ", 5); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt10(int a0) { gBuf.reset(); gBuf.write_const("len(strlist) = ", 15); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt11(int a0) { gBuf.reset(); gBuf.write_const("len(blank) = ", 13); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt12(int a0) { gBuf.reset(); gBuf.write_const("myint = ", 8); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt13(Str* a0) { gBuf.reset(); gBuf.write_const("mystr = ", 8); gBuf.format_s(a0); return gBuf.getvalue(); } inline Str* fmt14(int a0) { gBuf.reset(); gBuf.write_const("glist_int = ", 12); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt15(int a0) { gBuf.reset(); gBuf.write_const("glist_str = ", 12); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt16(int a0) { gBuf.reset(); gBuf.write_const("Appended ", 9); gBuf.format_d(a0); gBuf.write_const(" items to 2 lists", 17); return gBuf.getvalue(); } } // declare namespace containers namespace containers { // define Str* gstr = str0; GLOBAL_LIST(int, 2, glist_int, {1 COMMA 2}); GLOBAL_LIST(Str*, 2, glist_str, {str1 COMMA str2}); void ListDemo() { List* intlist = nullptr; List* local_list = nullptr; List* strlist = nullptr; Str* x = nullptr; int n; Str* no_str = nullptr; List* blank = nullptr; StackRoots _roots({&intlist, &local_list, &strlist, &x, &no_str, &blank}); intlist = Alloc>(); intlist->append(1); intlist->append(2); intlist->append(3); local_list = NewList(std::initializer_list{1, 2}); println_stderr(fmt0(len(local_list))); intlist->set(1, 42); println_stderr(fmt1(len(intlist))); for (ListIter it(intlist); !it.Done(); it.Next()) { int i = it.Value(); StackRoots _for({&i }); println_stderr(fmt2(i)); } println_stderr(fmt3(list_contains(intlist, 1))); println_stderr(fmt4(list_contains(intlist, 42))); intlist->clear(); println_stderr(fmt5(len(intlist))); strlist = Alloc>(); strlist->append(str9); strlist->append(str10); println_stderr(fmt6(len(strlist))); for (ListIter it(strlist); !it.Done(); it.Next()) { Str* s = it.Value(); StackRoots _for({&s }); println_stderr(fmt7(s)); } println_stderr(fmt8(list_contains(strlist, str14))); println_stderr(fmt9(list_contains(strlist, str16))); x = strlist->pop(); println_stderr(fmt10(len(strlist))); n = 3; no_str = nullptr; blank = list_repeat(no_str, n); println_stderr(fmt11(len(blank))); } Point::Point(int x, int y) : gc_heap::Obj(Tag::FixedSize, kZeroMask, sizeof(Point)) { this->x = x; this->y = y; } void TupleDemo() { Tuple2* t2 = nullptr; int myint; Str* mystr = nullptr; int x; containers::Point* p = nullptr; Str* s = nullptr; StackRoots _roots({&t2, &mystr, &p, &s}); t2 = (Alloc>(3, str19)); Tuple2* tup0 = t2; myint = tup0->at0(); mystr = tup0->at1(); println_stderr(fmt12(myint)); println_stderr(fmt13(mystr)); x = 3; if ((x == 3 || x == 4 || x == 5)) { print(str22); } else { print(str23); } p = Alloc(3, 4); if ((p->x == 3 || p->x == 4 || p->x == 5)) { print(str24); } else { print(str25); } s = str26; if ((str_equals(s, str27) || str_equals(s, str28))) { print(str29); } else { print(str30); } println_stderr(fmt14(len(glist_int))); println_stderr(fmt15(len(glist_str))); } void DictDemo() { Dict* d = nullptr; StackRoots _roots({&d}); d = Alloc>(); d->set(str33, 42); } void run_tests() { ListDemo(); println_stderr(str34); TupleDemo(); println_stderr(str35); DictDemo(); } void run_benchmarks() { int n; int i; List* intlist = nullptr; List* strlist = nullptr; StackRoots _roots({&intlist, &strlist}); n = 1000000; i = 0; intlist = Alloc>(); strlist = Alloc>(); while (i < n) { intlist->append(i); strlist->append(str36); i += 1; } println_stderr(fmt16(n)); } } // define namespace containers