Planned control flow statements

This commit is contained in:
2024-11-16 17:57:41 +11:00
parent cd2113594d
commit 04f0653595

View File

@@ -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