diff options
| author | 2026-10-14 16:13:27 +0330 | |
|---|---|---|
| committer | 2026-10-14 16:13:27 +0330 | |
| commit | 01125f4b3ced32230e19503ce72a2a9dac63c58d (patch) | |
| tree | 9b48b00217333961bc9380a926eb5f8e7a687ba2 | |
| parent | fndef: a prototype after a definition in the same module keeps the definition... (diff) | |
globals: a float or f32 global takes a float literal initializer (appendix b.2.7)
| -rw-r--r-- | src/compiler/decl.zc | 3 | ||||
| -rw-r--r-- | tests/conformance/5-declarations/global_float_init_f32_and_list.zc | 8 | ||||
| -rw-r--r-- | tests/conformance/known.txt | 3 |
3 files changed, 12 insertions, 2 deletions
diff --git a/src/compiler/decl.zc b/src/compiler/decl.zc index 67aceb1..2c8d13e 100644 --- a/src/compiler/decl.zc +++ b/src/compiler/decl.zc @@ -478,6 +478,9 @@ int gdecl2(int t, int tg, int n) { if (arraytype[0] != TY_ARR || !is_op(s_lb)) serr("a global compound literal needs an array initializer"); else { array_literal(arraytype); vv = xcval; *(a + hdelta) = vv; if (!t) stype[s] = decay(xty); } } + else if (t && isfl(t) && tk == T_FLT) { // float g = 1.5; (appendix b.2.7) + vv = tv; next(); if (t == tf32) vv = rt.zs2f(rt.zf2s(vv)); *(a + hdelta) = vv; // an f32 scalar is a rounded double + } else { vv = cnorm(t, cexpr()); *(a + hdelta) = vv; if (gok) { if (!gty) gty = tint; else if (gty != tint) gok = 0; } diff --git a/tests/conformance/5-declarations/global_float_init_f32_and_list.zc b/tests/conformance/5-declarations/global_float_init_f32_and_list.zc new file mode 100644 index 0000000..57df35b --- /dev/null +++ b/tests/conformance/5-declarations/global_float_init_f32_and_list.zc @@ -0,0 +1,8 @@ +// expect: 1/1/ +// spec 5.3.2 and appendix b.2.7: float globals take a float literal initializer +include rt; +void pn(int x) { rt.put_n(x); rt.put_c(10); } +float a = 0.25, b = 2.5; +f32 c = 0.5; +fn main() { pn(a + b == 2.75); pn(c * 4.0 == 2.0); return 0; } +// zc0: skip diff --git a/tests/conformance/known.txt b/tests/conformance/known.txt index 7b2082c..044b862 100644 --- a/tests/conformance/known.txt +++ b/tests/conformance/known.txt @@ -3,8 +3,7 @@ # appendix b.2.8 (a global named in an initializer is its address) is documented behavior per 5.3.2 and 3.14.2: no entry 4-statements/o3_outside_function_ordinary_block.zc zy c top-level o3 { } is rejected; spec 4.10.2 says it is an ordinary block 5-declarations/enum_member_names_earlier_b2.zc zy a appendix b.2.2 confirmed: enum members can't name earlier members -5-declarations/fn_prototype_unnamed_params_b13.zc zy a appendix b.2.13 confirmed (message now: doesn't match its earlier declaration) -5-declarations/global_float_init_b7.zc zy a appendix b.2.7 confirmed: float global initializer rejected +5-declarations/fn_prototype_unnamed_params_b13.zc zy c spec 5.2.1 says prototype parameter names are required, and a type name alone is read as an untyped parameter of that name (paramstart); b.2.13 is a spec choice, not fixed 5-declarations/include_in_function_b9.zc zy a appendix b.2.9 confirmed: include in a body gives "return outside a function" 9-appendix-b/b2_05_ternary_int_float.zc zy a appendix b.2.5 confirmed 9-appendix-b/b2_06_bool_normalises.zc zy a appendix b.2.6 confirmed |
