aboutsummaryrefslogtreecommitdiff
path: root/source/state/definition.d
blob: 6f4127538864f4d2f0d1eb1544e61de48e9d7532 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
module state.definition;

import std.string;
import std.variant;
import std.typecons;

import std.container : DList;

import state.stack;

alias Words = Stack!Token[string];

Words words;

bool handle(Token token) {
	return token.visit!(
		(int    value) => handle(value),
		(bool   value) => handle(value),
		(string word ) => handle(word)
	);
}

Stack!Token get(string word) {
	if ( word in words ) {
		return words[word].dup;
	} else {
		return Stack!Token(Token(word));
	}
}

private {

Nullable!(DList!Token) definition;

void register(DList!Token definition) {
	string wordToBeDefined;

	definition.front.visit!(
		(int    value) => wordToBeDefined = "",
		(bool   value) => wordToBeDefined = "",
		(string name ) => wordToBeDefined = name
	);

	if ( wordToBeDefined == "" ) {
		throw new Exception("words may not be numeric or boolean");
	}

	definition.removeFront;
	words[wordToBeDefined] = Stack!Token(definition[]);
}

template handle(T)
if ( is(T == int) || is(T == bool) ) {
	bool handle(T value) {
		if ( definition.isNull ) {
			return false;
		} else {
			definition.insertBack(Token(value));
			return true;
		}
	}
}

bool handle(string word) {
	if ( definition.isNull ) {
		if ( word == "§" ) {
			definition = DList!Token();
			return true;
		} else {
			return false;
		}
	} else {
		switch ( word ) {
			case "§" :
				throw new Exception("definitions may not be nested");
			case ";" :
				register(definition);
				definition.nullify;
				break;
			default:
				definition.insertBack(Token(word));
		}

		return true;
	}
}

}