OILS / asdl / gen_cpp_test.cc View on Github | oilshell.org

324 lines, 203 significant
1#include <stdarg.h> // va_list, etc.
2#include <stdio.h>
3
4#include "_gen/asdl/examples/shared_variant.asdl.h"
5#include "_gen/asdl/examples/typed_arith.asdl.h"
6#include "_gen/asdl/examples/typed_demo.asdl.h" // has simple Sum, etc
7#include "mycpp/runtime.h"
8#include "prebuilt/asdl/runtime.mycpp.h"
9#include "vendor/greatest.h"
10
11using typed_arith_asdl::pipeline;
12
13using typed_arith_asdl::arith_expr; // variant type namespace
14using typed_arith_asdl::arith_expr_e; // variant tag type
15using typed_arith_asdl::arith_expr_t; // sum type
16
17using typed_arith_asdl::arith_expr__Big;
18using typed_arith_asdl::arith_expr__Const;
19using typed_arith_asdl::arith_expr__FuncCall;
20using typed_arith_asdl::arith_expr__Unary;
21using typed_arith_asdl::arith_expr__Var;
22using typed_arith_asdl::CompoundWord;
23
24using typed_demo_asdl::bool_expr__Binary;
25using typed_demo_asdl::bool_expr__LogicalBinary;
26using typed_demo_asdl::op_array;
27using typed_demo_asdl::op_id_e;
28
29using hnode_asdl::hnode__Leaf;
30using hnode_asdl::hnode_e;
31
32void PrintTag(arith_expr_t* a) {
33 switch (a->tag()) {
34 case arith_expr_e::Const:
35 log("Const");
36 break;
37 case arith_expr_e::Var:
38 log("Var");
39 break;
40 default:
41 log("OTHER");
42 }
43 log("");
44}
45
46TEST misc_test() {
47 auto c = Alloc<arith_expr::Const>(42);
48 log("sizeof *c = %d", sizeof *c); // 16 bytes
49
50 ASSERT_EQ_FMT(42, c->i, "%d");
51 log("c->tag = %d", c->tag());
52 PrintTag(c);
53
54 auto v = Alloc<arith_expr__Var>(StrFromC("foo"));
55 log("sizeof *v = %d", sizeof *v); // 24 bytes
56
57 ASSERT(str_equals(StrFromC("foo"), v->name));
58 log("v->tag = %d", v->tag());
59 PrintTag(v);
60
61 auto u = Alloc<arith_expr__Unary>(StrFromC("-"), v);
62 log("u->op = %s", u->op->data_);
63
64 auto v1 = Alloc<arith_expr__Var>(StrFromC("v1"));
65 auto v2 = Alloc<arith_expr__Var>(StrFromC("v2"));
66 auto args = NewList<arith_expr_t*>({v1, v2});
67
68 auto f = Alloc<arith_expr__FuncCall>(StrFromC("f"), args);
69 log("f->name = %s", f->name->data_);
70
71 auto p = Alloc<pipeline>(true);
72 log("p->negated = %d", p->negated);
73
74#if 0
75 if (t->tag() == hnode_e::Leaf) {
76 hnode__Leaf* t2 = static_cast<hnode__Leaf*>(t);
77 log("%s", hnode_str(t2->tag()));
78 log("%s", color_str(t2->color));
79 log("%s", t2->s->data_);
80 }
81#endif
82
83 // NOTE: This is self-initialization!!!
84 /*
85 if (t->tag == hnode_e::Leaf) {
86 hnode__Leaf* t = static_cast<hnode__Leaf*>(t);
87 log("%s", hnode_str(t->tag));
88 log("%s", color_str(t->color));
89 log("%s", t->s->data_);
90 }
91 */
92
93 PASS();
94}
95
96using shared_variant_asdl::DoubleQuoted;
97using shared_variant_asdl::word_part_e;
98using shared_variant_asdl::word_part_t;
99
100using shared_variant_asdl::tok;
101using shared_variant_asdl::tok_e;
102using shared_variant_asdl::tok_t;
103using shared_variant_asdl::Token;
104
105TEST shared_variant_test() {
106 auto* dq = Alloc<DoubleQuoted>(0, Alloc<List<BigStr*>>());
107
108 word_part_t* wp = nullptr;
109 wp = dq; // assign to base type
110
111 log("wp->tag() %d", wp->tag());
112
113 auto* token = Alloc<Token>(0, StrFromC("hi"));
114 tok_t* tok = nullptr;
115 tok = token;
116
117 log("tok->tag() for Token = %d", tok->tag());
118
119 auto* eof = tok::Eof;
120 tok = eof;
121 log("tok->tag() for Eof = %d", tok->tag());
122
123 PASS();
124}
125
126using typed_demo_asdl::bool_expr_str;
127
128TEST pretty_print_test() {
129 // typed_demo.asdl
130
131 // auto o = op_id_e::Plus;
132 // Note: this is NOT prevented at compile time, even though it's illegal.
133 // left and right are not optional.
134 // auto b = new bool_expr__LogicalBinary(o, nullptr, nullptr);
135
136 auto w1 = Alloc<typed_demo_asdl::word>(StrFromC("left"));
137 auto w2 = Alloc<typed_demo_asdl::word>(StrFromC("right"));
138 auto b = Alloc<bool_expr__Binary>(w1, w2);
139
140 hnode_t* t1 = b->PrettyTree();
141 ASSERT_EQ_FMT(hnode_e::Record, t1->tag(), "%d");
142
143 auto f = mylib::Stdout();
144 auto ast_f = Alloc<format::TextOutput>(f);
145 format::PrintTree(t1, ast_f);
146 printf("\n");
147
148 log("bool_expr_str = %s", bool_expr_str(b->tag())->data_);
149 ASSERT(str_equals0("bool_expr.Binary", bool_expr_str(b->tag())));
150
151 ASSERT(str_equals0("Binary", bool_expr_str(b->tag(), false)));
152
153 // typed_arith.asdl
154 auto* c = Alloc<arith_expr__Const>(42);
155 hnode_t* t2 = c->PrettyTree();
156 ASSERT_EQ(hnode_e::Record, t2->tag());
157 format::PrintTree(t2, ast_f);
158 printf("\n");
159
160 auto* big = Alloc<arith_expr__Big>(mops::BigInt(INT64_MAX));
161 hnode_t* t3 = big->PrettyTree();
162 ASSERT_EQ(hnode_e::Record, t3->tag());
163 format::PrintTree(t3, ast_f);
164 printf("\n");
165
166 auto* args =
167 NewList<arith_expr_t*>(std::initializer_list<arith_expr_t*>{c, big});
168 auto* func = Alloc<arith_expr::FuncCall>(StrFromC("myfunc"), args);
169 hnode_t* t4 = func->PrettyTree();
170 ASSERT_EQ(hnode_e::Record, t4->tag());
171 format::PrintTree(t4, ast_f);
172
173 PASS();
174}
175
176TEST dicts_test() {
177 auto m = typed_demo_asdl::Dicts::CreateNull();
178 log("m.ss = %p", m->ss);
179 log("m.ib = %p", m->ib);
180
181 m->ss = Alloc<Dict<BigStr*, BigStr*>>();
182 m->ib = Alloc<Dict<int, bool>>();
183
184 m->ss->set(StrFromC("foo"), StrFromC("bar"));
185
186 m->ib->set(42, true);
187 // note: Dict<int, bool>::get() doesn't compile because nullptr isn't valid
188 // to return. But Dict<int, bool>::index() does compile.
189 log("mm.ib[42] = %d", m->ib->at(42));
190
191 hnode_t* t = m->PrettyTree();
192 auto f = mylib::Stdout();
193 auto ast_f = Alloc<format::TextOutput>(f);
194 // fails with repr(void *)
195 // OK change the pretty printer!
196 format::PrintTree(t, ast_f);
197
198 PASS();
199}
200
201using typed_demo_asdl::flag_type;
202using typed_demo_asdl::flag_type__Bool;
203using typed_demo_asdl::SetToArg_;
204
205ObjHeader make_global(ObjHeader header) {
206 header.heap_tag = HeapTag::Global;
207 return header;
208}
209
210// TODO: We should always use these, rather than 'new flag_type::Bool()'
211GcGlobal<flag_type__Bool> g_ft = {make_global(flag_type__Bool::obj_header())};
212
213// Use __ style
214using typed_demo_asdl::cflow__Return;
215GcGlobal<cflow__Return> g_ret = {make_global(cflow__Return::obj_header()), {5}};
216
217int i0 = 7;
218
219// NOTE: This causes an failed assert() in the GC runtime
220#if 0
221List<int>* g_list = NewList<int>({i0, 8, 9});
222#endif
223
224// Dict<BigStr*, int> g_dict = {4, 5, 6};
225
226TEST literal_test() {
227 // Interesting, initializer list part of the constructor "runs". Otherwise
228 // this doesn't work.
229 log("g_ft.tag() = %d", g_ft.obj.tag());
230 auto ft = flag_type::Bool;
231 ASSERT_EQ(g_ft.obj.tag(), ft->tag());
232
233 log("g_ret.tag() = %d", g_ret.obj.tag());
234 log("g_ret.status = %d", g_ret.obj.status);
235 auto ret = Alloc<cflow__Return>(5);
236 ASSERT_EQ(g_ret.obj.tag(), ret->tag());
237 ASSERT_EQ(g_ret.obj.status, ret->status);
238
239#if 0
240 // Wow this works too? Is it the the constexpr interpreter, or is this code
241 // inserted before main()?
242 ASSERT_EQ(3, len(g_list));
243 ASSERT_EQ_FMT(7, g_list->at(0), "%d");
244 ASSERT_EQ_FMT(8, g_list->at(1), "%d");
245 ASSERT_EQ_FMT(9, g_list->at(2), "%d");
246#endif
247
248 PASS();
249}
250
251TEST string_defaults_test() {
252 auto st = Alloc<typed_demo_asdl::Strings>(kEmptyString, kEmptyString);
253 ASSERT_EQ(kEmptyString, st->required);
254 ASSERT_EQ(kEmptyString, st->optional);
255
256 st = typed_demo_asdl::Strings::CreateNull();
257 ASSERT_EQ(kEmptyString, st->required);
258 ASSERT_EQ(nullptr, st->optional);
259
260 st = Alloc<typed_demo_asdl::Strings>(kEmptyString, nullptr);
261 ASSERT_EQ(kEmptyString, st->required);
262 ASSERT_EQ(nullptr, st->optional);
263
264 PASS();
265}
266
267TEST list_defaults_test() {
268 auto o = op_array::CreateNull();
269 ASSERT_EQ(nullptr, o->ops);
270
271 // Empty list
272 auto o2 = op_array::CreateNull(true);
273 ASSERT_EQ(0, len(o2->ops));
274
275 PASS();
276}
277
278TEST subtype_test() {
279 // TODO: Also need to test GC header for List[int] subtypes
280
281 auto c = Alloc<CompoundWord>();
282
283 log("len = %d", len(c));
284
285 c->append(arith_expr::NoOp);
286 c->append(Alloc<arith_expr::Const>(42));
287
288 log("len = %d", len(c));
289
290#if 1
291 hnode_t* t1 = c->PrettyTree();
292 // ASSERT_EQ_FMT(hnode_e::Record, t1->tag(), "%d");
293
294 auto f = mylib::Stdout();
295 auto ast_f = Alloc<format::TextOutput>(f);
296 format::PrintTree(t1, ast_f);
297 printf("\n");
298#endif
299
300 PASS();
301}
302
303GREATEST_MAIN_DEFS();
304
305int main(int argc, char** argv) {
306 gHeap.Init();
307
308 GREATEST_MAIN_BEGIN();
309
310 RUN_TEST(misc_test);
311 RUN_TEST(shared_variant_test);
312 RUN_TEST(pretty_print_test);
313 RUN_TEST(dicts_test);
314 RUN_TEST(literal_test);
315 RUN_TEST(string_defaults_test);
316 RUN_TEST(list_defaults_test);
317 RUN_TEST(subtype_test);
318
319 gHeap.CleanProcessExit();
320
321 GREATEST_MAIN_END();
322
323 return 0;
324}