
//
// This is example code from Chapter 6.7 "Trying the second version" of
// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
//

#include "..\std_lib_facilities.h"

const char print = ';'; ///t.kind==print means that t is print Token
const char quit = 'q'; /// t.kind==quit means that t is a quit Token
const char number = '8'; /// t.kind==number means that t is a number Token
const string prompt = "> ";
const string result = "= "; /// used to indicate that what follows is a result
const char name = 'a'; // name token
const char let = 'L'; // declaration token
const string declkey = "let"; // declaration keyword
///------------------------------------------------------------------------------

bool debug = false;
///------------------------------------------------------------------------------

class Token {
public:
    char kind;        // what kind of token
    double value;     // for numbers: a value
    string var_name;
    Token(char ch)    // make a Token from a char
        :kind(ch), value(0) { }
    Token(char ch, double val)     // make a Token from a char and a double
        :kind(ch), value(val) { }
    Token(char ch, string s)
        :kind(ch), var_name(s) { }
};

//------------------------------------------------------------------------------

class Token_stream {
public:
    Token_stream();   // make a Token_stream that reads from cin
    Token get();      // get a Token (get() is defined elsewhere)
    void putback(Token t);    // put a Token back
private:
    bool full;        // is there a Token in the buffer?
    Token buffer;     // here is where we keep a Token put back using putback()
};

//------------------------------------------------------------------------------

// The constructor just sets full to indicate that the buffer is empty:
Token_stream::Token_stream()
:full(false), buffer(0)    // no Token in buffer
{
}

//------------------------------------------------------------------------------

// The putback() member function puts its argument back into the Token_stream's buffer:
void Token_stream::putback(Token t)
{
    if (full) error("putback() into a full buffer");
    buffer = t;       // copy t to buffer
    full = true;      // buffer is now full
}

//------------------------------------------------------------------------------

Token Token_stream::get()
{
    if (full) {       // do we already have a Token ready?
        // remove token from buffer
        full=false;
        if(debug) cout << "\nget() popped (" << buffer.kind << "," << buffer.value << ")" << endl;
        return buffer;
    }

    char ch;
    cin >> ch;    // note that >> skips whitespace (space, newline, tab, etc.)

    switch (ch) {
    case ';':    // for "print"
    case 'q':    // for "quit"
    case '(': case ')': case '+': case '-': case '*':
    case '/': case '!': case '%': case '=':
        return Token(ch);        // let each character represent itself
    case '.':
    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
    {
        cin.putback(ch);         // put digit back into the input stream
        double val;
        cin >> val;              // read a floating-point number
        return Token(number,val);   /// number token
    }
    default:
        if (isalpha(ch)) {
            ///cin.putback(ch);
            string s;
            s+=ch;
            ///cin>>s;
            while(cin.get(ch) && (isalpha(ch) || isdigit(ch))) s += ch;
            if (s == declkey) return Token(let); // declaration keyword
            return Token{name,s};
        }
        error("Bad token");
    }
}

//------------------------------------------------------------------------------

class Variable {
public:
    string name;
    double value;
    Variable(string s, double d) : name(s), value(d) { }
};

vector<Variable> var_table;

double get_value(string s)
// return the value of the Variable named s
{
    for(int i = 0; i < var_table.size(); ++i) ///(const Variable& v : var_table)
        ///if (v.name == s) return v.value;
        if(var_table[i].name == s) return var_table[i].value;
    error("get: undefined variable ", s);
}

void set_value(string s, double d)
// set the Variable named s to d
{
    for (Variable& v : var_table)
        if (v.name == s) {
            v.value = d;
            return;
        }
    error("set: undefined variable ", s);
}
//------------------------------------------------------------------------------

bool is_declared(string var)
// is var already in var_table?
{
    for (const Variable& v : var_table)
        if (v.name == var) return true;
    return false;
}

double define_name(string var, double val)
// add (var,val) to var_table
{
    if (is_declared(var)) error(var," declared twice");
    var_table.push_back(Variable(var,val));
    return val;
}

double expression(Token_stream&);    // declaration so that primary() can call expression()


