L1: structured control flow for DVM-BASIC (fixes #100) - #101
Closed
liqdmetal wants to merge 3 commits into
Closed
Conversation
…F/ELSE/ENDIF
Replaces GOTO-only spaghetti with structured loops and blocks (spec
dero-improvements-agenda.md L1, P0). Halves contract size, cuts the
len(scdata)*1.5 code-size fee term, makes contracts auditable.
New dvm/control_flow.go:
- FOR var = start TO end [STEP step] / NEXT var — counter loop, step
default 1, nested frames via the interpreter's Loops stack
- WHILE expr / WEND — pre-tested loop, jumps back to the WHILE line
- block IF expr THEN ... [ELSE ...] ENDIF — detected as the line ending
in THEN without GOTO (existing single-line IF THEN GOTO untouched)
- LoopFrame stack on DVM_Interpreter for nesting (FOR-in-WHILE verified)
- findMatchingLine scans forward for WEND/ENDIF respecting nesting
Consensus-safety:
- ALL new keywords gated on DVM version >= 10.0.0 (contract must call
version("10.0.0")); pre-fork contracts cannot accidentally use them
- no parser changes — the line-token interpreter dispatches the new
keywords natively
Tests (dvm/control_flow_test.go, 8 cases):
- FOR/NEXT sum(1..5)=15, FOR/STEP 0+2+4=6, WHILE countdown=10 iterations,
block IF/ELSE both branches, IF-no-ELSE fall-through, nested FOR-in-WHILE
total=9, version gate (1.2.3 rejected), no-version gate (0.0.0 rejected)
Full dvm suite green, whole tree builds.
…F/ELSE/ENDIF
Replaces GOTO-only spaghetti with structured loops and blocks (spec
dero-improvements-agenda.md L1, P0). Halves contract size, cuts the
len(scdata)*1.5 code-size fee term, makes contracts auditable.
New dvm/control_flow.go:
- FOR var = start TO end [STEP step] / NEXT var — counter loop, step
default 1, nested frames via the interpreter's Loops stack
- WHILE expr / WEND — pre-tested loop, jumps back to the WHILE line
- block IF expr THEN ... [ELSE ...] ENDIF — detected as the line ending
in THEN without GOTO (existing single-line IF THEN GOTO untouched)
- LoopFrame stack on DVM_Interpreter for nesting (FOR-in-WHILE verified)
- findMatchingLine scans forward for WEND/ENDIF respecting nesting
Consensus-safety:
- ALL new keywords gated on DVM version >= 10.0.0 (contract must call
version("10.0.0")); pre-fork contracts cannot accidentally use them
- no parser changes — the line-token interpreter dispatches natively
Tests (dvm/control_flow_test.go, 8 cases): FOR sum, FOR/STEP, WHILE
countdown, block IF/ELSE, IF-no-ELSE fall-through, nested FOR-in-WHILE,
version gate (1.2.3), no-version gate (0.0.0). All green.
Also carries the build-manifest fix (go.mod/go.sum) for fresh-clone builds.
This was referenced Aug 23, 2026
…ndor) Same two fixes as the sibling PRs: remove the l.Operation.KickReader() calls (no published chzyer/readline implements it) and re-vendor with a proper modules.txt. Fresh-clone go build now succeeds without -mod=mod.
Author
|
Superseded by PR #126 — the consolidated language package. Same code, one reviewable PR with no vendor noise. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
L1 from the DVM-BASIC language agenda (P0): structured control flow —
FOR/NEXT,WHILE/WEND, and blockIF/ELSE/ENDIF— replacing GOTO-only spaghetti. Halves contract size (cuts thelen(scdata)*1.5code-size fee term), makes contracts auditable, and reduces GOTO bugs.The syntax
Implementation
New
dvm/control_flow.go+ aLoopFramestack onDVM_Interpreter:FOR var = start TO end [STEP step]/NEXT var— counter loop, step defaults to 1, counter lives inLocalsso it's visible to the bodyWHILE expr/WEND— pre-tested loop;WENDjumps back to theWHILEline which re-evaluatesIF expr THEN ... [ELSE ...] ENDIF— detected as anIFline ending inTHENwithoutGOTO; the existing single-lineIF expr THEN GOTO x [ELSE GOTO y]form is untouchedfindMatchingLinescans forward for the matchingWEND/ENDIFrespecting nesting (verified with FOR-in-WHILE)No parser changes — the line-token interpreter dispatches the new keywords natively.
Consensus safety
All new keywords are gated on the contract's declared DVM version (
>= 10.0.0— the contract must callversion("10.0.0")). Pre-fork contracts cannot accidentally use the new syntax; a contract at1.2.3or with noversion()call gets a clear rejection. This matches the existing intrinsic-versioning model.Tests (
dvm/control_flow_test.go, 8 cases)TestL1_ForNextFOR 1..5sums to 15TestL1_ForStepSTEP 2→ 0+2+4 = 6TestL1_WhileWendTestL1_BlockIfElseTestL1_BlockIfNoElseTestL1_NestedTestL1_VersionGateversion("1.2.3")+ FOR → rejectedTestL1_NoVersionversion()+ FOR → rejectedFull dvm suite green;
dvmpackage builds clean on the community-dev base.Relationship
>= 10.0.0, so it slots into the same HF window asverify_proof(PR feat(dvm): verify_proof PoC with executing-block roothash bind (fixes #93) #94)Branch:
feature/dvm-l1-control-flow-prin the forkliqdmetal/derohe-improvements-by-liqdmetal. Carries the build-manifest fix (go.mod/go.sum).