add atid operation

This commit is contained in:
pantor
2020-07-26 12:52:36 +02:00
parent 8feaa3be43
commit c56cbd63e9
8 changed files with 53 additions and 3 deletions
+1
View File
@@ -36,6 +36,7 @@ public:
Division,
Power,
Modulo,
AtId,
At,
Default,
DivisibleBy,
+2
View File
@@ -108,6 +108,8 @@ class Lexer {
return make_token(Token::Kind::Power);
case '%':
return make_token(Token::Kind::Percent);
case '.':
return make_token(Token::Kind::Dot);
case ',':
return make_token(Token::Kind::Comma);
case ':':
+5 -1
View File
@@ -141,7 +141,7 @@ public:
size_t number_args;
CallbackFunction callback;
explicit FunctionNode(nonstd::string_view name, size_t pos) : ExpressionNode(pos), precedence(5), associativity(Associativity::Left), operation(Op::Callback), name(name), number_args(1) { }
explicit FunctionNode(nonstd::string_view name, size_t pos) : ExpressionNode(pos), precedence(8), associativity(Associativity::Left), operation(Op::Callback), name(name), number_args(1) { }
explicit FunctionNode(Op operation, size_t pos) : ExpressionNode(pos), operation(operation), number_args(1) {
switch (operation) {
case Op::Not: {
@@ -208,6 +208,10 @@ public:
precedence = 4;
associativity = Associativity::Left;
} break;
case Op::AtId: {
precedence = 8;
associativity = Associativity::Left;
} break;
default: {
precedence = 1;
associativity = Associativity::Left;
+5 -1
View File
@@ -166,7 +166,8 @@ class Parser {
case Token::Kind::Times:
case Token::Kind::Slash:
case Token::Kind::Power:
case Token::Kind::Percent: {
case Token::Kind::Percent:
case Token::Kind::Dot: {
parse_operator:
FunctionStorage::Operation operation;
@@ -220,6 +221,9 @@ class Parser {
case Token::Kind::Percent: {
operation = FunctionStorage::Operation::Modulo;
} break;
case Token::Kind::Dot: {
operation = FunctionStorage::Operation::AtId;
} break;
default: {
throw_parser_error("unknown operator in parser.");
}
+10
View File
@@ -311,6 +311,16 @@ class Renderer : public NodeVisitor {
json_tmp_stack.push_back(result_ptr);
json_eval_stack.push(result_ptr.get());
} break;
case Op::AtId: {
json_eval_stack.pop(); // Pop id nullptr
auto container = get_arguments<1, false>(node)[0];
if (not_found_stack.empty()) {
throw_renderer_error("could not find element with given name", node);
}
auto id_node = not_found_stack.top();
not_found_stack.pop();
json_eval_stack.push(&container->at(id_node->name));
} break;
case Op::At: {
auto args = get_arguments<2>(node);
json_eval_stack.push(&args[0]->at(args[1]->get<int>()));
+1
View File
@@ -33,6 +33,7 @@ struct Token {
Percent, // %
Power, // ^
Comma, // ,
Dot, // .
Colon, // :
LeftParen, // (
RightParen, // )