Beispiel #1
0
expr_t parse_op_expr(tokenizer_t t)
{
  // struct token tok = cur_tok(t);
  expr_t expr;

  if(cur_tok(t).kind == TOK_BANG){
    eat_it(t, TOK_BANG);
    expr = parse_unary_expr(t);
    return mk_expr_un_op(t->filename, t->line, op_kind_logneg, expr);
  }else if(cur_tok(t).kind == TOK_PLUS){
    eat_it(t, TOK_PLUS);
    expr = parse_unary_expr(t);
    return mk_expr_un_op(t->filename, t->line, op_kind_un_plus, expr);
  }else if(cur_tok(t).kind == TOK_MINUS){
    eat_it(t, TOK_MINUS);
    expr = parse_unary_expr(t);
    return mk_expr_un_op(t->filename, t->line, op_kind_un_minus, expr);
  }else{
    syntax_error(t, "error: parse_op_expr (expected TOK_BANG, TOK_PLUS, TOK_MINUS)");
    return NULL;
    // exit
  }
  // }
}
Beispiel #2
0
expr_t parse_unary_expr(tokenizer_t t) {
    char * filename = cur_tok(t).filename;
    int line = cur_tok(t).line_num;
    char * f =(char *)safe_malloc(sizeof(char) * 50) ;
    if(cur_tok(t).kind == TOK_INT_LITERAL) {
        strcpy(f, cur_tok(t).num);
        f = (char *)realloc(f, sizeof(char) * strlen(f) + 1);
        eat_it(t, TOK_INT_LITERAL);
        return mk_expr_int_literal(filename, line, f);
    }
    else if(cur_tok(t).kind == TOK_ID) {
        strcpy(f, parse_identifier(t));
        f = (char *)realloc(f, sizeof(char) * strlen(f) + 1);
        if(cur_tok(t).kind == TOK_LPAREN) {
            eat_it(t, TOK_LPAREN);
            expr_list_t args = parse_arg_expr_list(t);
            eat_it(t, TOK_RPAREN);
            return mk_expr_call(filename, line, f, args);
        }
        else {
            return mk_expr_id(filename, line, f);
        }
    }
    else if(cur_tok(t).kind == TOK_LPAREN) {
        eat_it(t, TOK_LPAREN);
        expr_t e = parse_expr(t);
        eat_it(t, TOK_RPAREN);
        return mk_expr_paren(filename, line, e);
    }
    else if((cur_tok(t).kind == TOK_PLUS)|(cur_tok(t).kind == TOK_MINUS)|(cur_tok(t).kind == TOK_BANG)) {
        op_kind_t op = parse_unary_oper(t);
        expr_t exp = parse_unary_expr(t);
        return mk_expr_un_op(filename, line, op, exp);
    }
    else {
        printf("exit_5\n");
        exit(1);
    }
}