diff options
author | Adrian Kummerlaender | 2017-04-10 21:41:32 +0200 |
---|---|---|
committer | Adrian Kummerlaender | 2017-04-10 21:41:32 +0200 |
commit | 57ab42eabb5a9a7ddf7d6a416bf18eca63336a87 (patch) | |
tree | 9d146599a3a79ce266a90b198f3f1475698c4482 /repl.cc | |
parent | c74d8917f3890ec1ed9bab158ccea309cb05e0cd (diff) | |
download | slang-57ab42eabb5a9a7ddf7d6a416bf18eca63336a87.tar slang-57ab42eabb5a9a7ddf7d6a416bf18eca63336a87.tar.gz slang-57ab42eabb5a9a7ddf7d6a416bf18eca63336a87.tar.bz2 slang-57ab42eabb5a9a7ddf7d6a416bf18eca63336a87.tar.lz slang-57ab42eabb5a9a7ddf7d6a416bf18eca63336a87.tar.xz slang-57ab42eabb5a9a7ddf7d6a416bf18eca63336a87.tar.zst slang-57ab42eabb5a9a7ddf7d6a416bf18eca63336a87.zip |
Rewrite in D, support for word definitions
Diffstat (limited to 'repl.cc')
-rw-r--r-- | repl.cc | 99 |
1 files changed, 0 insertions, 99 deletions
diff --git a/repl.cc b/repl.cc deleted file mode 100644 index 31d8b07..0000000 --- a/repl.cc +++ /dev/null @@ -1,99 +0,0 @@ -#include <iostream> - -#include <stack> -#include <string> -#include <algorithm> - -#include <unordered_map> -#include <functional> - -#include <cctype> -#include <cstdlib> - -bool is_number(const std::string& token) { - return std::all_of( - token.begin(), - token.end(), - [](char c) { return std::isdigit(c); } - ); -} - -int main(int, char*[]) { - std::stack<int> stack; - std::string token; - - 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); - } - }, - { - "-", [&stack]() { - 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); - } - }, - { - ".", [&stack]() { - std::cout << stack.top() << std::endl; - } - }, - { - "swp", [&stack]() { - const int b = stack.top(); - stack.pop(); - const int a = stack.top(); - stack.pop(); - - 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 { - const auto& impl = builtin.find(token); - - if ( impl != builtin.end() ) { - impl->second(); - } - } - } - } - - return 0; -} |