aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2017-03-31 19:18:35 +0200
committerAdrian Kummerlaender2017-03-31 19:18:35 +0200
commitc74d8917f3890ec1ed9bab158ccea309cb05e0cd (patch)
tree2b95ac1290722c2a08f12dde14a73e1c452f4a9f
parent1181260cd68fe25be5d312bc132215a3a427179a (diff)
downloadslang-c74d8917f3890ec1ed9bab158ccea309cb05e0cd.tar
slang-c74d8917f3890ec1ed9bab158ccea309cb05e0cd.tar.gz
slang-c74d8917f3890ec1ed9bab158ccea309cb05e0cd.tar.bz2
slang-c74d8917f3890ec1ed9bab158ccea309cb05e0cd.tar.lz
slang-c74d8917f3890ec1ed9bab158ccea309cb05e0cd.tar.xz
slang-c74d8917f3890ec1ed9bab158ccea309cb05e0cd.tar.zst
slang-c74d8917f3890ec1ed9bab158ccea309cb05e0cd.zip
Implement primitives as lambda expressions in an unordered set
-rw-r--r--repl.cc115
1 files changed, 66 insertions, 49 deletions
diff --git a/repl.cc b/repl.cc
index a44a733..31d8b07 100644
--- a/repl.cc
+++ b/repl.cc
@@ -4,6 +4,9 @@
#include <string>
#include <algorithm>
+#include <unordered_map>
+#include <functional>
+
#include <cctype>
#include <cstdlib>
@@ -15,65 +18,79 @@ bool is_number(const std::string& token) {
);
}
-bool is_primitive(const std::string& token) {
- return token == "+"
- || token == "-"
- || token == "*"
- || token == "."
- || token == "swp"
- || token == "dup"
- || token == "del";
-}
-
-void process(std::stack<int>& stack, const std::string& token) {
- if ( token == "+" ) {
- const int b = stack.top();
- stack.pop();
- const int a = stack.top();
- stack.pop();
+int main(int, char*[]) {
+ std::stack<int> stack;
+ std::string token;
- stack.push(a + b);
- } else if ( token == "-" ) {
- const int b = stack.top();
- stack.pop();
- const int a = stack.top();
- stack.pop();
+ std::unordered_map<std::string, std::function<void(void)>> builtin{
+ {
+ "+", [&stack]() {
+ const int b = stack.top();
+ stack.pop();
+ const int a = stack.top();
+ stack.pop();
- stack.push(a - b);
- } else if ( token == "*" ) {
- const int b = stack.top();
- stack.pop();
- const int a = stack.top();
- stack.pop();
+ stack.push(a + b);
+ }
+ },
+ {
+ "-", [&stack]() {
+ const int b = stack.top();
+ stack.pop();
+ const int a = stack.top();
+ stack.pop();
- 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(a - b);
+ }
+ },
+ {
+ "*", [&stack]() {
+ 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();
- }
-}
+ stack.push(a * b);
+ }
+ },
+ {
+ ".", [&stack]() {
+ std::cout << stack.top() << std::endl;
+ }
+ },
+ {
+ "swp", [&stack]() {
+ const int b = stack.top();
+ stack.pop();
+ const int a = stack.top();
+ stack.pop();
-int main(int, char*[]) {
- std::stack<int> stack;
- std::string token;
+ stack.push(b);
+ stack.push(a);
+ }
+ },
+ {
+ "dup", [&stack]() {
+ stack.push(stack.top());
+ }
+ },
+ {
+ "del", [&stack]() {
+ stack.pop();
+ }
+ }
+ };
while ( std::cin.good() ) {
if ( std::cin >> token ) {
if ( is_number(token) ) {
stack.push(std::atoi(token.c_str()));
- } else if ( is_primitive(token) ) {
- process(stack, token);
+ } else {
+ const auto& impl = builtin.find(token);
+
+ if ( impl != builtin.end() ) {
+ impl->second();
+ }
}
}
}