aboutsummaryrefslogtreecommitdiff
path: root/source/primitives
diff options
context:
space:
mode:
authorAdrian Kummerlaender2017-04-14 23:21:51 +0200
committerAdrian Kummerlaender2017-04-14 23:21:51 +0200
commitc6d2b259a4f253403929f6f1104395a6a71b1be6 (patch)
tree6d4cdfa4ab50fbe0fa982db601d772c67e4dda20 /source/primitives
parent963ace2e5ba0337130e5f343d7ab97a30b4547ae (diff)
downloadslang-c6d2b259a4f253403929f6f1104395a6a71b1be6.tar
slang-c6d2b259a4f253403929f6f1104395a6a71b1be6.tar.gz
slang-c6d2b259a4f253403929f6f1104395a6a71b1be6.tar.bz2
slang-c6d2b259a4f253403929f6f1104395a6a71b1be6.tar.lz
slang-c6d2b259a4f253403929f6f1104395a6a71b1be6.tar.xz
slang-c6d2b259a4f253403929f6f1104395a6a71b1be6.tar.zst
slang-c6d2b259a4f253403929f6f1104395a6a71b1be6.zip
Convert structure to _dub_ build system
Diffstat (limited to 'source/primitives')
-rw-r--r--source/primitives/conditional.d77
-rw-r--r--source/primitives/core.d101
-rw-r--r--source/primitives/eval.d53
3 files changed, 231 insertions, 0 deletions
diff --git a/source/primitives/conditional.d b/source/primitives/conditional.d
new file mode 100644
index 0000000..678f516
--- /dev/null
+++ b/source/primitives/conditional.d
@@ -0,0 +1,77 @@
+module primitives.conditional;
+
+import std.variant;
+import std.typecons;
+import std.container : DList;
+
+import base.stack;
+
+Nullable!(DList!Token) buffer;
+bool concluded = true;
+bool drop_mode = false;
+
+void capture(Token token) {
+ if ( !drop_mode ) {
+ buffer.insertBack(token);
+ }
+}
+
+bool drop(Token token) {
+ if ( concluded && buffer.isNull ) {
+ return false;
+ }
+
+ if ( token.type == typeid(string) ) {
+ switch ( *token.peek!string ) {
+ case "if" : eval_if; break;
+ case "then" : eval_then; break;
+ case "else" : eval_else; break;
+ default : capture(token); break;
+ }
+ } else {
+ capture(token);
+ }
+
+ return true;
+}
+
+void eval_if() {
+ if ( concluded ) {
+ buffer = DList!Token();
+ drop_mode = !stack.pop.get!bool;
+ concluded = false;
+ } else {
+ throw new Exception("conditionals may not be nested directly");
+ }
+}
+
+void eval_then() {
+ if ( concluded ) {
+ throw new Exception("`then` without preceding `if`");
+ } else {
+ drop_mode = !drop_mode;
+ }
+}
+
+void eval_else() {
+ if ( concluded ) {
+ throw new Exception("`else` without preceding `if`");
+ } else {
+ drop_mode = false;
+ concluded = true;
+ }
+}
+
+bool dischargeable() {
+ return concluded && !buffer.isNull;
+}
+
+Stack!Token discharge() {
+ if ( concluded ) {
+ Stack!Token result = buffer[];
+ buffer.nullify;
+ return result;
+ } else {
+ throw new Exception("unconcluded conditional may not be discharged");
+ }
+}
diff --git a/source/primitives/core.d b/source/primitives/core.d
new file mode 100644
index 0000000..45bbeac
--- /dev/null
+++ b/source/primitives/core.d
@@ -0,0 +1,101 @@
+module primitives.core;
+
+import std.stdio;
+
+import base.stack;
+import definition = base.definition;
+
+Token[string] variables;
+
+void definition_start() {
+ definition.start;
+}
+
+void binary_op_variable_bind() {
+ string name = stack.pop.get!string;
+ Token value = stack.pop;
+ variables[name] = value;
+}
+
+void unary_op_variable_resolve() {
+ string name = stack.pop.get!string;
+
+ if ( name in variables ) {
+ stack.push(variables[name]);
+ }
+}
+
+void binary_op_add() {
+ int b = stack.pop.get!int;
+ int a = stack.pop.get!int;
+
+ stack.push(a + b);
+}
+
+void binary_op_multiply() {
+ int b = stack.pop.get!int;
+ int a = stack.pop.get!int;
+
+ stack.push(a * b);
+}
+
+void binary_op_divide() {
+ int b = stack.pop.get!int;
+ int a = stack.pop.get!int;
+
+ if ( b == 0 ) {
+ throw new Exception("division by 0 undefined");
+ } else {
+ stack.push(a / b);
+ }
+}
+
+void binary_op_modulo() {
+ int b = stack.pop.get!int;
+ int a = stack.pop.get!int;
+
+ if ( b == 0 ) {
+ throw new Exception("modulo 0 undefined");
+ } else {
+ stack.push(a % b);
+ }
+}
+
+void unary_op_io_print() {
+ writeln(stack.top);
+}
+
+void unary_op_stack_pop() {
+ stack.pop;
+}
+
+void unary_op_stack_dup() {
+ stack.push(stack.top);
+}
+
+void binary_op_stack_swp() {
+ auto b = stack.pop;
+ auto a = stack.pop;
+
+ stack.push(b);
+ stack.push(a);
+
+}
+
+void binary_cond_lt() {
+ int b = stack.pop.get!int;
+ int a = stack.pop.get!int;
+
+ stack.push(a < b);
+}
+
+void binary_cond_eq() {
+ auto b = stack.pop;
+ auto a = stack.pop;
+
+ stack.push(a == b);
+}
+
+void integral_value_bool(bool value) {
+ stack.push(Token(value));
+}
diff --git a/source/primitives/eval.d b/source/primitives/eval.d
new file mode 100644
index 0000000..0ddd65a
--- /dev/null
+++ b/source/primitives/eval.d
@@ -0,0 +1,53 @@
+module primitives.eval;
+
+import std.variant;
+
+import base.stack;
+import primitives.core;
+import conditional = primitives.conditional;
+
+bool evaluate_primitive(string word) {
+ switch ( word ) {
+ case "ยง" : definition_start; break;
+ case "$" : binary_op_variable_bind; break;
+ case "@" : unary_op_variable_resolve; break;
+ case "if" : conditional.eval_if; break;
+ case "then" : conditional.eval_then; break;
+ case "else" : conditional.eval_else; break;
+ case "+" : binary_op_add; break;
+ case "*" : binary_op_multiply; break;
+ case "/" : binary_op_divide; break;
+ case "%" : binary_op_modulo; break;
+ case "." : unary_op_io_print; break;
+ case "pop" : unary_op_stack_pop; break;
+ case "dup" : unary_op_stack_dup; break;
+ case "swp" : binary_op_stack_swp; break;
+ case "true" : integral_value_bool(true); break;
+ case "false" : integral_value_bool(false); break;
+ case "<" : binary_cond_lt; break;
+ case "=" : binary_cond_eq; break;
+ default : return false;
+ }
+
+ return true;
+}
+
+bool evaluate(Token token) {
+ if ( conditional.drop(token) ) {
+ return true;
+ } else {
+ return token.visit!(
+ (int value) => false,
+ (bool value) => false,
+ (string word ) => evaluate_primitive(word)
+ );
+ }
+}
+
+Stack!Token result() {
+ if ( conditional.dischargeable ) {
+ return conditional.discharge;
+ } else {
+ return Stack!Token();
+ }
+}