|
| 1 | +#ifndef PASS2_H |
| 2 | +#define PASS2_H |
| 3 | + |
| 4 | + |
| 5 | +string textOut= ""; |
| 6 | + |
| 7 | +void push(Token sym) { |
| 8 | + symListOut.push_back(sym); |
| 9 | + textOut += sym.content; |
| 10 | + reportln("push >>> " + sym.content, 2); |
| 11 | +} |
| 12 | + |
| 13 | +Token nextSymbol(string from, vector<TokenType> expected){ |
| 14 | + if (symList.empty()) |
| 15 | + throw invalid_argument("from <$from>: symbols missing!"); |
| 16 | + Token next = symList.back(); |
| 17 | + symList.pop_back(); |
| 18 | + if (expected.empty()) return next; |
| 19 | + if (count(expected.begin(), expected.end(), next.type) > 0) return next; |
| 20 | + errorsPresent = true; |
| 21 | + throw invalid_argument( |
| 22 | + "from <$from> at cursor=${next.cursor} symbol={${next.content}, ${next.typ}} NOT IN ${expected.contentToString()}" |
| 23 | + ); |
| 24 | + return next; |
| 25 | +} |
| 26 | +void bExpression(); |
| 27 | + |
| 28 | +void factor() { |
| 29 | + //next symbol if success |
| 30 | + switch(symIn.type) { |
| 31 | + case VARI: case LIT: |
| 32 | + push(symIn); // push??? |
| 33 | + symIn = nextSymbol("factor", {TimesDiv, PlusMin, BEXPE, ELV_Q, EOT}); |
| 34 | + break; |
| 35 | + default: bExpression(); |
| 36 | + break; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +void term() { |
| 42 | + if (isa(symIn, {CHS})) { |
| 43 | + Token save = symIn; |
| 44 | + symIn = nextSymbol("term", {VARI}); |
| 45 | + factor(); |
| 46 | + int code = 177; |
| 47 | + if (save.content == "-") push(Token({CHS, 0, "${code.toChar()}", symIn.cursor})); // push??? |
| 48 | + } |
| 49 | + factor(); |
| 50 | + while (isa(symIn, {TimesDiv})) { |
| 51 | + Token save = symIn; |
| 52 | + symIn = nextSymbol("term", {VARI, BEXPS}); |
| 53 | + factor(); |
| 54 | + push(save); // push ??? |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +void expression(){ |
| 59 | + term(); |
| 60 | + while (isa(symIn, {PlusMin, ELV_Q, ELV_C})) { |
| 61 | + Token save = symIn; |
| 62 | + symIn = nextSymbol("expression", {VARI, BEXPS}); |
| 63 | + term(); |
| 64 | + push(save); //??? |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | + |
| 69 | +void bExpression() { |
| 70 | + if (isa(symIn, {BEXPS})) { |
| 71 | + symIn = nextSymbol("bExpression", {VARI, BEXPS, CHS}); |
| 72 | + expression(); |
| 73 | + symIn = nextSymbol("bExpression", {VARI, TimesDiv, PlusMin, BEXPS, BEXPE, ELV_Q, ELV_C, EOT}); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | + |
| 78 | +void clear2() { |
| 79 | + symListOut.clear(); |
| 80 | + cursor = 0; |
| 81 | + errorsPresent = false; |
| 82 | + textOut = ""; |
| 83 | +} |
| 84 | + |
| 85 | +vector<Token> parse2(){ |
| 86 | + reverse(symList.begin(), symList.end()); |
| 87 | + clear2(); |
| 88 | + cursor = 0; |
| 89 | + errorsPresent = false; |
| 90 | + try { |
| 91 | + symIn = nextSymbol("parse", {}); |
| 92 | + expression(); |
| 93 | + } catch (invalid_argument) { |
| 94 | + cout << "PARSE ERROR: ${e.message}" << endl; |
| 95 | + }; |
| 96 | + if (!errorsPresent) |
| 97 | + cout << "expression parser PASS1 ended succesfully\n "; |
| 98 | + return symListOut; |
| 99 | +}; |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +#endif // PASS2_H |
0 commit comments