Loading...
Searching...
No Matches
node.inl
1#pragma once
2
3#include "core/builtin/container/tbicontainable.inl"
4#include "core/ast/args.hpp"
7#include "core/ast/node.hpp"
8#include "core/ast/params.hpp"
9#include "core/ast/scope.hpp"
10#include "core/ast/tpriorities.inl"
11
12namespace by {
13
14#define ME node
15#define TEMPLATE template <typename T>
16
17 template <typename T> class _nout tprioritiesBucket: public std::vector<tnarr<tprior<T>>> {
18 typedef tnarr<tprior<T>> elem;
19 typedef std::vector<elem> super;
20
21 public:
22 tprioritiesBucket(): _topPriority(priorType(priorType::NO_MATCH - 1)) {}
23
24 public:
25 elem& operator[](nidx n) {
26 while(n >= this->size())
27 this->super::push_back(elem());
28 return this->super::operator[](n);
29 }
30 const elem& operator[](nidx n) const BY_CONST_FUNC(tprioritiesBucket<T>, operator[](n))
31
32 public:
33 tpriorities<T> join() const {
35 for(int n = 0; n < this->size(); n++) {
36 const auto& matches = (*this)[n];
37 if(matches.len() == 0) continue;
39 for(const tprior<T>& match: matches) {
41 if(!first) {
42 first = elem;
43 ret.setPriorType(first->type);
44 }
45 if(!first->isSamePrecedence(*elem)) break;
46
47 ret.add(*elem->elem);
48 }
49 break;
50 }
51 return ret;
52 }
53
54 using super::push_back;
55
56 void push_back(const tprior<T>& elem) {
57 WHEN(elem.type > _topPriority) .ret(); // optimization.
58
59 (*this)[elem.type].add(elem);
60 _topPriority = _topPriority < elem.type ? _topPriority : elem.type;
61 }
62
63 private:
64 priorType _topPriority;
65 };
66
67 TEMPLATE
68 T* ME::sub(std::function<nbool(const std::string&, const T&)> l) { return subs().get<T>(l); }
69
70 TEMPLATE
71 T* ME::sub() { return subs().get<T>(); }
72
73 TEMPLATE
74 T* ME::sub(const std::string& name) {
75#if BY_IS_DBG
76 ncnt n = 0;
77#endif
78 return subs().get<T>([&](const std::string& key, const T& val) {
79 BY_DI("sub: [%d/%d] %s --> %s@%s", ++n, subs().len(), name, key, (void*) &val);
80 return key == name;
81 });
82 }
83
84 TEMPLATE
85 T* ME::sub(const std::string& name, const args& a) {
86#if BY_IS_DBG
87 ncnt n = 0;
88#endif
89 std::string argStr = a.toStr();
90 return subs().get<T>([&](const std::string& key, const T& val) {
92 if(key == name) p = val.prioritize(a);
93
94 BY_DI("sub: [%d/%d] %s(%s) --> %s@%s = %s", ++n, subs().len(), name, argStr, key, (void*) &val,
95 getPriorTypeName(p));
96 return p != NO_MATCH;
97 });
98 }
99
100 TEMPLATE
101 tnarr<T, strTactic> ME::subAll(std::function<nbool(const std::string&, const T&)> l) const {
102 return subs().getAll<T>(l);
103 }
104
105 TEMPLATE
106 tnarr<T, strTactic> ME::subAll() const { return subs().template getAll<T>(); }
107
108 TEMPLATE
109 tpriorities<T> ME::subAll(const std::string& name) const { return subAll<T>(name, nullptr); }
110
111 TEMPLATE
112 tpriorities<T> ME::subAll(const std::string& name, const args* a) const {
113 // subs is arranged already to its scope:
114 // so if priorType of sub was same level, I need to keep the priorType of original
115 // container.
116
117#if BY_IS_DBG
118 ncnt n = 0;
119 std::string myName = getType().createNameWithParams();
120 std::string argStr = a ? "(" + a->toStr() + ")" : "";
121#endif
122 tprioritiesBucket<T> ps;
123 const scope* e = &subs();
124 ncnt lv = 0;
125 while(e) {
127 e->getContainer().each<T>([&](const std::string& key, const T& val) {
128 WHEN(key != name) .ret(true);
129
130 p = a ? val.prioritize(*a) : EXACT_MATCH;
131 if(p != NO_MATCH) ps.push_back(*new tprior<T>(val, p, lv));
132
133 const baseFunc* f = val.template cast<baseFunc>();
134 std::string valArgs = f ? "(" + f->getParams().toStr() + ")" : "";
135 BY_DI("subAll: [%d/%d] %s%s --> %s.%s%s@%s = priority(type=%s, lv=%d)", n++, subs().len(), name, argStr,
136 myName, key, valArgs, (void*) &val, getPriorTypeName(p), lv);
137 return true;
138 });
139
140 e = e->getNext();
141 lv++;
142 }
143 return ps.join();
144 }
145
146 TEMPLATE
147 tpriorities<T> ME::subAll(const std::string& name, const args& a) const { return subAll<T>(name, &a); }
148
149#undef TEMPLATE
150#undef ME
151} // namespace by
Definition tnarr.hpp:9
Definition node.inl:17
tnchain< std::string, node, tnmap< std::string, node, immutableTactic > > scope
Symbol scope container.
Definition node.hpp:22
priorType
Definition tpriorities.hpp:10
@ NO_MATCH
Definition tpriorities.hpp:18
to Top