void test_proc(TokenStream &ts) { using namespace Tokens; Token t; t = ts.get(); switch (t.kind) { case create: makeCharacter(); break; default: cout << "No command" << endl; return; } }
std::stack<std::unique_ptr<Token> > exprTree::getStack(TokenStream &ts) { std::stack<std::unique_ptr<Token> > reverse; while(!ts.empty()) { std::unique_ptr<Token> t = ts.get(); reverse.push(std::move(t)); } std::stack<std::unique_ptr<Token> > returnStack; while (!reverse.empty()) { returnStack.push(std::move(reverse.top())); reverse.pop(); } return returnStack; }
std::stack<std::unique_ptr<Token> > exprTree::inToPost(TokenStream &ts) { /* This will take your lousy stream of infix tokens and turn it into * a glorious stack of tokens in postfix notation * * It is created in such a way the the begginning of the equation lies at the bottom of the stack * so the stack is reversed at the end */ std::stack<std::unique_ptr<Token> > postEqn; // Reverse std::stack<std::unique_ptr<Token> > workingStack; while (!ts.empty()) { std::unique_ptr<Token> t = ts.get(); if (t->kind == operand) { postEqn.push(std::move(t)); } else if (t->kind == bracket) { auto temp = static_cast<Bracket*>(t.release()); auto b = std::unique_ptr<Bracket>(temp); if (b->typ == '(') { workingStack.push(std::move(b)); } else { // Closed bracket bool matched = false; // Used for error checking while(!workingStack.empty()) { if (workingStack.top()->kind == bracket) { // It must be open becuse closed don't go on this stack workingStack.pop(); // We discard that open bracket matched = true; break; } else { // a little redundant beause if true it returns anyway // Add operand to the eqn postEqn.push(std::move(workingStack.top())); workingStack.pop(); } } if (!matched) { std::cerr << "Unequal brackets!!" << std::endl; } } } else if (t->kind == oprtor) { auto temp = static_cast<Oprtor*>(t.release()); auto o = std::unique_ptr<Oprtor>(temp); while(!workingStack.empty() && workingStack.top()->kind != bracket && (static_cast<Oprtor*>(workingStack.top().get()))->precedence >= o->precedence) { postEqn.push(std::move(workingStack.top())); workingStack.pop(); } workingStack.push(std::move(o)); } } while(!workingStack.empty()) { postEqn.push(std::move(workingStack.top())); workingStack.pop(); } // This is where it's reversed to we can actually use it std::stack<std::unique_ptr<Token> > returnStack; while (!postEqn.empty()) { returnStack.push(std::move(postEqn.top())); postEqn.pop(); } return returnStack; }