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

Function term

code_snippets/Chapter07/chapter.7.6.2.cpp:127–160  ·  view source on GitHub ↗

deal with *, /, and %

Source from the content-addressed store, hash-verified

125
126// deal with *, /, and %
127double term()
128{
129 double left = primary();
130 Token t = ts.get(); // get the next token from token stream
131
132 while(true) {
133 switch (t.kind) {
134 case '*':
135 left *= primary();
136 t = ts.get();
137 break;
138 case '/':
139 {
140 double d = primary();
141 if (d == 0) error("divide by zero");
142 left /= d;
143 t = ts.get();
144 break;
145 }
146 case '%':
147 {
148 int i1 = narrow_cast<int>(left);
149 int i2 = narrow_cast<int>(term());
150 if (i2 == 0) error("%: divide by zero");
151 left = i1%i2;
152 t = ts.get();
153 break;
154 }
155 default:
156 ts.putback(t); // put t back into the token stream
157 return left;
158 }
159 }
160}
161
162//------------------------------------------------------------------------------
163

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