Skip to content

L2: subroutines for DVM-BASIC — GOSUB/RETURN (fixes #102, stacks on #101) - #103

Closed
liqdmetal wants to merge 4 commits into
DEROFDN:community-devfrom
liqdmetal:feature/dvm-l2-subroutines
Closed

L2: subroutines for DVM-BASIC — GOSUB/RETURN (fixes #102, stacks on #101)#103
liqdmetal wants to merge 4 commits into
DEROFDN:community-devfrom
liqdmetal:feature/dvm-l2-subroutines

Conversation

@liqdmetal

Copy link
Copy Markdown

Summary

L2 from the DVM-BASIC language agenda (P0): subroutines via GOSUB/RETURN — internal code reuse so entrypoints stop re-implementing helpers. Pairs with L1 structured control flow (PR #101): loops for repetition, subroutines for reuse.

The syntax

Function Settle(x Uint64) Uint64
	5 version("10.0.0")
	10 DIM r AS Uint64
	20 GOSUB 100        ' call the helper
	30 RETURN r
	100 LET r = compute(x)   ' helper body (shared Locals)
	110 RETURN          ' back to line 30
End Function

Implementation

  • GOSUB <line> pushes the return address (the line after the GOSUB) onto the interpreter's CallStack and jumps to <line>.
  • RETURN pops the CallStack when non-empty (subroutine return — the value is ignored; subroutines communicate via shared Locals) and jumps back; when the stack is empty it behaves exactly as today (function return with value).
  • Fully backward-compatible: existing contracts never push a CallStack, so their RETURN semantics are untouched.
  • Nests correctly: GOSUB-in-GOSUB and GOSUB-in-FOR both verified.

Consensus safety

Gated on DVM version >= 10.0.0 like L1 — the contract must call version("10.0.0"). Pre-fork contracts cannot accidentally use it.

Tests (dvm/control_flow_l2_test.go, 5 cases)

Test Verifies
TestL2_Gosub shared-Locals helper (x·2)
TestL2_NestedGosub nested calls → 1 + 10 = 11
TestL2_GosubInFor helper inside a FOR body → 1+2+3 = 6
TestL2_FunctionReturnStillWorks plain function RETURN unaffected
TestL2_VersionGate GOSUB at 1.2.3 rejected

Full dvm suite green; dvm package builds clean.

Relationship


Branch: feature/dvm-l2-subroutines in the fork liqdmetal/derohe-improvements-by-liqdmetal (stacked on feature/dvm-l1-control-flow-pr). Carries the build-manifest fix.

…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.
The second P0 language item (spec dero-improvements-agenda.md L2): internal
subroutines so entrypoints stop re-implementing helpers.

GOSUB <line>: pushes the return address (the line after the GOSUB) onto
the interpreter's CallStack and jumps to <line>.
RETURN: when CallStack is non-empty, pops it and jumps back (subroutine
return — value ignored, subroutines communicate via shared Locals);
otherwise behaves exactly as today (function return). Fully
backward-compatible: existing contracts never push a CallStack.

Gated on DVM version >= 10.0.0 like L1 (contract must call
version("10.0.0")). Nests correctly (GOSUB-in-GOSUB, GOSUB-in-FOR).

Tests (dvm/control_flow_l2_test.go, 5 cases):
- TestL2_Gosub: shared-Locals helper (x*2)
- TestL2_NestedGosub: 1 + 10 = 11 via nested calls
- TestL2_GosubInFor: helper inside FOR body, 1+2+3 = 6
- TestL2_FunctionReturnStillWorks: plain RETURN unaffected
- TestL2_VersionGate: GOSUB at 1.2.3 rejected

Stacks on L1 (feature/dvm-l1-control-flow-pr, PR DEROFDN#101).
…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