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 | // Code derived from Stroustrup's PPP2 book
// From Chs. 6 & 7, The Calculator Project - CLI edition
// -and beginning on p 174
#pragma once
#include <string>
using std::string;
//------------------------------------------------------------------------------
char const number = '8'; // t.kind==number means that t is a number Token
char const quit = 'q'; // t.kind==quit means that t is a quit Token
char const print = ';'; // t.kind==print means that t is a print Token
char const name = 'a'; // name token
char const let = 'L'; // declaration token
char const con = 'C'; // const declaration token
string const declkey = "let"; // declaration keyword
string const constkey = "const"; // const keyword
string const prompt = "> "; // used to indicate we're waiting on user input
string const result = "= "; // used to indicate that what follows is a result
//------------------------------------------------------------------------------
/** the type used to populate the std::vector var_table
*/
class Variable {
public:
/** Param'd ctor
*
* @param n
* @param v
* @param va - variable (true) or constant (false); defaults to true
*/
Variable(std::string const& n, double const v, bool const va = true);
string name;
double value;
bool var; // variable (true) or constant (false)
};
//------------------------------------------------------------------------------
/** add (s, val, var) to var_table
*
* @param s
* @param val
* @param var - variable (true) or constant (false); defaults to true
* @return val
* @note throws runtime_error on twice-declared variable
*/
double define_name(std::string const& s, double const val,
bool const var = true);
/** is var already declared (stored) in var_table?
*
* @param var
* @return true if it is
*/
bool is_declared(std::string const& var);
/** return the value of the Variable named s
*
* @param s
* @return s' value
* @note throws runtime_error on undefined variable
*/
double get_value(std::string const& s);
/** assign the value d to the Variable named s
*
* @param s
* @param d
* @note throws runtime_error on assigning to const, or on undefined variable
*/
void set_value(std::string const& s, double const d);
//------------------------------------------------------------------------------
/** throws a runtime_error with message s1
*
* @param s1
*/
void error(std::string const& s1);
/** throws a runtime_error with message s1 + s2
*
* @param s1
* @param s2
*/
void error(std::string const& s1, std::string const& s2);
/** run-time checked narrowing cast (type conversion)
*
* @return value a, of the type R specified for the narrowing cast
* @note throws runtime_error on information loss
*/
template <class R, class A>
R narrow_cast(A const& a)
{
R r = R(a);
if (A(r) != a)
error(string{"narrow_cast() info loss"});
return r;
}
|
main_calculator.cpp • Calculator.h • Calculator.cpp • calc_util.cpp