From 04f065359585b858a069ffd5019c6a74a11e0da0 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 16 Nov 2024 17:57:41 +1100 Subject: [PATCH] Planned control flow statements --- .notes/control-flow-opcodes.txt | 120 ++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 .notes/control-flow-opcodes.txt diff --git a/.notes/control-flow-opcodes.txt b/.notes/control-flow-opcodes.txt new file mode 100644 index 0000000..5ea6673 --- /dev/null +++ b/.notes/control-flow-opcodes.txt @@ -0,0 +1,120 @@ +types: + if-then-else + ternary ?: + while + do-while + for + foreach (compounds) + switch-case-default (and continue/break?) + continue + break + +if-then: + cond-branch + if false jump end + { + then-branch + } + end + +if-then-else: (also ternary) + cond-branch + if false jump else + { + then-branch + } + jump end + { + else-branch + } + end + +while: + begin + cond-branch + if false jump end + { + then-branch + } + jump begin + end + +do-while: + begin + { + then-branch + } + cond-branch + if true jump begin + end + +for: + { + pre-branch + begin + cond-branch + if false jump end + { + then-branch + } + post-branch + jump begin + end + } + +foreach: + ...needs more planning + +switch-case-default: + ...needs more planning + +continue: + jump begin + unwind scopes + +break: + jump end + unwind scopes + +--- + +Notes: + The additional scope in 'for' is to safely encapsulate pre-branch, as variables can be declared here. + do-while's 'end' is only there for the break/continue keywords. + break and continue will also unwind scopes, up to the innermost control-flow level. + +break/continue within nested scopes: + Because control flows can be nested, a stack of scope depths may be needed for break/continue... + However, this would get more complicated with closures. + To fix this, scopes may need to remember their own depth, and the depth of the innermost control-flow level. + If a scope's depth is 10 and the inner control-flow-level is 7, then calling break/continue will jump AND unwind to the inner control-flow level. + +--- + +JUMP word: + opcode + type (absolute, relative) + conditional (always, if_true, if_false) + - + + value + +absolute: + "value" is relative to the code sections starting position + +relative: + "value" is relative to the program counter, after this opcode has been read from the bytecode + +always: + Always jump + +if_true: + pop the stack top + if the popped value is true, then jump + else ignore + +if_false: + pop the stack top + if the popped value is true, then ignore + else jump +