deal with + and -
| 142 | |
| 143 | // deal with + and - |
| 144 | double expression() |
| 145 | { |
| 146 | double left = term(); // read and evaluate a Term |
| 147 | Token t = ts.get(); // get the next token from token stream |
| 148 | |
| 149 | while(true) { |
| 150 | switch(t.kind) { |
| 151 | case '+': |
| 152 | left += term(); // evaluate Term and add |
| 153 | t = ts.get(); |
| 154 | break; |
| 155 | case '-': |
| 156 | left -= term(); // evaluate Term and subtract |
| 157 | t = ts.get(); |
| 158 | break; |
| 159 | default: |
| 160 | ts.putback(t); // put t back into the token stream |
| 161 | return left; // finally: no more + or -: return the answer |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | //------------------------------------------------------------------------------ |
| 167 |