00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00031 #include <assert.h>
00032 #include <stdlib.h>
00033 #include "bigint.h"
00034 #include "cspan.h"
00035 #include "debug.h"
00036 #include "lex.h"
00037 #include "list.h"
00038 #include "mytypes.h"
00039 #include "p_type.h"
00040 #include "parse.h"
00041 #include "stree.h"
00042
00043 #include "p_expr.h"
00044
00045 static stree_expr_t *parse_assign(parse_t *parse);
00046 static stree_expr_t *parse_disjunctive(parse_t *parse);
00047 static stree_expr_t *parse_conjunctive(parse_t *parse);
00048 static stree_expr_t *parse_comparative(parse_t *parse);
00049 static stree_expr_t *parse_additive(parse_t *parse);
00050 static stree_expr_t *parse_multip(parse_t *parse);
00051 static stree_expr_t *parse_prefix(parse_t *parse);
00052 static stree_expr_t *parse_prefix_new(parse_t *parse);
00053 static stree_expr_t *parse_postfix(parse_t *parse);
00054 static stree_expr_t *parse_pf_access(parse_t *parse, stree_expr_t *a);
00055 static stree_expr_t *parse_pf_call(parse_t *parse, stree_expr_t *a);
00056 static stree_expr_t *parse_pf_index(parse_t *parse, stree_expr_t *a);
00057 static stree_expr_t *parse_pf_as(parse_t *parse, stree_expr_t *a);
00058 static stree_expr_t *parse_paren(parse_t *parse);
00059 static stree_expr_t *parse_primitive(parse_t *parse);
00060 static stree_expr_t *parse_nameref(parse_t *parse);
00061 static stree_expr_t *parse_lit_bool(parse_t *parse);
00062 static stree_expr_t *parse_lit_char(parse_t *parse);
00063 static stree_expr_t *parse_lit_int(parse_t *parse);
00064 static stree_expr_t *parse_lit_ref(parse_t *parse);
00065 static stree_expr_t *parse_lit_string(parse_t *parse);
00066 static stree_expr_t *parse_self_ref(parse_t *parse);
00067
00068 static stree_expr_t *parse_recovery_expr(parse_t *parse);
00069
00080 stree_expr_t *parse_expr(parse_t *parse)
00081 {
00082 #ifdef DEBUG_PARSE_TRACE
00083 printf("Parse expression.\n");
00084 #endif
00085 if (parse_is_error(parse))
00086 return parse_recovery_expr(parse);
00087
00088 return parse_assign(parse);
00089 }
00090
00095 static stree_expr_t *parse_assign(parse_t *parse)
00096 {
00097 stree_expr_t *a, *b, *tmp;
00098 stree_assign_t *assign;
00099
00100 a = parse_disjunctive(parse);
00101
00102 switch (lcur_lc(parse)) {
00103 case lc_assign:
00104 assign = stree_assign_new(ac_set);
00105 break;
00106 case lc_increase:
00107 assign = stree_assign_new(ac_increase);
00108 break;
00109 default:
00110 return a;
00111 }
00112
00113 lskip(parse);
00114 b = parse_disjunctive(parse);
00115
00116 assign->dest = a;
00117 assign->src = b;
00118
00119 tmp = stree_expr_new(ec_assign);
00120 tmp->u.assign = assign;
00121 tmp->cspan = cspan_merge(a->cspan, b->cspan);
00122
00123 assign->expr = tmp;
00124
00125 return tmp;
00126 }
00127
00132 static stree_expr_t *parse_disjunctive(parse_t *parse)
00133 {
00134 stree_expr_t *a, *b, *tmp;
00135 stree_binop_t *binop;
00136 cspan_t *cs;
00137
00138 a = parse_conjunctive(parse);
00139 cs = a->cspan;
00140
00141 while (lcur_lc(parse) == lc_or) {
00142 if (parse_is_error(parse))
00143 break;
00144
00145 lskip(parse);
00146 b = parse_conjunctive(parse);
00147
00148 binop = stree_binop_new(bo_or);
00149 binop->arg1 = a;
00150 binop->arg2 = b;
00151
00152 tmp = stree_expr_new(ec_binop);
00153 tmp->u.binop = binop;
00154 tmp->cspan = cspan_merge(cs, b->cspan);
00155 binop->expr = tmp;
00156
00157 a = tmp;
00158 cs = tmp->cspan;
00159 }
00160
00161 return a;
00162 }
00163
00168 static stree_expr_t *parse_conjunctive(parse_t *parse)
00169 {
00170 stree_expr_t *a, *b, *tmp;
00171 stree_binop_t *binop;
00172 cspan_t *cs;
00173
00174 a = parse_comparative(parse);
00175 cs = a->cspan;
00176
00177 while (lcur_lc(parse) == lc_and) {
00178 if (parse_is_error(parse))
00179 break;
00180
00181 lskip(parse);
00182 b = parse_comparative(parse);
00183
00184 binop = stree_binop_new(bo_and);
00185 binop->arg1 = a;
00186 binop->arg2 = b;
00187
00188 tmp = stree_expr_new(ec_binop);
00189 tmp->u.binop = binop;
00190 tmp->cspan = cspan_merge(cs, b->cspan);
00191 binop->expr = tmp;
00192
00193 a = tmp;
00194 cs = tmp->cspan;
00195 }
00196
00197 return a;
00198 }
00199
00204 static stree_expr_t *parse_comparative(parse_t *parse)
00205 {
00206 stree_expr_t *a, *b, *tmp;
00207 stree_binop_t *binop;
00208 binop_class_t bc;
00209 cspan_t *cs;
00210
00211 a = parse_additive(parse);
00212 cs = a->cspan;
00213
00214 while (lcur_lc(parse) == lc_equal || lcur_lc(parse) == lc_notequal ||
00215 lcur_lc(parse) == lc_lt || lcur_lc(parse) == lc_gt ||
00216 lcur_lc(parse) == lc_lt_equal || lcur_lc(parse) == lc_gt_equal) {
00217
00218 if (parse_is_error(parse))
00219 break;
00220
00221 switch (lcur_lc(parse)) {
00222 case lc_equal: bc = bo_equal; break;
00223 case lc_notequal: bc = bo_notequal; break;
00224 case lc_lt: bc = bo_lt; break;
00225 case lc_gt: bc = bo_gt; break;
00226 case lc_lt_equal: bc = bo_lt_equal; break;
00227 case lc_gt_equal: bc = bo_gt_equal; break;
00228 default: assert(b_false);
00229 }
00230
00231 lskip(parse);
00232 b = parse_additive(parse);
00233
00234 binop = stree_binop_new(bc);
00235 binop->arg1 = a;
00236 binop->arg2 = b;
00237
00238 tmp = stree_expr_new(ec_binop);
00239 tmp->u.binop = binop;
00240 tmp->cspan = cspan_merge(cs, b->cspan);
00241 binop->expr = tmp;
00242
00243 a = tmp;
00244 cs = tmp->cspan;
00245 }
00246
00247 return a;
00248 }
00249
00254 static stree_expr_t *parse_additive(parse_t *parse)
00255 {
00256 stree_expr_t *a, *b, *tmp;
00257 stree_binop_t *binop;
00258 binop_class_t bc;
00259 cspan_t *cs;
00260
00261 a = parse_multip(parse);
00262 cs = a->cspan;
00263
00264 while (lcur_lc(parse) == lc_plus || lcur_lc(parse) == lc_minus) {
00265 if (parse_is_error(parse))
00266 break;
00267
00268 switch (lcur_lc(parse)) {
00269 case lc_plus: bc = bo_plus; break;
00270 case lc_minus: bc = bo_minus; break;
00271 default: assert(b_false);
00272 }
00273
00274 lskip(parse);
00275 b = parse_multip(parse);
00276
00277 binop = stree_binop_new(bc);
00278 binop->arg1 = a;
00279 binop->arg2 = b;
00280
00281 tmp = stree_expr_new(ec_binop);
00282 tmp->u.binop = binop;
00283 tmp->cspan = cspan_merge(cs, b->cspan);
00284 binop->expr = tmp;
00285
00286 a = tmp;
00287 cs = tmp->cspan;
00288 }
00289
00290 return a;
00291 }
00292
00297 static stree_expr_t *parse_multip(parse_t *parse)
00298 {
00299 stree_expr_t *a, *b, *tmp;
00300 stree_binop_t *binop;
00301 binop_class_t bc;
00302 cspan_t *cs;
00303
00304 a = parse_prefix(parse);
00305 cs = a->cspan;
00306
00307 while (lcur_lc(parse) == lc_mult) {
00308 if (parse_is_error(parse))
00309 break;
00310
00311 switch (lcur_lc(parse)) {
00312 case lc_mult: bc = bo_mult; break;
00313 default: assert(b_false);
00314 }
00315
00316 lskip(parse);
00317 b = parse_prefix(parse);
00318
00319 binop = stree_binop_new(bc);
00320 binop->arg1 = a;
00321 binop->arg2 = b;
00322
00323 tmp = stree_expr_new(ec_binop);
00324 tmp->u.binop = binop;
00325 tmp->cspan = cspan_merge(cs, b->cspan);
00326 binop->expr = tmp;
00327
00328 a = tmp;
00329 cs = tmp->cspan;
00330 }
00331
00332 return a;
00333 }
00334
00339 static stree_expr_t *parse_prefix(parse_t *parse)
00340 {
00341 stree_expr_t *a;
00342 stree_expr_t *tmp;
00343 stree_unop_t *unop;
00344 unop_class_t uc;
00345 cspan_t *cs0;
00346
00347 switch (lcur_lc(parse)) {
00348 case lc_plus:
00349 case lc_minus:
00350 case lc_not:
00351 if (parse_is_error(parse))
00352 return parse_recovery_expr(parse);
00353
00354 switch (lcur_lc(parse)) {
00355 case lc_plus: uc = uo_plus; break;
00356 case lc_minus: uc = uo_minus; break;
00357 case lc_not: uc = uo_not; break;
00358 default: assert(b_false);
00359 }
00360
00361 cs0 = lcur_span(parse);
00362 lskip(parse);
00363 a = parse_postfix(parse);
00364
00365 unop = stree_unop_new(uc);
00366 unop->arg = a;
00367
00368 tmp = stree_expr_new(ec_unop);
00369 tmp->u.unop = unop;
00370 tmp->cspan = cspan_merge(cs0, a->cspan);
00371 unop->expr = tmp;
00372 a = tmp;
00373 break;
00374 case lc_new:
00375 a = parse_prefix_new(parse);
00376 break;
00377 default:
00378 a = parse_postfix(parse);
00379 break;
00380 }
00381
00382 return a;
00383 }
00384
00389 static stree_expr_t *parse_prefix_new(parse_t *parse)
00390 {
00391 stree_texpr_t *texpr;
00392 stree_new_t *new_op;
00393 stree_expr_t *expr;
00394 stree_expr_t *arg;
00395 cspan_t *cs0, *cs1;
00396
00397 cs0 = lcur_span(parse);
00398 lmatch(parse, lc_new);
00399 texpr = parse_texpr(parse);
00400
00401
00402 cs1 = lprev_span(parse);
00403
00404 new_op = stree_new_new();
00405 new_op->texpr = texpr;
00406 expr = stree_expr_new(ec_new);
00407 expr->u.new_op = new_op;
00408
00409 list_init(&new_op->ctor_args);
00410
00411
00412 if (texpr->tc != tc_tindex) {
00413 lmatch(parse, lc_lparen);
00414
00415
00416
00417 if (lcur_lc(parse) != lc_rparen) {
00418 while (!parse_is_error(parse)) {
00419 arg = parse_expr(parse);
00420 list_append(&new_op->ctor_args, arg);
00421
00422 if (lcur_lc(parse) == lc_rparen)
00423 break;
00424 lmatch(parse, lc_comma);
00425 }
00426 }
00427
00428 lmatch(parse, lc_rparen);
00429 cs1 = cspan_merge(cs0, lprev_span(parse));
00430 }
00431
00432 expr->cspan = cspan_merge(cs0, cs1);
00433 new_op->expr = expr;
00434
00435 return expr;
00436 }
00437
00442 static stree_expr_t *parse_postfix(parse_t *parse)
00443 {
00444 stree_expr_t *a;
00445 stree_expr_t *tmp;
00446
00447 a = parse_paren(parse);
00448
00449 while (lcur_lc(parse) == lc_period || lcur_lc(parse) == lc_lparen ||
00450 lcur_lc(parse) == lc_lsbr || lcur_lc(parse) == lc_as) {
00451
00452 if (parse_is_error(parse))
00453 break;
00454
00455 switch (lcur_lc(parse)) {
00456 case lc_period:
00457 tmp = parse_pf_access(parse, a);
00458 break;
00459 case lc_lparen:
00460 tmp = parse_pf_call(parse, a);
00461 break;
00462 case lc_lsbr:
00463 tmp = parse_pf_index(parse, a);
00464 break;
00465 case lc_as:
00466 tmp = parse_pf_as(parse, a);
00467 break;
00468 default:
00469 assert(b_false);
00470 }
00471
00472 a = tmp;
00473 }
00474
00475 return a;
00476 }
00477
00482 static stree_expr_t *parse_pf_access(parse_t *parse, stree_expr_t *a)
00483 {
00484 stree_ident_t *ident;
00485 stree_expr_t *expr;
00486 stree_access_t *access;
00487 cspan_t *cs1;
00488
00489 lmatch(parse, lc_period);
00490 ident = parse_ident(parse);
00491
00492
00493 cs1 = lprev_span(parse);
00494
00495 access = stree_access_new();
00496 access->arg = a;
00497 access->member_name = ident;
00498
00499 expr = stree_expr_new(ec_access);
00500 expr->u.access = access;
00501 expr->cspan = cspan_merge(a->cspan, cs1);
00502
00503 access->expr = expr;
00504
00505 return expr;
00506 }
00507
00512 static stree_expr_t *parse_pf_call(parse_t *parse, stree_expr_t *a)
00513 {
00514 stree_expr_t *expr;
00515 stree_call_t *call;
00516 stree_expr_t *arg;
00517 cspan_t *cs1;
00518
00519 lmatch(parse, lc_lparen);
00520
00521 call = stree_call_new();
00522 call->fun = a;
00523 list_init(&call->args);
00524
00525
00526
00527 if (lcur_lc(parse) != lc_rparen) {
00528 while (!parse_is_error(parse)) {
00529 arg = parse_expr(parse);
00530 list_append(&call->args, arg);
00531
00532 if (lcur_lc(parse) == lc_rparen)
00533 break;
00534 lmatch(parse, lc_comma);
00535 }
00536 }
00537
00538 lmatch(parse, lc_rparen);
00539 cs1 = lprev_span(parse);
00540
00541 expr = stree_expr_new(ec_call);
00542 expr->u.call = call;
00543 expr->cspan = cspan_merge(a->cspan, cs1);
00544 call->expr = expr;
00545
00546 return expr;
00547 }
00548
00553 static stree_expr_t *parse_pf_index(parse_t *parse, stree_expr_t *a)
00554 {
00555 stree_expr_t *expr;
00556 stree_index_t *index;
00557 stree_expr_t *arg;
00558 cspan_t *cs1;
00559
00560 lmatch(parse, lc_lsbr);
00561
00562 index = stree_index_new();
00563 index->base = a;
00564 list_init(&index->args);
00565
00566
00567
00568 if (lcur_lc(parse) != lc_rsbr) {
00569 while (!parse_is_error(parse)) {
00570 arg = parse_expr(parse);
00571 list_append(&index->args, arg);
00572
00573 if (lcur_lc(parse) == lc_rsbr)
00574 break;
00575 lmatch(parse, lc_comma);
00576 }
00577 }
00578
00579 lmatch(parse, lc_rsbr);
00580 cs1 = lprev_span(parse);
00581
00582 expr = stree_expr_new(ec_index);
00583 expr->u.index = index;
00584 expr->cspan = cspan_merge(a->cspan, cs1);
00585 index->expr = expr;
00586
00587 return expr;
00588 }
00589
00594 static stree_expr_t *parse_pf_as(parse_t *parse, stree_expr_t *a)
00595 {
00596 stree_expr_t *expr;
00597 stree_texpr_t *texpr;
00598 stree_as_t *as_op;
00599 cspan_t *cs1;
00600
00601 lmatch(parse, lc_as);
00602 texpr = parse_texpr(parse);
00603
00604
00605 cs1 = lprev_span(parse);
00606
00607 as_op = stree_as_new();
00608 as_op->arg = a;
00609 as_op->dtype = texpr;
00610
00611 expr = stree_expr_new(ec_as);
00612 expr->u.as_op = as_op;
00613 expr->cspan = cspan_merge(a->cspan, cs1);
00614
00615 as_op->expr = expr;
00616
00617 return expr;
00618 }
00619
00624 static stree_expr_t *parse_paren(parse_t *parse)
00625 {
00626 stree_expr_t *expr;
00627 cspan_t *cs0, *cs1;
00628
00629 if (lcur_lc(parse) == lc_lparen) {
00630 cs0 = lcur_span(parse);
00631 lskip(parse);
00632 expr = parse_expr(parse);
00633 lmatch(parse, lc_rparen);
00634 cs1 = lprev_span(parse);
00635
00636 expr->cspan = cspan_merge(cs0, cs1);
00637 } else {
00638 expr = parse_primitive(parse);
00639 }
00640
00641 return expr;
00642 }
00643
00648 static stree_expr_t *parse_primitive(parse_t *parse)
00649 {
00650 stree_expr_t *expr;
00651
00652 switch (lcur_lc(parse)) {
00653 case lc_ident:
00654 expr = parse_nameref(parse);
00655 break;
00656 case lc_false:
00657 case lc_true:
00658 expr = parse_lit_bool(parse);
00659 break;
00660 case lc_lit_char:
00661 expr = parse_lit_char(parse);
00662 break;
00663 case lc_lit_int:
00664 expr = parse_lit_int(parse);
00665 break;
00666 case lc_nil:
00667 expr = parse_lit_ref(parse);
00668 break;
00669 case lc_lit_string:
00670 expr = parse_lit_string(parse);
00671 break;
00672 case lc_self:
00673 expr = parse_self_ref(parse);
00674 break;
00675 default:
00676 lunexpected_error(parse);
00677 expr = parse_recovery_expr(parse);
00678 }
00679
00680 return expr;
00681 }
00682
00687 static stree_expr_t *parse_nameref(parse_t *parse)
00688 {
00689 stree_nameref_t *nameref;
00690 stree_expr_t *expr;
00691
00692 nameref = stree_nameref_new();
00693 nameref->name = parse_ident(parse);
00694 expr = stree_expr_new(ec_nameref);
00695 expr->u.nameref = nameref;
00696 expr->cspan = lprev_span(parse);
00697 nameref->expr = expr;
00698
00699 return expr;
00700 }
00701
00706 static stree_expr_t *parse_lit_bool(parse_t *parse)
00707 {
00708 stree_literal_t *literal;
00709 stree_expr_t *expr;
00710 bool_t value;
00711
00712 switch (lcur_lc(parse)) {
00713 case lc_false: value = b_false; break;
00714 case lc_true: value = b_true; break;
00715 default: assert(b_false);
00716 }
00717
00718 lskip(parse);
00719
00720 literal = stree_literal_new(ltc_bool);
00721 literal->u.lit_bool.value = value;
00722
00723 expr = stree_expr_new(ec_literal);
00724 expr->u.literal = literal;
00725 expr->cspan = lprev_span(parse);
00726 literal->expr = expr;
00727
00728 return expr;
00729 }
00730
00735 static stree_expr_t *parse_lit_char(parse_t *parse)
00736 {
00737 stree_literal_t *literal;
00738 stree_expr_t *expr;
00739
00740 lcheck(parse, lc_lit_char);
00741
00742 literal = stree_literal_new(ltc_char);
00743 bigint_clone(&lcur(parse)->u.lit_char.value,
00744 &literal->u.lit_char.value);
00745
00746 lskip(parse);
00747
00748 expr = stree_expr_new(ec_literal);
00749 expr->u.literal = literal;
00750 expr->cspan = lprev_span(parse);
00751 literal->expr = expr;
00752
00753 return expr;
00754 }
00755
00760 static stree_expr_t *parse_lit_int(parse_t *parse)
00761 {
00762 stree_literal_t *literal;
00763 stree_expr_t *expr;
00764
00765 lcheck(parse, lc_lit_int);
00766
00767 literal = stree_literal_new(ltc_int);
00768 bigint_clone(&lcur(parse)->u.lit_int.value,
00769 &literal->u.lit_int.value);
00770
00771 lskip(parse);
00772
00773 expr = stree_expr_new(ec_literal);
00774 expr->u.literal = literal;
00775 expr->cspan = lprev_span(parse);
00776 literal->expr = expr;
00777
00778 return expr;
00779 }
00780
00785 static stree_expr_t *parse_lit_ref(parse_t *parse)
00786 {
00787 stree_literal_t *literal;
00788 stree_expr_t *expr;
00789
00790 lmatch(parse, lc_nil);
00791
00792 literal = stree_literal_new(ltc_ref);
00793
00794 expr = stree_expr_new(ec_literal);
00795 expr->u.literal = literal;
00796 expr->cspan = lprev_span(parse);
00797 literal->expr = expr;
00798
00799 return expr;
00800 }
00801
00806 static stree_expr_t *parse_lit_string(parse_t *parse)
00807 {
00808 stree_literal_t *literal;
00809 stree_expr_t *expr;
00810
00811 lcheck(parse, lc_lit_string);
00812
00813 literal = stree_literal_new(ltc_string);
00814 literal->u.lit_string.value = lcur(parse)->u.lit_string.value;
00815
00816 lskip(parse);
00817
00818 expr = stree_expr_new(ec_literal);
00819 expr->u.literal = literal;
00820 expr->cspan = lprev_span(parse);
00821 literal->expr = expr;
00822
00823 return expr;
00824 }
00825
00830 static stree_expr_t *parse_self_ref(parse_t *parse)
00831 {
00832 stree_self_ref_t *self_ref;
00833 stree_expr_t *expr;
00834
00835 lmatch(parse, lc_self);
00836
00837 self_ref = stree_self_ref_new();
00838
00839 expr = stree_expr_new(ec_self_ref);
00840 expr->u.self_ref = self_ref;
00841 expr->cspan = lprev_span(parse);
00842 self_ref->expr = expr;
00843
00844 return expr;
00845 }
00846
00851 static stree_expr_t *parse_recovery_expr(parse_t *parse)
00852 {
00853 stree_literal_t *literal;
00854 stree_expr_t *expr;
00855
00856 (void) parse;
00857
00858 literal = stree_literal_new(ltc_ref);
00859
00860 expr = stree_expr_new(ec_literal);
00861 expr->u.literal = literal;
00862 literal->expr = expr;
00863
00864 return expr;
00865 }