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

+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;