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

+3 -3
View File
@@ -20,7 +20,7 @@
enum CAT_BYTECODE_Op {
CAT_BYTECODE_OP_NOP = 0,
CAT_BYTECODE_OP_CONST, /* A: index into consts[] */
CAT_BYTECODE_OP_NIL, /* push nil */
CAT_BYTECODE_OP_EMPTY, /* push the empty object */
CAT_BYTECODE_OP_TRUE, /* push true */
CAT_BYTECODE_OP_FALSE, /* push false */
CAT_BYTECODE_OP_GETLOCAL, /* A: local slot -> stack */
@@ -49,7 +49,7 @@ enum CAT_BYTECODE_Op {
CAT_BYTECODE_OP_JUMPIF, /* A: target; pop cond */
CAT_BYTECODE_OP_JUMPIFNOT, /* A: target; pop cond */
CAT_BYTECODE_OP_RETURN, /* pop -> return */
CAT_BYTECODE_OP_RETURN0, /* return nil */
CAT_BYTECODE_OP_RETURN0, /* return the empty object */
CAT_BYTECODE_OP_POP, /* discard top */
CAT_BYTECODE_OP_PRINT, /* pop, print */
CAT_BYTECODE_OP_CLOSURE, /* A: index into protos[] */
@@ -59,7 +59,7 @@ enum CAT_BYTECODE_Op {
CAT_BYTECODE_OP_GETIDX, /* pop idx, obj -> push obj[idx] */
CAT_BYTECODE_OP_SETIDX, /* pop val, idx, obj -> obj[idx]=val */
CAT_BYTECODE_OP_YIELD, /* suspend a generator, yielding top */
CAT_BYTECODE_OP_NEXT, /* pop it -> push next(it) or nil */
CAT_BYTECODE_OP_NEXT, /* pop it -> push next(it) or empty */
CAT_BYTECODE_OP_TRYSETUP, /* A: handler target addr */
CAT_BYTECODE_OP_TRYEND, /* pop the innermost handler */
CAT_BYTECODE_OP_RAISE, /* pop value -> raise it */
+24
View File
@@ -0,0 +1,24 @@
/*
* cat/types/empty.h -- the empty object (catscript has no nil/null).
*
* Naming rule: symbols here are cat_types_empty_* / CAT_TYPES_EMPTY_*.
*
* catscript has no `nil`/`null`. The value for "no value" is an empty
* object: an `Object` with no members. It is a shared, immutable
* singleton, so it is safe to compare by identity and needs no
* allocation. Setting a member on it raises.
*/
#ifndef CAT_TYPES_EMPTY_H
#define CAT_TYPES_EMPTY_H
#include "cat/value.h"
/* The canonical empty object (a singleton). */
CAT_VALUE_Value *cat_types_empty(void);
CAT_VALUE_Vtable *cat_types_empty_vtable(void);
/* True if v is the empty object singleton. */
int cat_types_empty_is(CAT_VALUE_Value *v);
#endif /* CAT_TYPES_EMPTY_H */
+2 -2
View File
@@ -51,10 +51,10 @@ enum {
CAT_TYPES_FFI_RET_STRING
};
/* Load a shared library by path; returns a Library value (nil on error). */
/* Load a shared library by path; returns a Library value (empty on error). */
CAT_VALUE_Value *cat_types_ffi_load(const char *path);
/* Bind a symbol with a signature; returns a Function value (nil on error). */
/* Bind a symbol with a signature; returns a Function value (empty on error). */
CAT_VALUE_Value *cat_types_ffi_bind(CAT_VALUE_Value *lib, const char *name,
const char *sig);
-20
View File
@@ -1,20 +0,0 @@
/*
* cat/types/nil.h -- the `nil` singleton value.
*
* Naming rule: symbols here are cat_types_nil_* / CAT_TYPES_NIL_*.
* `nil` is falsy and participates in no arithmetic.
*/
#ifndef CAT_TYPES_NIL_H
#define CAT_TYPES_NIL_H
#include "cat/value.h"
typedef struct CAT_TYPES_NIL_Nil {
CAT_VALUE_Value base;
} CAT_TYPES_NIL_Nil;
/* The one shared nil value (never freed by callers). */
CAT_VALUE_Value *cat_types_nil_get(void);
CAT_VALUE_Vtable *cat_types_nil_vtable(void);
#endif /* CAT_TYPES_NIL_H */
+4 -5
View File
@@ -83,7 +83,7 @@ typedef struct CAT_IMPL_SetAttribute {
CAT_ERROR_Error *err);
} CAT_IMPL_SetAttribute;
/* Cat_ImplementsNext -- advance an iterator/generator: -> *out (nil ends). */
/* Cat_ImplementsNext -- advance an iterator/generator: -> *out (status). */
typedef struct CAT_IMPL_Next {
int (*fn)(CAT_VALUE_Value *self, CAT_VALUE_Value **out,
CAT_ERROR_Error *err);
@@ -111,12 +111,11 @@ enum {
CAT_VALUE_SLOT_DOUBLE, /* double member */
CAT_VALUE_SLOT_TEXT, /* char * member */
CAT_VALUE_SLOT_BOOL, /* int/bool member */
CAT_VALUE_SLOT_NIL /* no storage; always reads nil */
CAT_VALUE_SLOT_EMPTY /* no storage; always reads the empty object */
};
/* Runtime kind tag: structural families the VM can test cheaply. */
enum {
CAT_VALUE_KIND_NIL,
CAT_VALUE_KIND_BOOL, /* boolean family */
CAT_VALUE_KIND_NUMBER, /* Cat_Number family */
CAT_VALUE_KIND_TEXT, /* Cat_Text family */
@@ -181,8 +180,8 @@ int cat_value_equal(CAT_VALUE_Value *a, CAT_VALUE_Value *b, int *out,
/* Advance an iterator/generator. Returns 0 and sets *out to the next
* value, 1 when the iterator is exhausted (*out untouched), or -1 on
* error. A separate exhaustion status (rather than a nil/sentinel
* value) lets generators yield nil, false or 0 without ending the loop. */
* error. A separate exhaustion status (rather than an empty/sentinel
* value) lets generators yield false or 0 without ending the loop. */
int cat_value_next(CAT_VALUE_Value *v, CAT_VALUE_Value **out,
CAT_ERROR_Error *err);