aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2017-03-30 22:56:30 +0200
committerAdrian Kummerlaender2017-03-30 22:56:30 +0200
commit1181260cd68fe25be5d312bc132215a3a427179a (patch)
tree42d438e140219e1279ea74d2f56ccac5ba763f45
parent5046728ca7285f2a2139c14252ee657c0ab3abcf (diff)
downloadslang-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.cc17
1 files changed, 16 insertions, 1 deletions
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<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();
}
}