aboutsummaryrefslogtreecommitdiff
path: root/src/stack.d
blob: 756dafcbc90cf4b030edc710143783b41b99c44d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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));
	}
}