99 throw std::runtime_error(
"ImpScheme AST builder recursion depth exceeded limit 256");
103 if (std::isdigit(
static_cast<unsigned char>(expr.
atom[0]))) {
104 auto lit = std::make_shared<ScmLiteral>();
106 lit->int_val = std::stoll(expr.
atom);
109 auto v = std::make_shared<ScmVarRef>();
115 const auto& head = expr.
list[0].atom;
117 if (head ==
"program" || head ==
"impk-pipeline" || head ==
"pipeline" || head ==
"query" || head ==
"module") {
118 std::vector<AstPtr> steps;
119 for (
size_t i = 1; i < expr.
list.size(); ++i) {
120 if (expr.
list[i].is_list && !expr.
list[i].list.empty() && expr.
list[i].list[0].atom ==
"define-query") {
123 if (expr.
list[i].is_list && !expr.
list[i].list.empty() && expr.
list[i].list[0].atom ==
"define-kernel") {
126 auto step =
build(expr.
list[i], depth + 1);
127 if (step) steps.push_back(step);
129 return std::make_shared<ScmProgram>(std::move(steps));
130 }
else if (head ==
"define-query") {
131 if (expr.
list.size() > 2) {
132 std::vector<AstPtr> steps;
133 for (
size_t i = 2; i < expr.
list.size(); ++i) {
134 auto step =
build(expr.
list[i], depth + 1);
135 if (step) steps.push_back(step);
137 return std::make_shared<ScmProgram>(std::move(steps));
139 }
else if (head ==
"let" && expr.
list.size() > 1) {
140 auto let_node = std::make_shared<ScmLet>();
141 const auto& bindings = expr.
list[1];
142 if (bindings.is_list) {
143 for (
const auto& bind : bindings.list) {
144 if (bind.is_list && bind.list.size() == 2) {
145 let_node->vars.push_back(bind.list[0].atom);
146 let_node->inits.push_back(
build(bind.list[1], depth + 1));
150 for (
size_t i = 2; i < expr.
list.size(); ++i) {
151 auto step =
build(expr.
list[i], depth + 1);
152 if (step) let_node->body.push_back(step);
155 }
else if (head ==
"loop-while") {
156 if (expr.
list.size() > 1) {
157 auto loop_node = std::make_shared<ScmLoopWhile>();
158 loop_node->condition =
build(expr.
list[1], depth + 1);
159 for (
size_t i = 2; i < expr.
list.size(); ++i) {
160 auto step =
build(expr.
list[i], depth + 1);
161 if (step) loop_node->body.push_back(step);
165 }
else if (head ==
"set!" && expr.
list.size() == 3) {
166 auto set_node = std::make_shared<ScmSet>();
167 set_node->var = expr.
list[1].atom;
168 set_node->expr =
build(expr.
list[2], depth + 1);
170 }
else if (head ==
"bitset:empty") {
171 auto b = std::make_shared<ScmBitsetInit>();
174 }
else if (head ==
"bitset:all") {
175 auto b = std::make_shared<ScmBitsetInit>();
178 }
else if (head ==
"bitset:from" && expr.
list.size() == 2) {
179 auto b = std::make_shared<ScmBitsetInit>();
181 b->param = expr.
list[1].atom;
183 }
else if (head ==
"bitset:cardinality" && expr.
list.size() == 2) {
184 auto c = std::make_shared<ScmCardinality>();
185 c->var = expr.
list[1].atom;
187 }
else if (head ==
">" && expr.
list.size() == 3) {
191 }
else if (head ==
"csr-walk" || head ==
"g:walk-csr" || head ==
"mxv" || head ==
"pagerank-step" || head ==
"cc-afforest" || head ==
"ewise-add" || head ==
"ewise-mul" || head ==
"degree-norm") {
192 std::string rel =
"edge";
193 for (
size_t i = 1; i < expr.
list.size(); ++i) {
194 if (!expr.
list[i].is_list && expr.
list[i].atom !=
"g" && expr.
list[i].atom !=
"frontier" && expr.
list[i].atom !=
"updateEdge") {
195 rel = expr.
list[i].atom;
199 }
else if (head ==
"topk") {
201 }
else if (head ==
"csc-walk" || head ==
"g:walk-csc") {
202 std::string rel =
"edge";
203 for (
size_t i = 1; i < expr.
list.size(); ++i) {
204 if (!expr.
list[i].is_list && expr.
list[i].atom !=
"g" && expr.
list[i].atom !=
"frontier" && expr.
list[i].atom !=
"updateEdge") {
205 rel = expr.
list[i].atom;
209 }
else if (head ==
"collect-bitset") {
211 }
else if (head ==
"collect-count") {
213 }
else if (head ==
"set:difference" || head ==
"set:union" || head ==
"set:intersect") {
214 if (expr.
list.size() == 3) {
215 auto op = std::make_shared<ScmSetOp>();
219 op->lhs =
build(expr.
list[1], depth + 1);
220 op->rhs =
build(expr.
list[2], depth + 1);
223 }
else if (head ==
"return" && expr.
list.size() == 2) {
224 auto r = std::make_shared<ScmReturn>();
225 r->expr =
build(expr.
list[1], depth + 1);
227 }
else if (head ==
"csr-walk-2hop" && expr.
list.size() >= 3) {
228 return std::make_shared<ScmWalk2Hop>(expr.
list[1].atom, expr.
list[2].atom);
229 }
else if (head ==
"lane-init-seeds") {
230 uint16_t sreg = (expr.
list.size() > 1 && !expr.
list[1].atom.empty()) ?
static_cast<uint16_t
>(std::stoul(expr.
list[1].atom)) : 0;
231 uint8_t wflag = (expr.
list.size() > 2 && !expr.
list[2].atom.empty()) ?
static_cast<uint8_t
>(std::stoul(expr.
list[2].atom)) : 0;
232 return std::make_shared<ScmLaneInitSeeds>(sreg, wflag);
233 }
else if (head ==
"lane-init-groups" && expr.
list.size() >= 3) {
234 uint16_t preg =
static_cast<uint16_t
>(std::stoul(expr.
list[1].atom));
235 uint16_t ngroups =
static_cast<uint16_t
>(std::stoul(expr.
list[2].atom));
236 return std::make_shared<ScmLaneInitGroups>(preg, ngroups);
237 }
else if (head ==
"lane-set-edge-mask" && expr.
list.size() >= 3) {
238 uint16_t preg =
static_cast<uint16_t
>(std::stoul(expr.
list[1].atom));
239 std::string rel = expr.
list[2].atom;
240 int rel_id = (!rel.empty() && std::isdigit(
static_cast<unsigned char>(rel[0]))) ? std::stoi(rel) : -1;
241 return std::make_shared<ScmLaneSetEdgeMask>(preg, rel, rel_id);
242 }
else if ((head ==
"lane-walk-csr" || head ==
"lane-csr-walk") && expr.
list.size() >= 2) {
243 std::string rel = expr.
list[1].atom;
244 bool fp = (expr.
list.size() > 2 && (expr.
list[2].atom ==
"1" || expr.
list[2].atom ==
"#t" || expr.
list[2].atom ==
"true"));
246 if (!rel.empty() && std::isdigit(
static_cast<unsigned char>(rel[0]))) {
247 w->physical_rel_id = std::stoi(rel);
250 }
else if ((head ==
"lane-walk-csc" || head ==
"lane-csc-walk") && expr.
list.size() >= 2) {
251 std::string rel = expr.
list[1].atom;
252 bool fp = (expr.
list.size() > 2 && (expr.
list[2].atom ==
"1" || expr.
list[2].atom ==
"#t" || expr.
list[2].atom ==
"true"));
254 if (!rel.empty() && std::isdigit(
static_cast<unsigned char>(rel[0]))) {
255 w->physical_rel_id = std::stoi(rel);
258 }
else if (head ==
"lane-prune-mask" && expr.
list.size() >= 3) {
259 uint8_t mode =
static_cast<uint8_t
>(std::stoul(expr.
list[1].atom));
260 uint16_t arg0 =
static_cast<uint16_t
>(std::stoul(expr.
list[2].atom));
261 uint16_t arg1 = (expr.
list.size() > 3 && !expr.
list[3].atom.empty()) ?
static_cast<uint16_t
>(std::stoul(expr.
list[3].atom)) : 0;
262 return std::make_shared<ScmLanePruneMask>(mode, arg0, arg1);
263 }
else if (head ==
"lane-filter-quorum" && expr.
list.size() >= 2) {
264 uint16_t mq =
static_cast<uint16_t
>(std::stoul(expr.
list[1].atom));
265 uint16_t mreg = (expr.
list.size() > 2 && !expr.
list[2].atom.empty()) ?
static_cast<uint16_t
>(std::stoul(expr.
list[2].atom)) : 0xFF;
266 return std::make_shared<ScmLaneFilterQuorum>(mq, mreg);
267 }
else if (head ==
"lane-reduce-and") {
268 return std::make_shared<ScmLaneReduceAnd>();
269 }
else if (head ==
"lane-reduce-attr" && expr.
list.size() >= 3) {
270 uint16_t aid =
static_cast<uint16_t
>(std::stoul(expr.
list[1].atom));
271 uint8_t op =
static_cast<uint8_t
>(std::stoul(expr.
list[2].atom));
272 return std::make_shared<ScmLaneReduceAttr>(aid, op);
273 }
else if (head ==
"lane-set-cover") {
274 uint16_t mreg = (expr.
list.size() > 1 && !expr.
list[1].atom.empty()) ?
static_cast<uint16_t
>(std::stoul(expr.
list[1].atom)) : 0xFF;
275 uint8_t mode = (expr.
list.size() > 2 && !expr.
list[2].atom.empty()) ?
static_cast<uint8_t
>(std::stoul(expr.
list[2].atom)) : 0;
276 return std::make_shared<ScmLaneSetCover>(mreg, mode);
277 }
else if (head ==
"vector-ann-search" && expr.
list.size() >= 3) {
278 uint16_t qreg =
static_cast<uint16_t
>(std::stoul(expr.
list[1].atom));
279 uint16_t top_k =
static_cast<uint16_t
>(std::stoul(expr.
list[2].atom));
280 uint16_t idx_id = (expr.
list.size() > 3 && !expr.
list[3].atom.empty()) ?
static_cast<uint16_t
>(std::stoul(expr.
list[3].atom)) : 0;
281 bool fused = (expr.
list.size() > 4 && (expr.
list[4].atom ==
"1" || expr.
list[4].atom ==
"#t" || expr.
list[4].atom ==
"true"));
282 return std::make_shared<ScmVectorAnnSearch>(qreg, top_k, idx_id, fused);
283 }
else if (head ==
"vector-rerank-dot" && expr.
list.size() >= 2) {
284 uint16_t qreg =
static_cast<uint16_t
>(std::stoul(expr.
list[1].atom));
285 uint16_t aid = (expr.
list.size() > 2 && !expr.
list[2].atom.empty()) ?
static_cast<uint16_t
>(std::stoul(expr.
list[2].atom)) : 0;
286 uint8_t met = (expr.
list.size() > 3 && !expr.
list[3].atom.empty()) ?
static_cast<uint8_t
>(std::stoul(expr.
list[3].atom)) : 0;
287 return std::make_shared<ScmVectorRerankDot>(qreg, aid, met);
288 }
else if (head ==
"fixpoint" && expr.
list.size() >= 2) {
289 std::string rel = expr.
list[1].atom;
290 int max_iter = (expr.
list.size() > 2 && !expr.
list[2].atom.empty() && std::isdigit(
static_cast<unsigned char>(expr.
list[2].atom[0])))
291 ? std::stoi(expr.
list[2].atom) : 100;
293 std::vector<AstPtr> body;
294 for (
size_t i = 2; i < expr.
list.size(); ++i) {
296 else if (expr.
list[i].is_list) {
298 if (b) body.push_back(b);
301 auto fp = std::make_shared<ScmFixpoint>(rel, dir, max_iter);
302 if (!rel.empty() && std::isdigit(
static_cast<unsigned char>(rel[0]))) {
303 fp->physical_rel_id = std::stoi(rel);
305 fp->body_steps = std::move(body);