Skip to content

v0.4.4: multi-statement constant-literal collection sequences

Choose a tag to compare

@tamnd tamnd released this 28 Apr 05:25
· 73 commits to main since this release
v0.4.4
2a6fc99

v0.4.4 — multi-statement constant-literal collection sequences

What's new

Handle module-level bodies consisting of an optional docstring followed by
two or more (or one with docstring) constant-literal collection assignments.
This enables keyword.py-style modules with a docstring, a small list, a large
list, and another small list — all in one pass.

New classification modConstLitSeq fires when:

  • stmtString + ≥1 stmtConstLitColl statements, or
  • ≥2 stmtConstLitColl statements with no docstring.

Co-consts ordering (matches CPython)

CPython places constants in three phases:

  1. Atomic strings: docstring (if any), then each large-list (n≥31) element in source order.
  2. None sentinel.
  3. Tuples in source order: one ConstTuple per LIST_EXTEND or tuple statement.

Statements with n≥31 contribute their elements to phase 1; LIST_EXTEND (n=3..30)
and tuple statements contribute their tuple to phase 3. This matches the ordering
CPython produces for keyword.py (co_consts = (docstring, elem0..elemN-1, None, tuple0, tuple1)).

Linetable encoding

Each statement produces entries exactly as in the standalone case, but:

  • The docstring covers 2 CUs (LOAD_CONST + STORE_NAME __doc__) in one entry at [0, docEndCol).
  • Intermediate STORE_NAME uses SHORT0 n=1.
  • The final statement's STORE_NAME uses SHORT0 n=3 (merged with LOAD_CONST None + RETURN_VALUE).

New fixtures (224–226)

  • 224 """doc"""\nx = ['a','b','c'] — docstring + 1 LIST_EXTEND
  • 225 """doc"""\n__all__ = [...]\nsoftkwlist = [...] — docstring + 2 LIST_EXTEND
  • 226 keyword.py pattern — docstring + LIST_EXTEND + large list (n=35) + LIST_EXTEND

Files changed

  • bytecode/assign.go — added appendValueEntryN (generalises appendValueEntry to N CUs)
  • bytecode/const_lit_coll.go — added ConstLitSeqStmt, ConstLitSeqLineTable
  • compiler/classify.go — added modConstLitSeq, constLitSeqClassify; updated stmtsToClassification
  • compiler/compiler.go — added compileConstLitSeq