functions are objects: closures carry a member table (_doc_)
- CAT_TYPES_FUNCTION_Closure gains an attrs table; get/set_attribute give closures real members, so a function can hold _doc_ and any other attribute like a table - cat_types_function_attrs() accessor; attributes() enumerates a closure's members; help() reads a function's _doc_ - compiler: generalise dotted assignment to chains (name.a.b = v), so a method can be documented (Calc.add._doc_ = ...) - tests/functions_as_objects.cat + 7 assertions (84 total, all green) - README: 'funcoes tambem sao objetos' + reflection sections
This commit is contained in:
1 parent
1203acd64b
commit
e0bf102f2a
7 files changed
+183
-18
No files matched your search
+40
-17
@@ -933,25 +933,48 @@ static void stat(Compiler *c)
|
||||
break;
|
||||
}
|
||||
if (la.kind == CAT_LEXER_TOK_DOT) {
|
||||
CAT_LEXER_Token attr;
|
||||
cat_lexer_next(&c->lx, &attr);
|
||||
if (attr.kind == CAT_LEXER_TOK_NAME) {
|
||||
CAT_LEXER_Token eq;
|
||||
cat_lexer_next(&c->lx, &eq);
|
||||
if (eq.kind == CAT_LEXER_TOK_ASSIGN) {
|
||||
int slot = find_local(c, name);
|
||||
c->tok = eq;
|
||||
next(c);
|
||||
if (slot >= 0)
|
||||
emit(c, CAT_BYTECODE_OP_GETLOCAL, slot, 0, line);
|
||||
else
|
||||
emit(c, CAT_BYTECODE_OP_GETGLOBAL,
|
||||
add_string(c, name), 0, line);
|
||||
expr(c);
|
||||
emit(c, CAT_BYTECODE_OP_SETATTR,
|
||||
add_string(c, attr.text), 0, line);
|
||||
/* dotted assignment, possibly chained: name.a.b = value.
|
||||
* Collect the attribute chain; if it ends in '=', emit the
|
||||
* object expression followed by a final SETATTR. Otherwise
|
||||
* rewind and parse as an ordinary expression. */
|
||||
char chain[16][256];
|
||||
int nchain = 0;
|
||||
int is_assign = 0;
|
||||
for (;;) {
|
||||
CAT_LEXER_Token attr, nx;
|
||||
cat_lexer_next(&c->lx, &attr);
|
||||
if (attr.kind != CAT_LEXER_TOK_NAME)
|
||||
break;
|
||||
if (nchain < 16)
|
||||
snprintf(chain[nchain], sizeof(chain[0]),
|
||||
"%s", attr.text);
|
||||
nchain++;
|
||||
cat_lexer_next(&c->lx, &nx);
|
||||
if (nx.kind == CAT_LEXER_TOK_DOT)
|
||||
continue;
|
||||
if (nx.kind == CAT_LEXER_TOK_ASSIGN) {
|
||||
c->tok = nx;
|
||||
is_assign = 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (is_assign && nchain > 0 && nchain <= 16) {
|
||||
int slot = find_local(c, name);
|
||||
int k;
|
||||
next(c);
|
||||
if (slot >= 0)
|
||||
emit(c, CAT_BYTECODE_OP_GETLOCAL, slot, 0, line);
|
||||
else
|
||||
emit(c, CAT_BYTECODE_OP_GETGLOBAL,
|
||||
add_string(c, name), 0, line);
|
||||
/* walk to the object that owns the final attribute */
|
||||
for (k = 0; k < nchain - 1; k++)
|
||||
emit(c, CAT_BYTECODE_OP_GETATTR,
|
||||
add_string(c, chain[k]), 0, line);
|
||||
expr(c);
|
||||
emit(c, CAT_BYTECODE_OP_SETATTR,
|
||||
add_string(c, chain[nchain - 1]), 0, line);
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* not an assignment: rewind and parse as an expression, keeping the
|
||||
|
||||
Reference in new issue
Block a user