aboutsummaryrefslogtreecommitdiff
path: root/source/state/stack.d
blob: 3c6230fae461c0a82e957f89ee641ef9c29ef1e9 (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
39
40
41
42
43
44
45
46
47
48
module state.stack;

import std.conv;
import std.string;
import std.variant;
import std.container : SList;

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;
}

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));
	}
}

void push(ref Stack!Token stack, Token token) {
	stack.insertFront(token);
}

void push(ref Stack!Token stack, Stack!Token prefix) {
	stack.insertFront(prefix[]);
}