remove nil/null: absence is an error, empty object is the only no-value

- delete the nil type; catscript has no nil and no null
- add the empty object (Object with no members), exposed as the global
  'empty'; it is a shared immutable singleton
- 'nil' is no longer a keyword: it is an undefined global, which raises
- undefined global -> error (consistent with a missing attribute)
- only 'false' is falsy (numbers/objects are truthy); booleans kept
- OP_NIL -> OP_EMPTY; compiler, VM, stdlib, toml, ffi, generator, file,
  list, selector, value all migrated
- rename the table type to Object
- std/ front-end and examples/tests migrated to 'empty'
- 84/84 tests
This commit is contained in:
Minus committed 2026-11-16 04:08:24 +00:00
1 parent e0bf102f2a
commit d29b0706c9
36 files changed
+257 -244

No files matched your search

+28 -25
View File
@@ -10,7 +10,7 @@
#include "cat/stdlib/toml.h"
#include "cat/compiler.h"
#include "cat/bytecode.h"
#include "cat/types/nil.h"
#include "cat/types/empty.h"
#include "cat/types/bool.h"
#include "cat/types/number.h"
#include "cat/types/int.h"
@@ -23,6 +23,7 @@
#include "cat/types/function.h"
#include "cat/types/file.h"
#include "cat/types/ffi.h"
#include "cat/types/empty.h"
#include <stdio.h>
#include <stdlib.h>
@@ -33,8 +34,8 @@ static const char *value_to_text(CAT_VALUE_Value *v)
{
static char buf[512];
if (v == NULL || v == cat_types_nil_get())
return "nil";
if (v == NULL || v == cat_types_empty())
return "empty";
if (v->vtable && v->vtable->kind == CAT_VALUE_KIND_TEXT)
return cat_types_text_bytes(v);
if (v->vtable && v->vtable->kind == CAT_VALUE_KIND_NUMBER) {
@@ -62,7 +63,7 @@ static int lf_print(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
fputs(value_to_text(args[i]), stdout);
}
putchar('\n');
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
@@ -82,7 +83,7 @@ static int lf_tonumber(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
(void)vm;
if (nargs < 1) {
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
if (args[0]->vtable && args[0]->vtable->kind == CAT_VALUE_KIND_NUMBER) {
@@ -92,10 +93,10 @@ static int lf_tonumber(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
s = cat_types_text_bytes(args[0]);
if (strchr(s, '.') != NULL) {
double d = strtod(s, &end);
*out = (*end == '\0') ? cat_types_float_new(d) : cat_types_nil_get();
*out = (*end == '\0') ? cat_types_float_new(d) : cat_types_empty();
} else {
long long n = strtoll(s, &end, 10);
*out = (*end == '\0') ? cat_types_int_new(n) : cat_types_nil_get();
*out = (*end == '\0') ? cat_types_int_new(n) : cat_types_empty();
}
return 0;
}
@@ -105,7 +106,7 @@ static int lf_type(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
{
(void)vm;
*out = cat_types_ascii_text_new(nargs > 0 ? cat_value_type_name(args[0])
: "nil");
: "empty");
return 0;
}
@@ -235,7 +236,7 @@ static int lf_help(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
(void)vm;
if (obj == NULL) {
printf("help(value) -- show documentation for any value.\n");
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
cat_error_clear(&e);
@@ -243,7 +244,7 @@ static int lf_help(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
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();
*out = cat_types_empty();
return 0;
}
/* no `_doc_`: describe the type and its members */
@@ -263,7 +264,7 @@ static int lf_help(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
}
}
}
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
@@ -276,12 +277,12 @@ static int lf_toml_parse(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
(void)vm;
cat_error_clear(&e);
if (nargs < 1) {
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
if (cat_stdlib_toml_parse(cat_types_text_bytes(args[0]), &tbl, &e) != 0) {
fprintf(stderr, "toml_parse: %s\n", e.message);
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
*out = tbl;
@@ -316,13 +317,13 @@ static int lf_read_file(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
(void)vm;
if (nargs < 1) {
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
path = (char *)cat_types_text_bytes(args[0]);
f = fopen(path, "rb");
if (f == NULL) {
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
fseek(f, 0, SEEK_END);
@@ -331,7 +332,7 @@ static int lf_read_file(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
buf = malloc((size_t)sz + 1);
if (buf == NULL) {
fclose(f);
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
if (fread(buf, 1, (size_t)sz, f) != (size_t)sz) { /* short read ok */ }
@@ -349,7 +350,7 @@ static int lf_write_file(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
(void)vm;
if (nargs < 2) {
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
f = fopen(cat_types_text_bytes(args[0]), "wb");
@@ -358,7 +359,7 @@ static int lf_write_file(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
fwrite(s, 1, strlen(s), f);
fclose(f);
}
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
@@ -435,12 +436,12 @@ static int lf_dofile(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
CAT_ERROR_Error e;
if (nargs < 1) {
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
f = fopen(cat_types_text_bytes(args[0]), "rb");
if (f == NULL) {
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
fseek(f, 0, SEEK_END);
@@ -449,7 +450,7 @@ static int lf_dofile(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
src = malloc((size_t)sz + 1);
if (src == NULL) {
fclose(f);
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
if (fread(src, 1, (size_t)sz, f) != (size_t)sz) { /* short read ok */ }
@@ -460,7 +461,7 @@ static int lf_dofile(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
free(src);
if (fn == NULL) {
fprintf(stderr, "dofile: %s\n", e.message);
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
cat_error_clear(&e);
@@ -493,7 +494,7 @@ static int lf_class_new(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
(void)vm;
if (nargs < 1) {
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
cls = args[0];
@@ -529,7 +530,7 @@ static int lf_abstractfile(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
(void)nargs;
fprintf(stderr, "AbstractFile is abstract; use File::open, "
"StringIO or BytesIO\n");
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
@@ -559,7 +560,7 @@ int cat_stdlib_stdlib_install(CAT_VM_State *vm)
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)." },
{ "tonumber", lf_tonumber, "tonumber(s) -- converte texto em n\u00famero (ou vazio se inv\u00e1lido)." },
{ "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())." },
@@ -592,5 +593,7 @@ int cat_stdlib_stdlib_install(CAT_VM_State *vm)
cat_types_native_new("BytesIO", lf_bytesio));
/* the `ffi` library: call native code (ctypes/CDLL-like) */
cat_vm_set_global(vm, "ffi", cat_types_ffi_module());
/* the empty object: the value for "no value" (catscript has no nil) */
cat_vm_set_global(vm, "empty", cat_types_empty());
return 0;
}