aboutsummaryrefslogtreecommitdiff
path: root/repl.d
blob: e3aef2b895559003d8172b965a127f07da46e561 (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
import std.stdio;
import std.string;
import std.conv;
import std.variant;
import std.typecons;

import std.container : SList;

import src.stack;

static import definition = src.definition;
static import primitives = src.primitives.eval;

void process(int x) {
	try {
		if ( !primitives.evaluate(x) ) {
			stack.push(x);
		}
	}
	catch (Exception ex) {
		writeln("Error: ", ex.msg);
	}
}

void process(string word) {
	try {
		if ( !primitives.evaluate(word) ) {
			if ( word in definition.words ) {
				foreach ( token; definition.words[word] ) {
					process(token);
				}
			} else {
				stack.push(word);
			}
		}
	}
	catch (Exception ex) {
		writeln("Error: ", ex.msg);
	}
}

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

void main() {
	while ( !stdin.eof ) {
		foreach ( token; stdin.readln.split ) {
			if ( token.isNumeric ) {
				immutable int value = parse!int(token);

				if ( !definition.handle(value) ) {
					process(value);
				}
			} else {
				if ( !definition.handle(token) ) {
					process(token);
				}
			}
		}
	}
}