1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | // Code derived from Stroustrup's PPP2 book
// From Chs. 6 & 7, The Calculator Project - CLI edition
// -and beginning on p 174
#include "Calculator.h"
#include <cctype>
#include <iostream>
#include <stdexcept>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::exception;
using std::isalpha;
using std::isdigit;
//------------------------------------------------------------------------------
Token::Token(char const ch) : kind{ch}, value{0} {}
Token::Token(char const ch, double const val) : kind{ch}, value{val} {}
Token::Token(char const ch, std::string const& n) : kind{ch}, name{n} {}
//------------------------------------------------------------------------------
Token_stream::Token_stream() : full{false}, buffer{0} // no Token in buffer
{
}
//------------------------------------------------------------------------------
Token Token_stream::get() // read characters from cin and compose a Token
{
if (full) { // check if we already have a Token ready
full = false;
return buffer;
}
char ch;
cin >> ch; // note that >> skips whitespace (space, newline, tab, etc.)
// clang-format off
switch (ch) {
case quit:
case print:
case '(':
case ')':
case '+':
case '-':
case '*':
case '/':
case '%':
case '=': return Token(ch); // let each character represent itself
case '.': // a floating-point literal can start with a dot
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': { // numeric literal
cin.putback(ch); // put digit back into the input stream
double val;
cin >> val; // read a floating-point number
return Token(number, val);
}
default:
if (isalpha(ch)) { // start with a letter
string s;
s += ch;
while (cin.get(ch) && (isalpha(ch) || isdigit(ch) || ch == '_'))
s += ch; // letters digits and underscores
cin.putback(ch);
if (s == declkey)
return Token{let}; // keyword "let"
if (s == constkey)
return Token{con}; // keyword "const"
return Token{name, s};
}
error("Bad token");
}
// clang-format on
// shouldn't reach here
return Token('K'); // there is no valid 'K' token kind
}
//------------------------------------------------------------------------------
void Token_stream::putback(Token const t)
{
if (full)
error("putback() into a full buffer");
buffer = t; // copy t to buffer
full = true; // buffer is now full
}
//------------------------------------------------------------------------------
void Token_stream::ignore(char const c) // c represents the kind of a Token
{
// first look in buffer:
if (full && c == buffer.kind) {
full = false;
return;
}
// now search input:
full = false;
char ch = 0;
while (cin >> ch)
if (ch == c)
return;
}
//------------------------------------------------------------------------------
Calculator::Calculator() { set_constants(); }
//------------------------------------------------------------------------------
void Calculator::set_constants()
{
// note: these pre-defiend names are constants:
define_name("pi", 3.1415926535, false);
define_name("e", 2.7182818284, false);
}
//------------------------------------------------------------------------------
double Calculator::primary()
{
Token t = ts.get();
switch (t.kind) {
case '(': { // handle '(' expression ')'
double d = expression();
t = ts.get();
if (t.kind != ')')
error("')' expected");
return d;
}
case number: return t.value; // return the number's value
case name: {
Token next = ts.get();
if (next.kind == '=') { // handle name = expression
double d = expression();
set_value(t.name, d);
return d;
} else {
ts.putback(next); // not an assignment: return the value
return get_value(t.name); // return the variable's value
}
}
case '-': return -primary();
case '+': return primary();
default: error("primary expected");
}
// shouldn't reach here
return 0.0;
}
//------------------------------------------------------------------------------
double Calculator::term()
{
double left = primary();
Token t = ts.get(); // get the next token from token stream
while (true) {
switch (t.kind) {
case '*': left *= primary(); break;
case '/': {
double d = primary();
if (d == 0)
error("divide by zero");
left /= d;
break;
}
case '%': {
int i1 = narrow_cast<int>(left);
int i2 = narrow_cast<int>(term());
if (i2 == 0)
error("%: divide by zero");
left = i1 % i2;
break;
}
default:
ts.putback(t); // put t back into the token stream
return left;
}
t = ts.get();
}
}
//------------------------------------------------------------------------------
double Calculator::expression()
{
double left = term(); // read and evaluate a Term
Token t = ts.get(); // get the next token from token stream
while (true) {
switch (t.kind) {
case '+':
left += term(); // evaluate Term and add
break;
case '-':
left -= term(); // evaluate Term and subtract
break;
default:
ts.putback(t); // put t back into the token stream
return left; // finally: no more + or -: return the answer
}
t = ts.get();
}
}
//------------------------------------------------------------------------------
double Calculator::declaration(Token const k)
{
Token t = ts.get();
if (t.kind != name)
error("name expected in declaration");
string var_name = t.name;
Token t2 = ts.get();
if (t2.kind != '=')
error("= missing in declaration of ", var_name);
double d = expression();
define_name(var_name, d, k.kind == let);
return d;
}
//------------------------------------------------------------------------------
double Calculator::statement()
{
Token t = ts.get();
switch (t.kind) {
case let:
case con: return declaration(t.kind);
default: ts.putback(t); return expression();
}
}
//------------------------------------------------------------------------------
void Calculator::clean_up_mess() { ts.ignore(print); }
//------------------------------------------------------------------------------
void Calculator::run()
{
while (cin) {
try {
cout << prompt;
// First we'll 'peek ahead' in the Token_stream to see what's next:
Token t = ts.get();
while (t.kind == print) // discard all (consecutive) "prints"
t = ts.get(); //
if (t.kind == quit)
return;
ts.putback(t);
// Having put the Token back, we can now proceed to parse the grammar
cout << result << statement() << endl;
} catch (exception const& e) {
cerr << e.what() << endl; // write error message
clean_up_mess();
}
}
}
|
main_calculator.cpp • Calculator.h • calc_util.h • calc_util.cpp