OILS / asdl / cpp_runtime.h View on Github | oilshell.org

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