interface + structural is: runtime-checked interfaces

Add nominal interface declarations with structural, runtime checking
(the owner asked for the operator AND the runtime check).

- lexer: `interface` and `is` are keywords; `implements` is a soft
  keyword (matched by text in the class clause) so the `implements()`
  stdlib function is not shadowed.
- `interface NAME [extends BASE] <function m() end> end` builds a table
  tagged `__interface`; members name the required methods (with an
  optional default body, shared via `__index`). Interface inheritance
  follows `__index`.
- class: `class C [extends B] [implements I, ...]` -- after the class
  global is set, `__implements_check` runs and raises if the class does
  not satisfy each declared interface.
- new opcode `IS` (`obj is iface`): structural membership test, and it
  degrades to an exact type (vtable) match when the right side is not an
  interface, so `x is SomeClass` reads naturally. Backed by
  `cat_value_is()`.
- stdlib: `implements(obj, iface)` (the same test as a function).
- tests/interface.cat + suite block: interface satisfaction, interface
  inheritance, duck typing (no `implements` needed), and a missing
  method failing. Suite 130 -> 136, all green.
This commit is contained in:
Minus committed 2026-11-18 15:56:10 +00:00
1 parent 7cdf388bf4
commit b945182739
11 files changed
+300 -3

No files matched your search

+8
View File
@@ -254,6 +254,14 @@ esac
# ---- self-hosted front-end (written in catscript) -------------------
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`"
check "iface-method" "draw circle" "`echo "$out" | sed -n 3p`"
check "iface-duck" "blob is Shape: true" "`echo "$out" | sed -n 6p`"
check "iface-missing" "line is Shape: false" "`echo "$out" | sed -n 8p`"
check "iface-fn" "implements(c, Shape): true" "`echo "$out" | sed -n 9p`"
out=`$BIN tests/selfhost_lexer.cat`
case "$out" in
*"NAME [x]"*) check "selfhost-lexer" "yes" "yes" ;;