aboutsummaryrefslogtreecommitdiff
path: root/source/base
diff options
context:
space:
mode:
authorAdrian Kummerlaender2017-04-14 23:21:51 +0200
committerAdrian Kummerlaender2017-04-14 23:21:51 +0200
commitc6d2b259a4f253403929f6f1104395a6a71b1be6 (patch)
tree6d4cdfa4ab50fbe0fa982db601d772c67e4dda20 /source/base
parent963ace2e5ba0337130e5f343d7ab97a30b4547ae (diff)
downloadslang-c6d2b259a4f253403929f6f1104395a6a71b1be6.tar
slang-c6d2b259a4f253403929f6f1104395a6a71b1be6.tar.gz
slang-c6d2b259a4f253403929f6f1104395a6a71b1be6.tar.bz2
slang-c6d2b259a4f253403929f6f1104395a6a71b1be6.tar.lz
slang-c6d2b259a4f253403929f6f1104395a6a71b1be6.tar.xz
slang-c6d2b259a4f253403929f6f1104395a6a71b1be6.tar.zst
slang-c6d2b259a4f253403929f6f1104395a6a71b1be6.zip
Convert structure to _dub_ build system
Diffstat (limited to 'source/base')
-rw-r--r--source/base/definition.d62
-rw-r--r--source/base/stack.d48
2 files changed, 110 insertions, 0 deletions
diff --git a/source/base/definition.d b/source/base/definition.d
new file mode 100644
index 0000000..cf64f3e
--- /dev/null
+++ b/source/base/definition.d
@@ -0,0 +1,62 @@
+module base.definition;
+
+import std.string;
+import std.variant;
+import std.typecons;
+
+import std.container : DList;
+
+import base.stack;
+
+alias Words = Stack!Token[string];
+
+Nullable!(DList!Token) definition;
+Words words;
+
+void start() {
+ definition = DList!Token();
+}
+
+void end() {
+ string wordToBeDefined;
+
+ definition.front.visit!(
+ (int value) => wordToBeDefined = "",
+ (bool value) => wordToBeDefined = "",
+ (string name ) => wordToBeDefined = name
+ );
+
+ if ( wordToBeDefined == "" ) {
+ throw new Exception("words may not be numeric or boolean");
+ }
+
+ definition.removeFront;
+ words[wordToBeDefined] = Stack!Token(definition[]);
+ definition.nullify;
+}
+
+bool handle(Token token) {
+ if ( definition.isNull ) {
+ return false;
+ } else {
+ if ( token.type == typeid(string) ) {
+ if ( *token.peek!string == ";" ) {
+ end;
+ } else {
+ definition.insertBack(token);
+ }
+ } else {
+ definition.insertBack(token);
+ }
+
+ return true;
+ }
+}
+
+Stack!Token get(string word) {
+ if ( word in words ) {
+ return words[word].dup;
+ } else {
+ return Stack!Token(Token(word));
+ }
+}
diff --git a/source/base/stack.d b/source/base/stack.d
new file mode 100644
index 0000000..253bac0
--- /dev/null
+++ b/source/base/stack.d
@@ -0,0 +1,48 @@
+module base.stack;
+
+import std.conv;
+import std.string;
+import std.variant;
+import std.container : SList;
+
+import definition = base.definition;
+
+alias Token = Algebraic!(int, bool, string);
+alias Stack = SList;
+
+Stack!Token stack;
+
+Token toToken(string value) {
+ if ( value.isNumeric ) {
+ return Token(parse!int(value));
+ } else {
+ return Token(value);
+ }
+}
+
+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, Token token) {
+ if ( !definition.handle(token) ) {
+ stack.insertFront(token);
+ }
+}
+
+template push(T)
+if ( is(T == int) || is(T == bool) || is (T == string) ) {
+ void push(ref Stack!Token stack, T value) {
+ stack.push(Token(value));
+ }
+}