expr_t parse_multi_expr(tokenizer_t t) { char * filename = cur_tok(t).filename; int line = cur_tok(t).line_num; expr_t e = parse_unary_expr(t); while((cur_tok(t).kind == TOK_MUL)|(cur_tok(t).kind == TOK_DIV)|(cur_tok(t).kind == TOK_REM)) { op_kind_t op = parse_multi_oper(t); expr_t e0 = parse_unary_expr(t); e = mk_expr_bin_op(filename, line, op, e, e0); } return e; }
expr_t parse_relat_expr(tokenizer_t t) { char * filename = cur_tok(t).filename; int line = cur_tok(t).line_num; expr_t e = parse_add_expr(t); while((cur_tok(t).kind==TOK_LT)|(cur_tok(t).kind==TOK_GT)|(cur_tok(t).kind==TOK_LE)|(cur_tok(t).kind==TOK_GE)) { op_kind_t op = parse_relat_oper(t); expr_t e0 = parse_add_expr(t); e = mk_expr_bin_op(filename, line, op, e, e0); } return e; }
expr_t parse_expr(tokenizer_t t) { char * filename = cur_tok(t).filename; int line = cur_tok(t).line_num; expr_t e0 = parse_equal_expr(t); if(cur_tok(t).kind == TOK_ASSIGN) { eat_it(t, TOK_ASSIGN); expr_t e1 = parse_equal_expr(t); return mk_expr_bin_op(filename, line, op_kind_assign, e0, e1); } else { return e0; } }
expr_t parse_add_expr(tokenizer_t t){ expr_t expr_left, expr_right; expr_left = parse_mult_expr(t); struct token tok = cur_tok(t); while(tok.kind == TOK_PLUS || tok.kind == TOK_MINUS){ eat_it(t, TOK_ANY); // なんでも食べる expr_right = parse_mult_expr(t); expr_left = mk_expr_bin_op(t->filename, t->line, (tok.kind == TOK_PLUS) ? op_kind_bin_plus : op_kind_bin_minus // +, - のどちらかしかない , expr_left, expr_right); // left にたたんでいく tok = cur_tok(t); } return expr_left; }
/** * parse expression */ expr_t parse_expr(tokenizer_t t){ expr_t expr_left, expr_right; expr_left = parse_eq_expr(t); struct token tok = cur_tok(t); if(tok.kind == TOK_ASSIGN){ eat_it(t, TOK_ASSIGN); expr_right = parse_expr(t); expr_t assign_expr = mk_expr_bin_op(t->filename, t->line, op_kind_assign, expr_left, expr_right); return assign_expr; }else{ // これは式文 return expr_left; } }
expr_t parse_eq_expr(tokenizer_t t){ expr_t expr_left, expr_right; expr_left = parse_relat_expr(t); struct token tok = cur_tok(t); while(tok.kind == TOK_EQ || tok.kind == TOK_NEQ){ eat_it(t, TOK_ANY); // なんでも食べる expr_right = parse_relat_expr(t); expr_left = mk_expr_bin_op(t->filename, t->line, (tok.kind == TOK_EQ) ? op_kind_eq : op_kind_neq // == , != のどちらかしかない , expr_left, expr_right); // left にたたんでいく tok = cur_tok(t); } return expr_left; }
expr_t parse_mult_expr(tokenizer_t t){ expr_t expr_left, expr_right; expr_left = parse_unary_expr(t); struct token tok = cur_tok(t); // relational-operator while(tok.kind == TOK_MUL || tok.kind == TOK_DIV || tok.kind == TOK_REM){ eat_it(t, TOK_ANY); // なんでも食べる expr_right = parse_unary_expr(t); op_kind_t op_kind; if(tok.kind == TOK_MUL) op_kind = op_kind_mult; if(tok.kind == TOK_DIV) op_kind = op_kind_div; if(tok.kind == TOK_REM) op_kind = op_kind_rem; expr_left = mk_expr_bin_op(t->filename, t->line, op_kind, expr_left, expr_right); // left にたたんでいく tok = cur_tok(t); } return expr_left; }
expr_t parse_relat_expr(tokenizer_t t){ expr_t expr_left, expr_right; expr_left = parse_add_expr(t); struct token tok = cur_tok(t); // relational-operator while(tok.kind == TOK_LT || tok.kind == TOK_GT || tok.kind == TOK_LE || tok.kind == TOK_GE){ eat_it(t, TOK_ANY); // なんでも食べる expr_right = parse_add_expr(t); op_kind_t op_kind; if(tok.kind == TOK_LT) op_kind = op_kind_lt; if(tok.kind == TOK_GT) op_kind = op_kind_gt; if(tok.kind == TOK_LE) op_kind = op_kind_le; if(tok.kind == TOK_GE) op_kind = op_kind_ge; expr_left = mk_expr_bin_op(t->filename, t->line, op_kind, expr_left, expr_right); // left にたたんでいく tok = cur_tok(t); } return expr_left; // struct token tok = cur_tok(t); // expr_list_t list = mk_expr_list(); // expr_list_add(list, parse_add_expr(t)); // while(1) // { // if(tok.kind == TOK_LT || tok.kind == TOK_GT || tok.kind == TOK_LE || tok.kind == TOK_GE) // { // eat_it(t, TOK_ANY); // なんでも食べる // expr_list_add(list, parse_add_expr(t)); // } // else break; // } // expr_t expr = ; }