generics: <T, ...> type parameters on classes and interfaces
- parse_type_params(): accepts an optional `<T, U, ...>` list after a class/interface/trait name and records the names. - The declaration is tagged `__generic` with the comma-joined parameter list (e.g. "T" or "K,V"). Parameters are an erasure model: they are compile-time only, so a generic class is an ordinary class at runtime. - tests/generics.cat + suite: generic class, generic interface, a class implementing a generic interface, and a two-parameter class. Suite 152 -> 155.
This commit is contained in:
1 parent
33a1152958
commit
841c445a05
3 files changed
+91
No files matched your search
@@ -263,6 +263,38 @@ static CAT_TYPES_TYPE_Type *parse_type_hint(Compiler *c)
|
|||||||
return parse_type_core(c, 1);
|
return parse_type_core(c, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Parse an optional `<T, U, ...>` type-parameter list at the cursor.
|
||||||
|
* Returns the number of parameters (0 when there is none). When `out`
|
||||||
|
* is given, the parameter names are written there joined by commas
|
||||||
|
* (e.g. "T,U"), so a declaration can tag itself with its parameters. */
|
||||||
|
static int parse_type_params(Compiler *c, char *out, size_t outsz)
|
||||||
|
{
|
||||||
|
int n = 0;
|
||||||
|
size_t used = 0;
|
||||||
|
|
||||||
|
if (!check(c, CAT_LEXER_TOK_LT))
|
||||||
|
return 0;
|
||||||
|
next(c);
|
||||||
|
if (out != NULL && outsz > 0)
|
||||||
|
out[0] = '\0';
|
||||||
|
while (!check(c, CAT_LEXER_TOK_GT) && !check(c, CAT_LEXER_TOK_EOF)) {
|
||||||
|
if (check(c, CAT_LEXER_TOK_NAME)) {
|
||||||
|
if (out != NULL && used + strlen(c->tok.text) + 2 < outsz) {
|
||||||
|
if (used > 0)
|
||||||
|
out[used++] = ',';
|
||||||
|
strcpy(out + used, c->tok.text);
|
||||||
|
used += strlen(c->tok.text);
|
||||||
|
}
|
||||||
|
n++;
|
||||||
|
next(c);
|
||||||
|
}
|
||||||
|
if (!accept(c, CAT_LEXER_TOK_COMMA))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
expect(c, CAT_LEXER_TOK_GT, "'>'");
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
/* The type of the value the cursor is on, when it is a literal; NULL when
|
/* The type of the value the cursor is on, when it is a literal; NULL when
|
||||||
* the expression is not a simple literal (then we say nothing). */
|
* the expression is not a simple literal (then we say nothing). */
|
||||||
static CAT_TYPES_TYPE_Type *literal_type(Compiler *c)
|
static CAT_TYPES_TYPE_Type *literal_type(Compiler *c)
|
||||||
@@ -939,15 +971,19 @@ static void stat(Compiler *c)
|
|||||||
char name[256];
|
char name[256];
|
||||||
char base[256];
|
char base[256];
|
||||||
char impl[8][256];
|
char impl[8][256];
|
||||||
|
char tparams[256];
|
||||||
|
int nparams_t;
|
||||||
int nimpl = 0;
|
int nimpl = 0;
|
||||||
int i;
|
int i;
|
||||||
next(c);
|
next(c);
|
||||||
name[0] = '\0';
|
name[0] = '\0';
|
||||||
base[0] = '\0';
|
base[0] = '\0';
|
||||||
|
tparams[0] = '\0';
|
||||||
if (check(c, CAT_LEXER_TOK_NAME)) {
|
if (check(c, CAT_LEXER_TOK_NAME)) {
|
||||||
snprintf(name, sizeof(name), "%s", c->tok.text);
|
snprintf(name, sizeof(name), "%s", c->tok.text);
|
||||||
next(c);
|
next(c);
|
||||||
}
|
}
|
||||||
|
nparams_t = parse_type_params(c, tparams, sizeof(tparams));
|
||||||
if (accept(c, CAT_LEXER_TOK_EXTENDS)) {
|
if (accept(c, CAT_LEXER_TOK_EXTENDS)) {
|
||||||
if (check(c, CAT_LEXER_TOK_NAME)) {
|
if (check(c, CAT_LEXER_TOK_NAME)) {
|
||||||
snprintf(base, sizeof(base), "%s", c->tok.text);
|
snprintf(base, sizeof(base), "%s", c->tok.text);
|
||||||
@@ -1005,6 +1041,13 @@ static void stat(Compiler *c)
|
|||||||
}
|
}
|
||||||
expect(c, CAT_LEXER_TOK_END, "'end'");
|
expect(c, CAT_LEXER_TOK_END, "'end'");
|
||||||
emit(c, CAT_BYTECODE_OP_SETGLOBAL, add_string(c, name), 0, line);
|
emit(c, CAT_BYTECODE_OP_SETGLOBAL, add_string(c, name), 0, line);
|
||||||
|
if (nparams_t > 0) {
|
||||||
|
emit(c, CAT_BYTECODE_OP_GETGLOBAL, add_string(c, name), 0, line);
|
||||||
|
emit(c, CAT_BYTECODE_OP_CONST,
|
||||||
|
add_string(c, tparams), 0, line);
|
||||||
|
emit(c, CAT_BYTECODE_OP_SETATTR,
|
||||||
|
add_string(c, "__generic"), 0, line);
|
||||||
|
}
|
||||||
/* runtime assertion: the class must satisfy each declared interface
|
/* runtime assertion: the class must satisfy each declared interface
|
||||||
* (structural check, performed by __implements_check), and gets any
|
* (structural check, performed by __implements_check), and gets any
|
||||||
* trait's default methods mixed in (a no-op for plain interfaces). */
|
* trait's default methods mixed in (a no-op for plain interfaces). */
|
||||||
@@ -1037,14 +1080,18 @@ static void stat(Compiler *c)
|
|||||||
* that `implements` it has those methods mixed in. */
|
* that `implements` it has those methods mixed in. */
|
||||||
char name[256];
|
char name[256];
|
||||||
char base[256];
|
char base[256];
|
||||||
|
char tparams[256];
|
||||||
|
int nparams_t;
|
||||||
int is_trait = (c->tok.kind == CAT_LEXER_TOK_TRAIT);
|
int is_trait = (c->tok.kind == CAT_LEXER_TOK_TRAIT);
|
||||||
next(c);
|
next(c);
|
||||||
name[0] = '\0';
|
name[0] = '\0';
|
||||||
base[0] = '\0';
|
base[0] = '\0';
|
||||||
|
tparams[0] = '\0';
|
||||||
if (check(c, CAT_LEXER_TOK_NAME)) {
|
if (check(c, CAT_LEXER_TOK_NAME)) {
|
||||||
snprintf(name, sizeof(name), "%s", c->tok.text);
|
snprintf(name, sizeof(name), "%s", c->tok.text);
|
||||||
next(c);
|
next(c);
|
||||||
}
|
}
|
||||||
|
nparams_t = parse_type_params(c, tparams, sizeof(tparams));
|
||||||
if (accept(c, CAT_LEXER_TOK_EXTENDS)) {
|
if (accept(c, CAT_LEXER_TOK_EXTENDS)) {
|
||||||
if (check(c, CAT_LEXER_TOK_NAME)) {
|
if (check(c, CAT_LEXER_TOK_NAME)) {
|
||||||
snprintf(base, sizeof(base), "%s", c->tok.text);
|
snprintf(base, sizeof(base), "%s", c->tok.text);
|
||||||
@@ -1091,6 +1138,13 @@ static void stat(Compiler *c)
|
|||||||
}
|
}
|
||||||
expect(c, CAT_LEXER_TOK_END, "'end'");
|
expect(c, CAT_LEXER_TOK_END, "'end'");
|
||||||
emit(c, CAT_BYTECODE_OP_SETGLOBAL, add_string(c, name), 0, line);
|
emit(c, CAT_BYTECODE_OP_SETGLOBAL, add_string(c, name), 0, line);
|
||||||
|
if (nparams_t > 0) {
|
||||||
|
emit(c, CAT_BYTECODE_OP_GETGLOBAL, add_string(c, name), 0, line);
|
||||||
|
emit(c, CAT_BYTECODE_OP_CONST,
|
||||||
|
add_string(c, tparams), 0, line);
|
||||||
|
emit(c, CAT_BYTECODE_OP_SETATTR,
|
||||||
|
add_string(c, "__generic"), 0, line);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
-- tests/generics.cat -- generic type parameters (erasure model).
|
||||||
|
--
|
||||||
|
-- `class Box<T>` / `interface Container<T>` accept a `<T, ...>` parameter
|
||||||
|
-- list. Parameter names are compile-time only (erased at runtime), so a
|
||||||
|
-- generic declaration is an ordinary class/interface tagged with its
|
||||||
|
-- parameters in `__generic` -- structural, like the rest of the typing.
|
||||||
|
|
||||||
|
class Box<T>
|
||||||
|
function init(self, v) self.v = v end
|
||||||
|
function get(self) return self.v end
|
||||||
|
end
|
||||||
|
|
||||||
|
interface Container<T>
|
||||||
|
function size(self) end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Vec implements Container
|
||||||
|
function size(self) return 3 end
|
||||||
|
end
|
||||||
|
|
||||||
|
local b = Box("hi")
|
||||||
|
print("box:", b.get(), b::__generic)
|
||||||
|
|
||||||
|
local v = Vec()
|
||||||
|
print("vec:", v.size(), v is Container)
|
||||||
|
|
||||||
|
-- two parameters
|
||||||
|
class Pair<K, V>
|
||||||
|
function init(self, k, val) self.k = k self.val = val end
|
||||||
|
end
|
||||||
|
local p = Pair("x", 9)
|
||||||
|
print("pair:", p.k, p.val, p::__generic)
|
||||||
@@ -263,6 +263,11 @@ check "trait-robot" "robot is Greeter: true" "`echo "$out" | sed -n 6p`"
|
|||||||
check "trait-sized" "sized: true" "`echo "$out" | sed -n 7p`"
|
check "trait-sized" "sized: true" "`echo "$out" | sed -n 7p`"
|
||||||
check "trait-sized-fn" "implements Sized: true" "`echo "$out" | sed -n 8p`"
|
check "trait-sized-fn" "implements Sized: true" "`echo "$out" | sed -n 8p`"
|
||||||
|
|
||||||
|
out=`$BIN tests/generics.cat`
|
||||||
|
check "generic-box" "box: hi T" "`echo "$out" | sed -n 1p`"
|
||||||
|
check "generic-iface" "vec: 3 true" "`echo "$out" | sed -n 2p`"
|
||||||
|
check "generic-pair" "pair: x 9 K,V" "`echo "$out" | sed -n 3p`"
|
||||||
|
|
||||||
out=`$BIN tests/aot.cat`
|
out=`$BIN tests/aot.cat`
|
||||||
exp=`$BIN tests/aot.cat`
|
exp=`$BIN tests/aot.cat`
|
||||||
$BIN --build tests/aot.cat build/tests/aot_native >/dev/null 2>&1
|
$BIN --build tests/aot.cat build/tests/aot_native >/dev/null 2>&1
|
||||||
|
|||||||
Reference in new issue
Block a user