diff -r ff576305ae6e -r 713ec3da79ec src/server/util/object.c --- a/src/server/util/object.c Wed Nov 16 12:14:45 2022 +0100 +++ b/src/server/util/object.c Sun Nov 20 11:39:46 2022 +0100 @@ -128,11 +128,13 @@ CxList *ex_stack; CxList *tokens; size_t *pos; - int expect_value; + WSBool expect_value; + WSBool expect_arg; } ExprParser; typedef struct { NSAPIExpressionOperator operator; + cxstring identifier; int expect_value; int open_parenthesis; } ExprOpStackItem; @@ -153,7 +155,9 @@ } static NSAPIExpressionOperator expr_operator(cxstring token) { - if(!cx_strcmp(token, cx_str("+"))) { + if(!cx_strcmp(token, cx_str(","))) { + return NSAPI_EXPRESSION_ARG; + } else if(!cx_strcmp(token, cx_str("+"))) { return NSAPI_EXPRESSION_ADD; } else if(!cx_strcmp(token, cx_str("-"))) { return NSAPI_EXPRESSION_SUB; @@ -263,9 +267,18 @@ // in that case, the - operator is a unary expression if(op->expect_value) { // We expected a value but got an operator? This must be an unary operator + + if(op->operator == NSAPI_EXPRESSION_ARG && !parser->expect_arg) { + // simplify unary arg + pool_free(parser->pool, exp); + parser->expect_value = TRUE; + parser->expect_arg = FALSE; + return 0; + } + exp->type = NSAPI_EXPRESSION_UNARY; exp->left = expr_parser_pop(parser); - exp->right = NULL; + exp->right = NULL; } else { // binary operator exp->type = NSAPI_EXPRESSION_BINARY; @@ -280,6 +293,33 @@ cxListAdd(parser->ex_stack, exp); parser->expect_value = TRUE; + parser->expect_arg = FALSE; + return 0; +} + +static int expr_add_func(ExprParser *parser, ExprOpStackItem *func) { + NSAPIExpression *exp = pool_malloc(parser->pool, sizeof(NSAPIExpression)); + if(!exp) { + return 1; + } + exp->type = NSAPI_EXPRESSION_IDENTIFIER; + exp->operator = NSAPI_EXPRESSION_CALL; + exp->left = NULL; + exp->right = NULL; + exp->value.str = func->identifier; + + if(parser->ex_stack->size > 0) { + NSAPIExpression *top = cxListAt(parser->ex_stack, parser->ex_stack->size - 1); + if(top && top->operator == NSAPI_EXPRESSION_ARG) { + exp->left = top; + cxListRemove(parser->ex_stack, parser->ex_stack->size - 1); + } + } + + if(cxListAdd(parser->ex_stack, exp)) { + return 1; + } + parser->expect_value = FALSE; return 0; } @@ -292,11 +332,46 @@ if(expr_set_value(parser->pool, exp, token)) { return 1; } + cxListAdd(parser->ex_stack, exp); + if(parser->expect_arg) { + ExprOpStackItem argList; + argList.expect_value = TRUE; + argList.identifier = (cxstring){NULL, 0}; + argList.open_parenthesis = FALSE; + argList.operator = NSAPI_EXPRESSION_ARG; + if(expr_add_operator(parser, &argList)) { + return 1; + } + } + parser->expect_value = FALSE; + parser->expect_arg = FALSE; return 0; } +static int token_is_identifier(cxstring token) { + if(token.length == 0) { + return 0; + } + + if(!isalpha(token.ptr[0])) { + return 0; + } + + for(int i=1;iop_stack; @@ -309,6 +384,7 @@ if(op != NSAPI_EXPRESSION_NOOP) { ExprOpStackItem new_op; new_op.operator = op; + new_op.identifier = (cxstring){NULL,0}; new_op.expect_value = parser->expect_value; new_op.open_parenthesis = FALSE; while(op_stack->size > 0) { @@ -327,23 +403,40 @@ } cxListAdd(op_stack, &new_op); parser->expect_value = TRUE; + parser->expect_arg = FALSE; } else if(token.length == 1 && token.ptr[0] == '(') { - ExprOpStackItem new_op; - new_op.operator = NSAPI_EXPRESSION_NOOP; - new_op.expect_value = 0; - new_op.open_parenthesis = 1; - cxListAdd(op_stack, &new_op); + ExprOpStackItem *prev_op = NULL; + if(op_stack->size > 0) { + prev_op = cxListAt(op_stack, op_stack->size - 1); + } + + if(prev_op && prev_op->operator == NSAPI_EXPRESSION_CALL) { + // function call open parenthesis + // we don't need to add a new op stack item, just mark function + // call as open parenthesis + prev_op->open_parenthesis = TRUE; + parser->expect_arg = TRUE; + } else { + ExprOpStackItem new_op; + new_op.operator = NSAPI_EXPRESSION_NOOP; + new_op.identifier = (cxstring){NULL,0}; + new_op.expect_value = 0; + new_op.open_parenthesis = TRUE; + cxListAdd(op_stack, &new_op); + } parser->expect_value = TRUE; } else if(token.length == 1 && token.ptr[0] == ')') { int found_open_bracket = FALSE; + ExprOpStackItem stack_item; while(op_stack->size > 0) { - ExprOpStackItem *stack_item = cxListAt(op_stack, op_stack->size-1); + ExprOpStackItem *stack_item_ptr = cxListAt(op_stack, op_stack->size-1); + stack_item = *stack_item_ptr; cxListRemove(op_stack, op_stack->size-1); - if(stack_item->open_parenthesis) { + if(stack_item.open_parenthesis) { found_open_bracket = TRUE; break; } else { - if(expr_add_operator(parser, stack_item)) { + if(expr_add_operator(parser, &stack_item)) { return NULL; } } @@ -351,7 +444,22 @@ if(!found_open_bracket) { return NULL; } + if(stack_item.operator == NSAPI_EXPRESSION_CALL) { + if(expr_add_func(parser, &stack_item)) { + return NULL; + } + } parser->expect_value = FALSE; + parser->expect_arg = FALSE; + } else if(token_is_identifier(token)) { + ExprOpStackItem new_op; + new_op.operator = NSAPI_EXPRESSION_CALL; + new_op.identifier = token; + new_op.expect_value = 0; + new_op.open_parenthesis = FALSE; + cxListAdd(op_stack, &new_op); + parser->expect_value = FALSE; + parser->expect_arg = FALSE; } else { if(expr_add_value(parser, token)) { return NULL; @@ -388,6 +496,7 @@ parser.tokens = tokens; parser.pos = pos; parser.expect_value = TRUE; + parser.expect_arg = FALSE; NSAPIExpression *ret = expr_parse_expr(&parser); cxListDestroy(op_stack);