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

+1 -1
View File
@@ -78,7 +78,7 @@ int cat_bytecode_add_proto(CAT_BYTECODE_Function *fn, CAT_BYTECODE_Function *p)
static const char *op_name(unsigned char op)
{
static const char *names[] = {
"NOP", "CONST", "NIL", "TRUE", "FALSE", "GETLOCAL", "SETLOCAL",
"NOP", "CONST", "EMPTY", "TRUE", "FALSE", "GETLOCAL", "SETLOCAL",
"GETGLOBAL", "SETGLOBAL", "ADD", "SUB", "MUL", "DIV", "REM",
"NEG", "NOT", "EQ", "NE", "LT", "LE", "GT", "GE", "GETATTR",
"SETATTR", "NEWTABLE", "CALL", "CALLMETHOD", "JUMP", "JUMPIF",
+6 -6
View File
@@ -301,7 +301,7 @@ static void primary(Compiler *c)
break;
}
case CAT_LEXER_TOK_NIL:
emit(c, CAT_BYTECODE_OP_NIL, 0, 0, t.line);
emit(c, CAT_BYTECODE_OP_EMPTY, 0, 0, t.line);
next(c);
break;
case CAT_LEXER_TOK_TRUE:
@@ -390,7 +390,7 @@ static void primary(Compiler *c)
default:
err_at(c, t.line, "unexpected token %s",
cat_lexer_kind_name(t.kind));
emit(c, CAT_BYTECODE_OP_NIL, 0, 0, t.line);
emit(c, CAT_BYTECODE_OP_EMPTY, 0, 0, t.line);
next(c);
return;
}
@@ -564,7 +564,7 @@ static void stat(Compiler *c)
if (accept(c, CAT_LEXER_TOK_ASSIGN))
expr(c);
else
emit(c, CAT_BYTECODE_OP_NIL, 0, 0, line);
emit(c, CAT_BYTECODE_OP_EMPTY, 0, 0, line);
slot = add_local(c, name);
if (slot >= 0)
emit(c, CAT_BYTECODE_OP_SETLOCAL, slot, 0, line);
@@ -654,7 +654,7 @@ static void stat(Compiler *c)
skip_type_hint(c);
if (check(c, CAT_LEXER_TOK_IN)) {
/* for NAME in expr do <body> end -- iterate any iterator
* (generator). Each round calls NEXT; nil ends the loop. */
* (generator). Each round calls NEXT; empty ends the loop. */
int islot;
next(c);
expr(c); /* the iterable */
@@ -839,7 +839,7 @@ static void stat(Compiler *c)
!check(c, CAT_LEXER_TOK_EXCEPT))
expr(c);
else
emit(c, CAT_BYTECODE_OP_NIL, 0, 0, line);
emit(c, CAT_BYTECODE_OP_EMPTY, 0, 0, line);
emit(c, CAT_BYTECODE_OP_YIELD, 0, 0, line);
ctx(c)->is_generator = 1;
accept(c, CAT_LEXER_TOK_SEMI);
@@ -852,7 +852,7 @@ static void stat(Compiler *c)
!check(c, CAT_LEXER_TOK_SEMI))
expr(c);
else
emit(c, CAT_BYTECODE_OP_NIL, 0, 0, line);
emit(c, CAT_BYTECODE_OP_EMPTY, 0, 0, line);
emit(c, CAT_BYTECODE_OP_RAISE, 0, 0, line);
accept(c, CAT_LEXER_TOK_SEMI);
break;
+1 -2
View File
@@ -31,7 +31,6 @@ static const struct kw g_keywords[] = {
{ "if", CAT_LEXER_TOK_IF },
{ "in", CAT_LEXER_TOK_IN },
{ "local", CAT_LEXER_TOK_LOCAL },
{ "nil", CAT_LEXER_TOK_NIL },
{ "not", CAT_LEXER_TOK_NOT },
{ "or", CAT_LEXER_TOK_OR },
{ "repeat", CAT_LEXER_TOK_REPEAT },
@@ -259,7 +258,7 @@ const char *cat_lexer_kind_name(int kind)
static const char *names[] = {
"EOF", "NAME", "INT", "FLOAT", "STRING",
"and", "break", "do", "else", "elseif", "end", "false", "for",
"function", "if", "in", "local", "nil", "not", "or", "repeat",
"function", "if", "in", "local", "not", "or", "repeat",
"return", "then", "true", "until", "while",
"yield", "try", "except", "raise",
"+", "-", "*", "/", "%", "^", "#", "==", "~=", "<=", ">=", "<",
+4 -4
View File
@@ -11,7 +11,7 @@
#include "cat/bytecode.h"
#include "cat/types/table.h"
#include "cat/types/int.h"
#include "cat/types/nil.h"
#include "cat/types/empty.h"
#include "cat/types/ascii_text.h"
#include "cat/error.h"
@@ -97,7 +97,7 @@ static int lf_bc_run(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
}
fn = build_function(args[0]);
if (fn == NULL) {
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
rc = cat_vm_run(vm, fn, out);
@@ -112,7 +112,7 @@ static int lf_bc_dump(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;
}
fn = build_function(args[0]);
@@ -120,7 +120,7 @@ static int lf_bc_dump(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
cat_bytecode_dump(fn, stdout);
cat_bytecode_function_free(fn);
}
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
+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;
}
+3 -3
View File
@@ -15,7 +15,7 @@
#include "cat/types/float.h"
#include "cat/types/ascii_text.h"
#include "cat/types/bool.h"
#include "cat/types/nil.h"
#include "cat/types/empty.h"
#include <ctype.h>
#include <stdio.h>
@@ -352,8 +352,8 @@ static void enc_string(FILE *f, const char *s)
static void enc_value(FILE *f, CAT_VALUE_Value *v)
{
if (v == NULL || v == cat_types_nil_get()) {
fputs("\"nil\"", f);
if (v == NULL || v == cat_types_empty()) {
fputs("\"\"", f);
} else if (v->vtable && v->vtable->kind == CAT_VALUE_KIND_TEXT) {
enc_string(f, cat_value_type_name(v) ? ((CAT_TYPES_TEXT_Text *)v)->bytes : "");
} else if (v->vtable && v->vtable->kind == CAT_VALUE_KIND_NUMBER) {
-7
View File
@@ -12,12 +12,6 @@ static void byte_destroy(CAT_VALUE_Value *self)
free(self);
}
static int byte_truthy(CAT_VALUE_Value *self, CAT_ERROR_Error *err)
{
(void)err;
return ((CAT_TYPES_NUMBER_Number *)self)->i != 0;
}
static CAT_VALUE_Vtable g_byte_vtable = {
.type_name = "byte",
.kind = CAT_VALUE_KIND_NUMBER,
@@ -30,7 +24,6 @@ static CAT_VALUE_Vtable g_byte_vtable = {
.raise = { NULL },
.get_attribute = { NULL },
.set_attribute = { NULL },
.truthy = byte_truthy,
.destroy = byte_destroy,
};
+54
View File
@@ -0,0 +1,54 @@
/*
* cat/types/empty.c -- the empty object (catscript has no nil/null).
*
* Naming rule: symbols here are cat_types_empty_* / CAT_TYPES_EMPTY_*.
*
* The empty object is the value for "no value". It is a shared
* singleton with no members: reading any attribute raises (there are no
* members), and it is truthy (only `false` is falsy). It is not
* settable, which keeps the singleton immutable.
*/
#include "cat/types/empty.h"
#include "cat/error.h"
static int empty_get_attribute(CAT_VALUE_Value *self, const char *name,
CAT_VALUE_Value **out, CAT_ERROR_Error *err)
{
(void)self;
(void)out;
return (cat_error_raise(err, "empty object has no attribute '%s'", name),
-1);
}
static int empty_set_attribute(CAT_VALUE_Value *self, const char *name,
CAT_VALUE_Value *value, CAT_ERROR_Error *err)
{
(void)self;
(void)value;
return (cat_error_raise(err,
"cannot set attribute '%s' on the empty object", name), -1);
}
static CAT_VALUE_Vtable g_empty_vtable = {
.type_name = "Object",
.kind = CAT_VALUE_KIND_TABLE,
.get_attribute = { empty_get_attribute },
.set_attribute = { empty_set_attribute },
};
CAT_VALUE_Vtable *cat_types_empty_vtable(void)
{
return &g_empty_vtable;
}
CAT_VALUE_Value *cat_types_empty(void)
{
static CAT_VALUE_Value g_empty;
g_empty.vtable = &g_empty_vtable;
return &g_empty;
}
int cat_types_empty_is(CAT_VALUE_Value *v)
{
return v != NULL && v->vtable == &g_empty_vtable;
}
+13 -13
View File
@@ -12,7 +12,7 @@
#include "cat/types/text.h"
#include "cat/types/ascii_text.h"
#include "cat/types/native.h"
#include "cat/types/nil.h"
#include "cat/types/empty.h"
#include "cat/types/int.h"
#include "cat/types/float.h"
#include "cat/types/bool.h"
@@ -215,7 +215,7 @@ static int function_call(CAT_VALUE_Value *self, CAT_VALUE_Value **args,
switch (f->ret_kind) {
case CAT_TYPES_FFI_RET_VOID:
*out = cat_types_nil_get();
*out = cat_types_empty();
break;
case CAT_TYPES_FFI_RET_DOUBLE:
*out = cat_types_float_new(ret.d);
@@ -274,9 +274,9 @@ static int library_call(CAT_VALUE_Value *self, CAT_VALUE_Value **args,
CAT_VALUE_Value *callargs[4];
callargs[0] = self;
callargs[1] = nargs >= 1 ? args[0] : cat_types_nil_get();
callargs[2] = nargs >= 2 ? args[1] : cat_types_nil_get();
callargs[3] = cat_types_nil_get();
callargs[1] = nargs >= 1 ? args[0] : cat_types_empty();
callargs[2] = nargs >= 2 ? args[1] : cat_types_empty();
callargs[3] = cat_types_empty();
return lib_method_func(NULL, callargs, 3, out);
}
@@ -311,14 +311,14 @@ CAT_VALUE_Value *cat_types_ffi_load(const char *path)
void *h;
if (path == NULL)
return cat_types_nil_get();
return cat_types_empty();
h = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
if (h == NULL)
return cat_types_nil_get();
return cat_types_empty();
l = calloc(1, sizeof(*l));
if (l == NULL) {
dlclose(h);
return cat_types_nil_get();
return cat_types_empty();
}
l->base.vtable = &g_library_vtable;
l->handle = h;
@@ -334,21 +334,21 @@ CAT_VALUE_Value *cat_types_ffi_bind(CAT_VALUE_Value *lib, const char *name,
void *sym;
if (lib == NULL || lib->vtable != &g_library_vtable || name == NULL)
return cat_types_nil_get();
return cat_types_empty();
l = (CAT_TYPES_FFI_Library *)lib;
sym = dlsym(l->handle, name);
if (sym == NULL)
return cat_types_nil_get();
return cat_types_empty();
f = calloc(1, sizeof(*f));
if (f == NULL)
return cat_types_nil_get();
return cat_types_empty();
f->base.vtable = &g_function_vtable;
f->sym = sym;
f->name = strdup(name);
f->sig = sig ? strdup(sig) : NULL;
if (prepare(f, sig) != 0) {
function_destroy(&f->base);
return cat_types_nil_get();
return cat_types_empty();
}
return &f->base;
}
@@ -373,7 +373,7 @@ static int ffi_load_native(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
}
}
if (path == NULL) {
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
*out = cat_types_ffi_load(cat_types_text_bytes(path));
+14 -14
View File
@@ -19,7 +19,7 @@
#include "cat/types/file.h"
#include "cat/types/native.h"
#include "cat/types/int.h"
#include "cat/types/nil.h"
#include "cat/types/empty.h"
#include "cat/types/ascii_text.h"
#include "cat/types/table.h"
#include "cat/error.h"
@@ -55,12 +55,12 @@ static int m_read(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;
}
af = AF(args[0]);
if (af->closed) {
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
want = (nargs > 1) ? as_int(args[1]) : -1;
@@ -71,7 +71,7 @@ static int m_read(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
long take = (want < 0 || want > avail) ? avail : (long)want;
char *buf = malloc((size_t)take + 1);
if (buf == NULL) {
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
memcpy(buf, m->buf + m->pos, (size_t)take);
@@ -86,7 +86,7 @@ static int m_read(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
char *buf = malloc(cap);
FILE *fp = (FILE *)f->fp;
if (buf == NULL) {
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
for (;;) {
@@ -167,7 +167,7 @@ static int m_close(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
}
af->closed = 1;
}
*out = cat_types_nil_get();
*out = cat_types_empty();
return 0;
}
@@ -279,12 +279,12 @@ CAT_VALUE_Value *cat_types_file_open(const char *path, const char *mode)
int m = 0;
if (path == NULL)
return cat_types_nil_get();
return cat_types_empty();
if (mode == NULL)
mode = "r";
fp = fopen(path, mode);
if (fp == NULL)
return cat_types_nil_get();
return cat_types_empty();
if (strchr(mode, 'w') != NULL)
m = CAT_TYPES_FILE_MODE_WRITE;
else if (strchr(mode, 'a') != NULL)
@@ -294,7 +294,7 @@ CAT_VALUE_Value *cat_types_file_open(const char *path, const char *mode)
f = calloc(1, sizeof(*f));
if (f == NULL) {
fclose(fp);
return cat_types_nil_get();
return cat_types_empty();
}
f->af.base.vtable = &g_file_vtable;
f->af.mode = m;
@@ -310,7 +310,7 @@ CAT_VALUE_Value *cat_types_file_stringio(const char *initial)
CAT_TYPES_FILE_MemFile *m = calloc(1, sizeof(*m));
if (m == NULL)
return cat_types_nil_get();
return cat_types_empty();
m->af.base.vtable = &g_memfile_vtable;
m->af.mode = CAT_TYPES_FILE_MODE_READ | CAT_TYPES_FILE_MODE_WRITE;
m->af.is_bytes = 1; /* in-memory */
@@ -318,7 +318,7 @@ CAT_VALUE_Value *cat_types_file_stringio(const char *initial)
m->buf = malloc((size_t)m->cap);
if (m->buf == NULL) {
free(m);
return cat_types_nil_get();
return cat_types_empty();
}
m->buf[0] = '\0';
if (initial != NULL) {
@@ -340,14 +340,14 @@ CAT_VALUE_Value *cat_types_file_bytesio(void)
CAT_TYPES_FILE_MemFile *m = calloc(1, sizeof(*m));
if (m == NULL)
return cat_types_nil_get();
return cat_types_empty();
m->af.base.vtable = &g_memfile_vtable;
m->af.mode = CAT_TYPES_FILE_MODE_READ | CAT_TYPES_FILE_MODE_WRITE;
m->cap = 128;
m->buf = malloc((size_t)m->cap);
if (m->buf == NULL) {
free(m);
return cat_types_nil_get();
return cat_types_empty();
}
m->buf[0] = '\0';
m->pos = 0;
@@ -364,7 +364,7 @@ static int cm_open(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;
}
path = as_text(args[1]);
-7
View File
@@ -12,12 +12,6 @@ static void float_destroy(CAT_VALUE_Value *self)
free(self);
}
static int float_truthy(CAT_VALUE_Value *self, CAT_ERROR_Error *err)
{
(void)err;
return ((CAT_TYPES_NUMBER_Number *)self)->f != 0.0;
}
static CAT_VALUE_Vtable g_float_vtable = {
.type_name = "float",
.kind = CAT_VALUE_KIND_NUMBER,
@@ -30,7 +24,6 @@ static CAT_VALUE_Vtable g_float_vtable = {
.raise = { NULL },
.get_attribute = { NULL },
.set_attribute = { NULL },
.truthy = float_truthy,
.destroy = float_destroy,
};
+7 -7
View File
@@ -5,7 +5,7 @@
*/
#include "cat/types/generator.h"
#include "cat/types/native.h"
#include "cat/types/nil.h"
#include "cat/types/empty.h"
#include "cat/types/function.h"
#include "cat/bytecode.h"
#include "cat/error.h"
@@ -56,7 +56,7 @@ CAT_VALUE_Value *cat_types_generator_new(CAT_VM_State *vm,
int i;
if (g == NULL)
return cat_types_nil_get();
return cat_types_empty();
g->base.vtable = &g_generator_vtable;
g->vm = vm;
g->cl = cl;
@@ -68,7 +68,7 @@ CAT_VALUE_Value *cat_types_generator_new(CAT_VM_State *vm,
g->stack = calloc((size_t)(g->frame.fn->ncode + 64), sizeof(*g->stack));
if (g->stack == NULL) {
free(g);
return cat_types_nil_get();
return cat_types_empty();
}
g->frame.stack = g->stack;
g->started = 0;
@@ -105,7 +105,7 @@ static int generator_next(CAT_VALUE_Value *self, CAT_VALUE_Value **out,
return -1;
}
if (rc == 1) { /* yielded */
*out = val ? val : cat_types_nil_get();
*out = val ? val : cat_types_empty();
return 0;
}
/* function returned: iteration ends */
@@ -113,7 +113,7 @@ static int generator_next(CAT_VALUE_Value *self, CAT_VALUE_Value **out,
return 1; /* exhausted */
}
/* `it:next()` -- returns the next value, or nil when exhausted. */
/* `it:next()` -- returns the next value, or empty when exhausted. */
static int gen_method_next(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
CAT_VALUE_Value **out)
{
@@ -123,11 +123,11 @@ static int gen_method_next(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;
}
rc = cat_value_next(args[0], out, &e);
if (rc != 0)
*out = cat_types_nil_get(); /* exhausted or error -> nil */
*out = cat_types_empty(); /* exhausted or error -> nil */
return 0;
}
-7
View File
@@ -12,12 +12,6 @@ static void int_destroy(CAT_VALUE_Value *self)
free(self);
}
static int int_truthy(CAT_VALUE_Value *self, CAT_ERROR_Error *err)
{
(void)err;
return ((CAT_TYPES_NUMBER_Number *)self)->i != 0;
}
static CAT_VALUE_Vtable g_int_vtable = {
.type_name = "int",
.kind = CAT_VALUE_KIND_NUMBER,
@@ -30,7 +24,6 @@ static CAT_VALUE_Vtable g_int_vtable = {
.raise = { NULL },
.get_attribute = { NULL },
.set_attribute = { NULL },
.truthy = int_truthy,
.destroy = int_destroy,
};
+1 -1
View File
@@ -6,7 +6,7 @@
#include "cat/types/list.h"
#include "cat/types/text.h"
#include "cat/types/int.h"
#include "cat/types/nil.h"
#include "cat/types/empty.h"
#include "cat/error.h"
#include <stdlib.h>
-31
View File
@@ -1,31 +0,0 @@
/*
* cat/types/nil.c -- the `nil` singleton value.
*
* Naming rule: symbols here are cat_types_nil_* / CAT_TYPES_NIL_*.
*/
#include "cat/types/nil.h"
static int nil_truthy(CAT_VALUE_Value *self, CAT_ERROR_Error *err)
{
(void)self;
(void)err;
return 0; /* nil is falsy */
}
static CAT_VALUE_Vtable g_nil_vtable = {
.type_name = "nil",
.kind = CAT_VALUE_KIND_NIL,
.truthy = nil_truthy,
};
CAT_VALUE_Vtable *cat_types_nil_vtable(void)
{
return &g_nil_vtable;
}
CAT_VALUE_Value *cat_types_nil_get(void)
{
static CAT_TYPES_NIL_Nil g_nil;
g_nil.base.vtable = &g_nil_vtable;
return &g_nil.base;
}
+3 -3
View File
@@ -12,7 +12,7 @@
#include "cat/types/selector.h"
#include "cat/types/text.h"
#include "cat/types/number.h"
#include "cat/types/nil.h"
#include "cat/types/empty.h"
#include "cat/types/table.h"
#include "cat/error.h"
@@ -51,7 +51,7 @@ static int selector_call(CAT_VALUE_Value *self, CAT_VALUE_Value **args,
case CAT_TYPES_SELECTOR_DOT_SET: /* _dot_set_(key, value) */
sel_key(nargs >= 2 ? args[1] : NULL, key, sizeof(key));
cat_value_set_attribute(rx, key, nargs >= 3 ? args[2] : NULL, err);
*out = nargs >= 3 ? args[2] : cat_types_nil_get();
*out = nargs >= 3 ? args[2] : cat_types_empty();
return 0;
case CAT_TYPES_SELECTOR_SQ: /* _squarebrackets_(idx) */
sel_key(nargs >= 2 ? args[1] : NULL, key, sizeof(key));
@@ -59,7 +59,7 @@ static int selector_call(CAT_VALUE_Value *self, CAT_VALUE_Value **args,
case CAT_TYPES_SELECTOR_SQ_SET: /* _squarebrackets_set_(idx, value) */
sel_key(nargs >= 2 ? args[1] : NULL, key, sizeof(key));
cat_value_set_attribute(rx, key, nargs >= 3 ? args[2] : NULL, err);
*out = nargs >= 3 ? args[2] : cat_types_nil_get();
*out = nargs >= 3 ? args[2] : cat_types_empty();
return 0;
case CAT_TYPES_SELECTOR_PAREN: /* _parenthesis_(args...) */
return cat_value_call(rx, nargs >= 1 ? &args[1] : args,
+1 -1
View File
@@ -84,7 +84,7 @@ static void table_destroy(CAT_VALUE_Value *self)
}
static CAT_VALUE_Vtable g_table_vtable = {
.type_name = "table",
.type_name = "Object",
.kind = CAT_VALUE_KIND_TABLE,
.call = { table_call },
.get_attribute = { table_get_attribute },
+2 -2
View File
@@ -10,7 +10,7 @@
*/
#include "cat/value.h"
#include "cat/error.h"
#include "cat/types/nil.h"
#include "cat/types/empty.h"
#include "cat/types/bool.h"
#include "cat/types/int.h"
#include "cat/types/float.h"
@@ -211,7 +211,7 @@ int cat_value_slot_get(CAT_VALUE_Value *v, const CAT_VALUE_Slot *s,
: cat_types_bool_false();
break;
default:
*out = cat_types_nil_get();
*out = cat_types_empty();
break;
}
if (*out == NULL)
+25 -19
View File
@@ -14,7 +14,7 @@
*/
#include "cat/vm.h"
#include "cat/bytecode.h"
#include "cat/types/nil.h"
#include "cat/types/empty.h"
#include "cat/types/bool.h"
#include "cat/types/int.h"
#include "cat/types/float.h"
@@ -101,7 +101,7 @@ static int call_value(CAT_VM_State *vm, CAT_VALUE_Value *fnv,
static void print_value(CAT_VALUE_Value *v)
{
if (v == NULL)
printf("nil\n");
printf("empty\n");
else if (v->vtable && v->vtable->kind == CAT_VALUE_KIND_TEXT)
printf("%s\n", cat_types_text_bytes(v));
else if (v->vtable && v->vtable->kind == CAT_VALUE_KIND_NUMBER) {
@@ -111,8 +111,8 @@ static void print_value(CAT_VALUE_Value *v)
printf("%lld\n", cat_types_number_as_int(v));
} else if (v->vtable && v->vtable->kind == CAT_VALUE_KIND_BOOL)
printf("%s\n", ((CAT_TYPES_BOOL_Bool *)v)->v ? "true" : "false");
else if (v == cat_types_nil_get())
printf("nil\n");
else if (v == cat_types_empty())
printf("empty\n");
else
printf("<%s>\n", cat_value_type_name(v));
}
@@ -139,7 +139,7 @@ int cat_vm_frame_run(CAT_VM_State *vm, CAT_VM_Frame *f, CAT_VALUE_Value **out)
g_current_vm = vm;
cat_error_clear(&e);
*out = cat_types_nil_get();
*out = cat_types_empty();
while (f->ip < fn->ncode) {
CAT_BYTECODE_Instr *in = &fn->code[f->ip];
@@ -150,8 +150,8 @@ int cat_vm_frame_run(CAT_VM_State *vm, CAT_VM_Frame *f, CAT_VALUE_Value **out)
case CAT_BYTECODE_OP_CONST:
stack[sp++] = fn->consts[in->a].value;
break;
case CAT_BYTECODE_OP_NIL:
stack[sp++] = cat_types_nil_get();
case CAT_BYTECODE_OP_EMPTY:
stack[sp++] = cat_types_empty();
break;
case CAT_BYTECODE_OP_TRUE:
stack[sp++] = cat_types_bool_true();
@@ -160,7 +160,7 @@ int cat_vm_frame_run(CAT_VM_State *vm, CAT_VM_Frame *f, CAT_VALUE_Value **out)
stack[sp++] = cat_types_bool_false();
break;
case CAT_BYTECODE_OP_GETLOCAL:
stack[sp++] = locals[in->a] ? locals[in->a] : cat_types_nil_get();
stack[sp++] = locals[in->a] ? locals[in->a] : cat_types_empty();
break;
case CAT_BYTECODE_OP_SETLOCAL:
locals[in->a] = stack[--sp];
@@ -169,7 +169,13 @@ int cat_vm_frame_run(CAT_VM_State *vm, CAT_VM_Frame *f, CAT_VALUE_Value **out)
CAT_VALUE_Value *g =
cat_vm_get_global(vm, cat_types_text_bytes(
fn->consts[in->a].value));
stack[sp++] = g ? g : cat_types_nil_get();
if (g == NULL) {
cat_error_raise(&e, "global '%s' is not defined",
cat_types_text_bytes(fn->consts[in->a].value));
deliver_error(vm, &e);
goto on_error;
}
stack[sp++] = g;
break;
}
case CAT_BYTECODE_OP_SETGLOBAL:
@@ -267,9 +273,9 @@ int cat_vm_frame_run(CAT_VM_State *vm, CAT_VM_Frame *f, CAT_VALUE_Value **out)
CAT_VALUE_Value *a = stack[--sp];
int cmp = 0, eq = 0, has_cmp = 0;
if (a == NULL)
a = cat_types_nil_get();
a = cat_types_empty();
if (b == NULL)
b = cat_types_nil_get();
b = cat_types_empty();
if (a == b) {
eq = 1; has_cmp = 1; cmp = 0;
} else if (a->vtable && b->vtable &&
@@ -335,7 +341,7 @@ int cat_vm_frame_run(CAT_VM_State *vm, CAT_VM_Frame *f, CAT_VALUE_Value **out)
st = call_value(vm, fnv, &stack[sp - n], n, &r, &e);
if (st != 0) { deliver_error(vm, &e); goto on_error; }
sp -= n + 1;
stack[sp++] = r ? r : cat_types_nil_get();
stack[sp++] = r ? r : cat_types_empty();
break;
}
case CAT_BYTECODE_OP_CALLMETHOD: {
@@ -357,7 +363,7 @@ int cat_vm_frame_run(CAT_VM_State *vm, CAT_VM_Frame *f, CAT_VALUE_Value **out)
int st = call_value(vm, method, callargs, n + 1, &r, &e);
if (st != 0) { deliver_error(vm, &e); goto on_error; }
sp -= n + 1;
stack[sp++] = r ? r : cat_types_nil_get();
stack[sp++] = r ? r : cat_types_empty();
}
break;
}
@@ -398,7 +404,7 @@ int cat_vm_frame_run(CAT_VM_State *vm, CAT_VM_Frame *f, CAT_VALUE_Value **out)
rc = 0;
goto done;
case CAT_BYTECODE_OP_RETURN0:
*out = cat_types_nil_get();
*out = cat_types_empty();
rc = 0;
goto done;
case CAT_BYTECODE_OP_YIELD: {
@@ -417,8 +423,8 @@ int cat_vm_frame_run(CAT_VM_State *vm, CAT_VM_Frame *f, CAT_VALUE_Value **out)
if (nr < 0) {
deliver_error(vm, &e); goto on_error;
}
/* NEXT yields nil when exhausted (method-style use). */
stack[sp++] = (nr == 0 && r) ? r : cat_types_nil_get();
/* NEXT yields the empty object when exhausted (method-style use). */
stack[sp++] = (nr == 0 && r) ? r : cat_types_empty();
break;
}
case CAT_BYTECODE_OP_FORNEXT: {
@@ -435,7 +441,7 @@ int cat_vm_frame_run(CAT_VM_State *vm, CAT_VM_Frame *f, CAT_VALUE_Value **out)
memcpy(f->locals, locals, sizeof(locals));
continue;
}
stack[sp++] = r ? r : cat_types_nil_get();
stack[sp++] = r ? r : cat_types_empty();
break;
}
case CAT_BYTECODE_OP_TRYSETUP:
@@ -491,7 +497,7 @@ on_error:
}
/* fell off the end */
*out = cat_types_nil_get();
*out = cat_types_empty();
rc = 0;
done:
@@ -535,7 +541,7 @@ int cat_vm_exec_closure(CAT_VM_State *vm, CAT_TYPES_FUNCTION_Closure *cl,
free(stack);
if (rc < 0)
return -1;
*out = val ? val : cat_types_nil_get();
*out = val ? val : cat_types_empty();
return 0;
}