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:
1 parent
4dae4ee1cd
commit
1203acd64b
8 files changed
+381
-14
No files matched your search
+150
-14
@@ -18,7 +18,9 @@
|
||||
#include "cat/types/text.h"
|
||||
#include "cat/types/ascii_text.h"
|
||||
#include "cat/types/table.h"
|
||||
#include "cat/types/list.h"
|
||||
#include "cat/types/native.h"
|
||||
#include "cat/types/function.h"
|
||||
#include "cat/types/file.h"
|
||||
#include "cat/types/ffi.h"
|
||||
|
||||
@@ -117,6 +119,8 @@ static int lf_len(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
}
|
||||
if (args[0]->vtable && args[0]->vtable->kind == CAT_VALUE_KIND_TEXT)
|
||||
*out = cat_types_int_new(cat_types_text_length(args[0]));
|
||||
else if (cat_types_list_is(args[0]))
|
||||
*out = cat_types_int_new(cat_types_list_count(args[0]));
|
||||
else if (args[0]->vtable && args[0]->vtable->kind == CAT_VALUE_KIND_TABLE)
|
||||
*out = cat_types_int_new(cat_types_table_count(args[0]));
|
||||
else
|
||||
@@ -124,6 +128,125 @@ static int lf_len(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* attributes(obj) -- the member names of an object (like Python dir()):
|
||||
* a table's keys, plus inherited keys reachable through `__index`, plus
|
||||
* any fixed slots its type exposes, in a List. */
|
||||
static void collect_attributes(CAT_VALUE_Value *obj, CAT_VALUE_Value *list,
|
||||
CAT_VALUE_Value **seen, int *nseen)
|
||||
{
|
||||
CAT_VALUE_Value *base = NULL;
|
||||
int i, j;
|
||||
|
||||
if (obj == NULL || obj->vtable == NULL)
|
||||
return;
|
||||
/* skip a table we have already visited (classes point __index at
|
||||
* themselves, so the chain can be cyclic) */
|
||||
for (i = 0; i < *nseen; i++)
|
||||
if (seen[i] == obj)
|
||||
return;
|
||||
if (*nseen < 8)
|
||||
seen[(*nseen)++] = obj;
|
||||
/* a table/object's own keys */
|
||||
if (obj->vtable == cat_types_table_vtable()) {
|
||||
for (i = 0; i < cat_types_table_count(obj); i++) {
|
||||
const char *k = cat_types_table_key_at(obj, 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));
|
||||
}
|
||||
}
|
||||
/* follow inheritance */
|
||||
if (obj->vtable == cat_types_table_vtable()) {
|
||||
CAT_ERROR_Error e2;
|
||||
cat_error_clear(&e2);
|
||||
if (cat_value_get_attribute(obj, "__index", &base, &e2) == 0 &&
|
||||
base != NULL && base != obj)
|
||||
collect_attributes(base, list, seen, nseen);
|
||||
}
|
||||
}
|
||||
|
||||
static int lf_attributes(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
CAT_VALUE_Value *obj = nargs > 0 ? args[0] : NULL;
|
||||
CAT_VALUE_Value *list = cat_types_list_new();
|
||||
CAT_VALUE_Value *seen[8];
|
||||
int nseen = 0;
|
||||
int i;
|
||||
|
||||
(void)vm;
|
||||
if (obj == NULL || obj->vtable == NULL) {
|
||||
*out = list;
|
||||
return 0;
|
||||
}
|
||||
collect_attributes(obj, list, seen, &nseen);
|
||||
/* a native's own members, so attributes(print) is not empty */
|
||||
if (cat_types_native_is(obj)) {
|
||||
cat_types_list_push(list, cat_types_ascii_text_new("name"));
|
||||
cat_types_list_push(list, cat_types_ascii_text_new("_doc_"));
|
||||
}
|
||||
/* fixed members from the type's slot layout */
|
||||
if (obj->vtable->slots != NULL) {
|
||||
for (i = 0; i < obj->vtable->nslots; i++)
|
||||
cat_types_list_push(list, cat_types_ascii_text_new(
|
||||
obj->vtable->slots[i].name));
|
||||
}
|
||||
*out = list;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* help(obj) -- print a value's documentation. Looks up its `_doc_`
|
||||
* attribute (an internal docstring) and otherwise lists what it offers. */
|
||||
static int lf_help(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
CAT_VALUE_Value *obj = nargs > 0 ? args[0] : NULL;
|
||||
CAT_ERROR_Error e;
|
||||
CAT_VALUE_Value *doc = NULL;
|
||||
|
||||
(void)vm;
|
||||
if (obj == NULL) {
|
||||
printf("help(value) -- show documentation for any value.\n");
|
||||
*out = cat_types_nil_get();
|
||||
return 0;
|
||||
}
|
||||
cat_error_clear(&e);
|
||||
if (cat_value_get_attribute(obj, "_doc_", &doc, &e) == 0 &&
|
||||
doc != NULL && doc->vtable != NULL &&
|
||||
doc->vtable->kind == CAT_VALUE_KIND_TEXT) {
|
||||
printf("%s\n", cat_types_text_bytes(doc));
|
||||
*out = cat_types_nil_get();
|
||||
return 0;
|
||||
}
|
||||
/* no `_doc_`: describe the type and its members */
|
||||
printf("%s\n", cat_value_type_name(obj));
|
||||
cat_error_clear(&e);
|
||||
{
|
||||
CAT_VALUE_Value *names = NULL;
|
||||
lf_attributes(vm, args, nargs, &names);
|
||||
if (names != NULL && cat_types_list_count(names) > 0) {
|
||||
int i;
|
||||
printf("attributes:\n");
|
||||
for (i = 0; i < cat_types_list_count(names); i++) {
|
||||
CAT_VALUE_Value *n = cat_types_list_at(names, i);
|
||||
if (n != NULL && n->vtable &&
|
||||
n->vtable->kind == CAT_VALUE_KIND_TEXT)
|
||||
printf(" %s\n", cat_types_text_bytes(n));
|
||||
}
|
||||
}
|
||||
}
|
||||
*out = cat_types_nil_get();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lf_toml_parse(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
@@ -411,20 +534,33 @@ static int lf_bytesio(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
|
||||
int cat_stdlib_stdlib_install(CAT_VM_State *vm)
|
||||
{
|
||||
cat_vm_register_native(vm, "print", lf_print);
|
||||
cat_vm_register_native(vm, "tostring", lf_tostring);
|
||||
cat_vm_register_native(vm, "tonumber", lf_tonumber);
|
||||
cat_vm_register_native(vm, "type", lf_type);
|
||||
cat_vm_register_native(vm, "len", lf_len);
|
||||
cat_vm_register_native(vm, "toml_parse", lf_toml_parse);
|
||||
cat_vm_register_native(vm, "toml_encode", lf_toml_encode);
|
||||
cat_vm_register_native(vm, "read_file", lf_read_file);
|
||||
cat_vm_register_native(vm, "write_file", lf_write_file);
|
||||
cat_vm_register_native(vm, "ord", lf_ord);
|
||||
cat_vm_register_native(vm, "chr", lf_chr);
|
||||
cat_vm_register_native(vm, "substr", lf_substr);
|
||||
cat_vm_register_native(vm, "dofile", lf_dofile);
|
||||
cat_vm_register_native(vm, "use", lf_use);
|
||||
struct { const char *name;
|
||||
int (*cb)(CAT_VM_State *, CAT_VALUE_Value **, int, CAT_VALUE_Value **);
|
||||
const char *doc; } std[] = {
|
||||
{ "print", lf_print, "print(a, ...) -- escreve os argumentos em stdout." },
|
||||
{ "tostring", lf_tostring, "tostring(v) -- representa\u00e7\u00e3o textual de v." },
|
||||
{ "tonumber", lf_tonumber, "tonumber(s) -- converte texto em n\u00famero (ou nil)." },
|
||||
{ "type", lf_type, "type(v) -- nome do tipo de v." },
|
||||
{ "len", lf_len, "len(v) -- tamanho de texto ou n\u00famero de membros de um objeto." },
|
||||
{ "attributes", lf_attributes, "attributes(obj) -- nomes dos membros de obj (lista), incluindo heran\u00e7a (como dir())." },
|
||||
{ "help", lf_help, "help(obj) -- mostra a documenta\u00e7\u00e3o (`_doc_`) de obj ou lista seus atributos." },
|
||||
{ "toml_parse", lf_toml_parse, "toml_parse(text) -- analisa TOML e devolve um objeto." },
|
||||
{ "toml_encode", lf_toml_encode, "toml_encode(obj) -- serializa um objeto em TOML." },
|
||||
{ "read_file", lf_read_file, "read_file(path) -- l\u00ea o conte\u00fado de um arquivo como texto." },
|
||||
{ "write_file", lf_write_file, "write_file(path, text) -- escreve texto em um arquivo." },
|
||||
{ "ord", lf_ord, "ord(s) -- c\u00f3digo do primeiro caractere de s." },
|
||||
{ "chr", lf_chr, "chr(n) -- caractere com c\u00f3digo n." },
|
||||
{ "substr", lf_substr, "substr(s, i, n) -- fatia de s a partir de i com n caracteres." },
|
||||
{ "dofile", lf_dofile, "dofile(path) -- compila e executa outro arquivo catscript." },
|
||||
{ "use", lf_use, "use(path) -- importa outro arquivo catscript (alias de dofile)." },
|
||||
{ NULL, NULL, NULL }
|
||||
};
|
||||
int i;
|
||||
|
||||
for (i = 0; std[i].name != NULL; i++)
|
||||
cat_vm_set_global(vm, std[i].name,
|
||||
cat_types_native_new_doc(std[i].name, std[i].cb,
|
||||
std[i].doc));
|
||||
cat_vm_register_native(vm, "__class_new", lf_class_new);
|
||||
/* the file family: AbstractFile (abstract base), File, StringIO, BytesIO */
|
||||
cat_vm_set_global(vm, "AbstractFile",
|
||||
|
||||
Reference in new issue
Block a user