L9: signed integers for DVM-BASIC (fixes #116, stacks on #115) - #117
Closed
liqdmetal wants to merge 10 commits into
Closed
L9: signed integers for DVM-BASIC (fixes #116, stacks on #115)#117liqdmetal wants to merge 10 commits into
liqdmetal wants to merge 10 commits into
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.
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).
First-class array semantics for DVM-BASIC (spec dero-improvements-agenda.md
L3, P1): DIM a(n) AS Uint64|String creates a zero-filled RAM array
(indices 0..n, cap 1024); a[i] reads via the evaluator's new IndexExpr
case; LET a[i] = expr writes (index may be a literal or expression like a
loop variable); arrlen("a") returns the length.
Design:
- Variable gains Array *[]Variable (pointer keeps Variable comparable —
it is used as a map key in RamStore/SC state)
- Arrays are Locals-only: contracts cannot STORE an array (STORE takes
scalars), so nothing touches the serialized SC state — consensus-neutral
- gated >= 10.0.0 like L1/L2 (contract must call version("10.0.0"))
- composes with L1: FOR i = 0 TO n / a[i] = ... / NEXT i
Tests (dvm/control_flow_l3_test.go, 4 cases): array basics (set/read/
arrlen), FOR-loop fill (sum of squares 0..10 = 385), string arrays,
version gate (1.2.3 rejected).
Full dvm suite green. Stacks on L2 (feature/dvm-l2-subroutines, PR DEROFDN#103).
mapkeys() -> String: comma-separated, sorted keys of the contract's state visible during execution (spec dero-improvements-agenda.md L4, P1). The effective key set is the union of: - RamStore: keys loaded from disk (via DiskLoader) during this call - RawKeys: keys written during this call (TX_Storage.RawKeys) Batch/paged contracts can now iterate their stored keys instead of hand-rolling key sets. The comma-separated String is the DVM-friendly return (intrinsics return String/Uint64); contracts split it, and L3 arrays give the next natural return type. Gated >= 10.0.0 like L1/L2/L3. Tests (dvm/control_flow_l4_test.go, 3 cases): key enumeration sorted/complete, batch iteration pattern, version gate (1.2.3 rejected). Full dvm suite green. Stacks on L3 (feature/dvm-l3-arrays, PR DEROFDN#107).
L7 (P2): CONST name = value declares an immutable named constant (uint64
or string literal) in the interpreter's Constants map. The evaluator
resolves identifiers against Constants first; LET on a constant is
rejected (immutability enforced). Declared inside a function, scoped to
the interpreter run — Locals-only, consensus-neutral.
L8 (P2): the version gate is now the enforced default — every new
syntax keyword (L1-L4, CONST) requires version("10.0.0"); a contract
with no version() call (0.0.0) or an old one (1.2.3) gets a hard
rejection. Pre-fork contracts cannot accidentally use new syntax.
Tests (dvm/control_flow_l7_test.go, 5 cases): CONST uint arithmetic
(BASE*SCALE+1=301), CONST string concat+compare, CONST immutability
(LET rejected), version gate (1.2.3), no-version gate (0.0.0).
Full dvm suite green (25 control-flow tests incl. L1-L4 regression).
Stacks on L4 (feature/dvm-l4-mapkeys, PR DEROFDN#109).
Adds a real boolean type to DVM-BASIC (spec dero-improvements-agenda.md
L6, P2): DIM b AS Bool, built-in TRUE/FALSE constants, boolean
assignment from comparisons, logical && / || / ! (existing evaluator
ops), Bool function parameters.
Design: Bool is Vtype 0x6, stored internally as uint64 0/1 — fully
compatible with the existing comparison/IF semantics (which already
return 0/1). check_valid_type("bool") + all the type-switch sites
(DIM, LET, eval Ident, arrays, params, CONST resolution) extended.
Gated >= 10.0.0 like the rest of the L-series. TRUE/FALSE seeded as
Bool-typed constants per interpreter run (they resolve through the L7
CONST path, which needed a Bool case).
Tests (dvm/control_flow_l6_test.go, 4 cases): Bool basics (TRUE/FALSE +
comparison assignment), Bool logic (&& / || / !), Bool parameter,
version gate (1.2.3 rejected).
Full dvm suite green (29 control-flow tests). Stacks on L7+L8
(feature/dvm-l7-l8-const-version, PR DEROFDN#111).
…rithmetic Adds signed 64-bit integers to DVM-BASIC (spec dero-improvements-agenda.md L9, P2): DIM i AS Int, negative literals (-5), signed arithmetic and comparisons, Int function parameters and returns. Ends the two's-complement tricks contracts currently use for delta accounting. Design: - Int = Vtype 0x7, stored in ValueInt64 (int64) - UnaryExpr SUB handled in eval -> negative literals - evalBinaryExpr gains an int64 path (ADD/SUB/MUL/QUO/REM + all comparisons); int64-vs-uint64 mixed operands are reinterpreted two's-complement so IntVar == 70 works - All type-switch sites extended (DIM, LET, arrays, params, returns, CONST resolution) - Locals-only: contracts cannot STORE an Int (STORE takes the uint64 scalar path), so serialized SC state is untouched — consensus-neutral - Gated >= 10.0.0 like the rest of the L-series Tests (dvm/control_flow_l9_test.go, 4 cases): signed delta accounting (100 + -30 = 70, balance-100 == -30), negative literals (-5, -x, x*-1), Int param with negative return, version gate (1.2.3 rejected). Full dvm suite green (33 control-flow tests). Stacks on L6 (feature/dvm-l6-bool, PR DEROFDN#115).
Pinned by TestWargameL9SilentOverflow + TestWargameL9MinIntDivNeg1: 1. SILENT OVERFLOW: int64 ADD/SUB/MUL had no overflow check — max+1 wrapped silently to min, min-1 to max. A contract computing balance+delta with a huge signed value silently corrupted state. Now: explicit overflow checks panic (recovered -> deterministic rejection) instead of wrapping. 2. MinInt64 / -1: Go 1.25 silently returns MinInt64 (no panic, no error) — the worst case: wrong value, no signal. Now explicitly rejected. MinInt64 % -1 returns 0 (mathematically correct). Valid signed arithmetic unaffected; full dvm suite green.
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
L9 from the DVM-BASIC language agenda (P2): signed 64-bit integers —
DIM i AS Int, negative literals, signed arithmetic and comparisons, Int function parameters and returns. Ends the two's-complement tricks contracts currently use for delta accounting.The syntax
Implementation
Int= Vtype 0x7, stored inValueInt64(Go int64)UnaryExpr{SUB}handled in the evaluatorevalBinaryExprgains an int64 path (ADD/SUB/MUL/QUO/REM + all comparisons); int64-vs-uint64 mixed operands reinterpret two's-complement soIntVar == 70works naturally>= 10.0.0like the rest of the L-seriesTests (
dvm/control_flow_l9_test.go, 4 cases)TestL9_IntBasicsTestL9_NegativeLiteralTestL9_IntParamTestL9_VersionGateFull dvm suite green (33 control-flow tests).
Relationship
Branch:
feature/dvm-l9-intin the forkliqdmetal/derohe-improvements-by-liqdmetal(stacked onfeature/dvm-l6-bool).