deal with + and -
| 216 | |
| 217 | // deal with + and - |
| 218 | double expression() |
| 219 | { |
| 220 | double left = term(); // read and evaluate a Term |
| 221 | Token t = ts.get(); // get the next token from token stream |
| 222 | |
| 223 | while(true) { |
| 224 | switch(t.kind) { |
| 225 | case '+': |
| 226 | left += term(); // evaluate Term and add |
| 227 | t = ts.get(); |
| 228 | break; |
| 229 | case '-': |
| 230 | left -= term(); // evaluate Term and subtract |
| 231 | t = ts.get(); |
| 232 | break; |
| 233 | default: |
| 234 | ts.putback(t); // put t back into the token stream |
| 235 | return left; // finally: no more + or -: return the answer |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | //------------------------------------------------------------------------------ |
| 241 |