From 45e4fe29a237ae5cda4147c803046ff5f6793770 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Wed, 12 Apr 2017 14:01:50 +0200 Subject: Modularize implementation --- src/stack.d | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/stack.d (limited to 'src/stack.d') 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)); + } +} -- cgit v1.2.3