aboutsummaryrefslogtreecommitdiff
path: root/src/stack.d
diff options
context:
space:
mode:
Diffstat (limited to 'src/stack.d')
-rw-r--r--src/stack.d38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/stack.d b/src/stack.d
new file mode 100644
index 0000000..756dafc
--- /dev/null
+++ b/src/stack.d
@@ -0,0 +1,38 @@
+module src.stack;
+
+import std.variant;
+import std.string;
+import std.container : SList;
+
+static import src.definition;
+
+alias Token = Algebraic!(int, string);
+alias Stack = SList;
+
+Stack!Token stack;
+
+Token top(ref Stack!Token stack) {
+ if ( stack.empty ) {
+ throw new Exception("stack is empty");
+ } else {
+ return stack.front;
+ }
+}
+
+Token pop(ref Stack!Token stack) {
+ Token token = stack.top;
+ stack.removeFront;
+ return token;
+}
+
+void push(ref Stack!Token stack, int value) {
+ if ( !src.definition.handle(value) ) {
+ stack.insertFront(Token(value));
+ }
+}
+
+void push(ref Stack!Token stack, string word) {
+ if ( !src.definition.handle(word) ) {
+ stack.insertFront(Token(word));
+ }
+}