double declaration(Token_stream& ts)
/// assume we have seen "let”
/// handle: name = expression
/// declare a variable called "name” with the initial value "expression”
{
    Token t = ts.get();
    if (t.kind != name) error ("name expected in declaration");

    string varName = t.var_name;
    Token t2 = ts.get();

    if (t2.kind != '=') error("= missing in declaration of ", varName);

    double d = expression(ts);
    define_name(varName,d);
    return d;
}

double statement(Token_stream& ts)
{
    Token t = ts.get();
    switch (t.kind) {
    case let:
        return declaration(ts);
    default:
        ts.putback(t);
        return expression(ts);
    }
}

double factorial(double);

//------------------------------------------------------------------------------


//------------------------------------------------------------------------------

// deal with numbers and parentheses
double primary(Token_stream& ts)
{
    Token t = ts.get();
    if(debug) cout << "\nprimary got token (" << t.kind << "," << t.value << ")" << endl;
    switch (t.kind) {
    case '-': return -primary(ts);
    case '+': return primary(ts);
    case name:
        return get_value(t.var_name); // return the variable's value
    case '(':    // handle '(' expression ')'
    {
        double d = expression(ts);
        t = ts.get();
        if (t.kind != ')') error("')' expected");
        return d;
    }
    case number:            // a number token is found
        return t.value;  // return the number's value
    default:
        error("primary missing");
    }
}

//------------------------------------------------------------------------------

// deal with *, /, and %
double term(Token_stream& ts)
{
    double left = primary(ts);
    if(debug) cout << "\nterm() assigned " << left << " to its left." << endl;
    Token t = ts.get();        // get the next token from token stream
    if(debug) cout << "\nterm() got (" << t.kind << "," << t.value << ")" << endl;
    while(true) {
        switch (t.kind) {
        case '*':
            left *= primary(ts);
            t = ts.get();
            break;
        case '/':
        {
            double d = primary(ts);
            if (d == 0) error("divide by zero");
            left /= d;
            t = ts.get();
            break;
        }
        case '%' :
        {
            double d = primary(ts);
            if(d==0) error("division by zero");
            left = fmod(left,d);
            t = ts.get();
            break;
        }
        case '!':
            return factorial(left);
        default:
            ts.putback(t);     /// put t back into the token stream buffer
            if(debug)  cout << "\nterm() put (" << t.kind << "," << t.value << ") into the buffer." << endl;
            return left;
        }
    }
}

//------------------------------------------------------------------------------

// deal with + and -
double expression(Token_stream& ts)
{
    double left = term(ts);      // read and evaluate a Term
    if(debug) cout << "\nexpression() assigned " << left << " to its left." << endl;
    Token t = ts.get();        // get the next token from token stream
    if(debug) cout << "\nexpression() got (" << t.kind << "," << t.value << ")" << endl;
    while(true) {
        switch(t.kind) {
        case '+':
            left += term(ts);    // evaluate Term and add
            t = ts.get();
            break;
        case '-':
            left -= term(ts);    // evaluate Term and subtract
            t = ts.get();
            break;
        default:
            ts.putback(t);     // put t back into the token stream
            return left;       // finally: no more + or -: return the answer
        }
    }
}

//------------------------------------------------------------------------------
void calculate() // expression evaluation loop
{
    Token_stream ts;        /// provides get() and putback()
    while (cin) {
        cout << prompt;
        Token t = ts.get();
        while (t.kind == print) t=ts.get(); // first discard all “prints”
        if (t.kind == quit) return;
        ts.putback(t);
        cout << result << statement(ts) << '\n';
    }
}

//------------------------------------------------------------------------------

int main()
try
{
    calculate();
    return 0;
}
catch (exception& e) {
    cerr << "error: " << e.what() << '\n';
    return 1;
}
catch (...) {
    cerr << "Oops: unknown exception!\n";
    return 2;
}

//------------------------------------------------------------------------------

double factorial(double n) {
    if(n!=int(n) || n < 0) error("factorial takes a positive integer.");
    /// base case
    if(n==0) return 1.0;
    else return(n*factorial(n-1));
}
