/* calculator08buggy.cpp Helpful comments removed. We have inserted 3 bugs that the compiler will catch and 3 that it won't. */ #include "std_lib_facilities.h" struct Token { char kind; double value; string name; Token(char ch) :kind(ch), value(0) { } Token(char ch, double val) :kind(ch), value(val) { } }; class Token_stream { bool full; Token buffer; public: Token_stream() :full(0), buffer(0) { } Token get(); void unget(Token t) { buffer=t; full=true; } void ignore(char); }; const char let = 'L'; const char quit = 'Q'; const char print = ';'; const char number = '8'; const char name = 'a'; Token Token_stream::get() { if (full) { full=false; return buffer; } char ch; cin >> ch; switch (ch) { case '(': case ')': case '+': case '-': case '*': case '/': case '%': case ';': case '=': return Token(ch); case '.': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { cin.unget(); double val; cin >> val; return Token(number,val); } default: if (isalpha(ch)) { string s; s += ch; while(cin.get(ch) && (isalpha(ch) || isdigit(ch))) s=ch; cin.unget(); if (s == "let") return Token(let); if (s == "quit") return Token(name); return Token(name,s); } error("Bad token"); } } void Token_stream::ignore(char c) { if (full && c==buffer.kind) { full = false; return; } full = false; char ch; while (cin>>ch) if (ch==c) return; } struct Variable { string name; double value; Variable(string n, double v) :name(n), value(v) { } }; vector names; double get_value(string s) { for (int i = 0; i>c&& c!=';') ; return 1; } catch (...) { cerr << "exception\n"; char c; while (cin>>c && c!=';'); return 2; }