aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt16
-rw-r--r--repl.cc99
-rw-r--r--repl.d104
3 files changed, 104 insertions, 115 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
deleted file mode 100644
index 4deaa23..0000000
--- a/CMakeLists.txt
+++ /dev/null
@@ -1,16 +0,0 @@
-cmake_minimum_required(VERSION 2.8)
-project(lang)
-
-set(
- CMAKE_CXX_FLAGS
- "${CMAKE_CXX_FLAGS} -std=c++14 -W -Wall -Wextra -Winline -pedantic"
-)
-
-include_directories(
- src/
-)
-
-add_executable(
- repl
- repl.cc
-)
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;
-}
diff --git a/repl.d b/repl.d
new file mode 100644
index 0000000..9ea36d6
--- /dev/null
+++ b/repl.d
@@ -0,0 +1,104 @@
+import std.stdio;
+import std.string;
+import std.conv;
+import std.container : SList;
+import std.container : DList;
+import std.variant;
+
+alias Token = Algebraic!(int, string);
+
+auto dataStack = SList!int();
+
+bool defMode = false;
+string defModeWord = "";
+
+DList!Token[string] wordMap;
+
+void push(ref DList!Token stack, int value) {
+ stack.insertBack(Token(value));
+}
+
+void push(ref DList!Token stack, string word) {
+ stack.insertBack(Token(word));
+}
+
+void push(ref DList!Token[string] map, int value) {
+ map[defModeWord].push(value);
+}
+
+void push(ref DList!Token[string] map, string word) {
+ if ( defModeWord == "" ) {
+ defModeWord = word;
+ map[defModeWord] = DList!Token();
+ } else {
+ map[defModeWord].push(word);
+ }
+}
+
+void push(ref SList!int stack, int value) {
+ if ( defMode ) {
+ wordMap.push(value);
+ } else {
+ stack.insertFront(value);
+ }
+}
+
+void push(ref SList!int stack, string word) {
+ if ( defMode ) {
+ if ( word == ";" ) {
+ defMode = false;
+ defModeWord = "";
+ } else {
+ wordMap.push(word);
+ }
+ } else {
+ switch ( word ) {
+ case "ยง":
+ defMode = true;
+ defModeWord = "";
+ break;
+ case "+":
+ auto a = stack.pop();
+ auto b = stack.pop();
+
+ stack.push(a + b);
+ break;
+ case "*":
+ auto a = stack.pop();
+ auto b = stack.pop();
+
+ stack.push(a * b);
+ break;
+ case ".":
+ writeln(stack.front);
+ break;
+ default:
+ foreach ( token; wordMap[word] ) {
+ if ( token.type == typeid(int) ) {
+ stack.push(*token.peek!int);
+ } else {
+ stack.push(*token.peek!string);
+ }
+ }
+ }
+ }
+}
+
+int pop(ref SList!int stack) {
+ auto x = stack.front;
+ stack.removeFront;
+
+ return x;
+}
+
+void main() {
+ while ( !stdin.eof ) {
+ foreach ( word; stdin.readln.split ) {
+ if ( word.isNumeric ) {
+ dataStack.push(parse!int(word));
+ } else {
+ dataStack.push(word);
+ }
+ }
+ }
+}