aboutsummaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorAdrian Kummerlaender2017-04-15 23:10:49 +0200
committerAdrian Kummerlaender2017-04-15 23:10:49 +0200
commitc06a6ade2d144cd8042392e597ead385756bcbc7 (patch)
treed4dd684ead8465afb341d0379fe3a5f86f842e76 /source
parentb1fa3024fd2fae76e6e0345861f3fa17b4a082ef (diff)
downloadslang-c06a6ade2d144cd8042392e597ead385756bcbc7.tar
slang-c06a6ade2d144cd8042392e597ead385756bcbc7.tar.gz
slang-c06a6ade2d144cd8042392e597ead385756bcbc7.tar.bz2
slang-c06a6ade2d144cd8042392e597ead385756bcbc7.tar.lz
slang-c06a6ade2d144cd8042392e597ead385756bcbc7.tar.xz
slang-c06a6ade2d144cd8042392e597ead385756bcbc7.tar.zst
slang-c06a6ade2d144cd8042392e597ead385756bcbc7.zip
Move top level processing into `machine` module
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();
+ }
+}
+
+}