aboutsummaryrefslogtreecommitdiff
path: root/src/stack.d
blob: 680587352b7db5decfd2e3ab68d980a2cf66551c (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
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, Token token) {
	if ( !token.visit!(
		(int    x   ) => src.definition.handle(x),
		(string word) => src.definition.handle(word)
	) ) {
		stack.insertFront(token);
	}
}

void push(ref Stack!Token stack, int value) {
	stack.push(Token(value));
}

void push(ref Stack!Token stack, string word) {
	stack.push(Token(word));
}