Skip to content

Commit

Permalink
Planned control flow statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratstail91 committed Nov 16, 2024
1 parent cd21135 commit 04f0653
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions .notes/control-flow-opcodes.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 04f0653

Please sign in to comment.