aboutsummaryrefslogtreecommitdiff
path: root/src/primitives/eval.d
diff options
context:
space:
mode:
authorAdrian Kummerlaender2017-04-13 21:51:54 +0200
committerAdrian Kummerlaender2017-04-13 21:51:54 +0200
commitd2126fe52fd3c002631e66fa7d6e2d5cd8862bea (patch)
treecd0bc233912b7ed06c6c4735e7a6475e4099e585 /src/primitives/eval.d
parent11d6996d9b56b537946b2894a66f56f5cf3576b8 (diff)
downloadslang-d2126fe52fd3c002631e66fa7d6e2d5cd8862bea.tar
slang-d2126fe52fd3c002631e66fa7d6e2d5cd8862bea.tar.gz
slang-d2126fe52fd3c002631e66fa7d6e2d5cd8862bea.tar.bz2
slang-d2126fe52fd3c002631e66fa7d6e2d5cd8862bea.tar.lz
slang-d2126fe52fd3c002631e66fa7d6e2d5cd8862bea.tar.xz
slang-d2126fe52fd3c002631e66fa7d6e2d5cd8862bea.tar.zst
slang-d2126fe52fd3c002631e66fa7d6e2d5cd8862bea.zip
Implement deferred word, conditional resolution
Due to the non-trivial way tokens were previously processed the compiler could not safely perform tail-call elimination. Thus the _slang_ evaluation depth was restricted by the maximum call stack size. This issue is mitigated by introducing deferred word resolution - i.e. pushing expanded tokens onto a buffer stack and processing them in an explicit loop. This change ties into the implementation of the language's conditional primitive. The previous implementation did implicitly not support direct nesting of conditional expressions such as: truthA if mainBranch truthB if secondaryMainBranch then else then alternativeBranch else This issue is now made explicit by disallowing direct nesting of conditionals as depicted above. Appropriate exceptions are generated when required and the conditional primitive is reimplemented in a more robust fashion under the assumption that this rule holds. Note that nesting is still fully supported iff. the nested conditional is contained in a deferredly evaluated word. As a positive side effect this will make it slightly harder to generate unreadable code by forcing developers to write simpler words. The main change of the conditional primitive lies in deferring branch token processing until the conditional expression is concluded by `else`. This is achieved by capturing non-dropped tokens in an internal buffer akin to how the word definition operator `§` is implemented. The branch buffer is discharged after `else` is evaluated. Discharging is triggered via the newly introduced `result` method of the primitive evaluation module. This avenue for injecting tokens into the processing stream may be used by further primitives in the future.
Diffstat (limited to 'src/primitives/eval.d')
-rw-r--r--src/primitives/eval.d98
1 files changed, 43 insertions, 55 deletions
diff --git a/src/primitives/eval.d b/src/primitives/eval.d
index 8b080e2..08d8fb9 100644
--- a/src/primitives/eval.d
+++ b/src/primitives/eval.d
@@ -1,65 +1,53 @@
module src.primitives.eval;
-import src.primitives.impl;
+import std.variant;
-bool evaluate(int value) {
- return drop_mode;
-}
+import src.stack;
+import src.primitives.core;
+import conditional = src.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;
+ }
-bool evaluate(bool value) {
- return drop_mode;
+ return true;
}
-bool evaluate(string word) {
- if ( drop_mode ) {
- switch ( word ) {
- case "then":
- return n_ary_conditional_then;
- case "else":
- return n_ary_conditional_else;
- default:
- 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)
+ );
}
+}
- switch ( word ) {
- case "§":
- return definition_start;
- case "$":
- return binary_op_variable_bind;
- case "@":
- return unary_op_variable_resolve;
- case "if":
- return unary_conditional_if;
- case "then":
- return n_ary_conditional_then;
- case "else":
- return n_ary_conditional_else;
- case "+":
- return binary_op_add;
- case "*":
- return binary_op_multiply;
- case "/":
- return binary_op_divide;
- case "%":
- return binary_op_modulo;
- case ".":
- return unary_op_io_print;
- case "pop":
- return unary_op_stack_pop;
- case "dup":
- return unary_op_stack_dup;
- case "swp":
- return binary_op_stack_swp;
- case "true":
- return integral_value_bool(true);
- case "false":
- return integral_value_bool(false);
- case "<":
- return binary_cond_lt;
- case "=":
- return binary_cond_eq;
- default:
- return false;
+Stack!Token result() {
+ if ( conditional.dischargeable ) {
+ return conditional.discharge;
+ } else {
+ return Stack!Token();
}
}