19#include <unordered_map>
65 explicit Lexer(std::string source) : src_(std::move(source)), cursor_(0) {}
68 skip_whitespace_and_comments();
69 if (cursor_ >= src_.size()) {
73 size_t start = cursor_;
74 char c = src_[cursor_++];
124 if (std::isalpha(
static_cast<unsigned char>(peek())) || peek() ==
'_') {
125 return parse_ident(start);
131 return parse_string(c, start);
134 if (std::isdigit(
static_cast<unsigned char>(c))) {
135 return parse_number(c, start);
137 if (std::isalpha(
static_cast<unsigned char>(c)) || c ==
'_') {
138 return parse_ident(start);
152 Token error_token(
size_t start)
const {
153 std::string txt = src_.substr(start, cursor_ - start);
154 if (txt.empty()) txt =
"?";
159 return cursor_ < src_.size() ? src_[cursor_] :
'\0';
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))) {
167 }
else if (c ==
'/' && cursor_ + 1 < src_.size() && src_[cursor_ + 1] ==
'/') {
169 while (cursor_ < src_.size() && src_[cursor_] !=
'\n') cursor_++;
176 Token parse_string(
char quote,
size_t start) {
178 while (cursor_ < src_.size()) {
179 char c = src_[cursor_++];
180 if (c == quote)
break;
181 if (c ==
'\\' && cursor_ < src_.size()) {
183 if (c ==
'n') s +=
'\n';
184 else if (c ==
't') s +=
'\t';
185 else if (c ==
'r') s +=
'\r';
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_++];
199 while (cursor_ < src_.size()) {
200 char c = src_[cursor_];
201 if (std::isxdigit(
static_cast<unsigned char>(c))) {
205 }
else if (c ==
'_') {
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);
220 t.int_val =
static_cast<int64_t
>(std::stoull(digits,
nullptr, 16));
223 if (first_digit ==
'0' && cursor_ < src_.size() && (src_[cursor_] ==
'b' || src_[cursor_] ==
'B')) {
224 std::string raw = std::string(
"0") + src_[cursor_++];
226 while (cursor_ < src_.size()) {
227 char c = src_[cursor_];
228 if (c ==
'0' || c ==
'1') {
232 }
else if (c ==
'_') {
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);
246 t.int_val =
static_cast<int64_t
>(std::stoull(digits,
nullptr, 2));
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))) {
257 }
else if (c ==
'_') {
259 }
else if (c ==
'.' && !is_float && cursor_ + 1 < src_.size() && std::isdigit(
static_cast<unsigned char>(src_[cursor_ + 1]))) {
263 }
else if (c ==
'e' || c ==
'E') {
267 if (cursor_ < src_.size() && (src_[cursor_] ==
'+' || src_[cursor_] ==
'-')) {
268 num_str += src_[cursor_++];
280 t.float_val = std::stod(num_str);
283 t.int_val = std::stoll(num_str);
285 }
catch (
const std::exception&) {
287 return error_token(start);
292 Token parse_ident(
size_t start) {
294 char first = src_[start];
295 if (first !=
'$' && first !=
'@') {
298 while (cursor_ < src_.size()) {
299 char c = src_[cursor_];
300 if (std::isalnum(
static_cast<unsigned char>(c)) || c ==
'_') {
313 }
else if (
id ==
"false") {
316 }
else if (
id ==
"in") {
318 }
else if (
id ==
"as") {
351 static std::shared_ptr<AstNode>
make_int(int64_t v) {
352 auto n = std::make_shared<AstNode>();
359 auto n = std::make_shared<AstNode>();
366 auto n = std::make_shared<AstNode>();
373 auto n = std::make_shared<AstNode>();
375 n->str_val = std::move(v);
379 static std::shared_ptr<AstNode>
make_ident(std::string name) {
380 auto n = std::make_shared<AstNode>();
382 n->text = std::move(name);
386 static std::shared_ptr<AstNode>
make_member(std::shared_ptr<AstNode> target, std::string field) {
387 auto n = std::make_shared<AstNode>();
389 n->text = std::move(field);
390 n->children.push_back(std::move(target));
394 static std::shared_ptr<AstNode>
make_unary(std::string op, std::shared_ptr<AstNode> operand) {
395 auto n = std::make_shared<AstNode>();
397 n->text = std::move(op);
398 n->children.push_back(std::move(operand));
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>();
405 n->text = std::move(op);
406 n->children.push_back(std::move(lhs));
407 n->children.push_back(std::move(rhs));
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>();
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));
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>();
423 n->text = std::move(func);
424 n->children = std::move(args);
428 static std::shared_ptr<AstNode>
make_list(std::vector<std::shared_ptr<AstNode>> elements) {
429 auto n = std::make_shared<AstNode>();
431 n->children = std::move(elements);
458 explicit Parser(std::string source) : lexer_(std::move(source)) {
464 throw std::runtime_error(
"CEL parse recursion depth exceeded limit 256");
477 if (!tree_complete(expr)) {
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;
496 curr_ = lexer_.next_token();
500 if (curr_.type == type) {
546 std::shared_ptr<AstNode> parse_prefix(
size_t depth = 0) {
548 throw std::runtime_error(
"CEL parse recursion depth exceeded limit 256");
568 auto operand = parse_precedence(
PREC_UNARY, depth + 1);
569 if (!operand)
return nullptr;
578 std::vector<std::shared_ptr<AstNode>> elements;
581 if (!first)
return nullptr;
582 elements.push_back(std::move(first));
585 if (!next)
return nullptr;
586 elements.push_back(std::move(next));
597 std::shared_ptr<AstNode> parse_precedence(
Precedence prec,
size_t depth = 0) {
599 throw std::runtime_error(
"CEL parse recursion depth exceeded limit 256");
601 auto left = parse_prefix(depth + 1);
602 if (!left)
return nullptr;
604 while (prec <= get_precedence(curr_.type)) {
613 if (!else_branch)
return nullptr;
618 std::string field = curr_.text;
622 std::vector<std::shared_ptr<AstNode>> args;
623 args.push_back(left);
626 if (!a)
return nullptr;
627 args.push_back(std::move(a));
630 if (!b)
return nullptr;
631 args.push_back(std::move(b));
644 std::vector<std::shared_ptr<AstNode>> args;
647 if (!a)
return nullptr;
648 args.push_back(std::move(a));
651 if (!b)
return nullptr;
652 args.push_back(std::move(b));
661 auto right = parse_precedence(next_prec, depth + 1);
662 if (!right)
return nullptr;
676 static std::string
to_impscheme(
const std::shared_ptr<AstNode>& node,
size_t depth = 0) {
677 if (!node)
return "()";
679 throw std::runtime_error(
"CelCompiler recursion depth exceeded limit 256");
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";
694 return node->bool_val ?
"#t" :
"#f";
696 return "\"" + node->str_val +
"\"";
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) +
")";
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) +
")";
719 std::string s =
"(" + node->text;
720 for (
const auto& arg : node->children) {
727 std::string s =
"(list";
728 for (
const auto& elem : node->children) {
740 static const std::unordered_map<std::string, int> kMathMap = {
764 auto it = kMathMap.find(name);
765 return it != kMathMap.end() ? it->second : -1;
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
779 if (!expr)
return false;
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;
786 switch (node->kind) {
790 ?
static_cast<float>(node->float_val)
791 :
static_cast<float>(node->int_val);
793 std::memcpy(&bits, &f,
sizeof(
float));
798 float f = node->bool_val ? 1.0f : 0.0f;
800 std::memcpy(&bits, &f,
sizeof(
float));
805 if (node->text ==
"lane") {
809 if (node->text ==
"id" || node->text ==
"src_id") {
813 if (node->text ==
"tgt_id") {
817 if (node->text ==
"edge_id") {
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;
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 :
"";
834 if (target_name ==
"src") {
839 if (field ==
"lane") {
843 if (field ==
"popcount") {
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;
855 if (target_name ==
"tgt") {
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;
868 if (target_name ==
"edge") {
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;
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;
890 if (node->text ==
"!") {
891 uint16_t r0 = (target_reg + 1) % 16;
892 if (!self(self, node->children[0], r0))
return false;
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;
905 return self(self, node->children[0], target_reg);
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));
924 else if (op ==
">=") {
927 }
else if (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;
945 const std::string& fn = node->text;
946 if ((fn ==
"popcount" || fn ==
"bit_count") && !node->children.empty()) {
947 const auto& arg = node->children[0];
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);
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;
967 out_shader.push_back(
impulse_instruction_t{opc, 0, r_val,
static_cast<uint32_t
>(target_reg | ((target_reg + 1) << 16))});
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;
985 if (!emit_stream_impl(emit_stream_impl, expr, out_stream_reg)) {
1001 const std::shared_ptr<AstNode>& expr,
1002 std::vector<impulse_instruction_t>& out_instructions,
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
1008 if (!expr)
return false;
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;
1013 switch (node->kind) {
1017 ?
static_cast<float>(node->float_val)
1018 :
static_cast<float>(node->int_val);
1020 std::memcpy(&bits, &f,
sizeof(
float));
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;
1032 uint32_t payload =
static_cast<uint32_t
>(domain_id) | (
static_cast<uint32_t
>(attr_idx) << 16);
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++;
1045 uint32_t payload =
static_cast<uint32_t
>(r_zero) | (
static_cast<uint32_t
>(r_child) << 8) | (1u << 16);
1049 return self(self, node->children[0], target_reg);
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;
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;
1068 uint32_t payload =
static_cast<uint32_t
>(r0) | (
static_cast<uint32_t
>(r1) << 8) | (
static_cast<uint32_t
>(func_id) << 16);
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);
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);
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);
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;
1121 uint32_t payload =
static_cast<uint32_t
>(r_cond) | (
static_cast<uint32_t
>(r_true) << 8) | (
static_cast<uint32_t
>(r_false) << 16);
1130 return emit_vec_impl(emit_vec_impl, expr, out_reg);
1146 static std::shared_ptr<AstNode>
optimize(
const std::shared_ptr<AstNode>& node,
size_t depth = 0) {
1147 if (!node)
return nullptr;
1149 throw std::runtime_error(
"AstOptimizer recursion depth exceeded limit 256");
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));
1158 auto result = std::make_shared<AstNode>(*node);
1161 for (
const auto& fc : folded_children) {
1162 if (!fc)
return result;
1164 result->children = std::move(folded_children);
1167 switch (result->kind) {
1169 return fold_unary(result);
1171 return fold_binary(result);
1173 return fold_ternary(result);
1175 return fold_function_call(result);
1182 static std::shared_ptr<AstNode> fold_unary(
const std::shared_ptr<AstNode>& node) {
1183 auto child = node->children[0];
1184 if (node->text ==
"!") {
1190 return child->children[0];
1192 }
else if (node->text ==
"-") {
1194 return AstNode::make_int(
static_cast<int64_t
>(0 -
static_cast<uint64_t
>(child->int_val)));
1199 }
else if (node->text ==
"~") {
1204 return child->children[0];
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;
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> {
1221 std::string new_op = op;
1223 if (op ==
"<") new_op =
">";
1224 else if (op ==
"<=") new_op =
">=";
1225 else if (op ==
">") new_op =
"<";
1226 else if (op ==
">=") new_op =
"<=";
1228 if (lhs->text ==
"sqrt") {
1230 if (new_op ==
"<" || new_op ==
"<=") {
1231 return AstNode::make_binary(
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))
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));
1240 if (c == 0.0 && (new_op ==
">" || new_op ==
">=")) {
1241 return AstNode::make_binary(new_op, lhs->children[0], AstNode::make_float(0.0));
1248 if (
auto res = check_pushdown(left, right,
false))
return res;
1249 if (
auto res = check_pushdown(right, left,
true))
return res;
1254 int64_t a = left->int_val;
1255 int64_t b = right->int_val;
1258 const uint64_t ua =
static_cast<uint64_t
>(a), ub =
static_cast<uint64_t
>(b);
1262 if (op ==
"/" && b != 0)
return AstNode::make_int(b == -1 ?
static_cast<int64_t
>(0 - ua) : a / b);
1273 if (op ==
"<<")
return AstNode::make_int((b < 0 || b >= 64) ? 0 :
static_cast<int64_t
>(
static_cast<uint64_t
>(a) << b));
1275 if (op ==
">>>")
return AstNode::make_int((b < 0 || b >= 64) ? 0 :
static_cast<int64_t
>(
static_cast<uint64_t
>(a) >> b));
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);
1297 bool a = left->bool_val;
1298 bool b = right->bool_val;
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 ==
"&&") {
1323 }
else if (op ==
"||") {
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];
1341 return cond->bool_val ? then_branch : else_branch;
1346 static std::shared_ptr<AstNode> fold_function_call(
const std::shared_ptr<AstNode>& node) {
1348 if (func_id <= 0)
return node;
1355 const uint64_t v =
static_cast<uint64_t
>(node->children[0]->int_val);
1365 if (node->children.size() == 1) {
1366 auto arg = node->children[0];
1375 if (node->children.size() == 2) {
1376 auto arg1 = node->children[0];
1377 auto arg2 = node->children[1];
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);
1388 if (node->children.size() == 3) {
1389 auto arg1 = node->children[0];
1390 auto arg2 = node->children[1];
1391 auto arg3 = node->children[2];
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);
1406 static bool is_zero(
const std::shared_ptr<AstNode>& node) {
1407 if (!node)
return false;
1413 static bool is_one(
const std::shared_ptr<AstNode>& node) {
1414 if (!node)
return false;
1427 using AttrMap = std::unordered_map<std::string, int64_t>;
1432 if (!node)
return false;
1433 switch (node->kind) {
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") {
1450 for (
const auto& c : node->children) {
1461 const uint64_t* lane_words,
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);
1479 auto it = float_attrs->find(node->text);
1480 if (it != float_attrs->end())
return it->second;
1483 auto it = int_attrs->find(node->text);
1484 if (it != int_attrs->end())
return static_cast<double>(it->second);
1489 if (node->text ==
"!") {
1490 return eval_bool(node->children[0], lane_words, num_words, int_attrs, float_attrs) ? 0.0 : 1.0;
1492 if (node->text ==
"~") {
1493 return static_cast<double>(
eval_int(node, lane_words, num_words, int_attrs, float_attrs));
1495 double v =
eval_double(node->children[0], lane_words, num_words, float_attrs, int_attrs);
1496 if (node->text ==
"-")
return -v;
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);
1504 const std::string& op = node->text;
1506 return eval_double(node->children[1], lane_words, num_words, float_attrs, int_attrs);
1509 return eval_bool(node, lane_words, num_words, int_attrs, float_attrs) ? 1.0 : 0.0;
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;
1515 if (op ==
"&" || op ==
"|" || op ==
"^" || op ==
"<<" || op ==
">>" || op ==
">>>") {
1516 return static_cast<double>(
eval_int(node, lane_words, num_words, int_attrs, float_attrs));
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;
1528 const std::string& fn = node->text;
1529 if ((fn ==
"popcount" || fn ==
"bit_count") && !node->children.empty()) {
1530 if (node->children[0]->text ==
"lane" ||
1533 for (
size_t w = 0; w < num_words; ++w) total += std::popcount(lane_words[w]);
1534 return static_cast<double>(total);
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));
1539 if ((fn ==
"first_lane" || fn ==
"ctz") && !node->children.empty()) {
1540 if (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]));
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));
1552 if (fn ==
"last_lane" && !node->children.empty()) {
1553 if (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]));
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));
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));
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;
1576 if (node->children.size() == 1) {
1577 double a =
eval_double(node->children[0], lane_words, num_words, float_attrs, int_attrs);
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);
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);
1602 const AttrMap* int_attrs =
nullptr) {
1603 return eval_double(node, &lane_mask, 1, float_attrs, int_attrs);
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)));
1617 static uint64_t
eval_bits(
const std::shared_ptr<AstNode>& node,
1618 const uint64_t* lane_words,
1620 const AttrMap* node_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;
1632 if (node->text ==
"lane") {
1633 return (lane_words && num_words > 0) ? lane_words[0] : 0ULL;
1636 auto it = node_attrs->find(node->text);
1637 if (it != node_attrs->end())
return static_cast<uint64_t
>(it->second);
1640 auto it = float_attrs->find(node->text);
1646 if (node->text ==
"!") {
1647 return eval_bool(node->children[0], lane_words, num_words, node_attrs, float_attrs) ? 0ULL : 1ULL;
1649 if (node->text ==
"~") {
1650 return ~eval_bits(node->children[0], lane_words, num_words, node_attrs, float_attrs);
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));
1656 return eval_bits(node->children[0], lane_words, num_words, node_attrs, float_attrs);
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);
1663 const std::string& op = node->text;
1665 return eval_bits(node->children[1], lane_words, num_words, node_attrs, float_attrs);
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);
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" ||
1693 for (
size_t w = 0; w < num_words; ++w) total += std::popcount(lane_words[w]);
1694 return static_cast<uint64_t
>(total);
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));
1699 if ((fn ==
"first_lane" || fn ==
"ctz") && !node->children.empty()) {
1700 if (num_words > 1 && (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]));
1707 return static_cast<uint64_t
>(-1LL);
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));
1712 if (fn ==
"last_lane" && !node->children.empty()) {
1713 if (num_words > 1 && (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]));
1720 return static_cast<uint64_t
>(-1LL);
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));
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));
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;
1739 static int64_t
eval_int(
const std::shared_ptr<AstNode>& node,
1740 const uint64_t* lane_words,
1742 const AttrMap* node_attrs =
nullptr,
1744 return static_cast<int64_t
>(
eval_bits(node, lane_words, num_words, node_attrs, float_attrs));
1747 static int64_t
eval_int(
const std::shared_ptr<AstNode>& node,
1749 const AttrMap* node_attrs =
nullptr,
1751 return static_cast<int64_t
>(
eval_bits(node, &lane_mask, 1, node_attrs, float_attrs));
1758 const uint64_t* lane_words,
1760 const AttrMap* node_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;
1771 if (node->text ==
"!")
return !
eval_bool(node->children[0], lane_words, num_words, node_attrs, float_attrs);
1773 return eval_double(node, lane_words, num_words, float_attrs, node_attrs) != 0.0;
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)
1779 : node->children[2],
1780 lane_words, num_words, node_attrs, float_attrs);
1782 const std::string& op = node->text;
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);
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);
1792 const auto& rhs = node->children[1];
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;
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;
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;
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;
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" ||
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;
1840 for (
size_t w = 0; w < num_words; ++w) {
1841 if (lane_words[w] != 0ULL)
return true;
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;
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;
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;
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" ||
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;
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;
1876 return eval_double(node, lane_words, num_words, float_attrs, node_attrs) != 0.0;
1878 return eval_int(node, lane_words, num_words, node_attrs, float_attrs) != 0;
1882 return eval_double(node, lane_words, num_words, float_attrs, node_attrs) != 0.0;
1884 return eval_int(node, lane_words, num_words, node_attrs, float_attrs) != 0;
1890 const AttrMap* node_attrs =
nullptr,
1892 return eval_bool(node, &lane_mask, 1, node_attrs, float_attrs);
1900 const AttrMap* node_attrs =
nullptr,
1902 return static_cast<uint64_t
>(
eval_int(node, lane_mask, node_attrs, float_attrs));
1906 uint64_t* lane_words,
1908 const AttrMap* node_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));
1919 enum class Kind : uint8_t { Null = 0, Bool, Int64, Uint64, Double, String };
1983 if (std::isnan(
dbl_val))
return false;
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));
2056 auto as_signed_or_unsigned = [](
const CelValue& v,
bool& is_neg, uint64_t& mag) {
2066 mag =
static_cast<uint64_t
>(-(v.
int_val + 1)) + 1ULL;
2069 mag =
static_cast<uint64_t
>(v.
int_val);
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);
2078 if (neg_a != neg_b) {
2079 cmp = neg_a ? -1 : 1;
2081 cmp = (mag_a > mag_b) ? -1 : ((mag_a < mag_b) ? 1 : 0);
2083 cmp = (mag_a < mag_b) ? -1 : ((mag_a > mag_b) ? 1 : 0);
2094 double da = 0.0, db = 0.0;
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
2113 switch (node->kind) {
2124 if (node->text ==
"null" || node->text ==
"NULL") {
2127 if (node->text ==
"lane") {
2128 uint64_t lw = (lane_words && num_lane_words > 0) ? lane_words[0] : 0ULL;
2133 (node->children[0]->text ==
"edge" || node->children[0]->text ==
"node" || node->children[0]->text ==
"self")) {
2134 return resolve_attr(node->text);
2136 return resolve_attr(node->text);
2139 CelValue sub =
eval(node->children[0], resolve_attr, lane_words, num_lane_words);
2140 if (node->text ==
"!") {
2146 if (node->text ==
"+")
return sub;
2147 if (node->text ==
"-") {
2154 if (node->text ==
"~") {
2162 CelValue c =
eval(node->children[0], resolve_attr, lane_words, num_lane_words);
2165 return eval(tb ? node->children[1] : node->children[2], resolve_attr, lane_words, num_lane_words);
2168 const std::string& op = node->text;
2170 return eval(node->children[1], resolve_attr, lane_words, num_lane_words);
2173 int tl =
eval(node->children[0], resolve_attr, lane_words, num_lane_words).to_tribool();
2175 int tr =
eval(node->children[1], resolve_attr, lane_words, num_lane_words).to_tribool();
2181 int tl =
eval(node->children[0], resolve_attr, lane_words, num_lane_words).to_tribool();
2183 int tr =
eval(node->children[1], resolve_attr, lane_words, num_lane_words).to_tribool();
2189 const auto& rhs = node->children[1];
2192 CelValue lhs =
eval(node->children[0], resolve_attr, lane_words, num_lane_words);
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);
2198 if (eq.
is_null()) saw_null =
true;
2203 if (op ==
"==" || op ==
"!=" || op ==
"<" || op ==
"<=" || op ==
">" || op ==
">=") {
2204 CelValue lhs =
eval(node->children[0], resolve_attr, lane_words, num_lane_words);
2206 CelValue rhs =
eval(node->children[1], resolve_attr, lane_words, num_lane_words);
2211 CelValue lhs =
eval(node->children[0], resolve_attr, lane_words, num_lane_words);
2213 CelValue rhs =
eval(node->children[1], resolve_attr, lane_words, num_lane_words);
2224 if (both_int && (op ==
"+" || op ==
"-" || op ==
"*" || op ==
"%")) {
2225 int64_t ia = 0, ib = 0;
2234 if (op ==
"+" || op ==
"-" || op ==
"*" || op ==
"/" || op ==
"%") {
2235 double da = 0.0, db = 0.0;
2244 int64_t ia = 0, ib = 0;
2249 if (op ==
"<<")
return CelValue::make_int((ib < 0 || ib >= 64) ? 0 :
static_cast<int64_t
>(
static_cast<uint64_t
>(ia) << ib));
2251 if (op ==
">>>")
return CelValue::make_int((ib < 0 || ib >= 64) ? 0 :
static_cast<int64_t
>(
static_cast<uint64_t
>(ia) >> ib));
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);
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);
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);
2268 CelValue p =
eval(node->children[1], resolve_attr, lane_words, num_lane_words);
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);
2280 CelValue p =
eval(node->children[1], resolve_attr, lane_words, num_lane_words);
2287 return CelValue::make_bool(sv.size() >= pv.size() && sv.substr(sv.size() - pv.size()) == pv);
2289 if (fn ==
"contains" && node->children.size() == 2) {
2290 CelValue s =
eval(node->children[0], resolve_attr, lane_words, num_lane_words);
2292 CelValue p =
eval(node->children[1], resolve_attr, lane_words, num_lane_words);
2301 if ((fn ==
"popcount" || fn ==
"bit_count") && !node->children.empty()) {
2302 if (node->children[0]->text ==
"lane") {
2304 for (
size_t w = 0; w < num_lane_words; ++w) total += std::popcount(lane_words[w]);
2307 CelValue v =
eval(node->children[0], resolve_attr, lane_words, num_lane_words);
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])));
2321 CelValue v =
eval(node->children[0], resolve_attr, lane_words, num_lane_words);
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)));
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])));
2336 CelValue v =
eval(node->children[0], resolve_attr, lane_words, num_lane_words);
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)));
2342 if (fn ==
"clz" && !node->children.empty()) {
2343 CelValue v =
eval(node->children[0], resolve_attr, lane_words, num_lane_words);
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)));
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) {
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)) {
2362 if (node->children.size() >= 2) {
2363 if (!
eval(node->children[1], resolve_attr, lane_words, num_lane_words).to_int64(req)) {
2366 return CelValue::make_bool((
static_cast<uint64_t
>(m) &
static_cast<uint64_t
>(req)) != 0ULL);
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)) {
2377 if (!
eval(node->children[1], resolve_attr, lane_words, num_lane_words).to_int64(req)) {
2380 return CelValue::make_bool((
static_cast<uint64_t
>(m) &
static_cast<uint64_t
>(req)) ==
static_cast<uint64_t
>(req));
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)) {
2389 if (!
eval(node->children[1], resolve_attr, lane_words, num_lane_words).to_int64(forbid)) {
2392 return CelValue::make_bool((
static_cast<uint64_t
>(m) &
static_cast<uint64_t
>(forbid)) == 0ULL);
2394 if (fn ==
"has_lane" && node->children.size() >= 2) {
2396 if (!
eval(node->children[1], resolve_attr, lane_words, num_lane_words).to_int64(idx)) {
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) {
2410 if (node->children.size() == 1) {
2412 if (!
eval(node->children[0], resolve_attr, lane_words, num_lane_words).to_double(a)) {
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)) {
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)) {
2442 template <
typename ResolveAttrFn>
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
2449 return eval(node, std::forward<ResolveAttrFn>(resolve_attr), lane_words, num_lane_words).to_tribool() == 1;
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