From 1181260cd68fe25be5d312bc132215a3a427179a Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Thu, 30 Mar 2017 22:56:30 +0200 Subject: Implement swap, duplicate and delete words --- repl.cc | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/repl.cc b/repl.cc index 616d0f3..a44a733 100644 --- a/repl.cc +++ b/repl.cc @@ -19,7 +19,10 @@ bool is_primitive(const std::string& token) { return token == "+" || token == "-" || token == "*" - || token == "."; + || token == "." + || token == "swp" + || token == "dup" + || token == "del"; } void process(std::stack& stack, const std::string& token) { @@ -46,6 +49,18 @@ void process(std::stack& stack, const std::string& token) { stack.push(a * b); } else if ( token == "." ) { std::cout << stack.top() << std::endl; + } else if ( token == "swp" ) { + const int b = stack.top(); + stack.pop(); + const int a = stack.top(); + stack.pop(); + + stack.push(b); + stack.push(a); + } else if ( token == "dup" ) { + stack.push(stack.top()); + } else if ( token == "del" ) { + stack.pop(); } } -- cgit v1.2.3