From c6d2b259a4f253403929f6f1104395a6a71b1be6 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Fri, 14 Apr 2017 23:21:51 +0200 Subject: Convert structure to _dub_ build system --- source/base/stack.d | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 source/base/stack.d (limited to 'source/base/stack.d') 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)); + } +} -- cgit v1.2.3