Impulse Graph C++ Engine 2.4.0
High-Performance Zero-Copy C-ABI Kernel and C++20 Fluent VM Engine
Loading...
Searching...
No Matches
impulse_sexpr.hpp
Go to the documentation of this file.
1
5
6#ifndef IMPULSE_SEXPR_HPP
7#define IMPULSE_SEXPR_HPP
8
9#include <string>
10#include <vector>
11#include <cctype>
12#include <stdexcept>
13#include <memory>
14#include "impulse_compiler.hpp"
15
16namespace impulse::impscm {
17
18using namespace impulse::compiler;
19
20struct SExpr {
21 std::string atom;
22 std::vector<SExpr> list;
23 bool is_list = false;
24};
25
27 std::string src;
28 size_t pos = 0;
29
30 void skip_whitespace() {
31 while (pos < src.size()) {
32 if (std::isspace(static_cast<unsigned char>(src[pos]))) {
33 pos++;
34 } else if (src[pos] == ';') {
35 while (pos < src.size() && src[pos] != '\n') pos++;
36 } else {
37 break;
38 }
39 }
40 }
41
42public:
43 static constexpr size_t MAX_PARSE_DEPTH = 256;
44
45 explicit SExprParser(std::string s) : src(std::move(s)) {}
46
47 SExpr parse(size_t depth = 0) {
48 if (depth > MAX_PARSE_DEPTH) {
49 throw std::runtime_error("ImpScheme S-expression recursion depth exceeded limit 256");
50 }
51 skip_whitespace();
52 if (pos >= src.size()) return {};
53
54 if (src[pos] == '(' || src[pos] == '[') {
55 char close = (src[pos] == '(') ? ')' : ']';
56 pos++;
57 SExpr expr;
58 expr.is_list = true;
59 while (pos < src.size()) {
60 skip_whitespace();
61 if (pos >= src.size()) break;
62 if (src[pos] == close) {
63 pos++;
64 break;
65 }
66 if (src[pos] == ')' || src[pos] == ']') {
67 throw std::runtime_error("ImpScheme S-expression mismatched closing delimiter");
68 }
69 expr.list.push_back(parse(depth + 1));
70 }
71 return expr;
72 } else if (src[pos] == ')' || src[pos] == ']') {
73 pos++;
74 throw std::runtime_error("Unexpected closing delimiter in S-Expression");
75 } else if (src[pos] == '"') {
76 pos++;
77 SExpr expr;
78 while (pos < src.size() && src[pos] != '"') {
79 expr.atom += src[pos++];
80 }
81 if (pos < src.size()) pos++; // skip closing quote
82 return expr;
83 } else {
84 SExpr expr;
85 while (pos < src.size() && !std::isspace(static_cast<unsigned char>(src[pos])) && src[pos] != '(' && src[pos] != ')' && src[pos] != '[' && src[pos] != ']') {
86 expr.atom += src[pos++];
87 }
88 return expr;
89 }
90 }
91};
92
94public:
95 static constexpr size_t MAX_PARSE_DEPTH = 256;
96
97 static AstPtr build(const SExpr& expr, size_t depth = 0) {
98 if (depth > MAX_PARSE_DEPTH) {
99 throw std::runtime_error("ImpScheme AST builder recursion depth exceeded limit 256");
100 }
101 if (!expr.is_list || expr.list.empty()) {
102 if (!expr.is_list && !expr.atom.empty()) {
103 if (std::isdigit(static_cast<unsigned char>(expr.atom[0]))) {
104 auto lit = std::make_shared<ScmLiteral>();
105 lit->type = ScmLiteral::LitType::INT;
106 lit->int_val = std::stoll(expr.atom);
107 return lit;
108 }
109 auto v = std::make_shared<ScmVarRef>();
110 v->var = expr.atom;
111 return v;
112 }
113 return nullptr;
114 }
115 const auto& head = expr.list[0].atom;
116
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") {
121 return build(expr.list[i], depth + 1);
122 }
123 if (expr.list[i].is_list && !expr.list[i].list.empty() && expr.list[i].list[0].atom == "define-kernel") {
124 continue;
125 }
126 auto step = build(expr.list[i], depth + 1);
127 if (step) steps.push_back(step);
128 }
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);
136 }
137 return std::make_shared<ScmProgram>(std::move(steps));
138 }
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));
147 }
148 }
149 }
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);
153 }
154 return let_node;
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);
162 }
163 return loop_node;
164 }
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);
169 return set_node;
170 } else if (head == "bitset:empty") {
171 auto b = std::make_shared<ScmBitsetInit>();
173 return b;
174 } else if (head == "bitset:all") {
175 auto b = std::make_shared<ScmBitsetInit>();
177 return b;
178 } else if (head == "bitset:from" && expr.list.size() == 2) {
179 auto b = std::make_shared<ScmBitsetInit>();
181 b->param = expr.list[1].atom;
182 return b;
183 } else if (head == "bitset:cardinality" && expr.list.size() == 2) {
184 auto c = std::make_shared<ScmCardinality>();
185 c->var = expr.list[1].atom;
186 return c;
187 } else if (head == ">" && expr.list.size() == 3) {
188 // For (> (bitset:cardinality frontier) 0)
189 // Just return the LHS cardinality node since our compiler currently handles this specifically
190 return build(expr.list[1], depth + 1);
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;
196 }
197 }
198 return ScmWalk::forward(rel);
199 } else if (head == "topk") {
200 return std::make_shared<ScmReduce>(ScmReduce::Op::ARGMAX);
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;
206 }
207 }
208 return ScmWalk::reverse(rel);
209 } else if (head == "collect-bitset") {
210 return ScmCollect::bitset();
211 } else if (head == "collect-count") {
212 return ScmCollect::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>();
216 if (head == "set:difference") op->op = ScmSetOp::DIFFERENCE;
217 else if (head == "set:union") op->op = ScmSetOp::UNION;
218 else op->op = ScmSetOp::INTERSECT;
219 op->lhs = build(expr.list[1], depth + 1);
220 op->rhs = build(expr.list[2], depth + 1);
221 return op;
222 }
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);
226 return r;
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"));
245 auto w = ScmLaneWalk::forward(rel, fp);
246 if (!rel.empty() && std::isdigit(static_cast<unsigned char>(rel[0]))) {
247 w->physical_rel_id = std::stoi(rel);
248 }
249 return w;
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"));
253 auto w = ScmLaneWalk::reverse(rel, fp);
254 if (!rel.empty() && std::isdigit(static_cast<unsigned char>(rel[0]))) {
255 w->physical_rel_id = std::stoi(rel);
256 }
257 return w;
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) {
295 if (expr.list[i].atom == ":csc") dir = WalkDirection::REVERSE_CSC;
296 else if (expr.list[i].is_list) {
297 auto b = build(expr.list[i], depth + 1);
298 if (b) body.push_back(b);
299 }
300 }
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);
304 }
305 fp->body_steps = std::move(body);
306 return fp;
307 }
308
309 return nullptr;
310 }
311
312 static std::shared_ptr<ScmProgram> parse(const std::string& script) {
313 SExprParser parser(script);
314 SExpr root = parser.parse();
315
316 auto ast = build(root);
317 if (ast) {
318 if (auto prog = std::dynamic_pointer_cast<ScmProgram>(ast)) {
319 return prog;
320 }
321 std::vector<AstPtr> steps;
322 steps.push_back(ast);
323 return std::make_shared<ScmProgram>(std::move(steps));
324 }
325
326 throw std::runtime_error("Failed to parse S-Expression AST.");
327 }
328};
329
330} // namespace impulse::impscm
331
332#endif // IMPULSE_SEXPR_HPP
@ ALL
Definition impulse_scm_ast.hpp:747
@ EMPTY
Definition impulse_scm_ast.hpp:747
@ FROM_NODE
Definition impulse_scm_ast.hpp:747
static std::shared_ptr< ScmCollect > bitset(uint16_t reg=0)
Definition impulse_scm_ast.hpp:654
static std::shared_ptr< ScmCollect > count(uint16_t reg=0)
Definition impulse_scm_ast.hpp:668
static std::shared_ptr< ScmLaneWalk > reverse(std::string rel, bool fp=false, uint8_t flg=0)
Definition impulse_scm_ast.hpp:493
static std::shared_ptr< ScmLaneWalk > forward(std::string rel, bool fp=false, uint8_t flg=0)
Definition impulse_scm_ast.hpp:484
@ INT
Definition impulse_scm_ast.hpp:166
@ ARGMAX
Definition impulse_scm_ast.hpp:242
@ DIFFERENCE
Definition impulse_scm_ast.hpp:767
@ UNION
Definition impulse_scm_ast.hpp:767
@ INTERSECT
Definition impulse_scm_ast.hpp:767
static std::shared_ptr< ScmWalk > forward(std::string rel, AstPtr pred=nullptr)
Definition impulse_scm_ast.hpp:322
static std::shared_ptr< ScmWalk > reverse(std::string rel, AstPtr pred=nullptr)
Definition impulse_scm_ast.hpp:330
Definition impulse_sexpr.hpp:93
static constexpr size_t MAX_PARSE_DEPTH
Definition impulse_sexpr.hpp:95
static AstPtr build(const SExpr &expr, size_t depth=0)
Definition impulse_sexpr.hpp:97
static std::shared_ptr< ScmProgram > parse(const std::string &script)
Definition impulse_sexpr.hpp:312
Definition impulse_sexpr.hpp:26
SExpr parse(size_t depth=0)
Definition impulse_sexpr.hpp:47
SExprParser(std::string s)
Definition impulse_sexpr.hpp:45
static constexpr size_t MAX_PARSE_DEPTH
Definition impulse_sexpr.hpp:43
Zero-Dependency C++20 Optimizing Compiler Pipeline & ImpScheme IR for Impulse Graph.
std::shared_ptr< ImpScmNode > AstPtr
Definition impulse_scm_ast.hpp:159
WalkDirection
Definition impulse_scm_ast.hpp:123
@ FORWARD_CSR
Definition impulse_scm_ast.hpp:124
@ REVERSE_CSC
Definition impulse_scm_ast.hpp:125
Definition impulse_impscm.hpp:14
Definition impulse_sexpr.hpp:20
std::vector< SExpr > list
Definition impulse_sexpr.hpp:22
bool is_list
Definition impulse_sexpr.hpp:23
std::string atom
Definition impulse_sexpr.hpp:21