aboutsummaryrefslogtreecommitdiff
path: root/source/base/definition.d
blob: cf64f3ecf621d754508e2532f12d0c41a714883b (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
module base.definition;

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

import std.container : DList;

import base.stack;

alias Words = Stack!Token[string];

Nullable!(DList!Token) definition;
Words                  words;

void start() {
	definition = DList!Token();
}

void end() {
	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[]);
	definition.nullify;
}

bool handle(Token token) {
	if ( definition.isNull ) {
		return false;
	} else {
		if ( token.type == typeid(string) ) {
			if ( *token.peek!string == ";" ) {
				end;
			} else {
				definition.insertBack(token);
			}
		} else {
			definition.insertBack(token);
		}

		return true;
	}
}

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