reflection: attributes() (dir-like) and help() with _doc_

- new list type (iterable, indexable) backing attributes()
- attributes(obj): member names of a table/object, follows __index
  inheritance, includes native and slot members (like Python dir())
- help(obj): prints the value's _doc_ documentation, else its type
  and attributes
- natives carry an optional _doc_ string (cat_types_native_new_doc);
  stdlib builtins documented
- len() handles lists
- examples/reflect.cat + 8 assertions wired into run_tests.sh (77 total)
This commit is contained in:
Minus committed 2026-11-16 03:48:44 +00:00
1 parent 4dae4ee1cd
commit 1203acd64b
8 files changed
+381 -14

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
/*
* cat/types/list.h -- a growable, iterable list of values.
*
* Naming rule: symbols here are cat_types_list_* / CAT_TYPES_LIST_*.
*
* `attributes(obj)` returns a List of member names, and `for name in
* attributes(obj)` iterates it. A List is a concrete iterator: its
* `next` advances an internal cursor over the stored items.
*/
#ifndef CAT_TYPES_LIST_H
#define CAT_TYPES_LIST_H
#include "cat/value.h"
/* CAT_TYPES_LIST_List -- ordered, iterable sequence of values. */
typedef struct CAT_TYPES_LIST_List {
CAT_VALUE_Value base;
CAT_VALUE_Value **items;
int n, cap;
int cursor; /* iteration state for `next` */
} CAT_TYPES_LIST_List;
CAT_VALUE_Value *cat_types_list_new(void);
int cat_types_list_push(CAT_VALUE_Value *list, CAT_VALUE_Value *v);
int cat_types_list_count(CAT_VALUE_Value *list);
CAT_VALUE_Value *cat_types_list_at(CAT_VALUE_Value *list, int i);
CAT_VALUE_Vtable *cat_types_list_vtable(void);
int cat_types_list_is(CAT_VALUE_Value *v);
#endif /* CAT_TYPES_LIST_H */
+6
View File
@@ -21,12 +21,18 @@ typedef struct CAT_VM_State CAT_VM_State;
typedef struct CAT_TYPES_NATIVE_Native {
CAT_VALUE_Value base;
char *name;
char *doc; /* optional `_doc_` documentation string */
int (*cb)(CAT_VM_State *, CAT_VALUE_Value **, int, CAT_VALUE_Value **);
} CAT_TYPES_NATIVE_Native;
CAT_VALUE_Value *cat_types_native_new(const char *name,
int (*cb)(CAT_VM_State *, CAT_VALUE_Value **, int, CAT_VALUE_Value **));
/* Like cat_types_native_new, but with an attached `_doc_` string. */
CAT_VALUE_Value *cat_types_native_new_doc(const char *name,
int (*cb)(CAT_VM_State *, CAT_VALUE_Value **, int, CAT_VALUE_Value **),
const char *doc);
CAT_VALUE_Vtable *cat_types_native_vtable(void);
/* True if v is a native function value. */