type checker, phase 1: hints are checked where concrete
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.
This commit is contained in:
1 parent
b945182739
commit
48b1b84ea6
6 files changed
+309
-17
No files matched your search
@@ -254,6 +254,21 @@ esac
|
||||
|
||||
# ---- self-hosted front-end (written in catscript) -------------------
|
||||
|
||||
out=`$BIN tests/types.cat`
|
||||
check "types-ok" "ok: 42 3.5 hi true" "`echo "$out" | sed -n 1p`"
|
||||
check "types-widen" "widen: 7" "`echo "$out" | sed -n 2p`"
|
||||
check "types-opaque" "opaque: 42 empty" "`echo "$out" | sed -n 3p`"
|
||||
|
||||
# type hints are CHECKED where both sides are concrete
|
||||
printf 'local x : int = "bad"\n' > /tmp/catscript_badtype.cat
|
||||
if $BIN /tmp/catscript_badtype.cat >/dev/null 2>&1
|
||||
then
|
||||
check "types-reject" "reject" "accepted"
|
||||
else
|
||||
check "types-reject" "reject" "reject"
|
||||
fi
|
||||
rm -f /tmp/catscript_badtype.cat
|
||||
|
||||
out=`$BIN tests/interface.cat`
|
||||
check "iface-is-sub" "circle is Drawable: true" "`echo "$out" | sed -n 1p`"
|
||||
check "iface-is-inherited" "circle is Shape: true" "`echo "$out" | sed -n 2p`"
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
-- 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)
|
||||
Reference in new issue
Block a user