Exemplo n.º 1
0
expr_t parse_id_expr(tokenizer_t t){
  char* v = strdup(cur_tok(t).name);
  eat_it(t, TOK_ID);
  // printf("parse_id_expr: %s\n", v);

  if(cur_tok(t).kind == TOK_LPAREN)
  { // 関数呼び出し
    eat_it(t, TOK_LPAREN);    
    expr_list_t list = parse_arg_expr(t);
    eat_it(t, TOK_RPAREN);
    return mk_expr_call(t->filename, t->line, v, list);
  }
  else
  { // 変数名
    return mk_expr_id(t->filename, t->line, v);
  }
}
Exemplo n.º 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);
    }
}