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

import std.string;
import std.variant;
import std.typecons;
import std.container : DList;

import src.stack : Token;

alias Words = DList!Token[string];

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

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

void end() {
	string wordToBeDefined;

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

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

	definition.removeFront;
	words[wordToBeDefined] = definition;
	definition.nullify;
}

bool handle(int value) {
	if ( definition.isNull ) {
		return false;
	} else {
		definition.insertBack(Token(value));
		return true;
	}
}

bool handle(string word) {
	if ( definition.isNull ) {
		return false;
	} else {
		if ( word == ";" ) {
			end();
		} else {
			definition.insertBack(Token(word));
		}

		return true;
	}
}