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:
1 parent
e0bf102f2a
commit
d29b0706c9
36 files changed
+257
-244
No files matched your search
@@ -31,7 +31,7 @@ SRCS := \
|
||||
src/cat/compiler.c \
|
||||
src/cat/vm.c \
|
||||
src/cat/module.c \
|
||||
src/cat/types/nil.c \
|
||||
src/cat/types/empty.c \
|
||||
src/cat/types/bool.c \
|
||||
src/cat/types/number.c \
|
||||
src/cat/types/int.c \
|
||||
|
||||
@@ -170,7 +170,7 @@ produz um **generator**. `next()` retoma a execução até o próximo
|
||||
`yield` (ou o `return`, que encerra a iteração). O mesmo protocolo
|
||||
(`next` na vtable) é o iterador geral, e `for x in it do ... end`
|
||||
consome qualquer iterador, terminando por **status de exaustão** — então
|
||||
geradores podem produzir `nil`, `false` ou `0` sem encerrar o loop:
|
||||
geradores podem produzir `empty`, `false` ou `0` sem encerrar o loop:
|
||||
|
||||
```lua
|
||||
function count_up(n)
|
||||
|
||||
+2
-2
@@ -11,7 +11,7 @@
|
||||
|
||||
function cpk_show(path)
|
||||
local text = read_file(path)
|
||||
if text == nil then
|
||||
if text == empty then
|
||||
print("cpk: cannot read " .. path)
|
||||
return 1
|
||||
end
|
||||
@@ -40,7 +40,7 @@ function cpk_main(argv)
|
||||
local cmd = argv[1]
|
||||
if cmd == "show" then
|
||||
local path = argv[2]
|
||||
if path == nil then
|
||||
if path == empty then
|
||||
path = "package.toml"
|
||||
end
|
||||
return cpk_show(path)
|
||||
|
||||
@@ -16,7 +16,7 @@ local g = count_up(3)
|
||||
print("next:", g:next())
|
||||
print("next:", g:next())
|
||||
print("next:", g:next())
|
||||
print("next:", g:next()) -- nil: exhausted
|
||||
print("next:", g:next()) -- empty: exhausted
|
||||
|
||||
-- for..in drives any iterator automatically
|
||||
local sum = 0
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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 */
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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
@@ -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);
|
||||
|
||||
|
||||
+1
-1
@@ -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
@@ -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
@@ -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",
|
||||
"+", "-", "*", "/", "%", "^", "#", "==", "~=", "<=", ">=", "<",
|
||||
|
||||
@@ -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
@@ -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;
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
|
||||
@@ -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
@@ -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
@@ -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]);
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -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
@@ -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
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -6,7 +6,7 @@
|
||||
-- Read a whole file, returning its contents ("" when missing).
|
||||
function fs_read(path)
|
||||
local f = File::open(path, "r")
|
||||
if f == nil then
|
||||
if f == empty then
|
||||
return ""
|
||||
end
|
||||
local data = f:read()
|
||||
@@ -17,7 +17,7 @@ end
|
||||
-- Write text to a file, replacing its contents.
|
||||
function fs_write(path, text)
|
||||
local f = File::open(path, "w")
|
||||
if f == nil then
|
||||
if f == empty then
|
||||
return false
|
||||
end
|
||||
f:write(text)
|
||||
@@ -28,7 +28,7 @@ end
|
||||
-- Append text to a file.
|
||||
function fs_append(path, text)
|
||||
local f = File::open(path, "a")
|
||||
if f == nil then
|
||||
if f == empty then
|
||||
return false
|
||||
end
|
||||
f:write(text)
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@ end
|
||||
-- Read a whole file as text ("" when missing).
|
||||
function read_text(path)
|
||||
local s = read_file(path)
|
||||
if s == nil then
|
||||
if s == empty then
|
||||
return ""
|
||||
end
|
||||
return s
|
||||
|
||||
@@ -28,7 +28,7 @@ function chk_diag(msg)
|
||||
end
|
||||
|
||||
function chk_expr(e)
|
||||
if e == nil then
|
||||
if e == empty then
|
||||
return
|
||||
end
|
||||
local t = e.t
|
||||
@@ -72,7 +72,7 @@ function chk_expr(e)
|
||||
end
|
||||
|
||||
function chk_block(stats)
|
||||
if stats == nil then
|
||||
if stats == empty then
|
||||
return
|
||||
end
|
||||
local i = 1
|
||||
|
||||
+12
-12
@@ -14,7 +14,7 @@
|
||||
--
|
||||
-- All state lives in globals prefixed CMP_ (closures capture no locals).
|
||||
|
||||
CMP_CHUNK = nil
|
||||
CMP_CHUNK = empty
|
||||
CMP_CONSTS = {}
|
||||
CMP_NC = 0
|
||||
CMP_PROTOS = {}
|
||||
@@ -80,7 +80,7 @@ function cmp_restore(s)
|
||||
end
|
||||
|
||||
function cmp_newchunk(name)
|
||||
CMP_CHUNK = nil
|
||||
CMP_CHUNK = empty
|
||||
CMP_CONSTS = {}
|
||||
CMP_NC = 0
|
||||
CMP_PROTOS = {}
|
||||
@@ -99,7 +99,7 @@ end
|
||||
|
||||
function cmp_expr(e)
|
||||
local t = e.t
|
||||
if t == "nil" then cmp_emit(2, 0, 0, 0)
|
||||
if t == "empty" then cmp_emit(2, 0, 0, 0)
|
||||
elseif t == "true" then cmp_emit(3, 0, 0, 0)
|
||||
elseif t == "false" then cmp_emit(4, 0, 0, 0)
|
||||
elseif t == "int" then cmp_emit(1, cmp_addconst(e.v), 0, 0)
|
||||
@@ -151,7 +151,7 @@ function cmp_expr(e)
|
||||
local i = 1
|
||||
while i <= len(e.fields) do
|
||||
local f = e.fields[i]
|
||||
if f.k ~= nil then
|
||||
if f.k ~= empty then
|
||||
cmp_emit(37, 0, 0, 0) -- DUP obj
|
||||
cmp_expr(f.v)
|
||||
cmp_emit(23, cmp_addstr(f.k), 0, 0)
|
||||
@@ -207,7 +207,7 @@ end
|
||||
-- ---- statements ------------------------------------------------------
|
||||
|
||||
function cmp_block(stats)
|
||||
if stats == nil then
|
||||
if stats == empty then
|
||||
return
|
||||
end
|
||||
local i = 1
|
||||
@@ -220,7 +220,7 @@ end
|
||||
function cmp_stat(s)
|
||||
local k = s.t
|
||||
if k == "local" then
|
||||
if s.init ~= nil then
|
||||
if s.init ~= empty then
|
||||
cmp_expr(s.init)
|
||||
else
|
||||
cmp_emit(2, 0, 0, 0)
|
||||
@@ -267,7 +267,7 @@ function cmp_stat(s)
|
||||
elseif k == "for" then
|
||||
cmp_for(s)
|
||||
elseif k == "return" then
|
||||
if s.e ~= nil then
|
||||
if s.e ~= empty then
|
||||
cmp_expr(s.e)
|
||||
cmp_emit(30, 0, 0, 0)
|
||||
else
|
||||
@@ -295,7 +295,7 @@ function cmp_if(s)
|
||||
cmp_block(s.conds[i].body)
|
||||
i = i + 1
|
||||
end
|
||||
if s.els ~= nil then
|
||||
if s.els ~= empty then
|
||||
nends = nends + 1
|
||||
ends[nends] = cmp_emit(27, 0, 0, 0)
|
||||
cmp_patch(jf, CMP_NI)
|
||||
@@ -317,7 +317,7 @@ function cmp_while(s)
|
||||
CMP_LOOPS[len(CMP_LOOPS) + 1] = { breaks = {} }
|
||||
cmp_block(s.body)
|
||||
local loop = CMP_LOOPS[len(CMP_LOOPS)]
|
||||
CMP_LOOPS[len(CMP_LOOPS)] = nil
|
||||
CMP_LOOPS[len(CMP_LOOPS)] = empty
|
||||
cmp_emit(27, jcond, 0, 0)
|
||||
cmp_patch(jend, CMP_NI)
|
||||
local i = 1
|
||||
@@ -332,7 +332,7 @@ function cmp_repeat(s)
|
||||
CMP_LOOPS[len(CMP_LOOPS) + 1] = { breaks = {} }
|
||||
cmp_block(s.body)
|
||||
local loop = CMP_LOOPS[len(CMP_LOOPS)]
|
||||
CMP_LOOPS[len(CMP_LOOPS)] = nil
|
||||
CMP_LOOPS[len(CMP_LOOPS)] = empty
|
||||
cmp_expr(s.cond)
|
||||
cmp_emit(29, jtop, 0, 0)
|
||||
local i = 1
|
||||
@@ -344,7 +344,7 @@ end
|
||||
|
||||
function cmp_for(s)
|
||||
cmp_expr(s.from)
|
||||
if s.step ~= nil then
|
||||
if s.step ~= empty then
|
||||
cmp_expr(s.step)
|
||||
else
|
||||
cmp_emit(1, cmp_addconst(1), 0, 0)
|
||||
@@ -365,7 +365,7 @@ function cmp_for(s)
|
||||
CMP_LOOPS[len(CMP_LOOPS) + 1] = { breaks = {} }
|
||||
cmp_block(s.body)
|
||||
local loop = CMP_LOOPS[len(CMP_LOOPS)]
|
||||
CMP_LOOPS[len(CMP_LOOPS)] = nil
|
||||
CMP_LOOPS[len(CMP_LOOPS)] = empty
|
||||
cmp_emit(5, slot, 0, 0)
|
||||
cmp_emit(5, slot + 2, 0, 0)
|
||||
cmp_emit(9, 0, 0, 0)
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ LEX_LINE = 1
|
||||
|
||||
LEX_KEYWORDS = {
|
||||
"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"
|
||||
}
|
||||
|
||||
|
||||
+19
-19
@@ -3,7 +3,7 @@
|
||||
-- Turns the token array from lexer.cat into an AST. AST nodes are
|
||||
-- ordinary tables with a `t` (tag) field:
|
||||
--
|
||||
-- { t="nil" } { t="true" } { t="false" }
|
||||
-- { t="empty" } { t="true" } { t="false" }
|
||||
-- { t="int", v=42 } { t="float", v=2.5 } { t="str", v="s" }
|
||||
-- { t="name", v="x" }
|
||||
-- { t="bin", op="+", l=L, r=R } { t="un", op="-", e=E }
|
||||
@@ -11,19 +11,19 @@
|
||||
-- { t="method", obj=O, name="m", args={...} }
|
||||
-- { t="index", obj=O, idx=I }
|
||||
-- { t="attr", obj=O, name="a" }
|
||||
-- { t="table", fields={ {k=..., v=...}, ... } } -- positional k is nil
|
||||
-- { t="table", fields={ {k=..., v=...}, ... } } -- positional k is empty
|
||||
-- { t="func", name=..., params={...}, body={stats} }
|
||||
--
|
||||
-- Statements:
|
||||
-- { t="local", name=..., init=E }
|
||||
-- { t="assign", name=..., value=E } { t="setidx", name, idx, value }
|
||||
-- { t="setattr", name, attr, value }
|
||||
-- { t="if", conds={ {cond=..., body={...}}, ... }, els={...}|nil }
|
||||
-- { t="if", conds={ {cond=..., body={...}}, ... }, els={...}|empty }
|
||||
-- { t="while", cond=E, body={...} }
|
||||
-- { t="repeat", body={...}, cond=E }
|
||||
-- { t="for", var=..., from=E, to=E, step=E|nil, body={...} }
|
||||
-- { t="for", var=..., from=E, to=E, step=E|empty, body={...} }
|
||||
-- { t="funcdef", name=..., func=F }
|
||||
-- { t="return", e=E|nil }
|
||||
-- { t="return", e=E|empty }
|
||||
-- { t="expr", e=E }
|
||||
--
|
||||
-- All parser state lives in globals prefixed PAR_ (closures do not
|
||||
@@ -62,7 +62,7 @@ end
|
||||
|
||||
function par_fail(msg)
|
||||
PAR_ERR = "line " .. tostring(par_cur().line) .. ": " .. msg
|
||||
return nil
|
||||
return empty
|
||||
end
|
||||
|
||||
function par_expect(kind, what)
|
||||
@@ -70,19 +70,19 @@ function par_expect(kind, what)
|
||||
return par_advance()
|
||||
end
|
||||
par_fail("expected " .. what .. ", got " .. par_cur().kind)
|
||||
return nil
|
||||
return empty
|
||||
end
|
||||
|
||||
-- forward decls (filled below; catscript resolves globals at call time)
|
||||
function par_block() return {} end
|
||||
function par_expr() return { t = "nil" } end
|
||||
function par_expr() return { t = "empty" } end
|
||||
|
||||
-- ---- expressions -----------------------------------------------------
|
||||
|
||||
function par_primary()
|
||||
local t = par_cur()
|
||||
local k = t.kind
|
||||
if k == "nil" then par_advance() return { t = "nil" } end
|
||||
if k == "empty" then par_advance() return { t = "empty" } end
|
||||
if k == "true" then par_advance() return { t = "true" } end
|
||||
if k == "false" then par_advance() return { t = "false" } end
|
||||
if k == "INT" then par_advance() return { t = "int", v = tonumber(t.text) } end
|
||||
@@ -103,7 +103,7 @@ function par_primary()
|
||||
return par_table()
|
||||
end
|
||||
par_fail("unexpected token " .. k)
|
||||
return { t = "nil" }
|
||||
return { t = "empty" }
|
||||
end
|
||||
|
||||
function par_table()
|
||||
@@ -111,8 +111,8 @@ function par_table()
|
||||
local fields = {}
|
||||
local n = 0
|
||||
while not par_at("}") and not par_at("EOF") do
|
||||
local fkey = nil
|
||||
if par_at("NAME") and PAR_TOKS[PAR_I + 1] ~= nil and PAR_TOKS[PAR_I + 1].kind == "=" then
|
||||
local fkey = empty
|
||||
if par_at("NAME") and PAR_TOKS[PAR_I + 1] ~= empty and PAR_TOKS[PAR_I + 1].kind == "=" then
|
||||
fkey = par_advance().text
|
||||
par_advance() -- =
|
||||
end
|
||||
@@ -249,7 +249,7 @@ end
|
||||
function par_stat()
|
||||
local t = par_cur()
|
||||
local k = t.kind
|
||||
if k == ";" then par_advance() return nil end
|
||||
if k == ";" then par_advance() return empty end
|
||||
|
||||
if k == "local" then
|
||||
par_advance()
|
||||
@@ -264,7 +264,7 @@ function par_stat()
|
||||
par_advance()
|
||||
par_advance()
|
||||
end
|
||||
local init = nil
|
||||
local init = empty
|
||||
if par_at("=") then
|
||||
par_advance()
|
||||
init = par_expr()
|
||||
@@ -294,7 +294,7 @@ function par_stat()
|
||||
nc = nc + 1
|
||||
conds[nc] = { cond = c2, body = par_block() }
|
||||
end
|
||||
local els = nil
|
||||
local els = empty
|
||||
if par_at("else") then
|
||||
par_advance()
|
||||
els = par_block()
|
||||
@@ -331,7 +331,7 @@ function par_stat()
|
||||
local from = par_expr()
|
||||
par_expect(",", "','")
|
||||
local to = par_expr()
|
||||
local step = nil
|
||||
local step = empty
|
||||
if par_at(",") then
|
||||
par_advance()
|
||||
step = par_expr()
|
||||
@@ -344,7 +344,7 @@ function par_stat()
|
||||
|
||||
if k == "return" then
|
||||
par_advance()
|
||||
local e = nil
|
||||
local e = empty
|
||||
if not (par_at("end") or par_at("EOF") or par_at(";") or par_at("else")
|
||||
or par_at("elseif") or par_at("until")) then
|
||||
e = par_expr()
|
||||
@@ -356,7 +356,7 @@ function par_stat()
|
||||
-- assignment or expression statement
|
||||
if k == "NAME" then
|
||||
local nxt = PAR_TOKS[PAR_I + 1]
|
||||
if nxt ~= nil and (nxt.kind == "=" or nxt.kind == "[" or nxt.kind == ".") then
|
||||
if nxt ~= empty and (nxt.kind == "=" or nxt.kind == "[" or nxt.kind == ".") then
|
||||
-- could still be a call/index expression; disambiguate
|
||||
local nm = par_advance().text
|
||||
if par_at("=") then
|
||||
@@ -403,7 +403,7 @@ function par_block()
|
||||
or par_at("elseif") or par_at("until")) do
|
||||
local before = PAR_I
|
||||
local s = par_stat()
|
||||
if s ~= nil then
|
||||
if s ~= empty then
|
||||
n = n + 1
|
||||
stats[n] = s
|
||||
end
|
||||
|
||||
+11
-11
@@ -1,24 +1,24 @@
|
||||
-- tests/ffi.cat -- edge cases for the `ffi` library.
|
||||
--
|
||||
-- Deterministic output (no getpid): loads, symbol binding, signatures,
|
||||
-- return conversions, and the nil-on-error contract.
|
||||
-- return conversions, and the empty-on-error contract.
|
||||
|
||||
local m = ffi::load("libm.so.6")
|
||||
local c = ffi::load("libc.so.6")
|
||||
|
||||
-- load failure / bad library -> nil
|
||||
-- load failure / bad library -> empty
|
||||
local bad = ffi::load("no_such_library_xyz.so.999")
|
||||
print("bad load nil:", bad == nil)
|
||||
print("bad load empty:", bad == empty)
|
||||
print("bad load type:", type(bad))
|
||||
|
||||
-- symbol binding
|
||||
local pow = m:func("pow", "double(double,double)")
|
||||
print("pow bound:", pow ~= nil)
|
||||
print("missing sym nil:", m:func("no_such_symbol_xyz", "int()") == nil)
|
||||
print("pow bound:", pow ~= empty)
|
||||
print("missing sym empty:", m:func("no_such_symbol_xyz", "int()") == empty)
|
||||
|
||||
-- invalid signature -> nil
|
||||
print("bad sig nil:", m:func("pow", "not a signature") == nil)
|
||||
print("bad rettype nil:", m:func("pow", "nope(double)") == nil)
|
||||
-- invalid signature -> empty
|
||||
print("bad sig empty:", m:func("pow", "not a signature") == empty)
|
||||
print("bad rettype empty:", m:func("pow", "nope(double)") == empty)
|
||||
|
||||
-- return conversions
|
||||
print("pow 2,10:", pow(2, 10))
|
||||
@@ -37,13 +37,13 @@ local strlen = c:func("strlen", "long(string)")
|
||||
print("strlen abc:", strlen("abc"))
|
||||
print("strlen empty:", strlen(""))
|
||||
|
||||
-- void return -> nil
|
||||
-- void return -> empty
|
||||
local srand = c:func("srand", "void(uint32)")
|
||||
print("void ret nil:", srand(1) == nil)
|
||||
print("void ret empty:", srand(1) == empty)
|
||||
|
||||
-- pointer-ish spelling accepted
|
||||
local strchr = c:func("strchr", "string(string,int)")
|
||||
print("strchr bound:", strchr ~= nil)
|
||||
print("strchr bound:", strchr ~= empty)
|
||||
|
||||
-- library is callable like :func
|
||||
local cosf = m("cos", "double(double)")
|
||||
|
||||
+7
-7
@@ -69,7 +69,7 @@ check "file-hints" "n=3 buf=abc" "`echo "$out" | sed -n 5p`"
|
||||
out=`$BIN examples/generators.cat`
|
||||
check "gen-type" "type: generator" "`echo "$out" | sed -n 1p`"
|
||||
check "gen-next-1" "next: 1" "`echo "$out" | sed -n 2p`"
|
||||
check "gen-next-nil" "next: nil" "`echo "$out" | sed -n 5p`"
|
||||
check "gen-next-nil" "next: empty" "`echo "$out" | sed -n 5p`"
|
||||
check "gen-forin-sum" "sum 1..5 = 15" "`echo "$out" | sed -n 6p`"
|
||||
check "gen-break-lazy" "taken < 3: 3" "`echo "$out" | sed -n 7p`"
|
||||
|
||||
@@ -119,12 +119,12 @@ check "ffi-abs" "abs(-42) = 42" "`echo "$out" | sed -n 5p`"
|
||||
# ---- ffi edge cases (deterministic) ---------------------------------
|
||||
|
||||
out=`$BIN tests/ffi.cat`
|
||||
check "ffi-badload-nil" "bad load nil: true" "`echo "$out" | sed -n 1p`"
|
||||
check "ffi-badload-type" "bad load type: nil" "`echo "$out" | sed -n 2p`"
|
||||
check "ffi-badload-empty" "bad load empty: true" "`echo "$out" | sed -n 1p`"
|
||||
check "ffi-badload-type" "bad load type: Object" "`echo "$out" | sed -n 2p`"
|
||||
check "ffi-bound" "pow bound: true" "`echo "$out" | sed -n 3p`"
|
||||
check "ffi-missing-sym" "missing sym nil: true" "`echo "$out" | sed -n 4p`"
|
||||
check "ffi-badsig" "bad sig nil: true" "`echo "$out" | sed -n 5p`"
|
||||
check "ffi-badret" "bad rettype nil: true" "`echo "$out" | sed -n 6p`"
|
||||
check "ffi-missing-sym" "missing sym empty: true" "`echo "$out" | sed -n 4p`"
|
||||
check "ffi-badsig" "bad sig empty: true" "`echo "$out" | sed -n 5p`"
|
||||
check "ffi-badret" "bad rettype empty: true" "`echo "$out" | sed -n 6p`"
|
||||
check "ffi-pow-2-10" "pow 2,10: 1024" "`echo "$out" | sed -n 7p`"
|
||||
check "ffi-pow-3-4" "pow 3,4: 81" "`echo "$out" | sed -n 8p`"
|
||||
check "ffi-floor" "floor 3.7: 3" "`echo "$out" | sed -n 9p`"
|
||||
@@ -132,7 +132,7 @@ check "ffi-sqrt-144" "sqrt 144: 12" "`echo "$out" | sed -n 10p
|
||||
check "ffi-abs-neg" "abs -42: 42" "`echo "$out" | sed -n 11p`"
|
||||
check "ffi-strlen-abc" "strlen abc: 3" "`echo "$out" | sed -n 12p`"
|
||||
check "ffi-strlen-empty" "strlen empty: 0" "`echo "$out" | sed -n 13p`"
|
||||
check "ffi-void-nil" "void ret nil: true" "`echo "$out" | sed -n 14p`"
|
||||
check "ffi-void-empty" "void ret empty: true" "`echo "$out" | sed -n 14p`"
|
||||
check "ffi-strchr-bound" "strchr bound: true" "`echo "$out" | sed -n 15p`"
|
||||
check "ffi-lib-callable" "cos 0: 1" "`echo "$out" | sed -n 16p`"
|
||||
|
||||
|
||||
Reference in new issue
Block a user