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
+92
-1
@@ -191,6 +191,7 @@ static int bp_of(int kind)
|
||||
case CAT_LEXER_TOK_EQ: case CAT_LEXER_TOK_NE:
|
||||
case CAT_LEXER_TOK_LT: case CAT_LEXER_TOK_LE:
|
||||
case CAT_LEXER_TOK_GT: case CAT_LEXER_TOK_GE: return 3;
|
||||
case CAT_LEXER_TOK_IS: return 3;
|
||||
case CAT_LEXER_TOK_CONCAT: return 4;
|
||||
case CAT_LEXER_TOK_PLUS: case CAT_LEXER_TOK_MINUS: return 5;
|
||||
case CAT_LEXER_TOK_STAR: case CAT_LEXER_TOK_SLASH:
|
||||
@@ -560,7 +561,10 @@ static void expr_bp(Compiler *c, int minbp)
|
||||
}
|
||||
next(c);
|
||||
expr_bp(c, bp + 1);
|
||||
emit(c, binop_for(kind), 0, 0, c->tok.line);
|
||||
if (kind == CAT_LEXER_TOK_IS)
|
||||
emit(c, CAT_BYTECODE_OP_IS, 0, 0, c->tok.line);
|
||||
else
|
||||
emit(c, binop_for(kind), 0, 0, c->tok.line);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -826,6 +830,9 @@ static void stat(Compiler *c)
|
||||
* class's `init` if present. The table is bound to a global NAME. */
|
||||
char name[256];
|
||||
char base[256];
|
||||
char impl[8][256];
|
||||
int nimpl = 0;
|
||||
int i;
|
||||
next(c);
|
||||
name[0] = '\0';
|
||||
base[0] = '\0';
|
||||
@@ -839,6 +846,21 @@ static void stat(Compiler *c)
|
||||
next(c);
|
||||
}
|
||||
}
|
||||
if (check(c, CAT_LEXER_TOK_NAME) &&
|
||||
strcmp(c->tok.text, "implements") == 0) {
|
||||
next(c);
|
||||
for (;;) {
|
||||
if (check(c, CAT_LEXER_TOK_NAME)) {
|
||||
if (nimpl < 8)
|
||||
snprintf(impl[nimpl], sizeof(impl[0]),
|
||||
"%s", c->tok.text);
|
||||
nimpl++;
|
||||
next(c);
|
||||
}
|
||||
if (!accept(c, CAT_LEXER_TOK_COMMA))
|
||||
break;
|
||||
}
|
||||
}
|
||||
emit(c, CAT_BYTECODE_OP_NEWTABLE, 0, 0, line);
|
||||
/* __index: base (inheritance) or the class itself */
|
||||
emit(c, CAT_BYTECODE_OP_DUP, 0, 0, line);
|
||||
@@ -875,6 +897,75 @@ static void stat(Compiler *c)
|
||||
}
|
||||
expect(c, CAT_LEXER_TOK_END, "'end'");
|
||||
emit(c, CAT_BYTECODE_OP_SETGLOBAL, add_string(c, name), 0, line);
|
||||
/* runtime assertion: the class must satisfy each declared interface
|
||||
* (structural check, performed by __implements_check). */
|
||||
for (i = 0; i < nimpl && i < 8; i++) {
|
||||
emit(c, CAT_BYTECODE_OP_GETGLOBAL,
|
||||
add_string(c, "__implements_check"), 0, line);
|
||||
emit(c, CAT_BYTECODE_OP_GETGLOBAL, add_string(c, name), 0, line);
|
||||
emit(c, CAT_BYTECODE_OP_GETGLOBAL, add_string(c, impl[i]), 0, line);
|
||||
emit(c, CAT_BYTECODE_OP_CALL, 2, 0, line);
|
||||
emit(c, CAT_BYTECODE_OP_POP, 0, 0, line);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case CAT_LEXER_TOK_INTERFACE: {
|
||||
/* interface NAME [extends BASE] <function m(...) end> end
|
||||
*
|
||||
* An interface is a table tagged `__interface`; its members name the
|
||||
* required methods (a body is allowed and becomes a default method,
|
||||
* shared through `__index`). The `is` operator checks satisfaction
|
||||
* structurally at runtime. */
|
||||
char name[256];
|
||||
char base[256];
|
||||
next(c);
|
||||
name[0] = '\0';
|
||||
base[0] = '\0';
|
||||
if (check(c, CAT_LEXER_TOK_NAME)) {
|
||||
snprintf(name, sizeof(name), "%s", c->tok.text);
|
||||
next(c);
|
||||
}
|
||||
if (accept(c, CAT_LEXER_TOK_EXTENDS)) {
|
||||
if (check(c, CAT_LEXER_TOK_NAME)) {
|
||||
snprintf(base, sizeof(base), "%s", c->tok.text);
|
||||
next(c);
|
||||
}
|
||||
}
|
||||
emit(c, CAT_BYTECODE_OP_NEWTABLE, 0, 0, line);
|
||||
/* the interface marker, so `is` knows to check structurally */
|
||||
emit(c, CAT_BYTECODE_OP_DUP, 0, 0, line);
|
||||
emit(c, CAT_BYTECODE_OP_TRUE, 0, 0, line);
|
||||
emit(c, CAT_BYTECODE_OP_SETATTR, add_string(c, "__interface"), 0, line);
|
||||
/* interface inheritance */
|
||||
if (base[0] != '\0') {
|
||||
emit(c, CAT_BYTECODE_OP_DUP, 0, 0, line);
|
||||
emit(c, CAT_BYTECODE_OP_GETGLOBAL, add_string(c, base), 0, line);
|
||||
emit(c, CAT_BYTECODE_OP_SETATTR, add_string(c, "__index"), 0, line);
|
||||
}
|
||||
while (!check(c, CAT_LEXER_TOK_END) && !check(c, CAT_LEXER_TOK_EOF)) {
|
||||
if (!check(c, CAT_LEXER_TOK_FUNCTION)) {
|
||||
err_at(c, c->tok.line,
|
||||
"expected 'function' or 'end' in interface");
|
||||
break;
|
||||
}
|
||||
next(c);
|
||||
{
|
||||
char mname[256];
|
||||
int proto;
|
||||
mname[0] = '\0';
|
||||
if (check(c, CAT_LEXER_TOK_NAME)) {
|
||||
snprintf(mname, sizeof(mname), "%s", c->tok.text);
|
||||
next(c);
|
||||
}
|
||||
proto = compile_function_literal(c, mname);
|
||||
emit(c, CAT_BYTECODE_OP_DUP, 0, 0, line);
|
||||
emit(c, CAT_BYTECODE_OP_CLOSURE, proto, 0, line);
|
||||
emit(c, CAT_BYTECODE_OP_SETATTR, add_string(c, mname), 0, line);
|
||||
}
|
||||
}
|
||||
expect(c, CAT_LEXER_TOK_END, "'end'");
|
||||
emit(c, CAT_BYTECODE_OP_SETGLOBAL, add_string(c, name), 0, line);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user