First slice of the static-typing foundation (traits/generics/Sized()
will build on it). Typing stays structural and permissive: an unknown
or opaque name is never a rejection, so existing programs keep working.
- new module cat/types/type.{h,c}: compile-time-only descriptors
(int/float/bool/text/empty/object/function/list/memory/any + interned
named/opaque types), with equal/assignable/join and int->float widening.
- compiler: skip_type_hint -> parse_type_hint, which returns the type of
the first identifier in a hint (the rest of a balanced hint is still
consumed and ignored, so `cbyte[sentinel_sized<0>]` parses).
- `local NAME : T = <literal>` is checked: assigning a known concrete
literal of the wrong kind to a known concrete type raises at compile
time ("line N: cannot assign text to int"). Unknown/opaque names and
non-literal expressions stay permissive.
- FuncCtx tracks a declared/inferred type per local slot.
- tests/types.cat + suite block: ok hints, int->float widening, opaque
names accepted, and a wrong assignment rejected. Suite 136 -> 140.
21 lines
539 B
Plaintext
21 lines
539 B
Plaintext
-- tests/types.cat -- compile-time type checking of hints.
|
|
--
|
|
-- Hints are now CHECKED where both sides are concrete and known; opaque
|
|
-- or unknown names remain permissive (structural typing never rejects a
|
|
-- program by guessing).
|
|
|
|
local x : int = 42
|
|
local y : float = 3.5
|
|
local z : text = "hi"
|
|
local b : bool = true
|
|
print("ok:", x, y, z, b)
|
|
|
|
-- int widens to float
|
|
local f : float = 7
|
|
print("widen:", f)
|
|
|
|
-- opaque type names stay permissive (no false rejection)
|
|
local n : cint = 42
|
|
local buf : cbyte[] = empty
|
|
print("opaque:", n, buf)
|