selectors: a.b sends with self, a::b without; drop a:b
The `.` and `::` selectors swap meaning and `a:b` is removed, so every access is one of: a.b read the member a.b(args) call the member WITH a as self a::b static read (no self) a::b(args) call the member WITHOUT self - compiler: postfix `.` emits CALLMETHOD (with self) when followed by `(`, else GETATTR; `::` emits GETATTR + CALL (no self); the `:` form is gone (with a helpful error suggesting `.`/`::`). - `:` remains only for type hints (local x : int), unchanged. - ffi: `lib.func(...)` (with self) and `ffi::load(...)` (no self); the native loaders no longer skip a receiver. - file: `File::open` is static, so cm_open reads args[0]/args[1]. - lexer: fix cat_lexer_kind_name, whose table had drifted from the enum (missing `nil`), mislabelling tokens. - self-hosted parser (std/lang/parser.cat): `.name(` -> method (self), `::name(` -> call (no self); compiler/checker unchanged. - tools/migrate_selectors.py: one-pass migration of the call forms. - regenerated std/glfw and std/vulkan; migrated examples/tests/std and the README; new tests/selectors.cat (130 green). Verified live: GLFW window + Vulkan device still work; `a:b` now errors.
This commit is contained in:
1 parent
04009c3c5e
commit
6f6f444beb
35 files changed
+1114
-964
No files matched your search
+66
-27
@@ -405,42 +405,81 @@ static void primary(Compiler *c)
|
||||
if (c->tok.kind == CAT_LEXER_TOK_NAME ||
|
||||
(c->tok.kind >= CAT_LEXER_TOK_AND &&
|
||||
c->tok.kind <= CAT_LEXER_TOK_EXTENDS)) {
|
||||
int k = add_string(c, c->tok.text);
|
||||
emit(c, CAT_BYTECODE_OP_GETATTR, k, 0, c->tok.line);
|
||||
char mname[256];
|
||||
int mline = c->tok.line;
|
||||
int k;
|
||||
snprintf(mname, sizeof(mname), "%s", c->tok.text);
|
||||
next(c);
|
||||
k = add_string(c, mname);
|
||||
if (check(c, CAT_LEXER_TOK_LPAREN)) {
|
||||
/* a.b(args) -- method call WITH self */
|
||||
int nargs = 0;
|
||||
next(c);
|
||||
while (!check(c, CAT_LEXER_TOK_RPAREN) &&
|
||||
!check(c, CAT_LEXER_TOK_EOF)) {
|
||||
expr(c);
|
||||
nargs++;
|
||||
if (!accept(c, CAT_LEXER_TOK_COMMA))
|
||||
break;
|
||||
}
|
||||
expect(c, CAT_LEXER_TOK_RPAREN, "')'");
|
||||
emit(c, CAT_BYTECODE_OP_CALLMETHOD, nargs, k,
|
||||
mline);
|
||||
} else {
|
||||
/* a.b -- read the member (a field, or a function
|
||||
* value to be called later) */
|
||||
emit(c, CAT_BYTECODE_OP_GETATTR, k, 0, mline);
|
||||
}
|
||||
}
|
||||
} else if ((check(c, CAT_LEXER_TOK_COLON) ||
|
||||
check(c, CAT_LEXER_TOK_DCOLON)) &&
|
||||
c->lx.src[c->lx.pos] != '\0') {
|
||||
/* method call: obj:name(args), or class method:
|
||||
* Class::name(args). Both call with the receiver as
|
||||
* the implicit first argument (`self`). */
|
||||
} else if (check(c, CAT_LEXER_TOK_DCOLON)) {
|
||||
/* a::b -- static access: no `self`. `a::b(args)` calls
|
||||
* `b` without the receiver; `a::b` reads the member. */
|
||||
next(c);
|
||||
if (c->tok.kind == CAT_LEXER_TOK_NAME ||
|
||||
(c->tok.kind >= CAT_LEXER_TOK_AND &&
|
||||
c->tok.kind <= CAT_LEXER_TOK_EXTENDS)) {
|
||||
char mname[256];
|
||||
int mline = c->tok.line;
|
||||
int k;
|
||||
snprintf(mname, sizeof(mname), "%s", c->tok.text);
|
||||
next(c);
|
||||
k = add_string(c, mname);
|
||||
emit(c, CAT_BYTECODE_OP_GETATTR, k, 0, mline);
|
||||
if (check(c, CAT_LEXER_TOK_LPAREN)) {
|
||||
/* call the member WITHOUT self */
|
||||
int nargs = 0;
|
||||
next(c);
|
||||
while (!check(c, CAT_LEXER_TOK_RPAREN) &&
|
||||
!check(c, CAT_LEXER_TOK_EOF)) {
|
||||
expr(c);
|
||||
nargs++;
|
||||
if (!accept(c, CAT_LEXER_TOK_COMMA))
|
||||
break;
|
||||
}
|
||||
expect(c, CAT_LEXER_TOK_RPAREN, "')'");
|
||||
emit(c, CAT_BYTECODE_OP_CALL, nargs, 0, mline);
|
||||
}
|
||||
}
|
||||
} else if (check(c, CAT_LEXER_TOK_COLON)) {
|
||||
/* The old `obj:method(args)` syntax was removed. Detect it
|
||||
* (name '.') and point the user at the new forms. */
|
||||
size_t sp = c->lx.pos;
|
||||
int sl = c->lx.line;
|
||||
CAT_LEXER_Token la;
|
||||
CAT_LEXER_Token la, nx;
|
||||
cat_lexer_next(&c->lx, &la);
|
||||
if (la.kind == CAT_LEXER_TOK_NAME) {
|
||||
char mname[256];
|
||||
int k, nargs = 0;
|
||||
snprintf(mname, sizeof(mname), "%s", la.text);
|
||||
c->tok = la;
|
||||
next(c);
|
||||
expect(c, CAT_LEXER_TOK_LPAREN, "'('");
|
||||
while (!check(c, CAT_LEXER_TOK_RPAREN) &&
|
||||
!check(c, CAT_LEXER_TOK_EOF)) {
|
||||
expr(c);
|
||||
nargs++;
|
||||
if (!accept(c, CAT_LEXER_TOK_COMMA))
|
||||
break;
|
||||
}
|
||||
expect(c, CAT_LEXER_TOK_RPAREN, "')'");
|
||||
k = add_string(c, mname);
|
||||
emit(c, CAT_BYTECODE_OP_CALLMETHOD, nargs, k, c->tok.line);
|
||||
} else {
|
||||
cat_lexer_next(&c->lx, &nx);
|
||||
c->lx.pos = sp;
|
||||
c->lx.line = sl;
|
||||
break;
|
||||
if (nx.kind == CAT_LEXER_TOK_LPAREN) {
|
||||
err_at(c, c->tok.line,
|
||||
"the ':' method syntax was removed; "
|
||||
"use '.' for obj.method(args) or "
|
||||
"'::' for obj::method(args)");
|
||||
c->failed = 1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
} else if (check(c, CAT_LEXER_TOK_LPAREN)) {
|
||||
int nargs = 0;
|
||||
next(c);
|
||||
|
||||
+8
-8
@@ -281,16 +281,16 @@ int cat_lexer_next(CAT_LEXER_Lexer *lx, CAT_LEXER_Token *out)
|
||||
|
||||
const char *cat_lexer_kind_name(int kind)
|
||||
{
|
||||
/* Must match enum CAT_LEXER_Kind exactly (same order). */
|
||||
static const char *names[] = {
|
||||
"EOF", "NAME", "INT", "FLOAT", "STRING",
|
||||
"and", "break", "do", "else", "elseif", "end", "false", "for",
|
||||
"function", "if", "in", "local", "not", "or", "repeat",
|
||||
"return", "then", "true", "until", "while",
|
||||
"yield", "try", "except", "raise",
|
||||
"+", "-", "*", "/", "%", "^", "#", "==", "~=", "<=", ">=", "<",
|
||||
">", "=", "(", ")", "{", "}", "[", "]", ";", ",", ".", ":",
|
||||
"::", "..", "->",
|
||||
"class", "extends", "use"
|
||||
"and", "break", "do", "else", "elseif", "end", "false",
|
||||
"for", "function", "if", "in", "local", "nil", "not",
|
||||
"or", "repeat", "return", "then", "true", "until", "while",
|
||||
"yield", "try", "except", "raise", "use", "class", "extends",
|
||||
"+", "-", "*", "/", "%", "^", "#", "==", "~=", "<=", ">=",
|
||||
"<", ">", "=", "(", ")", "{", "}", "[", "]", ";", ",",
|
||||
".", ":", "::", "..", "->"
|
||||
};
|
||||
if (kind >= 0 && kind < (int)(sizeof(names) / sizeof(names[0])))
|
||||
return names[kind];
|
||||
|
||||
+3
-3
@@ -3,7 +3,7 @@
|
||||
*
|
||||
* Naming rule: symbols here are cat_types_ffi_* / CAT_TYPES_FFI_*.
|
||||
*
|
||||
* `ffi.load(path)` dlopen()s a shared object. `lib:func(name, sig)`
|
||||
* `ffi.load(path)` dlopen()s a shared object. `lib.func(name, sig)`
|
||||
* dlsym()s a symbol and prepares a libffi call from the signature, e.g.
|
||||
* "double(double,double)". Calling the result makes the ABI-correct
|
||||
* native call and converts the result back to a catscript value.
|
||||
@@ -254,7 +254,7 @@ CAT_VALUE_Vtable *cat_types_ffi_function_vtable(void)
|
||||
|
||||
/* ---- the Library value ---------------------------------------------- */
|
||||
|
||||
/* lib:func(name, sig) -- the explicit form that carries the signature. */
|
||||
/* lib.func(name, sig) -- the explicit form that carries the signature. */
|
||||
static int lib_method_func(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
@@ -280,7 +280,7 @@ static int library_get_attr_full(CAT_VALUE_Value *self, const char *name,
|
||||
static int library_call(CAT_VALUE_Value *self, CAT_VALUE_Value **args,
|
||||
int nargs, CAT_VALUE_Value **out, CAT_ERROR_Error *err)
|
||||
{
|
||||
/* Calling a library directly: lib("name", "sig") == lib:func(...) */
|
||||
/* Calling a library directly: lib("name", "sig") == lib.func(...) */
|
||||
CAT_VALUE_Value *callargs[4];
|
||||
|
||||
callargs[0] = self;
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
* f = File::open("x.txt") -- a real file
|
||||
* s = StringIO("hello") -- an in-memory text stream
|
||||
* b = BytesIO() -- an in-memory byte stream
|
||||
* f:write("data"); f:close()
|
||||
* s:read()
|
||||
* f.write("data"); f.close()
|
||||
* s.read()
|
||||
*
|
||||
* Thread-safety: every stream owns its own buffer/handle; nothing here
|
||||
* touches shared mutable state, so streams created and used on separate
|
||||
@@ -363,12 +363,14 @@ static int cm_open(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
const char *path, *mode;
|
||||
|
||||
(void)vm;
|
||||
if (nargs < 2) {
|
||||
/* `File::open(path, mode)` is static (`::`), so the receiver is NOT
|
||||
* passed: path is args[0] and mode args[1]. */
|
||||
if (nargs < 1) {
|
||||
*out = cat_types_empty();
|
||||
return 0;
|
||||
}
|
||||
path = as_text(args[1]);
|
||||
mode = (nargs > 2) ? as_text(args[2]) : "r";
|
||||
path = as_text(args[0]);
|
||||
mode = (nargs > 1) ? as_text(args[1]) : "r";
|
||||
if (mode == NULL)
|
||||
mode = "r";
|
||||
*out = cat_types_file_open(path, mode);
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
static int generator_next(CAT_VALUE_Value *self, CAT_VALUE_Value **out,
|
||||
CAT_ERROR_Error *err);
|
||||
|
||||
/* `it:next()` method form: receiver is args[0]. */
|
||||
/* `it.next()` method form: receiver is args[0]. */
|
||||
static int gen_method_next(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out);
|
||||
|
||||
@@ -113,7 +113,7 @@ static int generator_next(CAT_VALUE_Value *self, CAT_VALUE_Value **out,
|
||||
return 1; /* exhausted */
|
||||
}
|
||||
|
||||
/* `it:next()` -- returns the next value, or empty when exhausted. */
|
||||
/* `it.next()` -- returns the next value, or empty when exhausted. */
|
||||
static int gen_method_next(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
|
||||
@@ -24,7 +24,7 @@ static int list_next(CAT_VALUE_Value *self, CAT_VALUE_Value **out,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* list[i] / list:get(i) -- 0-based indexing. */
|
||||
/* list[i] / list.get(i) -- 0-based indexing. */
|
||||
static int list_get_attribute(CAT_VALUE_Value *self, const char *name,
|
||||
CAT_VALUE_Value **out, CAT_ERROR_Error *err)
|
||||
{
|
||||
|
||||
@@ -111,7 +111,7 @@ static int mem_set_u64(CAT_VM_State *vm, CAT_VALUE_Value **a, int n, CAT_VALUE_V
|
||||
static int mem_set_f32(CAT_VM_State *vm, CAT_VALUE_Value **a, int n, CAT_VALUE_Value **o){ return mem_set(a[0], a+1, n-1, o, &vm->err, 4, 1); }
|
||||
static int mem_set_f64(CAT_VM_State *vm, CAT_VALUE_Value **a, int n, CAT_VALUE_Value **o){ return mem_set(a[0], a+1, n-1, o, &vm->err, 8, 1); }
|
||||
|
||||
/* m:text(off) -- read a NUL-terminated string starting at off. */
|
||||
/* m.text(off) -- read a NUL-terminated string starting at off. */
|
||||
static int mem_text(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
@@ -126,7 +126,7 @@ static int mem_text(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* m:set_text(off, s) -- write s plus a terminating NUL. */
|
||||
/* m.set_text(off, s) -- write s plus a terminating NUL. */
|
||||
static int mem_set_text(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
@@ -144,7 +144,7 @@ static int mem_set_text(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* m:ptr() -- the address as an int (for an FFI call). */
|
||||
/* m.ptr() -- the address as an int (for an FFI call). */
|
||||
static int mem_ptr(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
@@ -154,7 +154,7 @@ static int mem_ptr(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* m:size() -- the buffer length. */
|
||||
/* m.size() -- the buffer length. */
|
||||
static int mem_size_(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
@@ -164,7 +164,7 @@ static int mem_size_(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* m:fill(byte [, off, count]) -- set bytes. */
|
||||
/* m.fill(byte [, off, count]) -- set bytes. */
|
||||
static int mem_fill(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
@@ -183,7 +183,7 @@ static int mem_fill(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* m:copy(dst_off, src_memory, src_off, count) -- bounded memcpy. */
|
||||
/* m.copy(dst_off, src_memory, src_off, count) -- bounded memcpy. */
|
||||
static int mem_copy(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* Naming rule: symbols here are cat_types_selector_* / CAT_TYPES_SELECTOR_*.
|
||||
*
|
||||
* Each selector is a bound default method. When a program sends
|
||||
* `obj:_dot_(k)` and the object does not define `_dot_`, the attribute
|
||||
* `obj._dot_(k)` and the object does not define `_dot_`, the attribute
|
||||
* dispatcher hands back one of these instead, and calling it performs the
|
||||
* raw built-in access. This is what lets the compiler drop the special
|
||||
* GETATTR/SETATTR/GETIDX/SETIDX/CALL cases: they are all just `:` sends.
|
||||
@@ -35,7 +35,7 @@ static void sel_key(CAT_VALUE_Value *v, char *buf, size_t n)
|
||||
buf[0] = '\0';
|
||||
}
|
||||
|
||||
/* A selector is called as `receiver:_sel_(...)`, so args[0] is the
|
||||
/* A selector is called as `receiver._sel_(...)`, so args[0] is the
|
||||
* receiver and args[1..] are the selector's own arguments. */
|
||||
static int selector_call(CAT_VALUE_Value *self, CAT_VALUE_Value **args,
|
||||
int nargs, CAT_VALUE_Value **out, CAT_ERROR_Error *err)
|
||||
|
||||
Reference in new issue
Block a user