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_cel.h
Go to the documentation of this file.
1#include <iostream>
9
10#ifndef IMPULSE_CEL_H
11#define IMPULSE_CEL_H
12
13#include "impulse_vm.h"
14#include "impulse_math_ops.h"
15#include "impulse_assert.h"
16#include <string>
17#include <vector>
18#include <memory>
19#include <unordered_map>
20#include <sstream>
21#include <cstdint>
22#include <cmath>
23#include <cstring>
24#include <bit>
25
26namespace impulse {
27namespace cel {
28
53
54struct Token {
56 std::string text;
57 int64_t int_val = 0;
58 double float_val = 0.0;
59 bool bool_val = false;
60 size_t pos = 0;
61};
62
63class Lexer {
64public:
65 explicit Lexer(std::string source) : src_(std::move(source)), cursor_(0) {}
66
68 skip_whitespace_and_comments();
69 if (cursor_ >= src_.size()) {
70 return Token{TokenType::END_OF_FILE, "", 0, 0.0, false, cursor_};
71 }
72
73 size_t start = cursor_;
74 char c = src_[cursor_++];
75
76 switch (c) {
77 case '+': return Token{TokenType::PLUS, "+", 0, 0.0, false, start};
78 case '-': return Token{TokenType::MINUS, "-", 0, 0.0, false, start};
79 case '*': return Token{TokenType::STAR, "*", 0, 0.0, false, start};
80 case '/': return Token{TokenType::SLASH, "/", 0, 0.0, false, start};
81 case '%': return Token{TokenType::PERCENT, "%", 0, 0.0, false, start};
82 case '?': return Token{TokenType::QUESTION, "?", 0, 0.0, false, start};
83 case ':': return Token{TokenType::COLON, ":", 0, 0.0, false, start};
84 case '.': return Token{TokenType::DOT, ".", 0, 0.0, false, start};
85 case ',': return Token{TokenType::COMMA, ",", 0, 0.0, false, start};
86 case '(': return Token{TokenType::LPAREN, "(", 0, 0.0, false, start};
87 case ')': return Token{TokenType::RPAREN, ")", 0, 0.0, false, start};
88 case '[': return Token{TokenType::LBRACKET, "[", 0, 0.0, false, start};
89 case ']': return Token{TokenType::RBRACKET, "]", 0, 0.0, false, start};
90 case '{': return Token{TokenType::LBRACE, "{", 0, 0.0, false, start};
91 case '}': return Token{TokenType::RBRACE, "}", 0, 0.0, false, start};
92
93 case '=':
94 if (peek() == '=') { cursor_++; return Token{TokenType::EQ_EQ, "==", 0, 0.0, false, start}; }
95 return Token{TokenType::EQ, "=", 0, 0.0, false, start};
96 case '!':
97 if (peek() == '=') { cursor_++; return Token{TokenType::BANG_EQ, "!=", 0, 0.0, false, start}; }
98 return Token{TokenType::BANG, "!", 0, 0.0, false, start};
99 case '<':
100 if (peek() == '=') { cursor_++; return Token{TokenType::LT_EQ, "<=", 0, 0.0, false, start}; }
101 if (peek() == '<') { cursor_++; return Token{TokenType::LT_LT, "<<", 0, 0.0, false, start}; }
102 return Token{TokenType::LT, "<", 0, 0.0, false, start};
103 case '>':
104 if (peek() == '=') { cursor_++; return Token{TokenType::GT_EQ, ">=", 0, 0.0, false, start}; }
105 if (peek() == '>') {
106 cursor_++;
107 if (peek() == '>') { cursor_++; return Token{TokenType::GT_GT, ">>>", 0, 0.0, false, start}; }
108 return Token{TokenType::GT_GT, ">>", 0, 0.0, false, start};
109 }
110 return Token{TokenType::GT, ">", 0, 0.0, false, start};
111 case '&':
112 if (peek() == '&') { cursor_++; return Token{TokenType::AMP_AMP, "&&", 0, 0.0, false, start}; }
113 return Token{TokenType::AMP, "&", 0, 0.0, false, start};
114 case '|':
115 if (peek() == '|') { cursor_++; return Token{TokenType::PIPE_PIPE, "||", 0, 0.0, false, start}; }
116 return Token{TokenType::PIPE, "|", 0, 0.0, false, start};
117 case '^':
118 return Token{TokenType::CARET, "^", 0, 0.0, false, start};
119 case '~':
120 return Token{TokenType::TILDE, "~", 0, 0.0, false, start};
121
122 case '$':
123 case '@':
124 if (std::isalpha(static_cast<unsigned char>(peek())) || peek() == '_') {
125 return parse_ident(start);
126 }
127 break;
128
129 case '"':
130 case '\'':
131 return parse_string(c, start);
132
133 default:
134 if (std::isdigit(static_cast<unsigned char>(c))) {
135 return parse_number(c, start);
136 }
137 if (std::isalpha(static_cast<unsigned char>(c)) || c == '_') {
138 return parse_ident(start);
139 }
140 break;
141 }
142
143 return Token{TokenType::END_OF_FILE, std::string(1, c), 0, 0.0, false, start};
144 }
145
146private:
147 std::string src_;
148 size_t cursor_;
149
150 // Lexical error: reported as END_OF_FILE carrying the offending (non-empty) text, the same
151 // convention used for unknown characters; Parser::parse() rejects non-empty-text EOF.
152 Token error_token(size_t start) const {
153 std::string txt = src_.substr(start, cursor_ - start);
154 if (txt.empty()) txt = "?";
155 return Token{TokenType::END_OF_FILE, txt, 0, 0.0, false, start};
156 }
157
158 char peek() const {
159 return cursor_ < src_.size() ? src_[cursor_] : '\0';
160 }
161
162 void skip_whitespace_and_comments() {
163 while (cursor_ < src_.size()) {
164 char c = src_[cursor_];
165 if (std::isspace(static_cast<unsigned char>(c))) {
166 cursor_++;
167 } else if (c == '/' && cursor_ + 1 < src_.size() && src_[cursor_ + 1] == '/') {
168 cursor_ += 2;
169 while (cursor_ < src_.size() && src_[cursor_] != '\n') cursor_++;
170 } else {
171 break;
172 }
173 }
174 }
175
176 Token parse_string(char quote, size_t start) {
177 std::string s;
178 while (cursor_ < src_.size()) {
179 char c = src_[cursor_++];
180 if (c == quote) break;
181 if (c == '\\' && cursor_ < src_.size()) {
182 c = src_[cursor_++];
183 if (c == 'n') s += '\n';
184 else if (c == 't') s += '\t';
185 else if (c == 'r') s += '\r';
186 else s += c;
187 } else {
188 s += c;
189 }
190 }
191 Token t{TokenType::STRING_LITERAL, s, 0, 0.0, false, start};
192 return t;
193 }
194
195 Token parse_number(char first_digit, size_t start) {
196 if (first_digit == '0' && cursor_ < src_.size() && (src_[cursor_] == 'x' || src_[cursor_] == 'X')) {
197 std::string raw = std::string("0") + src_[cursor_++];
198 std::string digits;
199 while (cursor_ < src_.size()) {
200 char c = src_[cursor_];
201 if (std::isxdigit(static_cast<unsigned char>(c))) {
202 raw += c;
203 digits += c;
204 cursor_++;
205 } else if (c == '_') {
206 raw += c;
207 cursor_++;
208 } else {
209 break;
210 }
211 }
212 // "0x" with no digits, or more than 64 bits, is a lexical error (never a silent 0 / throw).
213 if (digits.empty()) return error_token(start);
214 size_t first_nz = digits.find_first_not_of('0');
215 if (first_nz != std::string::npos && digits.size() - first_nz > 16) return error_token(start);
216 Token t;
217 t.pos = start;
218 t.text = raw;
219 t.type = TokenType::INT_LITERAL;
220 t.int_val = static_cast<int64_t>(std::stoull(digits, nullptr, 16));
221 return t;
222 }
223 if (first_digit == '0' && cursor_ < src_.size() && (src_[cursor_] == 'b' || src_[cursor_] == 'B')) {
224 std::string raw = std::string("0") + src_[cursor_++];
225 std::string digits;
226 while (cursor_ < src_.size()) {
227 char c = src_[cursor_];
228 if (c == '0' || c == '1') {
229 raw += c;
230 digits += c;
231 cursor_++;
232 } else if (c == '_') {
233 raw += c;
234 cursor_++;
235 } else {
236 break;
237 }
238 }
239 if (digits.empty()) return error_token(start);
240 size_t first_nz = digits.find_first_not_of('0');
241 if (first_nz != std::string::npos && digits.size() - first_nz > 64) return error_token(start);
242 Token t;
243 t.pos = start;
244 t.text = raw;
245 t.type = TokenType::INT_LITERAL;
246 t.int_val = static_cast<int64_t>(std::stoull(digits, nullptr, 2));
247 return t;
248 }
249
250 std::string num_str(1, first_digit);
251 bool is_float = false;
252 while (cursor_ < src_.size()) {
253 char c = src_[cursor_];
254 if (std::isdigit(static_cast<unsigned char>(c))) {
255 num_str += c;
256 cursor_++;
257 } else if (c == '_') {
258 cursor_++;
259 } else if (c == '.' && !is_float && cursor_ + 1 < src_.size() && std::isdigit(static_cast<unsigned char>(src_[cursor_ + 1]))) {
260 is_float = true;
261 num_str += c;
262 cursor_++;
263 } else if (c == 'e' || c == 'E') {
264 is_float = true;
265 num_str += c;
266 cursor_++;
267 if (cursor_ < src_.size() && (src_[cursor_] == '+' || src_[cursor_] == '-')) {
268 num_str += src_[cursor_++];
269 }
270 } else {
271 break;
272 }
273 }
274 Token t;
275 t.pos = start;
276 t.text = num_str;
277 try {
278 if (is_float) {
280 t.float_val = std::stod(num_str);
281 } else {
282 t.type = TokenType::INT_LITERAL;
283 t.int_val = std::stoll(num_str);
284 }
285 } catch (const std::exception&) {
286 // Out-of-range / malformed numeric literal (e.g. 9223372036854775808, 1e999).
287 return error_token(start);
288 }
289 return t;
290 }
291
292 Token parse_ident(size_t start) {
293 std::string id;
294 char first = src_[start];
295 if (first != '$' && first != '@') {
296 id += first;
297 }
298 while (cursor_ < src_.size()) {
299 char c = src_[cursor_];
300 if (std::isalnum(static_cast<unsigned char>(c)) || c == '_') {
301 id += c;
302 cursor_++;
303 } else {
304 break;
305 }
306 }
307 Token t;
308 t.pos = start;
309 t.text = id;
310 if (id == "true") {
312 t.bool_val = true;
313 } else if (id == "false") {
315 t.bool_val = false;
316 } else if (id == "in") {
317 t.type = TokenType::KW_IN;
318 } else if (id == "as") {
319 t.type = TokenType::KW_AS;
320 } else {
321 t.type = TokenType::IDENTIFIER;
322 }
323 return t;
324 }
325};
326
327// Abstract Syntax Tree (AST)
341
342struct AstNode {
344 std::string text;
345 int64_t int_val = 0;
346 double float_val = 0.0;
347 bool bool_val = false;
348 std::string str_val;
349 std::vector<std::shared_ptr<AstNode>> children;
350
351 static std::shared_ptr<AstNode> make_int(int64_t v) {
352 auto n = std::make_shared<AstNode>();
353 n->kind = AstKind::LITERAL_INT;
354 n->int_val = v;
355 return n;
356 }
357
358 static std::shared_ptr<AstNode> make_float(double v) {
359 auto n = std::make_shared<AstNode>();
360 n->kind = AstKind::LITERAL_FLOAT;
361 n->float_val = v;
362 return n;
363 }
364
365 static std::shared_ptr<AstNode> make_bool(bool v) {
366 auto n = std::make_shared<AstNode>();
367 n->kind = AstKind::LITERAL_BOOL;
368 n->bool_val = v;
369 return n;
370 }
371
372 static std::shared_ptr<AstNode> make_string(std::string v) {
373 auto n = std::make_shared<AstNode>();
374 n->kind = AstKind::LITERAL_STRING;
375 n->str_val = std::move(v);
376 return n;
377 }
378
379 static std::shared_ptr<AstNode> make_ident(std::string name) {
380 auto n = std::make_shared<AstNode>();
381 n->kind = AstKind::IDENTIFIER;
382 n->text = std::move(name);
383 return n;
384 }
385
386 static std::shared_ptr<AstNode> make_member(std::shared_ptr<AstNode> target, std::string field) {
387 auto n = std::make_shared<AstNode>();
388 n->kind = AstKind::MEMBER_ACCESS;
389 n->text = std::move(field);
390 n->children.push_back(std::move(target));
391 return n;
392 }
393
394 static std::shared_ptr<AstNode> make_unary(std::string op, std::shared_ptr<AstNode> operand) {
395 auto n = std::make_shared<AstNode>();
396 n->kind = AstKind::UNARY_OP;
397 n->text = std::move(op);
398 n->children.push_back(std::move(operand));
399 return n;
400 }
401
402 static std::shared_ptr<AstNode> make_binary(std::string op, std::shared_ptr<AstNode> lhs, std::shared_ptr<AstNode> rhs) {
403 auto n = std::make_shared<AstNode>();
404 n->kind = AstKind::BINARY_OP;
405 n->text = std::move(op);
406 n->children.push_back(std::move(lhs));
407 n->children.push_back(std::move(rhs));
408 return n;
409 }
410
411 static std::shared_ptr<AstNode> make_ternary(std::shared_ptr<AstNode> cond, std::shared_ptr<AstNode> then_branch, std::shared_ptr<AstNode> else_branch) {
412 auto n = std::make_shared<AstNode>();
413 n->kind = AstKind::TERNARY_OP;
414 n->children.push_back(std::move(cond));
415 n->children.push_back(std::move(then_branch));
416 n->children.push_back(std::move(else_branch));
417 return n;
418 }
419
420 static std::shared_ptr<AstNode> make_call(std::string func, std::vector<std::shared_ptr<AstNode>> args) {
421 auto n = std::make_shared<AstNode>();
422 n->kind = AstKind::FUNCTION_CALL;
423 n->text = std::move(func);
424 n->children = std::move(args);
425 return n;
426 }
427
428 static std::shared_ptr<AstNode> make_list(std::vector<std::shared_ptr<AstNode>> elements) {
429 auto n = std::make_shared<AstNode>();
430 n->kind = AstKind::LIST_LITERAL;
431 n->children = std::move(elements);
432 return n;
433 }
434};
435
436// Operator Precedence levels for Pratt parser
453
454class Parser {
455public:
456 static constexpr size_t MAX_PARSE_DEPTH = 256;
457
458 explicit Parser(std::string source) : lexer_(std::move(source)) {
459 advance();
460 }
461
462 std::shared_ptr<AstNode> parse_expression(size_t depth = 0) {
463 if (depth > MAX_PARSE_DEPTH) {
464 throw std::runtime_error("CEL parse recursion depth exceeded limit 256");
465 }
466 return parse_precedence(PREC_ASSIGN, depth);
467 }
468
469 std::shared_ptr<AstNode> parse() {
470 auto expr = parse_expression(0);
471 // END_OF_FILE with non-empty text is a lexical error (unknown char / bad literal).
472 if (!expr || curr_.type != TokenType::END_OF_FILE || !curr_.text.empty()) {
473 return nullptr;
474 }
475 // Sub-expression parse failures yield nullptr children (e.g. `f(1, )`, `sqrt(x * )`);
476 // reject such trees so downstream passes (optimizer / lowering) never dereference them.
477 if (!tree_complete(expr)) {
478 return nullptr;
479 }
480 return expr;
481 }
482
483private:
484 static bool tree_complete(const std::shared_ptr<AstNode>& node) {
485 if (!node) return false;
486 for (const auto& child : node->children) {
487 if (!tree_complete(child)) return false;
488 }
489 return true;
490 }
491
492 Lexer lexer_;
493 Token curr_;
494
495 void advance() {
496 curr_ = lexer_.next_token();
497 }
498
499 bool match(TokenType type) {
500 if (curr_.type == type) {
501 advance();
502 return true;
503 }
504 return false;
505 }
506
507 Precedence get_precedence(TokenType type) const {
508 switch (type) {
509 case TokenType::EQ: return PREC_ASSIGN;
511 case TokenType::PIPE_PIPE: return PREC_OR;
512 case TokenType::AMP_AMP: return PREC_AND;
513 case TokenType::EQ_EQ:
515 case TokenType::LT:
516 case TokenType::LT_EQ:
517 case TokenType::GT:
518 case TokenType::GT_EQ:
519 case TokenType::KW_IN:
520 return PREC_COMPARE;
521 case TokenType::PIPE:
522 return PREC_BIT_OR;
523 case TokenType::CARET:
524 return PREC_BIT_XOR;
525 case TokenType::AMP:
526 return PREC_BIT_AND;
527 case TokenType::LT_LT:
528 case TokenType::GT_GT:
529 return PREC_SHIFT;
530 case TokenType::PLUS:
531 case TokenType::MINUS:
532 return PREC_ADD_SUB;
533 case TokenType::STAR:
534 case TokenType::SLASH:
536 return PREC_MUL_DIV;
537 case TokenType::DOT:
540 return PREC_CALL_MEMBER;
541 default:
542 return PREC_NONE;
543 }
544 }
545
546 std::shared_ptr<AstNode> parse_prefix(size_t depth = 0) {
547 if (depth > MAX_PARSE_DEPTH) {
548 throw std::runtime_error("CEL parse recursion depth exceeded limit 256");
549 }
550 Token t = curr_;
551 advance();
552
553 switch (t.type) {
555 return AstNode::make_int(t.int_val);
557 return AstNode::make_float(t.float_val);
559 return AstNode::make_bool(t.bool_val);
561 return AstNode::make_string(t.text);
563 return AstNode::make_ident(t.text);
564 case TokenType::BANG:
565 case TokenType::MINUS:
566 case TokenType::PLUS:
567 case TokenType::TILDE: {
568 auto operand = parse_precedence(PREC_UNARY, depth + 1);
569 if (!operand) return nullptr;
570 return AstNode::make_unary(t.text, operand);
571 }
572 case TokenType::LPAREN: {
573 auto expr = parse_expression(depth + 1);
574 if (!expr || !match(TokenType::RPAREN)) return nullptr;
575 return expr;
576 }
577 case TokenType::LBRACKET: {
578 std::vector<std::shared_ptr<AstNode>> elements;
579 if (curr_.type != TokenType::RBRACKET) {
580 auto first = parse_expression(depth + 1);
581 if (!first) return nullptr;
582 elements.push_back(std::move(first));
583 while (match(TokenType::COMMA)) {
584 auto next = parse_expression(depth + 1);
585 if (!next) return nullptr;
586 elements.push_back(std::move(next));
587 }
588 }
589 if (!match(TokenType::RBRACKET)) return nullptr;
590 return AstNode::make_list(std::move(elements));
591 }
592 default:
593 return nullptr;
594 }
595 }
596
597 std::shared_ptr<AstNode> parse_precedence(Precedence prec, size_t depth = 0) {
598 if (depth > MAX_PARSE_DEPTH) {
599 throw std::runtime_error("CEL parse recursion depth exceeded limit 256");
600 }
601 auto left = parse_prefix(depth + 1);
602 if (!left) return nullptr;
603
604 while (prec <= get_precedence(curr_.type)) {
605 Token op = curr_;
606 advance();
607
608 if (op.type == TokenType::QUESTION) {
609 // Ternary conditional ? :
610 auto then_branch = parse_expression(depth + 1);
611 if (!then_branch || !match(TokenType::COLON)) return nullptr;
612 auto else_branch = parse_precedence(PREC_CONDITIONAL, depth + 1);
613 if (!else_branch) return nullptr;
614 left = AstNode::make_ternary(left, then_branch, else_branch);
615 } else if (op.type == TokenType::DOT) {
616 // Field access target.field
617 if (curr_.type == TokenType::IDENTIFIER) {
618 std::string field = curr_.text;
619 advance();
620 // Could be member call: target.func(args)
621 if (match(TokenType::LPAREN)) {
622 std::vector<std::shared_ptr<AstNode>> args;
623 args.push_back(left); // receiver is first arg
624 if (curr_.type != TokenType::RPAREN) {
625 auto a = parse_expression(depth + 1);
626 if (!a) return nullptr;
627 args.push_back(std::move(a));
628 while (match(TokenType::COMMA)) {
629 auto b = parse_expression(depth + 1);
630 if (!b) return nullptr;
631 args.push_back(std::move(b));
632 }
633 }
634 if (!match(TokenType::RPAREN)) return nullptr;
635 left = AstNode::make_call(field, std::move(args));
636 } else {
637 left = AstNode::make_member(left, field);
638 }
639 } else {
640 return nullptr; // '.' must be followed by an identifier
641 }
642 } else if (op.type == TokenType::LPAREN) {
643 // Function call ident(args)
644 std::vector<std::shared_ptr<AstNode>> args;
645 if (curr_.type != TokenType::RPAREN) {
646 auto a = parse_expression(depth + 1);
647 if (!a) return nullptr;
648 args.push_back(std::move(a));
649 while (match(TokenType::COMMA)) {
650 auto b = parse_expression(depth + 1);
651 if (!b) return nullptr;
652 args.push_back(std::move(b));
653 }
654 }
655 if (!match(TokenType::RPAREN)) return nullptr;
656 if (left->kind != AstKind::IDENTIFIER) return nullptr; // only ident(args) is callable
657 left = AstNode::make_call(left->text, std::move(args));
658 } else {
659 // Binary operator
660 auto next_prec = static_cast<Precedence>(get_precedence(op.type) + (op.type == TokenType::EQ ? 0 : 1));
661 auto right = parse_precedence(next_prec, depth + 1);
662 if (!right) return nullptr;
663 left = AstNode::make_binary(op.text, left, right);
664 }
665 }
666
667 return left;
668 }
669};
670
671// CEL to ImpScheme & impOps Code Generator
673public:
674 static constexpr size_t MAX_PARSE_DEPTH = 256;
675
676 static std::string to_impscheme(const std::shared_ptr<AstNode>& node, size_t depth = 0) {
677 if (!node) return "()";
678 if (depth > MAX_PARSE_DEPTH) {
679 throw std::runtime_error("CelCompiler recursion depth exceeded limit 256");
680 }
681 IMPULSE_ASSERT(node != nullptr);
682
683 switch (node->kind) {
685 return std::to_string(node->int_val);
687 std::ostringstream ss;
688 ss << node->float_val;
689 std::string s = ss.str();
690 if (s.find('.') == std::string::npos && s.find('e') == std::string::npos) s += ".0";
691 return s;
692 }
694 return node->bool_val ? "#t" : "#f";
696 return "\"" + node->str_val + "\"";
698 return node->text;
700 return "(get-attr " + to_impscheme(node->children[0], depth + 1) + " \"" + node->text + "\")";
702 if (node->text == "!") return "(mask-not " + to_impscheme(node->children[0], depth + 1) + ")";
703 if (node->text == "-") return "(- 0 " + to_impscheme(node->children[0], depth + 1) + ")";
704 if (node->text == "~") return "(~ " + to_impscheme(node->children[0], depth + 1) + ")";
705 return to_impscheme(node->children[0], depth + 1);
706 case AstKind::BINARY_OP: {
707 std::string op = node->text;
708 if (op == "&&") return "(mask-and " + to_impscheme(node->children[0], depth + 1) + " " + to_impscheme(node->children[1], depth + 1) + ")";
709 if (op == "||") return "(mask-or " + to_impscheme(node->children[0], depth + 1) + " " + to_impscheme(node->children[1], depth + 1) + ")";
710 if (op == ">") return "(vec-cmp-gt " + to_impscheme(node->children[0], depth + 1) + " " + to_impscheme(node->children[1], depth + 1) + ")";
711 if (op == "<") return "(vec-cmp-lt " + to_impscheme(node->children[0], depth + 1) + " " + to_impscheme(node->children[1], depth + 1) + ")";
712 if (op == "==") return "(vec-cmp-eq " + to_impscheme(node->children[0], depth + 1) + " " + to_impscheme(node->children[1], depth + 1) + ")";
713 if (op == "!=") return "(mask-not (vec-cmp-eq " + to_impscheme(node->children[0], depth + 1) + " " + to_impscheme(node->children[1], depth + 1) + "))";
714 return "(" + op + " " + to_impscheme(node->children[0], depth + 1) + " " + to_impscheme(node->children[1], depth + 1) + ")";
715 }
717 return "(vec-blend " + to_impscheme(node->children[0], depth + 1) + " " + to_impscheme(node->children[1], depth + 1) + " " + to_impscheme(node->children[2], depth + 1) + ")";
719 std::string s = "(" + node->text;
720 for (const auto& arg : node->children) {
721 s += " " + to_impscheme(arg, depth + 1);
722 }
723 s += ")";
724 return s;
725 }
727 std::string s = "(list";
728 for (const auto& elem : node->children) {
729 s += " " + to_impscheme(elem, depth + 1);
730 }
731 s += ")";
732 return s;
733 }
734 }
735 return "()";
736 }
737
738 // Resolves CEL built-in function names to impulse_math_ops func_id
739 static int resolve_math_func(const std::string& name) {
740 static const std::unordered_map<std::string, int> kMathMap = {
741 {"abs", MATH_FUNC_ABS}, {"sqrt", MATH_FUNC_SQRT}, {"rsqrt", MATH_FUNC_RSQRT},
742 {"cbrt", MATH_FUNC_CBRT}, {"pow", MATH_FUNC_POW}, {"hypot", MATH_FUNC_HYPOT},
743 {"lerp", MATH_FUNC_LERP}, {"exp", MATH_FUNC_EXP}, {"exp2", MATH_FUNC_EXP2},
744 {"exp10", MATH_FUNC_EXP10}, {"expm1", MATH_FUNC_EXPM1}, {"log", MATH_FUNC_LOG},
745 {"log2", MATH_FUNC_LOG2}, {"log10", MATH_FUNC_LOG10}, {"log1p", MATH_FUNC_LOG1P},
746 {"sin", MATH_FUNC_SIN}, {"cos", MATH_FUNC_COS}, {"tan", MATH_FUNC_TAN},
747 {"asin", MATH_FUNC_ASIN}, {"acos", MATH_FUNC_ACOS}, {"atan", MATH_FUNC_ATAN},
748 {"atan2", MATH_FUNC_ATAN2}, {"sinc", MATH_FUNC_SINC}, {"sinh", MATH_FUNC_SINH},
749 {"cosh", MATH_FUNC_COSH}, {"tanh", MATH_FUNC_TANH}, {"asinh", MATH_FUNC_ASINH},
750 {"acosh", MATH_FUNC_ACOSH}, {"atanh", MATH_FUNC_ATANH}, {"floor", MATH_FUNC_FLOOR},
751 {"ceil", MATH_FUNC_CEIL}, {"trunc", MATH_FUNC_TRUNC}, {"round", MATH_FUNC_ROUND},
752 {"clamp", MATH_FUNC_CLAMP}, {"copysign", MATH_FUNC_COPYSIGN}, {"fmod", MATH_FUNC_FMOD},
753 {"relu", MATH_FUNC_RELU}, {"leaky_relu", MATH_FUNC_LEAKY_RELU}, {"sigmoid", MATH_FUNC_SIGMOID},
754 {"gelu", MATH_FUNC_GELU}, {"silu", MATH_FUNC_SILU}, {"softplus", MATH_FUNC_SOFTPLUS},
755 {"erf", MATH_FUNC_ERF}, {"erfc", MATH_FUNC_ERFC}, {"lgamma", MATH_FUNC_LGAMMA},
756 {"popcount", MATH_FUNC_POPCOUNT}, {"bit_count", MATH_FUNC_POPCOUNT},
757 {"clz", MATH_FUNC_CLZ}, {"ctz", MATH_FUNC_CTZ}, {"first_lane", MATH_FUNC_CTZ},
758 {"rotl", MATH_FUNC_ROTL}, {"rotr", MATH_FUNC_ROTR},
759 {"safeDiv", MATH_FUNC_SAFE_DIV}, {"safe_div", MATH_FUNC_SAFE_DIV},
760 {"isNan", MATH_FUNC_ISNAN}, {"isnan", MATH_FUNC_ISNAN},
761 {"isInf", MATH_FUNC_ISINF}, {"isinf", MATH_FUNC_ISINF},
762 {"isFinite", MATH_FUNC_ISFINITE}, {"isfinite", MATH_FUNC_ISFINITE}
763 };
764 auto it = kMathMap.find(name);
765 return it != kMathMap.end() ? it->second : -1;
766 }
767
773 const std::shared_ptr<AstNode>& expr,
774 std::vector<impulse_instruction_t>& out_shader,
775 uint16_t out_stream_reg = 0,
776 const std::unordered_map<std::string, uint16_t>* attr_name_to_id = nullptr,
777 bool is_filter = false
778 ) {
779 if (!expr) return false;
780
781 out_shader.push_back(impulse_instruction_t{OP_STREAM_FUNC_BEGIN, 0, 0, 0});
782
783 auto emit_stream_impl = [&](auto& self, const std::shared_ptr<AstNode>& node, uint16_t target_reg) -> bool {
784 if (!node || target_reg >= 16) return false;
785
786 switch (node->kind) {
789 float f = (node->kind == AstKind::LITERAL_FLOAT)
790 ? static_cast<float>(node->float_val)
791 : static_cast<float>(node->int_val);
792 uint32_t bits = 0;
793 std::memcpy(&bits, &f, sizeof(float));
794 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOAD_CONST, 0, target_reg, bits});
795 return true;
796 }
798 float f = node->bool_val ? 1.0f : 0.0f;
799 uint32_t bits = 0;
800 std::memcpy(&bits, &f, sizeof(float));
801 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOAD_CONST, 0, target_reg, bits});
802 return true;
803 }
804 case AstKind::IDENTIFIER: {
805 if (node->text == "lane") {
806 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOAD_SRC_ID, 2, target_reg, 0});
807 return true;
808 }
809 if (node->text == "id" || node->text == "src_id") {
810 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOAD_SRC_ID, 0, target_reg, 0});
811 return true;
812 }
813 if (node->text == "tgt_id") {
814 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOAD_TGT_ID, 0, target_reg, 0});
815 return true;
816 }
817 if (node->text == "edge_id") {
818 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOAD_EDGE_ID, 0, target_reg, 0});
819 return true;
820 }
821 uint16_t attr_id = 0;
822 if (attr_name_to_id) {
823 auto it = attr_name_to_id->find(node->text);
824 if (it != attr_name_to_id->end()) attr_id = it->second;
825 }
826 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOAD_EDGE, 0, target_reg, attr_id});
827 return true;
828 }
830 const std::string& field = node->text;
831 const auto& target = node->children[0];
832 std::string target_name = (target && target->kind == AstKind::IDENTIFIER) ? target->text : "";
833
834 if (target_name == "src") {
835 if (field == "id") {
836 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOAD_SRC_ID, 0, target_reg, 0});
837 return true;
838 }
839 if (field == "lane") {
840 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOAD_SRC_ID, 2, target_reg, 0});
841 return true;
842 }
843 if (field == "popcount") {
844 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOAD_SRC_ID, 1, target_reg, 0});
845 return true;
846 }
847 uint16_t attr_id = 0;
848 if (attr_name_to_id) {
849 auto it = attr_name_to_id->find(field);
850 if (it != attr_name_to_id->end()) attr_id = it->second;
851 }
852 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOAD_SRC, 0, target_reg, attr_id});
853 return true;
854 }
855 if (target_name == "tgt") {
856 if (field == "id") {
857 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOAD_TGT_ID, 0, target_reg, 0});
858 return true;
859 }
860 uint16_t attr_id = 0;
861 if (attr_name_to_id) {
862 auto it = attr_name_to_id->find(field);
863 if (it != attr_name_to_id->end()) attr_id = it->second;
864 }
865 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOAD_TGT, 0, target_reg, attr_id});
866 return true;
867 }
868 if (target_name == "edge") {
869 if (field == "id") {
870 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOAD_EDGE_ID, 0, target_reg, 0});
871 return true;
872 }
873 uint16_t attr_id = 0;
874 if (attr_name_to_id) {
875 auto it = attr_name_to_id->find(field);
876 if (it != attr_name_to_id->end()) attr_id = it->second;
877 }
878 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOAD_EDGE, 0, target_reg, attr_id});
879 return true;
880 }
881 uint16_t attr_id = 0;
882 if (attr_name_to_id) {
883 auto it = attr_name_to_id->find(field);
884 if (it != attr_name_to_id->end()) attr_id = it->second;
885 }
886 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOAD_EDGE, 0, target_reg, attr_id});
887 return true;
888 }
889 case AstKind::UNARY_OP: {
890 if (node->text == "!") {
891 uint16_t r0 = (target_reg + 1) % 16;
892 if (!self(self, node->children[0], r0)) return false;
893 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOGIC_NOT, 0, target_reg, r0});
894 return true;
895 }
896 if (node->text == "-") {
897 uint16_t r0 = (target_reg + 1) % 16;
898 uint16_t rz = (target_reg + 2) % 16;
899 if (!self(self, node->children[0], r0)) return false;
900 uint32_t zero_bits = 0;
901 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOAD_CONST, 0, rz, zero_bits});
902 out_shader.push_back(impulse_instruction_t{OP_STREAM_MATH_SUB, 0, target_reg, static_cast<uint32_t>(rz | (r0 << 16))});
903 return true;
904 }
905 return self(self, node->children[0], target_reg);
906 }
907 case AstKind::BINARY_OP: {
908 const std::string& op = node->text;
909 uint16_t r0 = (target_reg + 1) % 16;
910 uint16_t r1 = (target_reg + 2) % 16;
911 if (!self(self, node->children[0], r0)) return false;
912 if (!self(self, node->children[1], r1)) return false;
913 uint32_t payload = static_cast<uint32_t>(r0 | (r1 << 16));
914
915 if (op == "+") out_shader.push_back(impulse_instruction_t{OP_STREAM_MATH_ADD, 0, target_reg, payload});
916 else if (op == "-") out_shader.push_back(impulse_instruction_t{OP_STREAM_MATH_SUB, 0, target_reg, payload});
917 else if (op == "*") out_shader.push_back(impulse_instruction_t{OP_STREAM_MATH_MUL, 0, target_reg, payload});
918 else if (op == "/") out_shader.push_back(impulse_instruction_t{OP_STREAM_MATH_DIV, 0, target_reg, payload});
919 else if (op == "%") out_shader.push_back(impulse_instruction_t{OP_STREAM_MATH_MOD, 0, target_reg, payload});
920 else if (op == "==") out_shader.push_back(impulse_instruction_t{OP_STREAM_CMP_EQ, 0, target_reg, payload});
921 else if (op == "!=") out_shader.push_back(impulse_instruction_t{OP_STREAM_CMP_NEQ, 0, target_reg, payload});
922 else if (op == ">") out_shader.push_back(impulse_instruction_t{OP_STREAM_CMP_GT, 0, target_reg, payload});
923 else if (op == "<") out_shader.push_back(impulse_instruction_t{OP_STREAM_CMP_LT, 0, target_reg, payload});
924 else if (op == ">=") {
925 out_shader.push_back(impulse_instruction_t{OP_STREAM_CMP_LT, 0, target_reg, payload});
926 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOGIC_NOT, 0, target_reg, target_reg});
927 } else if (op == "<=") {
928 out_shader.push_back(impulse_instruction_t{OP_STREAM_CMP_GT, 0, target_reg, payload});
929 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOGIC_NOT, 0, target_reg, target_reg});
930 } else if (op == "&&") out_shader.push_back(impulse_instruction_t{OP_STREAM_LOGIC_AND, 0, target_reg, payload});
931 else if (op == "||") out_shader.push_back(impulse_instruction_t{OP_STREAM_LOGIC_OR, 0, target_reg, payload});
932 else return false;
933 return true;
934 }
935 case AstKind::TERNARY_OP: {
936 uint16_t r_cond = (target_reg + 1) % 16;
937 uint16_t r_true = (target_reg + 2) % 16;
938 if (!self(self, node->children[0], r_cond)) return false;
939 if (!self(self, node->children[2], target_reg)) return false;
940 if (!self(self, node->children[1], r_true)) return false;
941 out_shader.push_back(impulse_instruction_t{OP_STREAM_SELECT, 0, target_reg, static_cast<uint32_t>(r_cond | (r_true << 16))});
942 return true;
943 }
945 const std::string& fn = node->text;
946 if ((fn == "popcount" || fn == "bit_count") && !node->children.empty()) {
947 const auto& arg = node->children[0];
948 if (arg->text == "lane" || (arg->kind == AstKind::MEMBER_ACCESS && arg->text == "lane")) {
949 out_shader.push_back(impulse_instruction_t{OP_STREAM_LOAD_SRC_ID, 1, target_reg, 0});
950 return true;
951 }
952 }
953 if (fn == "sum" || fn == "min" || fn == "max") {
954 if (!node->children.empty()) {
955 uint16_t r_val = (target_reg + 1) % 16;
956 if (!self(self, node->children[0], r_val)) return false;
957 uint16_t monoid = (fn == "sum") ? 0 : ((fn == "max") ? 1 : 2);
958 out_shader.push_back(impulse_instruction_t{OP_STREAM_REDUCE, 0, r_val, static_cast<uint32_t>(target_reg | (monoid << 16))});
959 return true;
960 }
961 }
962 if (fn == "argmin" || fn == "argmax") {
963 if (!node->children.empty()) {
964 uint16_t r_val = (target_reg + 1) % 16;
965 if (!self(self, node->children[0], r_val)) return false;
966 uint8_t opc = (fn == "argmin") ? OP_STREAM_REDUCE_ARGMIN : OP_STREAM_REDUCE_ARGMAX;
967 out_shader.push_back(impulse_instruction_t{opc, 0, r_val, static_cast<uint32_t>(target_reg | ((target_reg + 1) << 16))});
968 return true;
969 }
970 }
971 int func_id = resolve_math_func(fn);
972 if (func_id > 0 && !node->children.empty()) {
973 uint16_t r0 = (target_reg + 1) % 16;
974 if (!self(self, node->children[0], r0)) return false;
975 out_shader.push_back(impulse_instruction_t{OP_STREAM_MATH_UNARY, 0, target_reg, static_cast<uint32_t>(r0 | (static_cast<uint32_t>(func_id) << 16))});
976 return true;
977 }
978 return false;
979 }
980 default:
981 return false;
982 }
983 };
984
985 if (!emit_stream_impl(emit_stream_impl, expr, out_stream_reg)) {
986 return false;
987 }
988
989 if (is_filter) {
990 out_shader.push_back(impulse_instruction_t{OP_STREAM_FILTER, 0, out_stream_reg, 0});
991 }
992
993 out_shader.push_back(impulse_instruction_t{OP_STREAM_FUNC_END, 0, 0, 0});
994 return true;
995 }
996
1001 const std::shared_ptr<AstNode>& expr,
1002 std::vector<impulse_instruction_t>& out_instructions,
1003 uint16_t out_reg,
1004 uint16_t& next_temp_reg,
1005 const std::unordered_map<std::string, uint16_t>* attr_name_to_id = nullptr,
1006 uint16_t domain_id = 0
1007 ) {
1008 if (!expr) return false;
1009
1010 auto emit_vec_impl = [&](auto& self, const std::shared_ptr<AstNode>& node, uint16_t target_reg) -> bool {
1011 if (!node || target_reg >= 64) return false;
1012
1013 switch (node->kind) {
1015 case AstKind::LITERAL_INT: {
1016 float f = (node->kind == AstKind::LITERAL_FLOAT)
1017 ? static_cast<float>(node->float_val)
1018 : static_cast<float>(node->int_val);
1019 uint32_t bits = 0;
1020 std::memcpy(&bits, &f, sizeof(float));
1021 out_instructions.push_back(impulse_instruction_t{OP_LOAD_CONST_FLOAT, 0, target_reg, bits});
1022 return true;
1023 }
1026 std::string attr_name = node->text;
1027 uint16_t attr_idx = 0;
1028 if (attr_name_to_id) {
1029 auto it = attr_name_to_id->find(attr_name);
1030 if (it != attr_name_to_id->end()) attr_idx = it->second;
1031 }
1032 uint32_t payload = static_cast<uint32_t>(domain_id) | (static_cast<uint32_t>(attr_idx) << 16);
1033 out_instructions.push_back(impulse_instruction_t{OP_LOAD_COLUMN_VECTOR, 0, target_reg, payload});
1034 return true;
1035 }
1036 case AstKind::UNARY_OP: {
1037 if (node->text == "-") {
1038 if (next_temp_reg >= 64) return false;
1039 uint16_t r_child = next_temp_reg++;
1040 if (!self(self, node->children[0], r_child)) return false;
1041 if (next_temp_reg >= 64) return false;
1042 uint16_t r_zero = next_temp_reg++;
1043 uint32_t zbits = 0;
1044 out_instructions.push_back(impulse_instruction_t{OP_LOAD_CONST_FLOAT, 0, r_zero, zbits});
1045 uint32_t payload = static_cast<uint32_t>(r_zero) | (static_cast<uint32_t>(r_child) << 8) | (1u << 16);
1046 out_instructions.push_back(impulse_instruction_t{OP_VEC_MATH_BINARY, 0, target_reg, payload});
1047 return true;
1048 }
1049 return self(self, node->children[0], target_reg);
1050 }
1051 case AstKind::BINARY_OP: {
1052 const std::string& op = node->text;
1053 if (next_temp_reg >= 64) return false;
1054 uint16_t r0 = next_temp_reg++;
1055 if (!self(self, node->children[0], r0)) return false;
1056 if (next_temp_reg >= 64) return false;
1057 uint16_t r1 = next_temp_reg++;
1058 if (!self(self, node->children[1], r1)) return false;
1059
1060 uint8_t func_id = 0;
1061 if (op == "+") func_id = 0;
1062 else if (op == "-") func_id = 1;
1063 else if (op == "*") func_id = 2;
1064 else if (op == "/") func_id = 3;
1065 else if (op == "%") func_id = MATH_FUNC_FMOD;
1066 else func_id = 0;
1067
1068 uint32_t payload = static_cast<uint32_t>(r0) | (static_cast<uint32_t>(r1) << 8) | (static_cast<uint32_t>(func_id) << 16);
1069 out_instructions.push_back(impulse_instruction_t{OP_VEC_MATH_BINARY, 0, target_reg, payload});
1070 return true;
1071 }
1073 int func_id = resolve_math_func(node->text);
1074 if (func_id <= 0) return false;
1075 if (node->children.size() == 1) {
1076 if (next_temp_reg >= 64) return false;
1077 uint16_t r0 = next_temp_reg++;
1078 if (!self(self, node->children[0], r0)) return false;
1079 uint32_t payload = static_cast<uint32_t>(r0) | (static_cast<uint32_t>(func_id) << 8);
1080 out_instructions.push_back(impulse_instruction_t{OP_VEC_MATH_UNARY, 0, target_reg, payload});
1081 return true;
1082 }
1083 if (node->children.size() == 2) {
1084 if (next_temp_reg >= 64) return false;
1085 uint16_t r0 = next_temp_reg++;
1086 if (!self(self, node->children[0], r0)) return false;
1087 if (next_temp_reg >= 64) return false;
1088 uint16_t r1 = next_temp_reg++;
1089 if (!self(self, node->children[1], r1)) return false;
1090 uint32_t payload = static_cast<uint32_t>(r0) | (static_cast<uint32_t>(r1) << 8) | (static_cast<uint32_t>(func_id) << 16);
1091 out_instructions.push_back(impulse_instruction_t{OP_VEC_MATH_BINARY, 0, target_reg, payload});
1092 return true;
1093 }
1094 if (node->children.size() == 3) {
1095 if (next_temp_reg >= 64) return false;
1096 uint16_t r0 = next_temp_reg++;
1097 if (!self(self, node->children[0], r0)) return false;
1098 if (next_temp_reg >= 64) return false;
1099 uint16_t r1 = next_temp_reg++;
1100 if (!self(self, node->children[1], r1)) return false;
1101 if (next_temp_reg >= 64) return false;
1102 uint16_t r2 = next_temp_reg++;
1103 if (!self(self, node->children[2], r2)) return false;
1104 uint32_t payload = static_cast<uint32_t>(r0) | (static_cast<uint32_t>(r1) << 8) | (static_cast<uint32_t>(r2) << 16) | (static_cast<uint32_t>(func_id) << 24);
1105 out_instructions.push_back(impulse_instruction_t{OP_VEC_MATH_TERNARY, 0, target_reg, payload});
1106 return true;
1107 }
1108 return false;
1109 }
1110 case AstKind::TERNARY_OP: {
1111 if (next_temp_reg >= 64) return false;
1112 uint16_t r_cond = next_temp_reg++;
1113 if (!self(self, node->children[0], r_cond)) return false;
1114 if (next_temp_reg >= 64) return false;
1115 uint16_t r_true = next_temp_reg++;
1116 if (!self(self, node->children[1], r_true)) return false;
1117 if (next_temp_reg >= 64) return false;
1118 uint16_t r_false = next_temp_reg++;
1119 if (!self(self, node->children[2], r_false)) return false;
1120
1121 uint32_t payload = static_cast<uint32_t>(r_cond) | (static_cast<uint32_t>(r_true) << 8) | (static_cast<uint32_t>(r_false) << 16);
1122 out_instructions.push_back(impulse_instruction_t{OP_VEC_BLEND, 0, target_reg, payload});
1123 return true;
1124 }
1125 default:
1126 return false;
1127 }
1128 };
1129
1130 return emit_vec_impl(emit_vec_impl, expr, out_reg);
1131 }
1132};
1133
1143public:
1144 static constexpr size_t MAX_PARSE_DEPTH = 256;
1145
1146 static std::shared_ptr<AstNode> optimize(const std::shared_ptr<AstNode>& node, size_t depth = 0) {
1147 if (!node) return nullptr;
1148 if (depth > MAX_PARSE_DEPTH) {
1149 throw std::runtime_error("AstOptimizer recursion depth exceeded limit 256");
1150 }
1151
1152 // 1. Bottom-up recursive optimization of children
1153 std::vector<std::shared_ptr<AstNode>> folded_children;
1154 for (const auto& child : node->children) {
1155 folded_children.push_back(optimize(child, depth + 1));
1156 }
1157
1158 auto result = std::make_shared<AstNode>(*node);
1159 // Defensive: an AST with missing (null) operands (e.g. from parse_expression() on malformed
1160 // input) is returned as-is rather than dereferenced by the folding passes.
1161 for (const auto& fc : folded_children) {
1162 if (!fc) return result;
1163 }
1164 result->children = std::move(folded_children);
1165
1166 // 2. Optimization passes
1167 switch (result->kind) {
1168 case AstKind::UNARY_OP:
1169 return fold_unary(result);
1170 case AstKind::BINARY_OP:
1171 return fold_binary(result);
1173 return fold_ternary(result);
1175 return fold_function_call(result);
1176 default:
1177 return result;
1178 }
1179 }
1180
1181private:
1182 static std::shared_ptr<AstNode> fold_unary(const std::shared_ptr<AstNode>& node) {
1183 auto child = node->children[0];
1184 if (node->text == "!") {
1185 if (child->kind == AstKind::LITERAL_BOOL) {
1186 return AstNode::make_bool(!child->bool_val);
1187 }
1188 if (child->kind == AstKind::UNARY_OP && child->text == "!") {
1189 // Double negation: !(!x) -> x
1190 return child->children[0];
1191 }
1192 } else if (node->text == "-") {
1193 if (child->kind == AstKind::LITERAL_INT) {
1194 return AstNode::make_int(static_cast<int64_t>(0 - static_cast<uint64_t>(child->int_val)));
1195 }
1196 if (child->kind == AstKind::LITERAL_FLOAT) {
1197 return AstNode::make_float(-child->float_val);
1198 }
1199 } else if (node->text == "~") {
1200 if (child->kind == AstKind::LITERAL_INT) {
1201 return AstNode::make_int(~child->int_val);
1202 }
1203 if (child->kind == AstKind::UNARY_OP && child->text == "~") {
1204 return child->children[0];
1205 }
1206 }
1207 return node;
1208 }
1209
1210 static std::shared_ptr<AstNode> fold_binary(const std::shared_ptr<AstNode>& node) {
1211 auto left = node->children[0];
1212 auto right = node->children[1];
1213 const std::string& op = node->text;
1214
1215 // Monotonic Inverse Pushdown
1216 if (op == "<" || op == "<=" || op == ">" || op == ">=" || op == "==" || op == "!=") {
1217 auto check_pushdown = [&](const std::shared_ptr<AstNode>& lhs, const std::shared_ptr<AstNode>& rhs, bool reverse_op) -> std::shared_ptr<AstNode> {
1218 if (lhs->kind == AstKind::FUNCTION_CALL && lhs->children.size() == 1 &&
1219 (rhs->kind == AstKind::LITERAL_FLOAT || rhs->kind == AstKind::LITERAL_INT)) {
1220 double c = (rhs->kind == AstKind::LITERAL_FLOAT) ? rhs->float_val : static_cast<double>(rhs->int_val);
1221 std::string new_op = op;
1222 if (reverse_op) {
1223 if (op == "<") new_op = ">";
1224 else if (op == "<=") new_op = ">=";
1225 else if (op == ">") new_op = "<";
1226 else if (op == ">=") new_op = "<=";
1227 }
1228 if (lhs->text == "sqrt") {
1229 if (c >= 0.0) {
1230 if (new_op == "<" || new_op == "<=") {
1231 return AstNode::make_binary(
1232 "&&",
1233 AstNode::make_binary(">=", lhs->children[0], AstNode::make_float(0.0)),
1234 AstNode::make_binary(new_op, lhs->children[0], AstNode::make_float(c * c))
1235 );
1236 }
1237 if (c > 0.0 && (new_op == "==" || new_op == ">" || new_op == ">=")) {
1238 return AstNode::make_binary(new_op, lhs->children[0], AstNode::make_float(c * c));
1239 }
1240 if (c == 0.0 && (new_op == ">" || new_op == ">=")) {
1241 return AstNode::make_binary(new_op, lhs->children[0], AstNode::make_float(0.0));
1242 }
1243 }
1244 }
1245 }
1246 return nullptr;
1247 };
1248 if (auto res = check_pushdown(left, right, false)) return res;
1249 if (auto res = check_pushdown(right, left, true)) return res;
1250 }
1251
1252 // 1. Integer Constant Folding
1253 if (left->kind == AstKind::LITERAL_INT && right->kind == AstKind::LITERAL_INT) {
1254 int64_t a = left->int_val;
1255 int64_t b = right->int_val;
1256 // Integer folding uses two's-complement wraparound (never signed-overflow UB):
1257 // INT64_MIN / -1 and INT64_MIN % -1 are the only division traps.
1258 const uint64_t ua = static_cast<uint64_t>(a), ub = static_cast<uint64_t>(b);
1259 if (op == "+") return AstNode::make_int(static_cast<int64_t>(ua + ub));
1260 if (op == "-") return AstNode::make_int(static_cast<int64_t>(ua - ub));
1261 if (op == "*") return AstNode::make_int(static_cast<int64_t>(ua * ub));
1262 if (op == "/" && b != 0) return AstNode::make_int(b == -1 ? static_cast<int64_t>(0 - ua) : a / b);
1263 if (op == "%" && b != 0) return AstNode::make_int(b == -1 ? 0 : a % b);
1264 if (op == "==") return AstNode::make_bool(a == b);
1265 if (op == "!=") return AstNode::make_bool(a != b);
1266 if (op == "<") return AstNode::make_bool(a < b);
1267 if (op == "<=") return AstNode::make_bool(a <= b);
1268 if (op == ">") return AstNode::make_bool(a > b);
1269 if (op == ">=") return AstNode::make_bool(a >= b);
1270 if (op == "&") return AstNode::make_int(a & b);
1271 if (op == "|") return AstNode::make_int(a | b);
1272 if (op == "^") return AstNode::make_int(a ^ b);
1273 if (op == "<<") return AstNode::make_int((b < 0 || b >= 64) ? 0 : static_cast<int64_t>(static_cast<uint64_t>(a) << b));
1274 if (op == ">>") return AstNode::make_int((b < 0 || b >= 64) ? 0 : (a >> b));
1275 if (op == ">>>") return AstNode::make_int((b < 0 || b >= 64) ? 0 : static_cast<int64_t>(static_cast<uint64_t>(a) >> b));
1276 }
1277
1278 // 2. Float Constant Folding
1279 if ((left->kind == AstKind::LITERAL_FLOAT || left->kind == AstKind::LITERAL_INT) &&
1280 (right->kind == AstKind::LITERAL_FLOAT || right->kind == AstKind::LITERAL_INT)) {
1281 double a = (left->kind == AstKind::LITERAL_FLOAT) ? left->float_val : static_cast<double>(left->int_val);
1282 double b = (right->kind == AstKind::LITERAL_FLOAT) ? right->float_val : static_cast<double>(right->int_val);
1283 if (op == "+") return AstNode::make_float(a + b);
1284 if (op == "-") return AstNode::make_float(a - b);
1285 if (op == "*") return AstNode::make_float(a * b);
1286 if (op == "/" && b != 0.0) return AstNode::make_float(a / b);
1287 if (op == "==") return AstNode::make_bool(a == b);
1288 if (op == "!=") return AstNode::make_bool(a != b);
1289 if (op == "<") return AstNode::make_bool(a < b);
1290 if (op == "<=") return AstNode::make_bool(a <= b);
1291 if (op == ">") return AstNode::make_bool(a > b);
1292 if (op == ">=") return AstNode::make_bool(a >= b);
1293 }
1294
1295 // 3. Boolean Constant Folding
1296 if (left->kind == AstKind::LITERAL_BOOL && right->kind == AstKind::LITERAL_BOOL) {
1297 bool a = left->bool_val;
1298 bool b = right->bool_val;
1299 if (op == "&&") return AstNode::make_bool(a && b);
1300 if (op == "||") return AstNode::make_bool(a || b);
1301 if (op == "==") return AstNode::make_bool(a == b);
1302 if (op == "!=") return AstNode::make_bool(a != b);
1303 }
1304
1305 // 4. Algebraic Identities
1306 if (op == "+") {
1307 if (is_zero(left)) return right;
1308 if (is_zero(right)) return left;
1309 } else if (op == "-") {
1310 if (is_zero(right)) return left;
1311 } else if (op == "*") {
1312 if (is_one(left)) return right;
1313 if (is_one(right)) return left;
1314 } else if (op == "/") {
1315 if (is_one(right)) return left;
1316 } else if (op == "&&") {
1317 if (left->kind == AstKind::LITERAL_BOOL) {
1318 return left->bool_val ? right : AstNode::make_bool(false);
1319 }
1320 if (right->kind == AstKind::LITERAL_BOOL) {
1321 return right->bool_val ? left : AstNode::make_bool(false);
1322 }
1323 } else if (op == "||") {
1324 if (left->kind == AstKind::LITERAL_BOOL) {
1325 return left->bool_val ? AstNode::make_bool(true) : right;
1326 }
1327 if (right->kind == AstKind::LITERAL_BOOL) {
1328 return right->bool_val ? AstNode::make_bool(true) : left;
1329 }
1330 }
1331
1332 return node;
1333 }
1334
1335 static std::shared_ptr<AstNode> fold_ternary(const std::shared_ptr<AstNode>& node) {
1336 auto cond = node->children[0];
1337 auto then_branch = node->children[1];
1338 auto else_branch = node->children[2];
1339
1340 if (cond->kind == AstKind::LITERAL_BOOL) {
1341 return cond->bool_val ? then_branch : else_branch;
1342 }
1343 return node;
1344 }
1345
1346 static std::shared_ptr<AstNode> fold_function_call(const std::shared_ptr<AstNode>& node) {
1347 int func_id = CelCompiler::resolve_math_func(node->text);
1348 if (func_id <= 0) return node;
1349
1350 // Integer bit intrinsics must never be folded through double (64-bit lane masks lose
1351 // precision and negative->uint64 conversion is UB). Fold with exact integer semantics that
1352 // match CelMultiLaneEvaluator, or leave symbolic.
1353 if (func_id == MATH_FUNC_POPCOUNT || func_id == MATH_FUNC_CLZ || func_id == MATH_FUNC_CTZ) {
1354 if (node->children.size() == 1 && node->children[0]->kind == AstKind::LITERAL_INT) {
1355 const uint64_t v = static_cast<uint64_t>(node->children[0]->int_val);
1356 if (func_id == MATH_FUNC_POPCOUNT) return AstNode::make_int(std::popcount(v));
1357 if (func_id == MATH_FUNC_CLZ) return AstNode::make_int(v == 0 ? 64 : std::countl_zero(v));
1358 return AstNode::make_int(v == 0 ? -1 : std::countr_zero(v));
1359 }
1360 return node;
1361 }
1362 if (func_id == MATH_FUNC_ROTL || func_id == MATH_FUNC_ROTR) return node;
1363
1364 // Unary constant folding
1365 if (node->children.size() == 1) {
1366 auto arg = node->children[0];
1367 if (arg->kind == AstKind::LITERAL_FLOAT || arg->kind == AstKind::LITERAL_INT) {
1368 double val = (arg->kind == AstKind::LITERAL_FLOAT) ? arg->float_val : static_cast<double>(arg->int_val);
1369 double res = impulse_math_unary_f64(static_cast<uint8_t>(func_id), val);
1370 return AstNode::make_float(res);
1371 }
1372 }
1373
1374 // Binary constant folding
1375 if (node->children.size() == 2) {
1376 auto arg1 = node->children[0];
1377 auto arg2 = node->children[1];
1378 if ((arg1->kind == AstKind::LITERAL_FLOAT || arg1->kind == AstKind::LITERAL_INT) &&
1379 (arg2->kind == AstKind::LITERAL_FLOAT || arg2->kind == AstKind::LITERAL_INT)) {
1380 double a = (arg1->kind == AstKind::LITERAL_FLOAT) ? arg1->float_val : static_cast<double>(arg1->int_val);
1381 double b = (arg2->kind == AstKind::LITERAL_FLOAT) ? arg2->float_val : static_cast<double>(arg2->int_val);
1382 double res = impulse_math_binary_f64(static_cast<uint8_t>(func_id), a, b);
1383 return AstNode::make_float(res);
1384 }
1385 }
1386
1387 // Ternary constant folding
1388 if (node->children.size() == 3) {
1389 auto arg1 = node->children[0];
1390 auto arg2 = node->children[1];
1391 auto arg3 = node->children[2];
1392 if ((arg1->kind == AstKind::LITERAL_FLOAT || arg1->kind == AstKind::LITERAL_INT) &&
1393 (arg2->kind == AstKind::LITERAL_FLOAT || arg2->kind == AstKind::LITERAL_INT) &&
1394 (arg3->kind == AstKind::LITERAL_FLOAT || arg3->kind == AstKind::LITERAL_INT)) {
1395 double a = (arg1->kind == AstKind::LITERAL_FLOAT) ? arg1->float_val : static_cast<double>(arg1->int_val);
1396 double b = (arg2->kind == AstKind::LITERAL_FLOAT) ? arg2->float_val : static_cast<double>(arg2->int_val);
1397 double c = (arg3->kind == AstKind::LITERAL_FLOAT) ? arg3->float_val : static_cast<double>(arg3->int_val);
1398 double res = impulse_math_ternary_f64(static_cast<uint8_t>(func_id), a, b, c);
1399 return AstNode::make_float(res);
1400 }
1401 }
1402
1403 return node;
1404 }
1405
1406 static bool is_zero(const std::shared_ptr<AstNode>& node) {
1407 if (!node) return false;
1408 if (node->kind == AstKind::LITERAL_INT && node->int_val == 0) return true;
1409 if (node->kind == AstKind::LITERAL_FLOAT && node->float_val == 0.0) return true;
1410 return false;
1411 }
1412
1413 static bool is_one(const std::shared_ptr<AstNode>& node) {
1414 if (!node) return false;
1415 if (node->kind == AstKind::LITERAL_INT && node->int_val == 1) return true;
1416 if (node->kind == AstKind::LITERAL_FLOAT && node->float_val == 1.0) return true;
1417 return false;
1418 }
1419};
1420
1426public:
1427 using AttrMap = std::unordered_map<std::string, int64_t>;
1428 using FloatAttrMap = std::unordered_map<std::string, double>;
1429
1430 static bool is_float_expr(const std::shared_ptr<AstNode>& node,
1431 const FloatAttrMap* float_attrs = nullptr) {
1432 if (!node) return false;
1433 switch (node->kind) {
1435 return true;
1438 if (node->text == "lane") return false;
1439 return float_attrs && float_attrs->find(node->text) != float_attrs->end();
1441 const std::string& fn = node->text;
1442 if (fn == "popcount" || fn == "bit_count" || fn == "first_lane" || fn == "ctz" ||
1443 fn == "last_lane" || fn == "clz" || fn == "has_any" || fn == "has_all" ||
1444 fn == "has_none" || fn == "has_lane") {
1445 return false;
1446 }
1447 return true;
1448 }
1449 default:
1450 for (const auto& c : node->children) {
1451 if (is_float_expr(c, float_attrs)) return true;
1452 }
1453 return false;
1454 }
1455 }
1456
1457 // -----------------------------------------------------------------------
1458 // eval_double (Numeric & floating-point evaluator)
1459 // -----------------------------------------------------------------------
1460 static double eval_double(const std::shared_ptr<AstNode>& node,
1461 const uint64_t* lane_words,
1462 size_t num_words,
1463 const FloatAttrMap* float_attrs = nullptr,
1464 const AttrMap* int_attrs = nullptr) {
1465 if (!node) return 0.0;
1466 switch (node->kind) {
1468 return node->float_val;
1470 return static_cast<double>(node->int_val);
1472 return node->bool_val ? 1.0 : 0.0;
1475 if (node->text == "lane") {
1476 return static_cast<double>((lane_words && num_words > 0) ? lane_words[0] : 0ULL);
1477 }
1478 if (float_attrs) {
1479 auto it = float_attrs->find(node->text);
1480 if (it != float_attrs->end()) return it->second;
1481 }
1482 if (int_attrs) {
1483 auto it = int_attrs->find(node->text);
1484 if (it != int_attrs->end()) return static_cast<double>(it->second);
1485 }
1486 return 0.0;
1487 }
1488 case AstKind::UNARY_OP: {
1489 if (node->text == "!") {
1490 return eval_bool(node->children[0], lane_words, num_words, int_attrs, float_attrs) ? 0.0 : 1.0;
1491 }
1492 if (node->text == "~") {
1493 return static_cast<double>(eval_int(node, lane_words, num_words, int_attrs, float_attrs));
1494 }
1495 double v = eval_double(node->children[0], lane_words, num_words, float_attrs, int_attrs);
1496 if (node->text == "-") return -v;
1497 return v;
1498 }
1499 case AstKind::TERNARY_OP: {
1500 bool c = eval_bool(node->children[0], lane_words, num_words, int_attrs, float_attrs);
1501 return eval_double(c ? node->children[1] : node->children[2], lane_words, num_words, float_attrs, int_attrs);
1502 }
1503 case AstKind::BINARY_OP: {
1504 const std::string& op = node->text;
1505 if (op == "=") {
1506 return eval_double(node->children[1], lane_words, num_words, float_attrs, int_attrs);
1507 }
1508 if (op == "in") {
1509 return eval_bool(node, lane_words, num_words, int_attrs, float_attrs) ? 1.0 : 0.0;
1510 }
1511 if (op == "&&" || op == "||" || op == "==" || op == "!=" ||
1512 op == "<" || op == "<=" || op == ">" || op == ">=") {
1513 return eval_bool(node, lane_words, num_words, int_attrs, float_attrs) ? 1.0 : 0.0;
1514 }
1515 if (op == "&" || op == "|" || op == "^" || op == "<<" || op == ">>" || op == ">>>") {
1516 return static_cast<double>(eval_int(node, lane_words, num_words, int_attrs, float_attrs));
1517 }
1518 double a = eval_double(node->children[0], lane_words, num_words, float_attrs, int_attrs);
1519 double b = eval_double(node->children[1], lane_words, num_words, float_attrs, int_attrs);
1520 if (op == "+") return a + b;
1521 if (op == "-") return a - b;
1522 if (op == "*") return a * b;
1523 if (op == "/") return (b != 0.0 && !std::isnan(b)) ? (a / b) : 0.0;
1524 if (op == "%") return (b != 0.0) ? std::fmod(a, b) : 0.0;
1525 return 0.0;
1526 }
1528 const std::string& fn = node->text;
1529 if ((fn == "popcount" || fn == "bit_count") && !node->children.empty()) {
1530 if (node->children[0]->text == "lane" ||
1531 (node->children[0]->kind == AstKind::MEMBER_ACCESS && node->children[0]->text == "lane")) {
1532 size_t total = 0;
1533 for (size_t w = 0; w < num_words; ++w) total += std::popcount(lane_words[w]);
1534 return static_cast<double>(total);
1535 }
1536 uint64_t v = eval_bits(node->children[0], lane_words, num_words, int_attrs, float_attrs);
1537 return static_cast<double>(std::popcount(v));
1538 }
1539 if ((fn == "first_lane" || fn == "ctz") && !node->children.empty()) {
1540 if (node->children[0]->text == "lane" ||
1541 (node->children[0]->kind == AstKind::MEMBER_ACCESS && node->children[0]->text == "lane")) {
1542 for (size_t w = 0; w < num_words; ++w) {
1543 if (lane_words[w] != 0ULL) {
1544 return static_cast<double>(w * 64 + std::countr_zero(lane_words[w]));
1545 }
1546 }
1547 return -1.0;
1548 }
1549 uint64_t v = eval_bits(node->children[0], lane_words, num_words, int_attrs, float_attrs);
1550 return (v == 0ULL) ? -1.0 : static_cast<double>(std::countr_zero(v));
1551 }
1552 if (fn == "last_lane" && !node->children.empty()) {
1553 if (node->children[0]->text == "lane" ||
1554 (node->children[0]->kind == AstKind::MEMBER_ACCESS && node->children[0]->text == "lane")) {
1555 for (size_t w = num_words; w > 0; --w) {
1556 if (lane_words[w - 1] != 0ULL) {
1557 return static_cast<double>((w - 1) * 64 + 63 - std::countl_zero(lane_words[w - 1]));
1558 }
1559 }
1560 return -1.0;
1561 }
1562 uint64_t v = eval_bits(node->children[0], lane_words, num_words, int_attrs, float_attrs);
1563 return (v == 0ULL) ? -1.0 : static_cast<double>(63 - std::countl_zero(v));
1564 }
1565 if (fn == "clz" && !node->children.empty()) {
1566 uint64_t v = eval_bits(node->children[0], lane_words, num_words, int_attrs, float_attrs);
1567 return (v == 0ULL) ? 64.0 : static_cast<double>(std::countl_zero(v));
1568 }
1569 if (fn == "has_any" || fn == "has_all" || fn == "has_none" || fn == "has_lane") {
1570 return eval_bool(node, lane_words, num_words, int_attrs, float_attrs) ? 1.0 : 0.0;
1571 }
1572
1573 // 48-Function Vector Math Catalog Dispatch
1574 int func_id = CelCompiler::resolve_math_func(fn);
1575 if (func_id > 0) {
1576 if (node->children.size() == 1) {
1577 double a = eval_double(node->children[0], lane_words, num_words, float_attrs, int_attrs);
1578 return impulse_math_unary_f64(static_cast<uint8_t>(func_id), a);
1579 }
1580 if (node->children.size() == 2) {
1581 double a = eval_double(node->children[0], lane_words, num_words, float_attrs, int_attrs);
1582 double b = eval_double(node->children[1], lane_words, num_words, float_attrs, int_attrs);
1583 return impulse_math_binary_f64(static_cast<uint8_t>(func_id), a, b);
1584 }
1585 if (node->children.size() == 3) {
1586 double a = eval_double(node->children[0], lane_words, num_words, float_attrs, int_attrs);
1587 double b = eval_double(node->children[1], lane_words, num_words, float_attrs, int_attrs);
1588 double c = eval_double(node->children[2], lane_words, num_words, float_attrs, int_attrs);
1589 return impulse_math_ternary_f64(static_cast<uint8_t>(func_id), a, b, c);
1590 }
1591 }
1592 return 0.0;
1593 }
1594 default:
1595 return 0.0;
1596 }
1597 }
1598
1599 static double eval_double(const std::shared_ptr<AstNode>& node,
1600 uint64_t lane_mask,
1601 const FloatAttrMap* float_attrs = nullptr,
1602 const AttrMap* int_attrs = nullptr) {
1603 return eval_double(node, &lane_mask, 1, float_attrs, int_attrs);
1604 }
1605
1606 // -----------------------------------------------------------------------
1607 // eval_bits / eval_int (Exact 64-bit integer & bitmask evaluator)
1608 // -----------------------------------------------------------------------
1609 static uint64_t double_to_bits_safe(double d) {
1610 if (!std::isfinite(d)) return 0ULL;
1611 if (d < -9223372036854775808.0) return static_cast<uint64_t>(INT64_MIN);
1612 if (d >= 18446744073709551616.0) return ~0ULL;
1613 if (d >= 9223372036854775808.0) return static_cast<uint64_t>(std::trunc(d));
1614 return static_cast<uint64_t>(static_cast<int64_t>(std::trunc(d)));
1615 }
1616
1617 static uint64_t eval_bits(const std::shared_ptr<AstNode>& node,
1618 const uint64_t* lane_words,
1619 size_t num_words,
1620 const AttrMap* node_attrs = nullptr,
1621 const FloatAttrMap* float_attrs = nullptr) {
1622 if (!node) return 0ULL;
1623 switch (node->kind) {
1625 return static_cast<uint64_t>(node->int_val);
1627 return node->bool_val ? 1ULL : 0ULL;
1629 return double_to_bits_safe(node->float_val);
1632 if (node->text == "lane") {
1633 return (lane_words && num_words > 0) ? lane_words[0] : 0ULL;
1634 }
1635 if (node_attrs) {
1636 auto it = node_attrs->find(node->text);
1637 if (it != node_attrs->end()) return static_cast<uint64_t>(it->second);
1638 }
1639 if (float_attrs) {
1640 auto it = float_attrs->find(node->text);
1641 if (it != float_attrs->end()) return double_to_bits_safe(it->second);
1642 }
1643 return 0ULL;
1644 }
1645 case AstKind::UNARY_OP: {
1646 if (node->text == "!") {
1647 return eval_bool(node->children[0], lane_words, num_words, node_attrs, float_attrs) ? 0ULL : 1ULL;
1648 }
1649 if (node->text == "~") {
1650 return ~eval_bits(node->children[0], lane_words, num_words, node_attrs, float_attrs);
1651 }
1652 if (node->text == "-") {
1653 int64_t sv = static_cast<int64_t>(eval_bits(node->children[0], lane_words, num_words, node_attrs, float_attrs));
1654 return static_cast<uint64_t>(0ULL - static_cast<uint64_t>(sv));
1655 }
1656 return eval_bits(node->children[0], lane_words, num_words, node_attrs, float_attrs);
1657 }
1658 case AstKind::TERNARY_OP: {
1659 bool c = eval_bool(node->children[0], lane_words, num_words, node_attrs, float_attrs);
1660 return eval_bits(c ? node->children[1] : node->children[2], lane_words, num_words, node_attrs, float_attrs);
1661 }
1662 case AstKind::BINARY_OP: {
1663 const std::string& op = node->text;
1664 if (op == "=") {
1665 return eval_bits(node->children[1], lane_words, num_words, node_attrs, float_attrs);
1666 }
1667 if (op == "+" || op == "-" || op == "*" || op == "/" || op == "%" ||
1668 op == "&" || op == "|" || op == "^" || op == "<<" || op == ">>" || op == ">>>") {
1669 uint64_t ua = eval_bits(node->children[0], lane_words, num_words, node_attrs, float_attrs);
1670 uint64_t ub = eval_bits(node->children[1], lane_words, num_words, node_attrs, float_attrs);
1671 int64_t ia = static_cast<int64_t>(ua);
1672 int64_t ib = static_cast<int64_t>(ub);
1673 if (op == "+") return ua + ub;
1674 if (op == "-") return ua - ub;
1675 if (op == "*") return ua * ub;
1676 if (op == "/") return (ib == 0) ? 0ULL : (ib == -1 ? (0ULL - ua) : static_cast<uint64_t>(ia / ib));
1677 if (op == "%") return (ib == 0 || ib == -1) ? 0ULL : static_cast<uint64_t>(ia % ib);
1678 if (op == "&") return ua & ub;
1679 if (op == "|") return ua | ub;
1680 if (op == "^") return ua ^ ub;
1681 if (op == "<<") return (ib < 0 || ib >= 64) ? 0ULL : (ua << ib);
1682 if (op == ">>") return (ib < 0 || ib >= 64) ? 0ULL : static_cast<uint64_t>(ia >> ib);
1683 if (op == ">>>") return (ib < 0 || ib >= 64) ? 0ULL : (ua >> ib);
1684 }
1685 return double_to_bits_safe(eval_double(node, lane_words, num_words, float_attrs, node_attrs));
1686 }
1688 const std::string& fn = node->text;
1689 if ((fn == "popcount" || fn == "bit_count") && !node->children.empty()) {
1690 if (num_words > 1 && (node->children[0]->text == "lane" ||
1691 (node->children[0]->kind == AstKind::MEMBER_ACCESS && node->children[0]->text == "lane"))) {
1692 size_t total = 0;
1693 for (size_t w = 0; w < num_words; ++w) total += std::popcount(lane_words[w]);
1694 return static_cast<uint64_t>(total);
1695 }
1696 uint64_t v = eval_bits(node->children[0], lane_words, num_words, node_attrs, float_attrs);
1697 return static_cast<uint64_t>(std::popcount(v));
1698 }
1699 if ((fn == "first_lane" || fn == "ctz") && !node->children.empty()) {
1700 if (num_words > 1 && (node->children[0]->text == "lane" ||
1701 (node->children[0]->kind == AstKind::MEMBER_ACCESS && node->children[0]->text == "lane"))) {
1702 for (size_t w = 0; w < num_words; ++w) {
1703 if (lane_words[w] != 0ULL) {
1704 return static_cast<uint64_t>(w * 64 + std::countr_zero(lane_words[w]));
1705 }
1706 }
1707 return static_cast<uint64_t>(-1LL);
1708 }
1709 uint64_t v = eval_bits(node->children[0], lane_words, num_words, node_attrs, float_attrs);
1710 return (v == 0ULL) ? static_cast<uint64_t>(-1LL) : static_cast<uint64_t>(std::countr_zero(v));
1711 }
1712 if (fn == "last_lane" && !node->children.empty()) {
1713 if (num_words > 1 && (node->children[0]->text == "lane" ||
1714 (node->children[0]->kind == AstKind::MEMBER_ACCESS && node->children[0]->text == "lane"))) {
1715 for (size_t w = num_words; w > 0; --w) {
1716 if (lane_words[w - 1] != 0ULL) {
1717 return static_cast<uint64_t>((w - 1) * 64 + 63 - std::countl_zero(lane_words[w - 1]));
1718 }
1719 }
1720 return static_cast<uint64_t>(-1LL);
1721 }
1722 uint64_t v = eval_bits(node->children[0], lane_words, num_words, node_attrs, float_attrs);
1723 return (v == 0ULL) ? static_cast<uint64_t>(-1LL) : static_cast<uint64_t>(63 - std::countl_zero(v));
1724 }
1725 if (fn == "clz" && !node->children.empty()) {
1726 uint64_t v = eval_bits(node->children[0], lane_words, num_words, node_attrs, float_attrs);
1727 return (v == 0ULL) ? 64ULL : static_cast<uint64_t>(std::countl_zero(v));
1728 }
1729 if (fn == "has_any" || fn == "has_all" || fn == "has_none" || fn == "has_lane") {
1730 return eval_bool(node, lane_words, num_words, node_attrs, float_attrs) ? 1ULL : 0ULL;
1731 }
1732 return double_to_bits_safe(eval_double(node, lane_words, num_words, float_attrs, node_attrs));
1733 }
1734 default:
1735 return double_to_bits_safe(eval_double(node, lane_words, num_words, float_attrs, node_attrs));
1736 }
1737 }
1738
1739 static int64_t eval_int(const std::shared_ptr<AstNode>& node,
1740 const uint64_t* lane_words,
1741 size_t num_words,
1742 const AttrMap* node_attrs = nullptr,
1743 const FloatAttrMap* float_attrs = nullptr) {
1744 return static_cast<int64_t>(eval_bits(node, lane_words, num_words, node_attrs, float_attrs));
1745 }
1746
1747 static int64_t eval_int(const std::shared_ptr<AstNode>& node,
1748 uint64_t lane_mask,
1749 const AttrMap* node_attrs = nullptr,
1750 const FloatAttrMap* float_attrs = nullptr) {
1751 return static_cast<int64_t>(eval_bits(node, &lane_mask, 1, node_attrs, float_attrs));
1752 }
1753
1754 // -----------------------------------------------------------------------
1755 // eval_bool
1756 // -----------------------------------------------------------------------
1757 static bool eval_bool(const std::shared_ptr<AstNode>& node,
1758 const uint64_t* lane_words,
1759 size_t num_words,
1760 const AttrMap* node_attrs = nullptr,
1761 const FloatAttrMap* float_attrs = nullptr) {
1762 if (!node) return false;
1763 switch (node->kind) {
1765 return node->bool_val;
1767 return node->int_val != 0;
1769 return node->float_val != 0.0;
1770 case AstKind::UNARY_OP:
1771 if (node->text == "!") return !eval_bool(node->children[0], lane_words, num_words, node_attrs, float_attrs);
1772 if (is_float_expr(node, float_attrs)) {
1773 return eval_double(node, lane_words, num_words, float_attrs, node_attrs) != 0.0;
1774 }
1775 return eval_int(node, lane_words, num_words, node_attrs, float_attrs) != 0;
1777 return eval_bool(eval_bool(node->children[0], lane_words, num_words, node_attrs, float_attrs)
1778 ? node->children[1]
1779 : node->children[2],
1780 lane_words, num_words, node_attrs, float_attrs);
1781 case AstKind::BINARY_OP: {
1782 const std::string& op = node->text;
1783 if (op == "&&") {
1784 return eval_bool(node->children[0], lane_words, num_words, node_attrs, float_attrs) &&
1785 eval_bool(node->children[1], lane_words, num_words, node_attrs, float_attrs);
1786 }
1787 if (op == "||") {
1788 return eval_bool(node->children[0], lane_words, num_words, node_attrs, float_attrs) ||
1789 eval_bool(node->children[1], lane_words, num_words, node_attrs, float_attrs);
1790 }
1791 if (op == "in") {
1792 const auto& rhs = node->children[1];
1793 if (rhs && rhs->kind == AstKind::LIST_LITERAL) {
1794 if (is_float_expr(node, float_attrs)) {
1795 double val = eval_double(node->children[0], lane_words, num_words, float_attrs, node_attrs);
1796 for (const auto& elem : rhs->children) {
1797 double elem_val = eval_double(elem, lane_words, num_words, float_attrs, node_attrs);
1798 if (std::abs(val - elem_val) < 1e-9) return true;
1799 }
1800 } else {
1801 int64_t val = eval_int(node->children[0], lane_words, num_words, node_attrs, float_attrs);
1802 for (const auto& elem : rhs->children) {
1803 if (val == eval_int(elem, lane_words, num_words, node_attrs, float_attrs)) return true;
1804 }
1805 }
1806 }
1807 return false;
1808 }
1809 if (is_float_expr(node->children[0], float_attrs) || is_float_expr(node->children[1], float_attrs)) {
1810 double a = eval_double(node->children[0], lane_words, num_words, float_attrs, node_attrs);
1811 double b = eval_double(node->children[1], lane_words, num_words, float_attrs, node_attrs);
1812 if (op == "==") return std::abs(a - b) < 1e-9;
1813 if (op == "!=") return std::abs(a - b) >= 1e-9;
1814 if (op == "<") return a < b;
1815 if (op == "<=") return a <= b;
1816 if (op == ">") return a > b;
1817 if (op == ">=") return a >= b;
1818 return eval_double(node, lane_words, num_words, float_attrs, node_attrs) != 0.0;
1819 }
1820 int64_t a = eval_int(node->children[0], lane_words, num_words, node_attrs, float_attrs);
1821 int64_t b = eval_int(node->children[1], lane_words, num_words, node_attrs, float_attrs);
1822 if (op == "==") return a == b;
1823 if (op == "!=") return a != b;
1824 if (op == "<") return a < b;
1825 if (op == "<=") return a <= b;
1826 if (op == ">") return a > b;
1827 if (op == ">=") return a >= b;
1828 return eval_int(node, lane_words, num_words, node_attrs, float_attrs) != 0;
1829 }
1831 const std::string& fn = node->text;
1832 if (fn == "has_any" && !node->children.empty()) {
1833 if (node->children.size() == 1 ||
1834 node->children[0]->text == "lane" ||
1835 (node->children[0]->kind == AstKind::MEMBER_ACCESS && node->children[0]->text == "lane")) {
1836 if (node->children.size() >= 2) {
1837 uint64_t req = eval_bits(node->children[1], lane_words, num_words, node_attrs, float_attrs);
1838 return ((lane_words && num_words > 0 ? lane_words[0] : 0ULL) & req) != 0ULL;
1839 }
1840 for (size_t w = 0; w < num_words; ++w) {
1841 if (lane_words[w] != 0ULL) return true;
1842 }
1843 return false;
1844 }
1845 uint64_t m = eval_bits(node->children[0], lane_words, num_words, node_attrs, float_attrs);
1846 uint64_t req = eval_bits(node->children[1], lane_words, num_words, node_attrs, float_attrs);
1847 return (m & req) != 0ULL;
1848 }
1849 if (fn == "has_all" && node->children.size() >= 2) {
1850 uint64_t m = (node->children[0]->text == "lane") ? (lane_words && num_words > 0 ? lane_words[0] : 0ULL)
1851 : eval_bits(node->children[0], lane_words, num_words, node_attrs, float_attrs);
1852 uint64_t req = eval_bits(node->children[1], lane_words, num_words, node_attrs, float_attrs);
1853 return (m & req) == req;
1854 }
1855 if (fn == "has_none" && node->children.size() >= 2) {
1856 uint64_t m = (node->children[0]->text == "lane") ? (lane_words && num_words > 0 ? lane_words[0] : 0ULL)
1857 : eval_bits(node->children[0], lane_words, num_words, node_attrs, float_attrs);
1858 uint64_t forbid = eval_bits(node->children[1], lane_words, num_words, node_attrs, float_attrs);
1859 return (m & forbid) == 0ULL;
1860 }
1861 if (fn == "has_lane" && node->children.size() >= 2) {
1862 int64_t idx = eval_int(node->children[1], lane_words, num_words, node_attrs, float_attrs);
1863 if (idx < 0) return false;
1864 if (num_words > 1 && (node->children[0]->text == "lane" ||
1865 (node->children[0]->kind == AstKind::MEMBER_ACCESS && node->children[0]->text == "lane"))) {
1866 size_t w = static_cast<size_t>(idx) / 64;
1867 size_t b = static_cast<size_t>(idx) % 64;
1868 if (w < num_words) return ((lane_words[w] >> b) & 1ULL) != 0ULL;
1869 return false;
1870 }
1871 if (idx >= 64) return false;
1872 uint64_t m = static_cast<uint64_t>(eval_int(node->children[0], lane_words, num_words, node_attrs, float_attrs));
1873 return ((m >> idx) & 1ULL) != 0ULL;
1874 }
1875 if (is_float_expr(node, float_attrs)) {
1876 return eval_double(node, lane_words, num_words, float_attrs, node_attrs) != 0.0;
1877 }
1878 return eval_int(node, lane_words, num_words, node_attrs, float_attrs) != 0;
1879 }
1880 default:
1881 if (is_float_expr(node, float_attrs)) {
1882 return eval_double(node, lane_words, num_words, float_attrs, node_attrs) != 0.0;
1883 }
1884 return eval_int(node, lane_words, num_words, node_attrs, float_attrs) != 0;
1885 }
1886 }
1887
1888 static bool eval_bool(const std::shared_ptr<AstNode>& node,
1889 uint64_t lane_mask,
1890 const AttrMap* node_attrs = nullptr,
1891 const FloatAttrMap* float_attrs = nullptr) {
1892 return eval_bool(node, &lane_mask, 1, node_attrs, float_attrs);
1893 }
1894
1895 // -----------------------------------------------------------------------
1896 // eval_project_lane
1897 // -----------------------------------------------------------------------
1898 static uint64_t eval_project_lane(const std::shared_ptr<AstNode>& node,
1899 uint64_t lane_mask,
1900 const AttrMap* node_attrs = nullptr,
1901 const FloatAttrMap* float_attrs = nullptr) {
1902 return static_cast<uint64_t>(eval_int(node, lane_mask, node_attrs, float_attrs));
1903 }
1904
1905 static void eval_project_lanes(const std::shared_ptr<AstNode>& node,
1906 uint64_t* lane_words,
1907 size_t num_words,
1908 const AttrMap* node_attrs = nullptr,
1909 const FloatAttrMap* float_attrs = nullptr) {
1910 if (!lane_words || num_words == 0) return;
1911 lane_words[0] = static_cast<uint64_t>(eval_int(node, lane_words, num_words, node_attrs, float_attrs));
1912 }
1913};
1914
1918struct CelValue {
1919 enum class Kind : uint8_t { Null = 0, Bool, Int64, Uint64, Double, String };
1920
1922 bool bool_val = false;
1923 bool uses_owned_str = false;
1924 int64_t int_val = 0;
1925 uint64_t uint_val = 0;
1926 double dbl_val = 0.0;
1927 std::string_view str_view{};
1928 std::string str_owned{};
1929
1930 static CelValue make_null() { return CelValue{}; }
1931 static CelValue make_bool(bool b) {
1932 CelValue v;
1933 v.kind = Kind::Bool;
1934 v.bool_val = b;
1935 return v;
1936 }
1937 static CelValue make_int(int64_t x) {
1938 CelValue v;
1939 v.kind = Kind::Int64;
1940 v.int_val = x;
1941 return v;
1942 }
1943 static CelValue make_uint(uint64_t x) {
1944 CelValue v;
1945 v.kind = Kind::Uint64;
1946 v.uint_val = x;
1947 return v;
1948 }
1949 static CelValue make_double(double x) {
1950 if (std::isnan(x)) return make_null();
1951 CelValue v;
1952 v.kind = Kind::Double;
1953 v.dbl_val = x;
1954 return v;
1955 }
1956 static CelValue make_string_view(std::string_view sv) {
1957 CelValue v;
1958 v.kind = Kind::String;
1959 v.uses_owned_str = false;
1960 v.str_view = sv;
1961 return v;
1962 }
1963 static CelValue make_string(std::string s) {
1964 CelValue v;
1965 v.kind = Kind::String;
1966 v.uses_owned_str = true;
1967 v.str_owned = std::move(s);
1968 return v;
1969 }
1970
1971 bool is_null() const { return kind == Kind::Null; }
1972
1973 std::string_view as_string_view() const {
1974 return uses_owned_str ? std::string_view(str_owned) : str_view;
1975 }
1976
1977 bool to_double(double& out) const {
1978 switch (kind) {
1979 case Kind::Bool: out = bool_val ? 1.0 : 0.0; return true;
1980 case Kind::Int64: out = static_cast<double>(int_val); return true;
1981 case Kind::Uint64: out = static_cast<double>(uint_val); return true;
1982 case Kind::Double:
1983 if (std::isnan(dbl_val)) return false;
1984 out = dbl_val;
1985 return true;
1986 default:
1987 return false;
1988 }
1989 }
1990
1991 bool to_int64(int64_t& out) const {
1992 switch (kind) {
1993 case Kind::Bool: out = bool_val ? 1 : 0; return true;
1994 case Kind::Int64: out = int_val; return true;
1995 case Kind::Uint64: out = static_cast<int64_t>(uint_val); return true;
1996 case Kind::Double:
1997 if (!std::isfinite(dbl_val) || dbl_val < -9223372036854775808.0 || dbl_val >= 9223372036854775808.0) return false;
1998 out = static_cast<int64_t>(std::trunc(dbl_val));
1999 return true;
2000 default:
2001 return false;
2002 }
2003 }
2004
2005 // Returns 1 = true, 0 = false, -1 = Kleene Unknown (NULL)
2006 int to_tribool() const {
2007 switch (kind) {
2008 case Kind::Null: return -1;
2009 case Kind::Bool: return bool_val ? 1 : 0;
2010 case Kind::Int64: return (int_val != 0) ? 1 : 0;
2011 case Kind::Uint64: return (uint_val != 0) ? 1 : 0;
2012 case Kind::Double: return std::isnan(dbl_val) ? -1 : ((dbl_val != 0.0) ? 1 : 0);
2013 case Kind::String: return as_string_view().empty() ? 0 : 1;
2014 }
2015 return -1;
2016 }
2017};
2018
2023public:
2024 static CelValue compare_values(const std::string& op, const CelValue& a, const CelValue& b) {
2025 if (a.is_null() || b.is_null()) return CelValue::make_null();
2026
2029 if (op == "==") return CelValue::make_bool(false);
2030 if (op == "!=") return CelValue::make_bool(true);
2031 return CelValue::make_null();
2032 }
2033 std::string_view sa = a.as_string_view();
2034 std::string_view sb = b.as_string_view();
2035 if (op == "==") return CelValue::make_bool(sa == sb);
2036 if (op == "!=") return CelValue::make_bool(sa != sb);
2037 if (op == "<") return CelValue::make_bool(sa < sb);
2038 if (op == "<=") return CelValue::make_bool(sa <= sb);
2039 if (op == ">") return CelValue::make_bool(sa > sb);
2040 if (op == ">=") return CelValue::make_bool(sa >= sb);
2041 return CelValue::make_null();
2042 }
2043
2045 if (op == "==") return CelValue::make_bool(a.bool_val == b.bool_val);
2046 if (op == "!=") return CelValue::make_bool(a.bool_val != b.bool_val);
2047 if (op == "<") return CelValue::make_bool(!a.bool_val && b.bool_val);
2048 if (op == "<=") return CelValue::make_bool(!a.bool_val || b.bool_val);
2049 if (op == ">") return CelValue::make_bool(a.bool_val && !b.bool_val);
2050 if (op == ">=") return CelValue::make_bool(a.bool_val || !b.bool_val);
2051 return CelValue::make_null();
2052 }
2053
2054 // Exact integer / bool comparisons (avoids double precision loss on int64 PKs & timestamps)
2056 auto as_signed_or_unsigned = [](const CelValue& v, bool& is_neg, uint64_t& mag) {
2057 if (v.kind == CelValue::Kind::Bool) {
2058 is_neg = false;
2059 mag = v.bool_val ? 1ULL : 0ULL;
2060 } else if (v.kind == CelValue::Kind::Uint64) {
2061 is_neg = false;
2062 mag = v.uint_val;
2063 } else {
2064 if (v.int_val < 0) {
2065 is_neg = true;
2066 mag = static_cast<uint64_t>(-(v.int_val + 1)) + 1ULL;
2067 } else {
2068 is_neg = false;
2069 mag = static_cast<uint64_t>(v.int_val);
2070 }
2071 }
2072 };
2073 bool neg_a = false, neg_b = false;
2074 uint64_t mag_a = 0, mag_b = 0;
2075 as_signed_or_unsigned(a, neg_a, mag_a);
2076 as_signed_or_unsigned(b, neg_b, mag_b);
2077 int cmp = 0;
2078 if (neg_a != neg_b) {
2079 cmp = neg_a ? -1 : 1;
2080 } else if (neg_a) {
2081 cmp = (mag_a > mag_b) ? -1 : ((mag_a < mag_b) ? 1 : 0);
2082 } else {
2083 cmp = (mag_a < mag_b) ? -1 : ((mag_a > mag_b) ? 1 : 0);
2084 }
2085 if (op == "==") return CelValue::make_bool(cmp == 0);
2086 if (op == "!=") return CelValue::make_bool(cmp != 0);
2087 if (op == "<") return CelValue::make_bool(cmp < 0);
2088 if (op == "<=") return CelValue::make_bool(cmp <= 0);
2089 if (op == ">") return CelValue::make_bool(cmp > 0);
2090 if (op == ">=") return CelValue::make_bool(cmp >= 0);
2091 return CelValue::make_null();
2092 }
2093
2094 double da = 0.0, db = 0.0;
2095 if (!a.to_double(da) || !b.to_double(db)) return CelValue::make_null();
2096 if (op == "==") return CelValue::make_bool(da == db);
2097 if (op == "!=") return CelValue::make_bool(da != db);
2098 if (op == "<") return CelValue::make_bool(da < db);
2099 if (op == "<=") return CelValue::make_bool(da <= db);
2100 if (op == ">") return CelValue::make_bool(da > db);
2101 if (op == ">=") return CelValue::make_bool(da >= db);
2102 return CelValue::make_null();
2103 }
2104
2105 template <typename ResolveAttrFn>
2107 const std::shared_ptr<AstNode>& node,
2108 ResolveAttrFn&& resolve_attr,
2109 const uint64_t* lane_words = nullptr,
2110 size_t num_lane_words = 0
2111 ) {
2112 if (!node) return CelValue::make_null();
2113 switch (node->kind) {
2115 return CelValue::make_bool(node->bool_val);
2117 return CelValue::make_int(node->int_val);
2119 return CelValue::make_double(node->float_val);
2121 return CelValue::make_string_view(node->str_val);
2124 if (node->text == "null" || node->text == "NULL") {
2125 return CelValue::make_null();
2126 }
2127 if (node->text == "lane") {
2128 uint64_t lw = (lane_words && num_lane_words > 0) ? lane_words[0] : 0ULL;
2129 return CelValue::make_int(static_cast<int64_t>(lw));
2130 }
2131 if (node->kind == AstKind::MEMBER_ACCESS && !node->children.empty() &&
2132 node->children[0] && node->children[0]->kind == AstKind::IDENTIFIER &&
2133 (node->children[0]->text == "edge" || node->children[0]->text == "node" || node->children[0]->text == "self")) {
2134 return resolve_attr(node->text);
2135 }
2136 return resolve_attr(node->text);
2137 }
2138 case AstKind::UNARY_OP: {
2139 CelValue sub = eval(node->children[0], resolve_attr, lane_words, num_lane_words);
2140 if (node->text == "!") {
2141 int tb = sub.to_tribool();
2142 if (tb < 0) return CelValue::make_null();
2143 return CelValue::make_bool(tb == 0);
2144 }
2145 if (sub.is_null()) return CelValue::make_null();
2146 if (node->text == "+") return sub;
2147 if (node->text == "-") {
2148 if (sub.kind == CelValue::Kind::Int64) return CelValue::make_int(-sub.int_val);
2149 if (sub.kind == CelValue::Kind::Uint64) return CelValue::make_double(-static_cast<double>(sub.uint_val));
2150 double dv = 0.0;
2151 if (sub.to_double(dv)) return CelValue::make_double(-dv);
2152 return CelValue::make_null();
2153 }
2154 if (node->text == "~") {
2155 int64_t iv = 0;
2156 if (sub.to_int64(iv)) return CelValue::make_int(~iv);
2157 return CelValue::make_null();
2158 }
2159 return CelValue::make_null();
2160 }
2161 case AstKind::TERNARY_OP: {
2162 CelValue c = eval(node->children[0], resolve_attr, lane_words, num_lane_words);
2163 int tb = c.to_tribool();
2164 if (tb < 0) return CelValue::make_null();
2165 return eval(tb ? node->children[1] : node->children[2], resolve_attr, lane_words, num_lane_words);
2166 }
2167 case AstKind::BINARY_OP: {
2168 const std::string& op = node->text;
2169 if (op == "=") {
2170 return eval(node->children[1], resolve_attr, lane_words, num_lane_words);
2171 }
2172 if (op == "&&") {
2173 int tl = eval(node->children[0], resolve_attr, lane_words, num_lane_words).to_tribool();
2174 if (tl == 0) return CelValue::make_bool(false);
2175 int tr = eval(node->children[1], resolve_attr, lane_words, num_lane_words).to_tribool();
2176 if (tr == 0) return CelValue::make_bool(false);
2177 if (tl == 1 && tr == 1) return CelValue::make_bool(true);
2178 return CelValue::make_null();
2179 }
2180 if (op == "||") {
2181 int tl = eval(node->children[0], resolve_attr, lane_words, num_lane_words).to_tribool();
2182 if (tl == 1) return CelValue::make_bool(true);
2183 int tr = eval(node->children[1], resolve_attr, lane_words, num_lane_words).to_tribool();
2184 if (tr == 1) return CelValue::make_bool(true);
2185 if (tl == 0 && tr == 0) return CelValue::make_bool(false);
2186 return CelValue::make_null();
2187 }
2188 if (op == "in") {
2189 const auto& rhs = node->children[1];
2190 if (!rhs || rhs->kind != AstKind::LIST_LITERAL) return CelValue::make_bool(false);
2191 if (rhs->children.empty()) return CelValue::make_bool(false);
2192 CelValue lhs = eval(node->children[0], resolve_attr, lane_words, num_lane_words);
2193 if (lhs.is_null()) return CelValue::make_null();
2194 bool saw_null = false;
2195 for (const auto& elem_node : rhs->children) {
2196 CelValue elem = eval(elem_node, resolve_attr, lane_words, num_lane_words);
2197 CelValue eq = compare_values("==", lhs, elem);
2198 if (eq.is_null()) saw_null = true;
2199 else if (eq.bool_val) return CelValue::make_bool(true);
2200 }
2201 return saw_null ? CelValue::make_null() : CelValue::make_bool(false);
2202 }
2203 if (op == "==" || op == "!=" || op == "<" || op == "<=" || op == ">" || op == ">=") {
2204 CelValue lhs = eval(node->children[0], resolve_attr, lane_words, num_lane_words);
2205 if (lhs.is_null()) return CelValue::make_null();
2206 CelValue rhs = eval(node->children[1], resolve_attr, lane_words, num_lane_words);
2207 if (rhs.is_null()) return CelValue::make_null();
2208 return compare_values(op, lhs, rhs);
2209 }
2210
2211 CelValue lhs = eval(node->children[0], resolve_attr, lane_words, num_lane_words);
2212 if (lhs.is_null()) return CelValue::make_null();
2213 CelValue rhs = eval(node->children[1], resolve_attr, lane_words, num_lane_words);
2214 if (rhs.is_null()) return CelValue::make_null();
2215
2216 if (op == "+" && lhs.kind == CelValue::Kind::String && rhs.kind == CelValue::Kind::String) {
2217 std::string s(lhs.as_string_view());
2218 s.append(rhs.as_string_view());
2219 return CelValue::make_string(std::move(s));
2220 }
2221
2222 bool both_int = (lhs.kind == CelValue::Kind::Int64 || lhs.kind == CelValue::Kind::Bool) &&
2224 if (both_int && (op == "+" || op == "-" || op == "*" || op == "%")) {
2225 int64_t ia = 0, ib = 0;
2226 lhs.to_int64(ia);
2227 rhs.to_int64(ib);
2228 if (op == "+") return CelValue::make_int(ia + ib);
2229 if (op == "-") return CelValue::make_int(ia - ib);
2230 if (op == "*") return CelValue::make_int(ia * ib);
2231 if (op == "%") return (ib == 0) ? CelValue::make_null() : CelValue::make_int(ia % ib);
2232 }
2233
2234 if (op == "+" || op == "-" || op == "*" || op == "/" || op == "%") {
2235 double da = 0.0, db = 0.0;
2236 if (!lhs.to_double(da) || !rhs.to_double(db)) return CelValue::make_null();
2237 if (op == "+") return CelValue::make_double(da + db);
2238 if (op == "-") return CelValue::make_double(da - db);
2239 if (op == "*") return CelValue::make_double(da * db);
2240 if (op == "/") return (db == 0.0) ? CelValue::make_null() : CelValue::make_double(da / db);
2241 if (op == "%") return (db == 0.0) ? CelValue::make_null() : CelValue::make_double(std::fmod(da, db));
2242 }
2243
2244 int64_t ia = 0, ib = 0;
2245 if (lhs.to_int64(ia) && rhs.to_int64(ib)) {
2246 if (op == "&") return CelValue::make_int(ia & ib);
2247 if (op == "|") return CelValue::make_int(ia | ib);
2248 if (op == "^") return CelValue::make_int(ia ^ ib);
2249 if (op == "<<") return CelValue::make_int((ib < 0 || ib >= 64) ? 0 : static_cast<int64_t>(static_cast<uint64_t>(ia) << ib));
2250 if (op == ">>") return CelValue::make_int((ib < 0 || ib >= 64) ? 0 : (ia >> ib));
2251 if (op == ">>>") return CelValue::make_int((ib < 0 || ib >= 64) ? 0 : static_cast<int64_t>(static_cast<uint64_t>(ia) >> ib));
2252 }
2253 return CelValue::make_null();
2254 }
2256 const std::string& fn = node->text;
2257 if ((fn == "is_null" || fn == "isNull") && node->children.size() == 1) {
2258 CelValue v = eval(node->children[0], resolve_attr, lane_words, num_lane_words);
2259 return CelValue::make_bool(v.is_null());
2260 }
2261 if ((fn == "is_not_null" || fn == "isNotNull") && node->children.size() == 1) {
2262 CelValue v = eval(node->children[0], resolve_attr, lane_words, num_lane_words);
2263 return CelValue::make_bool(!v.is_null());
2264 }
2265 if ((fn == "starts_with" || fn == "startsWith") && node->children.size() == 2) {
2266 CelValue s = eval(node->children[0], resolve_attr, lane_words, num_lane_words);
2267 if (s.is_null()) return CelValue::make_null();
2268 CelValue p = eval(node->children[1], resolve_attr, lane_words, num_lane_words);
2269 if (p.is_null()) return CelValue::make_null();
2271 return CelValue::make_null();
2272 }
2273 std::string_view sv = s.as_string_view();
2274 std::string_view pv = p.as_string_view();
2275 return CelValue::make_bool(sv.size() >= pv.size() && sv.substr(0, pv.size()) == pv);
2276 }
2277 if ((fn == "ends_with" || fn == "endsWith") && node->children.size() == 2) {
2278 CelValue s = eval(node->children[0], resolve_attr, lane_words, num_lane_words);
2279 if (s.is_null()) return CelValue::make_null();
2280 CelValue p = eval(node->children[1], resolve_attr, lane_words, num_lane_words);
2281 if (p.is_null()) return CelValue::make_null();
2283 return CelValue::make_null();
2284 }
2285 std::string_view sv = s.as_string_view();
2286 std::string_view pv = p.as_string_view();
2287 return CelValue::make_bool(sv.size() >= pv.size() && sv.substr(sv.size() - pv.size()) == pv);
2288 }
2289 if (fn == "contains" && node->children.size() == 2) {
2290 CelValue s = eval(node->children[0], resolve_attr, lane_words, num_lane_words);
2291 if (s.is_null()) return CelValue::make_null();
2292 CelValue p = eval(node->children[1], resolve_attr, lane_words, num_lane_words);
2293 if (p.is_null()) return CelValue::make_null();
2295 return CelValue::make_null();
2296 }
2297 return CelValue::make_bool(s.as_string_view().find(p.as_string_view()) != std::string_view::npos);
2298 }
2299
2300 // Multi-lane intrinsics
2301 if ((fn == "popcount" || fn == "bit_count") && !node->children.empty()) {
2302 if (node->children[0]->text == "lane") {
2303 int64_t total = 0;
2304 for (size_t w = 0; w < num_lane_words; ++w) total += std::popcount(lane_words[w]);
2305 return CelValue::make_int(total);
2306 }
2307 CelValue v = eval(node->children[0], resolve_attr, lane_words, num_lane_words);
2308 int64_t iv = 0;
2309 if (!v.to_int64(iv)) return CelValue::make_null();
2310 return CelValue::make_int(std::popcount(static_cast<uint64_t>(iv)));
2311 }
2312 if ((fn == "first_lane" || fn == "ctz") && !node->children.empty()) {
2313 if (node->children[0]->text == "lane") {
2314 for (size_t w = 0; w < num_lane_words; ++w) {
2315 if (lane_words[w] != 0ULL) {
2316 return CelValue::make_int(static_cast<int64_t>(w * 64 + std::countr_zero(lane_words[w])));
2317 }
2318 }
2319 return CelValue::make_int(-1);
2320 }
2321 CelValue v = eval(node->children[0], resolve_attr, lane_words, num_lane_words);
2322 int64_t iv = 0;
2323 if (!v.to_int64(iv)) return CelValue::make_null();
2324 uint64_t uv = static_cast<uint64_t>(iv);
2325 return CelValue::make_int(uv == 0ULL ? -1 : static_cast<int64_t>(std::countr_zero(uv)));
2326 }
2327 if (fn == "last_lane" && !node->children.empty()) {
2328 if (node->children[0]->text == "lane") {
2329 for (size_t w = num_lane_words; w > 0; --w) {
2330 if (lane_words[w - 1] != 0ULL) {
2331 return CelValue::make_int(static_cast<int64_t>((w - 1) * 64 + 63 - std::countl_zero(lane_words[w - 1])));
2332 }
2333 }
2334 return CelValue::make_int(-1);
2335 }
2336 CelValue v = eval(node->children[0], resolve_attr, lane_words, num_lane_words);
2337 int64_t iv = 0;
2338 if (!v.to_int64(iv)) return CelValue::make_null();
2339 uint64_t uv = static_cast<uint64_t>(iv);
2340 return CelValue::make_int(uv == 0ULL ? -1 : static_cast<int64_t>(63 - std::countl_zero(uv)));
2341 }
2342 if (fn == "clz" && !node->children.empty()) {
2343 CelValue v = eval(node->children[0], resolve_attr, lane_words, num_lane_words);
2344 int64_t iv = 0;
2345 if (!v.to_int64(iv)) return CelValue::make_null();
2346 uint64_t uv = static_cast<uint64_t>(iv);
2347 return CelValue::make_int(uv == 0ULL ? 64 : static_cast<int64_t>(std::countl_zero(uv)));
2348 }
2349 if (fn == "has_any" && !node->children.empty()) {
2350 if (node->children.size() == 1 && node->children[0]->text == "lane") {
2351 for (size_t w = 0; w < num_lane_words; ++w) {
2352 if (lane_words[w] != 0ULL) return CelValue::make_bool(true);
2353 }
2354 return CelValue::make_bool(false);
2355 }
2356 int64_t m = 0, req = 0;
2357 if (node->children[0]->text == "lane") {
2358 m = static_cast<int64_t>((lane_words && num_lane_words > 0) ? lane_words[0] : 0ULL);
2359 } else if (!eval(node->children[0], resolve_attr, lane_words, num_lane_words).to_int64(m)) {
2360 return CelValue::make_null();
2361 }
2362 if (node->children.size() >= 2) {
2363 if (!eval(node->children[1], resolve_attr, lane_words, num_lane_words).to_int64(req)) {
2364 return CelValue::make_null();
2365 }
2366 return CelValue::make_bool((static_cast<uint64_t>(m) & static_cast<uint64_t>(req)) != 0ULL);
2367 }
2368 return CelValue::make_bool(m != 0);
2369 }
2370 if (fn == "has_all" && node->children.size() >= 2) {
2371 int64_t m = 0, req = 0;
2372 if (node->children[0]->text == "lane") {
2373 m = static_cast<int64_t>((lane_words && num_lane_words > 0) ? lane_words[0] : 0ULL);
2374 } else if (!eval(node->children[0], resolve_attr, lane_words, num_lane_words).to_int64(m)) {
2375 return CelValue::make_null();
2376 }
2377 if (!eval(node->children[1], resolve_attr, lane_words, num_lane_words).to_int64(req)) {
2378 return CelValue::make_null();
2379 }
2380 return CelValue::make_bool((static_cast<uint64_t>(m) & static_cast<uint64_t>(req)) == static_cast<uint64_t>(req));
2381 }
2382 if (fn == "has_none" && node->children.size() >= 2) {
2383 int64_t m = 0, forbid = 0;
2384 if (node->children[0]->text == "lane") {
2385 m = static_cast<int64_t>((lane_words && num_lane_words > 0) ? lane_words[0] : 0ULL);
2386 } else if (!eval(node->children[0], resolve_attr, lane_words, num_lane_words).to_int64(m)) {
2387 return CelValue::make_null();
2388 }
2389 if (!eval(node->children[1], resolve_attr, lane_words, num_lane_words).to_int64(forbid)) {
2390 return CelValue::make_null();
2391 }
2392 return CelValue::make_bool((static_cast<uint64_t>(m) & static_cast<uint64_t>(forbid)) == 0ULL);
2393 }
2394 if (fn == "has_lane" && node->children.size() >= 2) {
2395 int64_t idx = 0;
2396 if (!eval(node->children[1], resolve_attr, lane_words, num_lane_words).to_int64(idx)) {
2397 return CelValue::make_null();
2398 }
2399 if (idx < 0) return CelValue::make_bool(false);
2400 size_t w = static_cast<size_t>(idx) / 64;
2401 size_t b = static_cast<size_t>(idx) % 64;
2402 if (lane_words && w < num_lane_words) {
2403 return CelValue::make_bool(((lane_words[w] >> b) & 1ULL) != 0ULL);
2404 }
2405 return CelValue::make_bool(false);
2406 }
2407
2408 int func_id = CelCompiler::resolve_math_func(fn);
2409 if (func_id > 0) {
2410 if (node->children.size() == 1) {
2411 double a = 0.0;
2412 if (!eval(node->children[0], resolve_attr, lane_words, num_lane_words).to_double(a)) {
2413 return CelValue::make_null();
2414 }
2415 return CelValue::make_double(impulse_math_unary_f64(static_cast<uint8_t>(func_id), a));
2416 }
2417 if (node->children.size() == 2) {
2418 double a = 0.0, b = 0.0;
2419 if (!eval(node->children[0], resolve_attr, lane_words, num_lane_words).to_double(a) ||
2420 !eval(node->children[1], resolve_attr, lane_words, num_lane_words).to_double(b)) {
2421 return CelValue::make_null();
2422 }
2423 return CelValue::make_double(impulse_math_binary_f64(static_cast<uint8_t>(func_id), a, b));
2424 }
2425 if (node->children.size() == 3) {
2426 double a = 0.0, b = 0.0, c = 0.0;
2427 if (!eval(node->children[0], resolve_attr, lane_words, num_lane_words).to_double(a) ||
2428 !eval(node->children[1], resolve_attr, lane_words, num_lane_words).to_double(b) ||
2429 !eval(node->children[2], resolve_attr, lane_words, num_lane_words).to_double(c)) {
2430 return CelValue::make_null();
2431 }
2432 return CelValue::make_double(impulse_math_ternary_f64(static_cast<uint8_t>(func_id), a, b, c));
2433 }
2434 }
2435 return CelValue::make_null();
2436 }
2437 default:
2438 return CelValue::make_null();
2439 }
2440 }
2441
2442 template <typename ResolveAttrFn>
2443 static bool eval_filter_bool(
2444 const std::shared_ptr<AstNode>& node,
2445 ResolveAttrFn&& resolve_attr,
2446 const uint64_t* lane_words = nullptr,
2447 size_t num_lane_words = 0
2448 ) {
2449 return eval(node, std::forward<ResolveAttrFn>(resolve_attr), lane_words, num_lane_words).to_tribool() == 1;
2450 }
2451};
2452
2453} // namespace cel
2454} // namespace impulse
2455
2456#endif // IMPULSE_CEL_H
Zero-Dependency AST Optimizer & Constant Folder for CEL ASTs.
Definition impulse_cel.h:1142
static std::shared_ptr< AstNode > optimize(const std::shared_ptr< AstNode > &node, size_t depth=0)
Definition impulse_cel.h:1146
static constexpr size_t MAX_PARSE_DEPTH
Definition impulse_cel.h:1144
Definition impulse_cel.h:672
static bool compile_stream_shader(const std::shared_ptr< AstNode > &expr, std::vector< impulse_instruction_t > &out_shader, uint16_t out_stream_reg=0, const std::unordered_map< std::string, uint16_t > *attr_name_to_id=nullptr, bool is_filter=false)
Compiles a CEL AST into an Edge Stream Shader instruction sequence. Encloses instructions with OP_STR...
Definition impulse_cel.h:772
static std::string to_impscheme(const std::shared_ptr< AstNode > &node, size_t depth=0)
Definition impulse_cel.h:676
static bool compile_node_vector_expr(const std::shared_ptr< AstNode > &expr, std::vector< impulse_instruction_t > &out_instructions, uint16_t out_reg, uint16_t &next_temp_reg, const std::unordered_map< std::string, uint16_t > *attr_name_to_id=nullptr, uint16_t domain_id=0)
Compiles an in-domain node CEL projection into VM instructions operating on vector registers.
Definition impulse_cel.h:1000
static int resolve_math_func(const std::string &name)
Definition impulse_cel.h:739
static constexpr size_t MAX_PARSE_DEPTH
Definition impulse_cel.h:674
Spec v0.10.0 Kleene 3-valued CEL evaluator for node, edge, and multi-lane predicates.
Definition impulse_cel.h:2022
static CelValue eval(const std::shared_ptr< AstNode > &node, ResolveAttrFn &&resolve_attr, const uint64_t *lane_words=nullptr, size_t num_lane_words=0)
Definition impulse_cel.h:2106
static CelValue compare_values(const std::string &op, const CelValue &a, const CelValue &b)
Definition impulse_cel.h:2024
static bool eval_filter_bool(const std::shared_ptr< AstNode > &node, ResolveAttrFn &&resolve_attr, const uint64_t *lane_words=nullptr, size_t num_lane_words=0)
Definition impulse_cel.h:2443
Runtime evaluator for CEL Multi-Lane predicates (.filter / .pruneWhere) and selective per-lane mask m...
Definition impulse_cel.h:1425
static bool is_float_expr(const std::shared_ptr< AstNode > &node, const FloatAttrMap *float_attrs=nullptr)
Definition impulse_cel.h:1430
std::unordered_map< std::string, int64_t > AttrMap
Definition impulse_cel.h:1427
static bool eval_bool(const std::shared_ptr< AstNode > &node, uint64_t lane_mask, const AttrMap *node_attrs=nullptr, const FloatAttrMap *float_attrs=nullptr)
Definition impulse_cel.h:1888
static void eval_project_lanes(const std::shared_ptr< AstNode > &node, uint64_t *lane_words, size_t num_words, const AttrMap *node_attrs=nullptr, const FloatAttrMap *float_attrs=nullptr)
Definition impulse_cel.h:1905
static double eval_double(const std::shared_ptr< AstNode > &node, const uint64_t *lane_words, size_t num_words, const FloatAttrMap *float_attrs=nullptr, const AttrMap *int_attrs=nullptr)
Definition impulse_cel.h:1460
std::unordered_map< std::string, double > FloatAttrMap
Definition impulse_cel.h:1428
static int64_t eval_int(const std::shared_ptr< AstNode > &node, uint64_t lane_mask, const AttrMap *node_attrs=nullptr, const FloatAttrMap *float_attrs=nullptr)
Definition impulse_cel.h:1747
static uint64_t eval_project_lane(const std::shared_ptr< AstNode > &node, uint64_t lane_mask, const AttrMap *node_attrs=nullptr, const FloatAttrMap *float_attrs=nullptr)
Definition impulse_cel.h:1898
static bool eval_bool(const std::shared_ptr< AstNode > &node, const uint64_t *lane_words, size_t num_words, const AttrMap *node_attrs=nullptr, const FloatAttrMap *float_attrs=nullptr)
Definition impulse_cel.h:1757
static uint64_t eval_bits(const std::shared_ptr< AstNode > &node, const uint64_t *lane_words, size_t num_words, const AttrMap *node_attrs=nullptr, const FloatAttrMap *float_attrs=nullptr)
Definition impulse_cel.h:1617
static double eval_double(const std::shared_ptr< AstNode > &node, uint64_t lane_mask, const FloatAttrMap *float_attrs=nullptr, const AttrMap *int_attrs=nullptr)
Definition impulse_cel.h:1599
static int64_t eval_int(const std::shared_ptr< AstNode > &node, const uint64_t *lane_words, size_t num_words, const AttrMap *node_attrs=nullptr, const FloatAttrMap *float_attrs=nullptr)
Definition impulse_cel.h:1739
static uint64_t double_to_bits_safe(double d)
Definition impulse_cel.h:1609
Token next_token()
Definition impulse_cel.h:67
Lexer(std::string source)
Definition impulse_cel.h:65
static constexpr size_t MAX_PARSE_DEPTH
Definition impulse_cel.h:456
std::shared_ptr< AstNode > parse_expression(size_t depth=0)
Definition impulse_cel.h:462
Parser(std::string source)
Definition impulse_cel.h:458
std::shared_ptr< AstNode > parse()
Definition impulse_cel.h:469
Zero-overhead debug, audit, coverage, fault-injection, and test-control primitives for Impulse Graph ...
#define IMPULSE_ASSERT(cond)
Definition impulse_assert.h:168
Table-Driven 42-Function Vector Math Engine & SIMD Transcendental Catalog.
static double impulse_math_unary_f64(uint8_t func_id, double x)
Evaluates a scalar unary math operation on double.
Definition impulse_math_ops.h:264
static double impulse_math_ternary_f64(uint8_t func_id, double x, double y, double z)
Evaluates a scalar ternary math operation on double.
Definition impulse_math_ops.h:419
static double impulse_math_binary_f64(uint8_t func_id, double x, double y)
Evaluates a scalar binary math operation on double.
Definition impulse_math_ops.h:372
@ MATH_FUNC_COPYSIGN
Definition impulse_math_ops.h:68
@ MATH_FUNC_COS
Definition impulse_math_ops.h:46
@ MATH_FUNC_SAFE_DIV
Definition impulse_math_ops.h:92
@ MATH_FUNC_ATAN
Definition impulse_math_ops.h:50
@ MATH_FUNC_ACOS
Definition impulse_math_ops.h:49
@ MATH_FUNC_HYPOT
Definition impulse_math_ops.h:31
@ MATH_FUNC_LOG10
Definition impulse_math_ops.h:41
@ MATH_FUNC_FMOD
Definition impulse_math_ops.h:69
@ MATH_FUNC_TANH
Definition impulse_math_ops.h:57
@ MATH_FUNC_CLZ
Definition impulse_math_ops.h:86
@ MATH_FUNC_COSH
Definition impulse_math_ops.h:56
@ MATH_FUNC_CEIL
Definition impulse_math_ops.h:64
@ MATH_FUNC_CBRT
Definition impulse_math_ops.h:29
@ MATH_FUNC_SIN
Definition impulse_math_ops.h:45
@ MATH_FUNC_SIGMOID
Definition impulse_math_ops.h:74
@ MATH_FUNC_ATANH
Definition impulse_math_ops.h:60
@ MATH_FUNC_EXP2
Definition impulse_math_ops.h:36
@ MATH_FUNC_RELU
Definition impulse_math_ops.h:72
@ MATH_FUNC_TRUNC
Definition impulse_math_ops.h:65
@ MATH_FUNC_FLOOR
Definition impulse_math_ops.h:63
@ MATH_FUNC_GELU
Definition impulse_math_ops.h:75
@ MATH_FUNC_ROUND
Definition impulse_math_ops.h:66
@ MATH_FUNC_CLAMP
Definition impulse_math_ops.h:67
@ MATH_FUNC_ROTL
Definition impulse_math_ops.h:88
@ MATH_FUNC_SILU
Definition impulse_math_ops.h:76
@ MATH_FUNC_LOG2
Definition impulse_math_ops.h:40
@ MATH_FUNC_ASIN
Definition impulse_math_ops.h:48
@ MATH_FUNC_SINC
Definition impulse_math_ops.h:52
@ MATH_FUNC_TAN
Definition impulse_math_ops.h:47
@ MATH_FUNC_ATAN2
Definition impulse_math_ops.h:51
@ MATH_FUNC_ACOSH
Definition impulse_math_ops.h:59
@ MATH_FUNC_POW
Definition impulse_math_ops.h:30
@ MATH_FUNC_SQRT
Definition impulse_math_ops.h:27
@ MATH_FUNC_POPCOUNT
Definition impulse_math_ops.h:85
@ MATH_FUNC_EXP
Definition impulse_math_ops.h:35
@ MATH_FUNC_EXPM1
Definition impulse_math_ops.h:38
@ MATH_FUNC_ISFINITE
Definition impulse_math_ops.h:95
@ MATH_FUNC_LEAKY_RELU
Definition impulse_math_ops.h:73
@ MATH_FUNC_ERF
Definition impulse_math_ops.h:80
@ MATH_FUNC_ROTR
Definition impulse_math_ops.h:89
@ MATH_FUNC_LOG
Definition impulse_math_ops.h:39
@ MATH_FUNC_LERP
Definition impulse_math_ops.h:32
@ MATH_FUNC_ASINH
Definition impulse_math_ops.h:58
@ MATH_FUNC_ABS
Definition impulse_math_ops.h:26
@ MATH_FUNC_ERFC
Definition impulse_math_ops.h:81
@ MATH_FUNC_LOG1P
Definition impulse_math_ops.h:42
@ MATH_FUNC_EXP10
Definition impulse_math_ops.h:37
@ MATH_FUNC_SINH
Definition impulse_math_ops.h:55
@ MATH_FUNC_ISNAN
Definition impulse_math_ops.h:93
@ MATH_FUNC_CTZ
Definition impulse_math_ops.h:87
@ MATH_FUNC_RSQRT
Definition impulse_math_ops.h:28
@ MATH_FUNC_ISINF
Definition impulse_math_ops.h:94
@ MATH_FUNC_LGAMMA
Definition impulse_math_ops.h:82
@ MATH_FUNC_SOFTPLUS
Definition impulse_math_ops.h:77
Impulse Graph Bytecode Virtual Machine Engine & Instruction Set Specification.
#define OP_STREAM_FILTER
Definition impulse_vm.h:226
#define OP_VEC_BLEND
Definition impulse_vm.h:88
#define OP_STREAM_CMP_EQ
Definition impulse_vm.h:235
#define OP_STREAM_LOGIC_AND
Definition impulse_vm.h:239
#define OP_STREAM_FUNC_BEGIN
Definition impulse_vm.h:220
#define OP_STREAM_LOAD_SRC_ID
Definition impulse_vm.h:245
#define OP_VEC_MATH_BINARY
Definition impulse_vm.h:95
#define OP_STREAM_MATH_UNARY
Definition impulse_vm.h:234
#define OP_STREAM_LOGIC_OR
Definition impulse_vm.h:240
#define OP_STREAM_CMP_GT
Definition impulse_vm.h:237
#define OP_STREAM_MATH_MUL
Definition impulse_vm.h:232
#define OP_STREAM_LOAD_TGT
Definition impulse_vm.h:230
#define OP_STREAM_LOAD_EDGE_ID
Definition impulse_vm.h:247
#define OP_STREAM_MATH_ADD
Definition impulse_vm.h:224
#define OP_STREAM_REDUCE_ARGMIN
Definition impulse_vm.h:243
#define OP_LOAD_CONST_FLOAT
Definition impulse_vm.h:51
impulse_instruction_t
Definition impulse_vm.h:354
#define OP_STREAM_CMP_NEQ
Definition impulse_vm.h:236
#define OP_STREAM_REDUCE
Definition impulse_vm.h:227
#define OP_STREAM_REDUCE_ARGMAX
Definition impulse_vm.h:244
#define OP_VEC_MATH_UNARY
Definition impulse_vm.h:94
#define OP_STREAM_LOAD_TGT_ID
Definition impulse_vm.h:246
#define OP_STREAM_FUNC_END
Definition impulse_vm.h:221
#define OP_VEC_MATH_TERNARY
Definition impulse_vm.h:96
#define OP_STREAM_LOAD_CONST
Definition impulse_vm.h:248
#define OP_STREAM_LOAD_SRC
Definition impulse_vm.h:222
#define OP_STREAM_SELECT
Definition impulse_vm.h:242
#define OP_STREAM_LOGIC_NOT
Definition impulse_vm.h:241
#define OP_STREAM_LOAD_EDGE
Definition impulse_vm.h:223
#define OP_STREAM_MATH_SUB
Definition impulse_vm.h:231
#define OP_STREAM_MATH_MOD
Definition impulse_vm.h:233
#define OP_LOAD_COLUMN_VECTOR
Definition impulse_vm.h:192
#define OP_STREAM_MATH_DIV
Definition impulse_vm.h:225
#define OP_STREAM_CMP_LT
Definition impulse_vm.h:238
Definition impulse_cel.h:27
Precedence
Definition impulse_cel.h:437
@ PREC_BIT_AND
Definition impulse_cel.h:446
@ PREC_BIT_XOR
Definition impulse_cel.h:445
@ PREC_CALL_MEMBER
Definition impulse_cel.h:451
@ PREC_AND
Definition impulse_cel.h:442
@ PREC_UNARY
Definition impulse_cel.h:450
@ PREC_CONDITIONAL
Definition impulse_cel.h:440
@ PREC_BIT_OR
Definition impulse_cel.h:444
@ PREC_MUL_DIV
Definition impulse_cel.h:449
@ PREC_ASSIGN
Definition impulse_cel.h:439
@ PREC_COMPARE
Definition impulse_cel.h:443
@ PREC_SHIFT
Definition impulse_cel.h:447
@ PREC_OR
Definition impulse_cel.h:441
@ PREC_ADD_SUB
Definition impulse_cel.h:448
@ PREC_NONE
Definition impulse_cel.h:438
TokenType
Definition impulse_cel.h:29
@ KW_IN
Definition impulse_cel.h:51
@ GT_GT
Definition impulse_cel.h:41
@ RBRACKET
Definition impulse_cel.h:47
@ QUESTION
Definition impulse_cel.h:42
@ EQ
Definition impulse_cel.h:43
@ LT_LT
Definition impulse_cel.h:41
@ CARET
Definition impulse_cel.h:41
@ DOT
Definition impulse_cel.h:43
@ LPAREN
Definition impulse_cel.h:46
@ BANG
Definition impulse_cel.h:40
@ COMMA
Definition impulse_cel.h:43
@ STRING_LITERAL
Definition impulse_cel.h:34
@ TILDE
Definition impulse_cel.h:41
@ END_OF_FILE
Definition impulse_cel.h:30
@ SLASH
Definition impulse_cel.h:38
@ PERCENT
Definition impulse_cel.h:38
@ PIPE
Definition impulse_cel.h:41
@ IDENTIFIER
Definition impulse_cel.h:31
@ GT_EQ
Definition impulse_cel.h:39
@ RBRACE
Definition impulse_cel.h:48
@ PIPE_PIPE
Definition impulse_cel.h:40
@ KW_AS
Definition impulse_cel.h:51
@ PLUS
Definition impulse_cel.h:38
@ STAR
Definition impulse_cel.h:38
@ AMP
Definition impulse_cel.h:41
@ BOOL_LITERAL
Definition impulse_cel.h:35
@ AMP_AMP
Definition impulse_cel.h:40
@ EQ_EQ
Definition impulse_cel.h:39
@ LT
Definition impulse_cel.h:39
@ INT_LITERAL
Definition impulse_cel.h:32
@ GT
Definition impulse_cel.h:39
@ LBRACKET
Definition impulse_cel.h:47
@ LBRACE
Definition impulse_cel.h:48
@ FLOAT_LITERAL
Definition impulse_cel.h:33
@ BANG_EQ
Definition impulse_cel.h:39
@ COLON
Definition impulse_cel.h:42
@ LT_EQ
Definition impulse_cel.h:39
@ RPAREN
Definition impulse_cel.h:46
@ MINUS
Definition impulse_cel.h:38
AstKind
Definition impulse_cel.h:328
@ BINARY_OP
Definition impulse_cel.h:336
@ LITERAL_BOOL
Definition impulse_cel.h:331
@ LITERAL_FLOAT
Definition impulse_cel.h:330
@ LITERAL_STRING
Definition impulse_cel.h:332
@ LITERAL_INT
Definition impulse_cel.h:329
@ IDENTIFIER
Definition impulse_cel.h:333
@ TERNARY_OP
Definition impulse_cel.h:337
@ UNARY_OP
Definition impulse_cel.h:335
@ LIST_LITERAL
Definition impulse_cel.h:339
@ MEMBER_ACCESS
Definition impulse_cel.h:334
@ FUNCTION_CALL
Definition impulse_cel.h:338
Definition contracts_chunk1.hpp:12
Definition impulse_cel.h:342
static std::shared_ptr< AstNode > make_ident(std::string name)
Definition impulse_cel.h:379
bool bool_val
Definition impulse_cel.h:347
int64_t int_val
Definition impulse_cel.h:345
static std::shared_ptr< AstNode > make_ternary(std::shared_ptr< AstNode > cond, std::shared_ptr< AstNode > then_branch, std::shared_ptr< AstNode > else_branch)
Definition impulse_cel.h:411
static std::shared_ptr< AstNode > make_bool(bool v)
Definition impulse_cel.h:365
AstKind kind
Definition impulse_cel.h:343
static std::shared_ptr< AstNode > make_string(std::string v)
Definition impulse_cel.h:372
static std::shared_ptr< AstNode > make_list(std::vector< std::shared_ptr< AstNode > > elements)
Definition impulse_cel.h:428
std::string str_val
Definition impulse_cel.h:348
std::vector< std::shared_ptr< AstNode > > children
Definition impulse_cel.h:349
static std::shared_ptr< AstNode > make_int(int64_t v)
Definition impulse_cel.h:351
static std::shared_ptr< AstNode > make_call(std::string func, std::vector< std::shared_ptr< AstNode > > args)
Definition impulse_cel.h:420
double float_val
Definition impulse_cel.h:346
std::string text
Definition impulse_cel.h:344
static std::shared_ptr< AstNode > make_float(double v)
Definition impulse_cel.h:358
static std::shared_ptr< AstNode > make_member(std::shared_ptr< AstNode > target, std::string field)
Definition impulse_cel.h:386
static std::shared_ptr< AstNode > make_unary(std::string op, std::shared_ptr< AstNode > operand)
Definition impulse_cel.h:394
static std::shared_ptr< AstNode > make_binary(std::string op, std::shared_ptr< AstNode > lhs, std::shared_ptr< AstNode > rhs)
Definition impulse_cel.h:402
Typed value with Kleene 3-valued NULL semantics (Spec v0.10.0).
Definition impulse_cel.h:1918
int to_tribool() const
Definition impulse_cel.h:2006
bool to_int64(int64_t &out) const
Definition impulse_cel.h:1991
Kind kind
Definition impulse_cel.h:1921
bool uses_owned_str
Definition impulse_cel.h:1923
static CelValue make_int(int64_t x)
Definition impulse_cel.h:1937
std::string_view as_string_view() const
Definition impulse_cel.h:1973
bool bool_val
Definition impulse_cel.h:1922
static CelValue make_bool(bool b)
Definition impulse_cel.h:1931
uint64_t uint_val
Definition impulse_cel.h:1925
static CelValue make_string(std::string s)
Definition impulse_cel.h:1963
std::string_view str_view
Definition impulse_cel.h:1927
static CelValue make_uint(uint64_t x)
Definition impulse_cel.h:1943
static CelValue make_string_view(std::string_view sv)
Definition impulse_cel.h:1956
double dbl_val
Definition impulse_cel.h:1926
int64_t int_val
Definition impulse_cel.h:1924
static CelValue make_double(double x)
Definition impulse_cel.h:1949
bool is_null() const
Definition impulse_cel.h:1971
Kind
Definition impulse_cel.h:1919
@ String
Definition impulse_cel.h:1919
@ Null
Definition impulse_cel.h:1919
@ Bool
Definition impulse_cel.h:1919
@ Uint64
Definition impulse_cel.h:1919
@ Double
Definition impulse_cel.h:1919
@ Int64
Definition impulse_cel.h:1919
bool to_double(double &out) const
Definition impulse_cel.h:1977
std::string str_owned
Definition impulse_cel.h:1928
static CelValue make_null()
Definition impulse_cel.h:1930
Definition impulse_cel.h:54
std::string text
Definition impulse_cel.h:56
TokenType type
Definition impulse_cel.h:55
double float_val
Definition impulse_cel.h:58
bool bool_val
Definition impulse_cel.h:59
int64_t int_val
Definition impulse_cel.h:57
size_t pos
Definition impulse_cel.h:60