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:
1 parent
7cdf388bf4
commit
b945182739
11 files changed
+300
-3
No files matched your search
@@ -65,6 +65,7 @@ enum CAT_BYTECODE_Op {
|
||||
CAT_BYTECODE_OP_RAISE, /* pop value -> raise it */
|
||||
CAT_BYTECODE_OP_FORNEXT, /* A: exit target; pop it -> push next or jump */
|
||||
CAT_BYTECODE_OP_PRINTEXPR, /* pop, print its repr (REPL echo) */
|
||||
CAT_BYTECODE_OP_IS, /* pop iface, obj -> push obj is iface (structural) */
|
||||
};
|
||||
|
||||
/* One instruction: opcode + three integer operands. */
|
||||
|
||||
+2
-1
@@ -28,7 +28,8 @@ enum CAT_LEXER_Kind {
|
||||
CAT_LEXER_TOK_TRUE, CAT_LEXER_TOK_UNTIL, CAT_LEXER_TOK_WHILE,
|
||||
CAT_LEXER_TOK_YIELD, CAT_LEXER_TOK_TRY, CAT_LEXER_TOK_EXCEPT,
|
||||
CAT_LEXER_TOK_RAISE, CAT_LEXER_TOK_USE, CAT_LEXER_TOK_CLASS,
|
||||
CAT_LEXER_TOK_EXTENDS,
|
||||
CAT_LEXER_TOK_EXTENDS, CAT_LEXER_TOK_INTERFACE, CAT_LEXER_TOK_IMPLEMENTS,
|
||||
CAT_LEXER_TOK_IS,
|
||||
/* symbols */
|
||||
CAT_LEXER_TOK_PLUS, CAT_LEXER_TOK_MINUS, CAT_LEXER_TOK_STAR,
|
||||
CAT_LEXER_TOK_SLASH, CAT_LEXER_TOK_PERCENT, CAT_LEXER_TOK_CARET,
|
||||
|
||||
@@ -208,6 +208,14 @@ int cat_value_truthy(CAT_VALUE_Value *v, int *out, CAT_ERROR_Error *err);
|
||||
int cat_value_equal(CAT_VALUE_Value *a, CAT_VALUE_Value *b, int *out,
|
||||
CAT_ERROR_Error *err);
|
||||
|
||||
/* Structural `is`: is `obj` a member-wise implementation of the interface
|
||||
* `iface`? An interface is a table carrying a `__interface` member whose
|
||||
* other members name the required methods; inheritance (`__index`) is
|
||||
* followed. When `iface` is not an interface, the test degrades to an
|
||||
* exact type (vtable) match. Sets *out to 0/1. */
|
||||
int cat_value_is(CAT_VALUE_Value *obj, CAT_VALUE_Value *iface, int *out,
|
||||
CAT_ERROR_Error *err);
|
||||
|
||||
/* Advance an iterator/generator. Returns 0 and sets *out to the next
|
||||
* value, 1 when the iterator is exhausted (*out untouched), or -1 on
|
||||
* error. A separate exhaustion status (rather than an empty/sentinel
|
||||
|
||||
Reference in new issue
Block a user