Ejemplo n.º 1
0
static sl_node_base_t*
for_expression(sl_parse_state_t* ps)
{
    sl_node_base_t *lval, *expr, *body, *else_body = NULL;
    sl_node_seq_t* seq_lval;
    sl_token_t* tok;
    sl_parse_scope_t scope;
    expect_token(ps, SL_TOK_FOR);
    /* save current token to allow rewinding and erroring */
    tok = peek_token(ps);
    lval = call_expression(ps);
    if(!sl_node_is_lval(lval)) {
        unexpected(ps, tok);
    }
    if(peek_token(ps)->type == SL_TOK_COMMA || peek_token(ps)->type == SL_TOK_FAT_COMMA) {
        seq_lval = sl_make_seq_node(ps);
        seq_lval->nodes[seq_lval->node_count++] = lval;
        while(peek_token(ps)->type == SL_TOK_COMMA || peek_token(ps)->type == SL_TOK_FAT_COMMA) {
            next_token(ps);
            if(seq_lval->node_count == seq_lval->node_capacity) {
                seq_lval->node_capacity *= 2;
                seq_lval->nodes = sl_realloc(ps->vm->arena, seq_lval->nodes, sizeof(sl_node_base_t*) * seq_lval->node_capacity);
            }
            tok = peek_token(ps);
            lval = call_expression(ps);
            if(!sl_node_is_lval(lval)) {
                unexpected(ps, tok);
            }
            seq_lval->nodes[seq_lval->node_count++] = lval;
        }
        lval = sl_make_array_node(ps, seq_lval->node_count, seq_lval->nodes);
    }
    expect_token(ps, SL_TOK_IN);
    expr = expression(ps);
    
    scope.prev = ps->scope;
    scope.flags = scope.prev->flags | SL_PF_CAN_NEXT_LAST;
    ps->scope = &scope;
    body = body_expression(ps);
    ps->scope = scope.prev;
    if(scope.flags & SL_PF_SCOPE_CLOSURE) {
        ps->scope->flags |= SL_PF_SCOPE_CLOSURE;
    }
    
    if(peek_token(ps)->type == SL_TOK_ELSE) {
        next_token(ps);
        else_body = body_expression(ps);
    }
    return sl_make_for_node(ps, lval, expr, body, else_body);
}
Ejemplo n.º 2
0
static sl_node_base_t*
use_expression(sl_parse_state_t* ps)
{
    expect_token(ps, SL_TOK_USE);
    sl_node_base_t* node = call_expression(ps);
    if(node->type != SL_NODE_CONST) {
        error(ps, sl_make_cstring(ps->vm, "Expected constant reference in use expression"), token(ps));
    }
    for(sl_node_base_t* i = node; i && i->type == SL_NODE_CONST; i = ((sl_node_const_t*)i)->obj) {
        i->type = SL_NODE_USE;
    }
    return node;
}
Ejemplo n.º 3
0
static sl_node_base_t*
inc_dec_expression(sl_parse_state_t* ps)
{
    if(peek_token(ps)->type == SL_TOK_INCREMENT) {
        next_token(ps);
        return sl_make_prefix_mutate_node(ps, call_expression(ps), "succ");
    }
    if(peek_token(ps)->type == SL_TOK_DECREMENT) {
        next_token(ps);
        return sl_make_prefix_mutate_node(ps, call_expression(ps), "pred");
    }
    sl_node_base_t* lval = call_expression(ps);
    if(sl_node_is_mutable_lval(lval)) {
        if(peek_token(ps)->type == SL_TOK_INCREMENT) {
            next_token(ps);
            return sl_make_postfix_mutate_node(ps, lval, "succ");
        }
        if(peek_token(ps)->type == SL_TOK_DECREMENT) {
            next_token(ps);
            return sl_make_postfix_mutate_node(ps, lval, "pred");
        }
    }
    return lval;
}
Ejemplo n.º 4
0
// Parse a postfix-expression.
//
//    postfix-expression:
//      primary-expression
//      postfix-expression '.' id
//      postfix-expression '(' [expression-list] ')'
//      postfix-expression '[' expression ']'
//
// TODO: Add lots of stuff here.
Expr&
Parser::postfix_expression()
{
  Expr* e = &primary_expression();
  while (true) {
    if (lookahead() == tk::dot_tok)
      e = &dot_expression(*e);
    else if (lookahead() == tk::lparen_tok)
      e = &call_expression(*e);
    else if (lookahead() == tk::lbracket_tok)
      e = &subscript_expression(*e);
    else
      break;
  }
  return *e;
}