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:
Minus committed 2026-11-16 03:50:54 +00:00
1 parent 1203acd64b
commit e0bf102f2a
7 files changed
+183 -18

No files matched your search

+21 -1
View File
@@ -180,7 +180,7 @@ static int lf_attributes(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
CAT_VALUE_Value *list = cat_types_list_new();
CAT_VALUE_Value *seen[8];
int nseen = 0;
int i;
int i, j;
(void)vm;
if (obj == NULL || obj->vtable == NULL) {
@@ -193,6 +193,26 @@ static int lf_attributes(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
cat_types_list_push(list, cat_types_ascii_text_new("name"));
cat_types_list_push(list, cat_types_ascii_text_new("_doc_"));
}
/* a closure's member table (functions are objects too) */
{
CAT_VALUE_Value *fattrs = cat_types_function_attrs(obj);
if (fattrs != NULL && fattrs->vtable == cat_types_table_vtable()) {
for (i = 0; i < cat_types_table_count(fattrs); i++) {
const char *k = cat_types_table_key_at(fattrs, i);
int dup = 0;
for (j = 0; j < cat_types_list_count(list); j++) {
CAT_VALUE_Value *n = cat_types_list_at(list, j);
if (n != NULL && n->vtable &&
n->vtable->kind == CAT_VALUE_KIND_TEXT &&
strcmp(cat_types_text_bytes(n), k) == 0)
dup = 1;
}
if (!dup)
cat_types_list_push(list,
cat_types_ascii_text_new(k));
}
}
}
/* fixed members from the type's slot layout */
if (obj->vtable->slots != NULL) {
for (i = 0; i < obj->vtable->nslots; i++)