diff options
author | Adrian Kummerlaender | 2017-03-30 22:56:30 +0200 |
---|---|---|
committer | Adrian Kummerlaender | 2017-03-30 22:56:30 +0200 |
commit | 1181260cd68fe25be5d312bc132215a3a427179a (patch) | |
tree | 42d438e140219e1279ea74d2f56ccac5ba763f45 | |
parent | 5046728ca7285f2a2139c14252ee657c0ab3abcf (diff) | |
download | slang-1181260cd68fe25be5d312bc132215a3a427179a.tar slang-1181260cd68fe25be5d312bc132215a3a427179a.tar.gz slang-1181260cd68fe25be5d312bc132215a3a427179a.tar.bz2 slang-1181260cd68fe25be5d312bc132215a3a427179a.tar.lz slang-1181260cd68fe25be5d312bc132215a3a427179a.tar.xz slang-1181260cd68fe25be5d312bc132215a3a427179a.tar.zst slang-1181260cd68fe25be5d312bc132215a3a427179a.zip |
Implement swap, duplicate and delete words
-rw-r--r-- | repl.cc | 17 |
1 files changed, 16 insertions, 1 deletions
@@ -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<int>& stack, const std::string& token) { @@ -46,6 +49,18 @@ void process(std::stack<int>& 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(); } } |