aboutsummaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
Diffstat (limited to 'source')
-rw-r--r--source/app.d50
-rw-r--r--source/machine.d52
2 files changed, 56 insertions, 46 deletions
diff --git a/source/app.d b/source/app.d
index 35b2d88..4e7b546 100644
--- a/source/app.d
+++ b/source/app.d
@@ -1,54 +1,12 @@
-import std.stdio;
-import std.string;
-import std.variant;
+import std.string : split;
+import std.stdio : stdin, readln, writeln;
-import std.container.util : make;
-
-import base.stack;
-
-import definition = base.definition;
-import primitives = primitives.eval;
-
-Stack!Token evaluate(Token token) {
- try {
- if ( primitives.evaluate(token) ) {
- return primitives.result;
- } else {
- return token.visit!(
- (int value) => Stack!Token(Token(value)),
- (bool value) => Stack!Token(Token(value)),
- (string word ) => definition.get(word)
- );
- }
- }
- catch (Exception ex) {
- writeln("Error: ", ex.msg);
- return Stack!Token();
- }
-}
-
-void process(string value) {
- auto buffer = make!(Stack!Token)(toToken(value));
-
- do {
- Token current = buffer.pop;
- Stack!Token result = evaluate(current);
-
- if ( !result.empty ) {
- if ( result.front == current ) {
- stack.push(current);
- } else {
- buffer.push(result);
- }
- }
- }
- while ( !buffer.empty );
-}
+static import machine;
void main() {
while ( !stdin.eof ) {
foreach ( token; stdin.readln.split ) {
- process(token);
+ machine.process(token);
}
}
}
diff --git a/source/machine.d b/source/machine.d
new file mode 100644
index 0000000..61d20cb
--- /dev/null
+++ b/source/machine.d
@@ -0,0 +1,52 @@
+module machine;
+
+import std.string;
+import std.variant;
+
+import std.stdio : writeln;
+import std.container.util : make;
+
+import definition = base.definition;
+import primitives = primitives.eval;
+
+import base.stack;
+
+void process(string value) {
+ auto buffer = make!(Stack!Token)(toToken(value));
+
+ do {
+ Token current = buffer.pop;
+ Stack!Token result = evaluate(current);
+
+ if ( !result.empty ) {
+ if ( result.front == current ) {
+ stack.push(current);
+ } else {
+ buffer.push(result);
+ }
+ }
+ }
+ while ( !buffer.empty );
+}
+
+private {
+
+Stack!Token evaluate(Token token) {
+ try {
+ if ( primitives.evaluate(token) ) {
+ return primitives.result;
+ } else {
+ return token.visit!(
+ (int value) => Stack!Token(Token(value)),
+ (bool value) => Stack!Token(Token(value)),
+ (string word ) => definition.get(word)
+ );
+ }
+ }
+ catch (Exception ex) {
+ writeln("Error: ", ex.msg);
+ return Stack!Token();
+ }
+}
+
+}