| 1 | // Experiment toward trying to generate less C++ code in *.asdl.cc
|
| 2 | // Problems:
|
| 3 | // - Id_t type -> dependency issue
|
| 4 |
|
| 5 | #ifndef ASDL_CPP_RUNTIME_H
|
| 6 | #define ASDL_CPP_RUNTIME_H
|
| 7 |
|
| 8 | #include "_gen/asdl/hnode.asdl.h"
|
| 9 | #include "mycpp/runtime.h"
|
| 10 |
|
| 11 | using hnode_asdl::color_e;
|
| 12 | using hnode_asdl::hnode;
|
| 13 | using hnode_asdl::hnode_t;
|
| 14 |
|
| 15 | //
|
| 16 | // ToPretty() overload set. asdl/gen_cpp.py targets this.
|
| 17 | //
|
| 18 |
|
| 19 | inline hnode_t* ToPretty(bool item) {
|
| 20 | // T and F are also in asdl/runtime.py
|
| 21 |
|
| 22 | // TODO: use constant, not StrFromC
|
| 23 | return Alloc<hnode::Leaf>(item ? StrFromC("T") : StrFromC("F"),
|
| 24 | color_e::OtherConst);
|
| 25 | }
|
| 26 |
|
| 27 | // uint16_t, int, double are the same
|
| 28 | inline hnode_t* ToPretty(uint16_t item) {
|
| 29 | return Alloc<hnode::Leaf>(str(item), color_e::OtherConst);
|
| 30 | }
|
| 31 |
|
| 32 | inline hnode_t* ToPretty(int item) {
|
| 33 | return Alloc<hnode::Leaf>(str(item), color_e::OtherConst);
|
| 34 | }
|
| 35 |
|
| 36 | inline hnode_t* ToPretty(double item) {
|
| 37 | return Alloc<hnode::Leaf>(str(item), color_e::OtherConst);
|
| 38 | }
|
| 39 |
|
| 40 | inline hnode_t* ToPretty(mops::BigInt item) {
|
| 41 | return Alloc<hnode::Leaf>(mops::ToStr(item), color_e::OtherConst);
|
| 42 | }
|
| 43 |
|
| 44 | inline hnode_t* ToPretty(BigStr* item) {
|
| 45 | // Note: if we had strict Optional[T], we might not need this
|
| 46 | if (item == nullptr) {
|
| 47 | // TODO: use constant, not StrFromC
|
| 48 | // asdl/gen_cpp.py also uses "_"
|
| 49 | return Alloc<hnode::Leaf>(StrFromC("_"), color_e::OtherConst);
|
| 50 | } else {
|
| 51 | return Alloc<hnode::Leaf>(item, color_e::StringConst);
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | #if 0
|
| 56 | // Problem: we can't distinguish between T* and void* ?
|
| 57 | // We need to call obj->PrettyTree() sometimes
|
| 58 | inline hnode_t* ToPretty(void* item) {
|
| 59 | return Alloc<hnode::External>(item);
|
| 60 | }
|
| 61 |
|
| 62 | // The T param here is the item type
|
| 63 | template <typename T>
|
| 64 | hnode_t* ListPretty(List<T>* li, Dict<int, bool>* seen) {
|
| 65 | seen = seen ? seen : Alloc<Dict<int, bool>>();
|
| 66 | int heap_id = ObjectId(li);
|
| 67 | if (dict_contains(seen, heap_id)) {
|
| 68 | return Alloc<hnode::AlreadySeen>(heap_id);
|
| 69 | }
|
| 70 |
|
| 71 | hnode::Array* a = Alloc<hnode::Array>(Alloc<List<hnode_t*>>());
|
| 72 | for (ListIter<T> it(li); !it.Done(); it.Next()) {
|
| 73 | T item = it.Value();
|
| 74 | hnode_t* h = ToPretty(item);
|
| 75 | a->children->append(h);
|
| 76 | }
|
| 77 | return a;
|
| 78 | }
|
| 79 | #endif
|
| 80 |
|
| 81 | #endif // ASDL_CPP_RUNTIME_H
|