Skip to content

L1: structured control flow for DVM-BASIC (fixes #100) - #101

Closed
liqdmetal wants to merge 3 commits into
DEROFDN:community-devfrom
liqdmetal:feature/dvm-l1-control-flow-pr
Closed

L1: structured control flow for DVM-BASIC (fixes #100)#101
liqdmetal wants to merge 3 commits into
DEROFDN:community-devfrom
liqdmetal:feature/dvm-l1-control-flow-pr

Conversation

@liqdmetal

Copy link
Copy Markdown

Summary

L1 from the DVM-BASIC language agenda (P0): structured control flow — FOR/NEXT, WHILE/WEND, and block IF/ELSE/ENDIF — replacing GOTO-only spaghetti. Halves contract size (cuts the len(scdata)*1.5 code-size fee term), makes contracts auditable, and reduces GOTO bugs.

The syntax

FOR i = 1 TO 10 [STEP 2]
    ... body ...
NEXT i

WHILE LOAD("x") < 10
    ... body ...
WEND

IF x > 3 THEN
    ... then-block ...
ELSE
    ... else-block ...
ENDIF

Implementation

New dvm/control_flow.go + a LoopFrame stack on DVM_Interpreter:

  • FOR var = start TO end [STEP step] / NEXT var — counter loop, step defaults to 1, counter lives in Locals so it's visible to the body
  • WHILE expr / WEND — pre-tested loop; WEND jumps back to the WHILE line which re-evaluates
  • block IF expr THEN ... [ELSE ...] ENDIF — detected as an IF line ending in THEN without GOTO; the existing single-line IF expr THEN GOTO x [ELSE GOTO y] form is untouched
  • findMatchingLine scans forward for the matching WEND/ENDIF respecting 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 call version("10.0.0")). Pre-fork contracts cannot accidentally use the new syntax; a contract at 1.2.3 or with no version() call gets a clear rejection. This matches the existing intrinsic-versioning model.

Tests (dvm/control_flow_test.go, 8 cases)

Test Verifies
TestL1_ForNext FOR 1..5 sums to 15
TestL1_ForStep STEP 2 → 0+2+4 = 6
TestL1_WhileWend WHILE countdown runs 10 iterations
TestL1_BlockIfElse both branches of IF/ELSE
TestL1_BlockIfNoElse false falls through past ENDIF
TestL1_Nested FOR inside WHILE, nested frames → 9
TestL1_VersionGate version("1.2.3") + FOR → rejected
TestL1_NoVersion no version() + FOR → rejected

Full dvm suite green; dvm package builds clean on the community-dev base.

Relationship


Branch: feature/dvm-l1-control-flow-pr in the fork liqdmetal/derohe-improvements-by-liqdmetal. Carries the build-manifest fix (go.mod/go.sum).

…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.
…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.
@liqdmetal

Copy link
Copy Markdown
Author

Superseded by PR #126 — the consolidated language package. Same code, one reviewable PR with no vendor noise.

@liqdmetal liqdmetal closed this Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant