aboutsummaryrefslogtreecommitdiff
path: root/repl.d
blob: 9f1d1318f6394c75da256c1700fbf0d39f13973d (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import std.stdio;
import std.string;
import std.conv;
import std.typecons;

import std.container : SList;
import std.container : DList;

import std.variant;

alias Token = Algebraic!(int, string);

SList!Token            stack;
DList!Token[string]    words;
int[string]            variables;
Nullable!(DList!Token) definition;

void startDefinition()
in  { assert( definition.isNull); }
out { assert(!definition.isNull); }
body {
	definition = DList!Token();
}

void endDefinition()
in  { assert(!definition.isNull); }
out { assert( definition.isNull); }
body {
	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;
}

void append(ref Nullable!(DList!Token) definition, int value) {
	definition.insertBack(Token(value));
}

void append(ref Nullable!(DList!Token) definition, string word) {
	if ( word == ";" ) {
		endDefinition();
	} else {
		definition.insertBack(Token(word));
	}
}

void evaluate(string word) {
	switch ( word ) {
		case "ยง":
			startDefinition();
			break;
		case "$":
			string name  = stack.pop().get!string;
			int    value = stack.pop().get!int;

			variables[name] = value;
			break;
		case "@":
			string name = stack.pop().get!string;

			if ( name in variables ) {
				stack.push(variables[name]);
			}
			break;
		case "+":
			int a = stack.pop().get!int;
			int b = stack.pop().get!int;

			stack.push(a + b);
			break;
		case "*":
			int a = stack.pop().get!int;
			int b = stack.pop().get!int;

			stack.push(a * b);
			break;
		case ".":
			writeln(stack.front);
			break;
		default:
			if ( word in words ) {
				foreach ( token; words[word] ) {
					process(token);
				}
			} else {
				stack.push(word);
			}
	}
}

void process(Token token) {
	token.visit!(
		(int    x   ) => process(x),
		(string word) => process(word)
	);
}

void process(int x) {
	stack.push(x);
}

void process(string word) {
	evaluate(word);
}

void push(ref SList!Token stack, int value) {
	if ( definition.isNull ) {
		stack.insertFront(Token(value));
	} else {
		definition.append(value);
	}
}

void push(ref SList!Token stack, string word) {
	if ( definition.isNull ) {
		stack.insertFront(Token(word));
	} else {
		definition.append(word);
	}
}

Token pop(ref SList!Token stack) {
	Token token = stack.front;
	stack.removeFront;
	return token;
}

void main() {
	while ( !stdin.eof ) {
		foreach ( token; stdin.readln.split ) {
			if ( definition.isNull ) {
				if ( token.isNumeric ) {
					process(parse!int(token));
				} else {
					process(token);
				}
			} else {
				if ( token.isNumeric ) {
					definition.append(parse!int(token));
				} else {
					definition.append(token);
				}
			}
		}
	}
}