Esempio n. 1
0
static nxt_int_t
njs_parser_unary_plus_expression(njs_vm_t *vm, njs_parser_t *parser,
    njs_token_t token, const void *data)
{
    return njs_parser_unary_expression(vm, parser,
                                       NJS_TOKEN_UNARY_PLUS, data);
}
Esempio n. 2
0
static nxt_int_t
njs_parser_unary_negation_expression(njs_vm_t *vm, njs_parser_t *parser,
    njs_token_t token, const void *data)
{
    return njs_parser_unary_expression(vm, parser,
                                       NJS_TOKEN_UNARY_NEGATION, data);
}
Esempio n. 3
0
static njs_token_t
njs_parser_unary_expression(njs_vm_t *vm, njs_parser_t *parser,
    const njs_parser_expression_t *expr, njs_token_t token)
{
    double                  num;
    njs_token_t             next;
    njs_parser_node_t       *node;
    njs_vmcode_operation_t  operation;

    switch (token) {

    case NJS_TOKEN_ADDITION:
        token = NJS_TOKEN_UNARY_PLUS;
        operation = njs_vmcode_unary_plus;
        break;

    case NJS_TOKEN_SUBSTRACTION:
        token = NJS_TOKEN_UNARY_NEGATION;
        operation = njs_vmcode_unary_negation;
        break;

    case NJS_TOKEN_LOGICAL_NOT:
        operation = njs_vmcode_logical_not;
        break;

    case NJS_TOKEN_BITWISE_NOT:
        operation = njs_vmcode_bitwise_not;
        break;

    case NJS_TOKEN_TYPEOF:
        operation = njs_vmcode_typeof;
        break;

    case NJS_TOKEN_VOID:
        operation = njs_vmcode_void;
        break;

    case NJS_TOKEN_DELETE:
        operation = njs_vmcode_delete;
        break;

    default:
        return njs_parser_inc_dec_expression(vm, parser, token);
    }

    next = njs_parser_token(parser);
    if (nxt_slow_path(next <= NJS_TOKEN_ILLEGAL)) {
        return next;
    }

    next = njs_parser_unary_expression(vm, parser, NULL, next);
    if (nxt_slow_path(next <= NJS_TOKEN_ILLEGAL)) {
        return next;
    }

    if (token == NJS_TOKEN_UNARY_PLUS
        && parser->node->token == NJS_TOKEN_NUMBER)
    {
        /* Skip the unary plus of number. */
        return next;
    }

    if (token == NJS_TOKEN_UNARY_NEGATION
        && parser->node->token == NJS_TOKEN_NUMBER)
    {
        /* Optimization of common negative number. */

        node = parser->node;
        num = -node->u.value.data.u.number;
        node->u.value.data.u.number = num;
        node->u.value.data.truth = njs_is_number_true(num);

        return next;
    }

    if (token == NJS_TOKEN_TYPEOF
        && parser->node->token == NJS_TOKEN_NAME)
    {
        parser->node->state = NJS_VARIABLE_TYPEOF;

    } else if (token == NJS_TOKEN_DELETE
               && parser->node->token == NJS_TOKEN_PROPERTY)
    {
        parser->node->token = NJS_TOKEN_PROPERTY_DELETE;
        parser->node->u.operation = njs_vmcode_property_delete;
        parser->code_size += sizeof(njs_vmcode_3addr_t);

        return next;
    }

    node = njs_parser_node_alloc(vm);
    if (nxt_slow_path(node == NULL)) {
        return NJS_TOKEN_ERROR;
    }

    node->token = token;
    node->u.operation = operation;
    node->left = parser->node;
    node->left->dest = node;
    parser->node = node;
    parser->code_size += sizeof(njs_vmcode_2addr_t);

    return next;
}