lang: struct consts migrate to const globals, inline fallback (PLAN-CONST-AGG step 4)
A struct constant whose every field serializes — literals, enum tags,
nested aggregates, and (new) const EXPRESSIONS over named consts /
const-aggregate leaves ('r = K + 1', 'g = LIT.r', 'b = A[1]') — becomes
an immutable global: one storage, reads load/GEP it, '@LIT' is
addressable, dead-global elimination drops unused ones. constExprValue
gained a fold-through tail (evalConstIntExpr/evalConstFloatExpr,
source-aware), which also enables const-expression ELEMENTS in array
consts.
A const with a NON-serializable field (a call, a runtime read) keeps
inline re-lowering, and that per-use evaluation is now the documented
contract for the class (pinned: 'CALL.r' reads 1 then 2, side effects
run per use; '#run' is the evaluate-once tool).
Examples: 0180 (migrated shapes + @ptr + copy independence),
0181 (the inline-fallback contract). m3te (23/23) + game rebuilt green.
This commit is contained in:
23
examples/0180-types-struct-const-globals.sx
Normal file
23
examples/0180-types-struct-const-globals.sx
Normal file
@@ -0,0 +1,23 @@
|
||||
// Serializable struct constants are IMMUTABLE GLOBALS (one storage, no
|
||||
// per-use rebuild): literal fields, const-EXPRESSION fields (`K + 1`),
|
||||
// another const's field (`LIT.r`), and a const array's element (`A[1]`)
|
||||
// all serialize. The const is addressable (`@LIT`) and copies stay
|
||||
// independent.
|
||||
|
||||
#import "modules/std.sx";
|
||||
|
||||
Color :: struct { r, g, b: s64; }
|
||||
K :: 10;
|
||||
A : [2]s64 : .[7, 8];
|
||||
LIT :: Color.{ r = 255, g = 0, b = 0 };
|
||||
EXPR :: Color.{ r = K + 1, g = K * 2, b = A[1] };
|
||||
REF :: Color.{ r = LIT.r, g = 1, b = 2 };
|
||||
|
||||
main :: () {
|
||||
print("lit={} expr={} {} {} ref={}\n", LIT.r, EXPR.r, EXPR.g, EXPR.b, REF.r);
|
||||
p := @LIT;
|
||||
print("via-ptr={}\n", p.r);
|
||||
c := LIT;
|
||||
c.r = 9;
|
||||
print("copy={} const={}\n", c.r, LIT.r);
|
||||
}
|
||||
18
examples/0181-types-struct-const-inline-fallback.sx
Normal file
18
examples/0181-types-struct-const-inline-fallback.sx
Normal file
@@ -0,0 +1,18 @@
|
||||
// A struct constant with a NON-serializable initializer field (a call, a
|
||||
// runtime read) keeps INLINE RE-LOWERING semantics: the initializer is
|
||||
// evaluated AT EACH USE. This is the documented contract for this class
|
||||
// — `CALL.r` may differ between reads and side effects run per use.
|
||||
// For evaluate-once semantics use `NAME :: #run f();`.
|
||||
|
||||
#import "modules/std.sx";
|
||||
|
||||
Color :: struct { r, g, b: s64; }
|
||||
counter : s64 = 0;
|
||||
bump :: () -> s64 { counter += 1; counter }
|
||||
CALL :: Color.{ r = bump(), g = 0, b = 0 };
|
||||
|
||||
main :: () {
|
||||
print("use1={}\n", CALL.r);
|
||||
print("use2={}\n", CALL.r);
|
||||
print("counter={}\n", counter);
|
||||
}
|
||||
1
examples/expected/0180-types-struct-const-globals.exit
Normal file
1
examples/expected/0180-types-struct-const-globals.exit
Normal file
@@ -0,0 +1 @@
|
||||
0
|
||||
1
examples/expected/0180-types-struct-const-globals.stderr
Normal file
1
examples/expected/0180-types-struct-const-globals.stderr
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
3
examples/expected/0180-types-struct-const-globals.stdout
Normal file
3
examples/expected/0180-types-struct-const-globals.stdout
Normal file
@@ -0,0 +1,3 @@
|
||||
lit=255 expr=11 20 8 ref=255
|
||||
via-ptr=255
|
||||
copy=9 const=255
|
||||
@@ -0,0 +1 @@
|
||||
0
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
use1=1
|
||||
use2=2
|
||||
counter=2
|
||||
Reference in New Issue
Block a user