MCPcopy Create free account
hub / github.com/bewuethr/stroustrup-ppp / term

Function term

code_snippets/Chapter07/chapter.7.8.2.cpp:278–311  ·  view source on GitHub ↗

deal with *, /, and %

Source from the content-addressed store, hash-verified

276
277// deal with *, /, and %
278double term()
279{
280 double left = primary();
281 Token t = ts.get(); // get the next token from token stream
282
283 while(true) {
284 switch (t.kind) {
285 case '*':
286 left *= primary();
287 t = ts.get();
288 break;
289 case '/':
290 {
291 double d = primary();
292 if (d == 0) error("divide by zero");
293 left /= d;
294 t = ts.get();
295 break;
296 }
297 case '%':
298 {
299 int i1 = narrow_cast<int>(left);
300 int i2 = narrow_cast<int>(term());
301 if (i2 == 0) error("%: divide by zero");
302 left = i1%i2;
303 t = ts.get();
304 break;
305 }
306 default:
307 ts.putback(t); // put t back into the token stream
308 return left;
309 }
310 }
311}
312
313//------------------------------------------------------------------------------
314

Callers 1

expressionFunction · 0.70

Calls 4

primaryFunction · 0.70
errorFunction · 0.70
getMethod · 0.45
putbackMethod · 0.45

Tested by

no test coverage detected