catscript: núcleo C, stdlib (TOML), família de arquivos e front-end self-hosted
- Modelo de objetos Cat_Value + vtable (interfaces abstratas) - VM de bytecode, sem GIL (VM por-thread, nativas como valores) - Stdlib com TOML embutido (toml_parse/toml_encode) - Família de arquivos: AbstractFile (abstrata), File::open, StringIO, BytesIO - Front-end (lexer/parser/checker/compiler) implementado em catscript - cpk (cat package manager) como primeiro programa; manifest package.toml - Type hints advisory, inclusive DynSized() e sentinel_sized<...> - 25/25 testes passando (make test)
This commit is contained in:
commit
52d118211f
69 files changed
+6938
No files matched your search
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* cat/stdlib/build.c -- bytecode builder primitives (see build.h).
|
||||
*
|
||||
* Naming rule: symbols here are cat_stdlib_build_* / CAT_STDLIB_BUILD_*.
|
||||
*
|
||||
* bc_run(chunk) -- build a CAT_BYTECODE_Function from a chunk table and
|
||||
* execute it, returning the chunk's return value.
|
||||
* bc_dump(chunk) -- print a disassembly of a chunk table.
|
||||
*/
|
||||
#include "cat/stdlib/build.h"
|
||||
#include "cat/bytecode.h"
|
||||
#include "cat/types/table.h"
|
||||
#include "cat/types/int.h"
|
||||
#include "cat/types/nil.h"
|
||||
#include "cat/types/ascii_text.h"
|
||||
#include "cat/error.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/* Read an integer field from a table (0 if absent). */
|
||||
static long long tbl_int(CAT_VALUE_Value *t, const char *key)
|
||||
{
|
||||
CAT_VALUE_Value *v = NULL;
|
||||
CAT_ERROR_Error e;
|
||||
|
||||
cat_error_clear(&e);
|
||||
if (t == NULL || cat_types_table_get(t, key, &v, &e) != 0 || v == NULL)
|
||||
return 0;
|
||||
return cat_types_number_as_int(v);
|
||||
}
|
||||
|
||||
static const char *tbl_str(CAT_VALUE_Value *t, const char *key)
|
||||
{
|
||||
CAT_VALUE_Value *v = NULL;
|
||||
CAT_ERROR_Error e;
|
||||
|
||||
cat_error_clear(&e);
|
||||
if (t == NULL || cat_types_table_get(t, key, &v, &e) != 0 || v == NULL)
|
||||
return NULL;
|
||||
return cat_types_text_bytes(v);
|
||||
}
|
||||
|
||||
/* Recursively convert a chunk table into a CAT_BYTECODE_Function. */
|
||||
static CAT_BYTECODE_Function *build_function(CAT_VALUE_Value *chunk)
|
||||
{
|
||||
CAT_BYTECODE_Function *fn = cat_bytecode_function_new(tbl_str(chunk, "name"));
|
||||
CAT_VALUE_Value *arr = NULL;
|
||||
CAT_ERROR_Error e;
|
||||
int i, n;
|
||||
|
||||
fn->nparams = (int)tbl_int(chunk, "nparams");
|
||||
fn->nlocals = (int)tbl_int(chunk, "nlocals");
|
||||
|
||||
/* constants */
|
||||
cat_error_clear(&e);
|
||||
if (cat_types_table_get(chunk, "consts", &arr, &e) == 0) {
|
||||
n = cat_types_table_count(arr);
|
||||
for (i = 0; i < n; i++)
|
||||
cat_bytecode_add_const(fn, cat_types_table_value_at(arr, i));
|
||||
}
|
||||
|
||||
/* nested prototypes */
|
||||
cat_error_clear(&e);
|
||||
if (cat_types_table_get(chunk, "protos", &arr, &e) == 0) {
|
||||
n = cat_types_table_count(arr);
|
||||
for (i = 0; i < n; i++)
|
||||
cat_bytecode_add_proto(fn,
|
||||
build_function(cat_types_table_value_at(arr, i)));
|
||||
}
|
||||
|
||||
/* instructions */
|
||||
cat_error_clear(&e);
|
||||
if (cat_types_table_get(chunk, "code", &arr, &e) == 0) {
|
||||
n = cat_types_table_count(arr);
|
||||
for (i = 0; i < n; i++) {
|
||||
CAT_VALUE_Value *in = cat_types_table_value_at(arr, i);
|
||||
cat_bytecode_emit(fn, (unsigned char)tbl_int(in, "op"),
|
||||
(int)tbl_int(in, "a"),
|
||||
(int)tbl_int(in, "b"),
|
||||
(int)tbl_int(in, "c"),
|
||||
(int)tbl_int(in, "line"));
|
||||
}
|
||||
}
|
||||
return fn;
|
||||
}
|
||||
|
||||
static int lf_bc_run(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
CAT_BYTECODE_Function *fn;
|
||||
CAT_ERROR_Error e;
|
||||
int rc;
|
||||
|
||||
(void)vm;
|
||||
cat_error_clear(&e);
|
||||
if (nargs < 1 || !cat_types_table_count(args[0])) { /* not a chunk */
|
||||
}
|
||||
fn = build_function(args[0]);
|
||||
if (fn == NULL) {
|
||||
*out = cat_types_nil_get();
|
||||
return 0;
|
||||
}
|
||||
rc = cat_vm_run(vm, fn, out);
|
||||
cat_bytecode_function_free(fn);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int lf_bc_dump(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
CAT_BYTECODE_Function *fn;
|
||||
|
||||
(void)vm;
|
||||
if (nargs < 1) {
|
||||
*out = cat_types_nil_get();
|
||||
return 0;
|
||||
}
|
||||
fn = build_function(args[0]);
|
||||
if (fn != NULL) {
|
||||
cat_bytecode_dump(fn, stdout);
|
||||
cat_bytecode_function_free(fn);
|
||||
}
|
||||
*out = cat_types_nil_get();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cat_stdlib_build_install(CAT_VM_State *vm)
|
||||
{
|
||||
cat_vm_register_native(vm, "bc_run", lf_bc_run);
|
||||
cat_vm_register_native(vm, "bc_dump", lf_bc_dump);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,387 @@
|
||||
/*
|
||||
* cat/stdlib/stdlib.c -- standard library native functions.
|
||||
*
|
||||
* Naming rule: symbols here are cat_stdlib_stdlib_*.
|
||||
*
|
||||
* Native signature: int cb(vm, args, nargs, out). Installed names:
|
||||
* print, tostring, tonumber, type, len, toml_parse, toml_encode.
|
||||
*/
|
||||
#include "cat/stdlib/stdlib.h"
|
||||
#include "cat/stdlib/toml.h"
|
||||
#include "cat/compiler.h"
|
||||
#include "cat/bytecode.h"
|
||||
#include "cat/types/nil.h"
|
||||
#include "cat/types/bool.h"
|
||||
#include "cat/types/number.h"
|
||||
#include "cat/types/int.h"
|
||||
#include "cat/types/float.h"
|
||||
#include "cat/types/text.h"
|
||||
#include "cat/types/ascii_text.h"
|
||||
#include "cat/types/table.h"
|
||||
#include "cat/types/native.h"
|
||||
#include "cat/types/file.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* toString for any value (heap string, caller does not free). */
|
||||
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->vtable && v->vtable->kind == CAT_VALUE_KIND_TEXT)
|
||||
return cat_types_text_bytes(v);
|
||||
if (v->vtable && v->vtable->kind == CAT_VALUE_KIND_NUMBER) {
|
||||
if (((CAT_TYPES_NUMBER_Number *)v)->is_float)
|
||||
snprintf(buf, sizeof(buf), "%g", cat_types_number_as_double(v));
|
||||
else
|
||||
snprintf(buf, sizeof(buf), "%lld", cat_types_number_as_int(v));
|
||||
return buf;
|
||||
}
|
||||
if (v->vtable && v->vtable->kind == CAT_VALUE_KIND_BOOL)
|
||||
return ((CAT_TYPES_BOOL_Bool *)v)->v ? "true" : "false";
|
||||
snprintf(buf, sizeof(buf), "<%s>", cat_value_type_name(v));
|
||||
return buf;
|
||||
}
|
||||
|
||||
static int lf_print(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
int i;
|
||||
|
||||
(void)vm;
|
||||
for (i = 0; i < nargs; i++) {
|
||||
if (i)
|
||||
putchar(' ');
|
||||
fputs(value_to_text(args[i]), stdout);
|
||||
}
|
||||
putchar('\n');
|
||||
*out = cat_types_nil_get();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lf_tostring(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
(void)vm;
|
||||
*out = cat_types_ascii_text_new(nargs > 0 ? value_to_text(args[0]) : "");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lf_tonumber(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
const char *s;
|
||||
char *end;
|
||||
|
||||
(void)vm;
|
||||
if (nargs < 1) {
|
||||
*out = cat_types_nil_get();
|
||||
return 0;
|
||||
}
|
||||
if (args[0]->vtable && args[0]->vtable->kind == CAT_VALUE_KIND_NUMBER) {
|
||||
*out = args[0];
|
||||
return 0;
|
||||
}
|
||||
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();
|
||||
} else {
|
||||
long long n = strtoll(s, &end, 10);
|
||||
*out = (*end == '\0') ? cat_types_int_new(n) : cat_types_nil_get();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lf_type(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
(void)vm;
|
||||
*out = cat_types_ascii_text_new(nargs > 0 ? cat_value_type_name(args[0])
|
||||
: "nil");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lf_len(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
(void)vm;
|
||||
if (nargs < 1) {
|
||||
*out = cat_types_int_new(0);
|
||||
return 0;
|
||||
}
|
||||
if (args[0]->vtable && args[0]->vtable->kind == CAT_VALUE_KIND_TEXT)
|
||||
*out = cat_types_int_new(cat_types_text_length(args[0]));
|
||||
else if (args[0]->vtable && args[0]->vtable->kind == CAT_VALUE_KIND_TABLE)
|
||||
*out = cat_types_int_new(cat_types_table_count(args[0]));
|
||||
else
|
||||
*out = cat_types_int_new(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lf_toml_parse(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
CAT_VALUE_Value *tbl = NULL;
|
||||
CAT_ERROR_Error e;
|
||||
|
||||
(void)vm;
|
||||
cat_error_clear(&e);
|
||||
if (nargs < 1) {
|
||||
*out = cat_types_nil_get();
|
||||
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();
|
||||
return 0;
|
||||
}
|
||||
*out = tbl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lf_toml_encode(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
CAT_ERROR_Error e;
|
||||
char *s;
|
||||
|
||||
(void)vm;
|
||||
cat_error_clear(&e);
|
||||
if (nargs < 1) {
|
||||
*out = cat_types_ascii_text_new("");
|
||||
return 0;
|
||||
}
|
||||
s = cat_stdlib_toml_encode(args[0], &e);
|
||||
*out = cat_types_ascii_text_new(s ? s : "");
|
||||
free(s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lf_read_file(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
FILE *f;
|
||||
long sz;
|
||||
char *buf = NULL;
|
||||
char *path;
|
||||
|
||||
(void)vm;
|
||||
if (nargs < 1) {
|
||||
*out = cat_types_nil_get();
|
||||
return 0;
|
||||
}
|
||||
path = (char *)cat_types_text_bytes(args[0]);
|
||||
f = fopen(path, "rb");
|
||||
if (f == NULL) {
|
||||
*out = cat_types_nil_get();
|
||||
return 0;
|
||||
}
|
||||
fseek(f, 0, SEEK_END);
|
||||
sz = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
buf = malloc((size_t)sz + 1);
|
||||
if (buf == NULL) {
|
||||
fclose(f);
|
||||
*out = cat_types_nil_get();
|
||||
return 0;
|
||||
}
|
||||
if (fread(buf, 1, (size_t)sz, f) != (size_t)sz) { /* short read ok */ }
|
||||
buf[sz] = '\0';
|
||||
fclose(f);
|
||||
*out = cat_types_ascii_text_new(buf);
|
||||
free(buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lf_write_file(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
FILE *f;
|
||||
|
||||
(void)vm;
|
||||
if (nargs < 2) {
|
||||
*out = cat_types_nil_get();
|
||||
return 0;
|
||||
}
|
||||
f = fopen(cat_types_text_bytes(args[0]), "wb");
|
||||
if (f != NULL) {
|
||||
const char *s = cat_types_text_bytes(args[1]);
|
||||
fwrite(s, 1, strlen(s), f);
|
||||
fclose(f);
|
||||
}
|
||||
*out = cat_types_nil_get();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lf_ord(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
const char *s;
|
||||
long long i;
|
||||
|
||||
(void)vm;
|
||||
if (nargs < 2) {
|
||||
*out = cat_types_int_new(-1);
|
||||
return 0;
|
||||
}
|
||||
s = cat_types_text_bytes(args[0]);
|
||||
i = cat_types_number_as_int(args[1]);
|
||||
if (i < 0 || i >= (long long)strlen(s))
|
||||
*out = cat_types_int_new(-1);
|
||||
else
|
||||
*out = cat_types_int_new((unsigned char)s[i]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lf_chr(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
char buf[2];
|
||||
|
||||
(void)vm;
|
||||
buf[0] = (char)(nargs > 0 ? cat_types_number_as_int(args[0]) : 0);
|
||||
buf[1] = '\0';
|
||||
*out = cat_types_ascii_text_new(buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lf_substr(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
const char *s;
|
||||
long long i, n;
|
||||
char *buf;
|
||||
|
||||
(void)vm;
|
||||
if (nargs < 3) {
|
||||
*out = cat_types_ascii_text_new("");
|
||||
return 0;
|
||||
}
|
||||
s = cat_types_text_bytes(args[0]);
|
||||
i = cat_types_number_as_int(args[1]);
|
||||
n = cat_types_number_as_int(args[2]);
|
||||
if (i < 0 || i > (long long)strlen(s) || n < 0) {
|
||||
*out = cat_types_ascii_text_new("");
|
||||
return 0;
|
||||
}
|
||||
buf = malloc((size_t)n + 1);
|
||||
if (buf == NULL) {
|
||||
*out = cat_types_ascii_text_new("");
|
||||
return 0;
|
||||
}
|
||||
memcpy(buf, s + i, (size_t)n);
|
||||
buf[n] = '\0';
|
||||
*out = cat_types_ascii_text_new(buf);
|
||||
free(buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lf_dofile(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
FILE *f;
|
||||
long sz;
|
||||
char *src;
|
||||
CAT_BYTECODE_Function *fn;
|
||||
CAT_ERROR_Error e;
|
||||
|
||||
if (nargs < 1) {
|
||||
*out = cat_types_nil_get();
|
||||
return 0;
|
||||
}
|
||||
f = fopen(cat_types_text_bytes(args[0]), "rb");
|
||||
if (f == NULL) {
|
||||
*out = cat_types_nil_get();
|
||||
return 0;
|
||||
}
|
||||
fseek(f, 0, SEEK_END);
|
||||
sz = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
src = malloc((size_t)sz + 1);
|
||||
if (src == NULL) {
|
||||
fclose(f);
|
||||
*out = cat_types_nil_get();
|
||||
return 0;
|
||||
}
|
||||
if (fread(src, 1, (size_t)sz, f) != (size_t)sz) { /* short read ok */ }
|
||||
src[sz] = '\0';
|
||||
fclose(f);
|
||||
cat_error_clear(&e);
|
||||
fn = cat_compiler_compile(src, &e);
|
||||
free(src);
|
||||
if (fn == NULL) {
|
||||
fprintf(stderr, "dofile: %s\n", e.message);
|
||||
*out = cat_types_nil_get();
|
||||
return 0;
|
||||
}
|
||||
cat_error_clear(&e);
|
||||
cat_vm_run(vm, fn, out);
|
||||
/* The compiled chunk is intentionally NOT freed: any `function`
|
||||
* definitions in it became globals whose closures point into this
|
||||
* bytecode tree. The chunk lives as long as the VM does. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lf_abstractfile(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
/* AbstractFile cannot be instantiated: it is the abstract base of the
|
||||
* stream family (File/StringIO/BytesIO). */
|
||||
(void)vm;
|
||||
(void)args;
|
||||
(void)nargs;
|
||||
fprintf(stderr, "AbstractFile is abstract; use File::open, "
|
||||
"StringIO or BytesIO\n");
|
||||
*out = cat_types_nil_get();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lf_stringio(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
(void)vm;
|
||||
*out = cat_types_file_stringio(nargs > 0 ?
|
||||
cat_types_text_bytes(args[0]) : "");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lf_bytesio(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
(void)vm;
|
||||
(void)args;
|
||||
(void)nargs;
|
||||
*out = cat_types_file_bytesio();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cat_stdlib_stdlib_install(CAT_VM_State *vm)
|
||||
{
|
||||
cat_vm_register_native(vm, "print", lf_print);
|
||||
cat_vm_register_native(vm, "tostring", lf_tostring);
|
||||
cat_vm_register_native(vm, "tonumber", lf_tonumber);
|
||||
cat_vm_register_native(vm, "type", lf_type);
|
||||
cat_vm_register_native(vm, "len", lf_len);
|
||||
cat_vm_register_native(vm, "toml_parse", lf_toml_parse);
|
||||
cat_vm_register_native(vm, "toml_encode", lf_toml_encode);
|
||||
cat_vm_register_native(vm, "read_file", lf_read_file);
|
||||
cat_vm_register_native(vm, "write_file", lf_write_file);
|
||||
cat_vm_register_native(vm, "ord", lf_ord);
|
||||
cat_vm_register_native(vm, "chr", lf_chr);
|
||||
cat_vm_register_native(vm, "substr", lf_substr);
|
||||
cat_vm_register_native(vm, "dofile", lf_dofile);
|
||||
/* the file family: AbstractFile (abstract base), File, StringIO, BytesIO */
|
||||
cat_vm_set_global(vm, "AbstractFile",
|
||||
cat_types_native_new("AbstractFile", lf_abstractfile));
|
||||
cat_vm_set_global(vm, "File", cat_types_file_class_table());
|
||||
cat_vm_set_global(vm, "StringIO",
|
||||
cat_types_native_new("StringIO", lf_stringio));
|
||||
cat_vm_set_global(vm, "BytesIO",
|
||||
cat_types_native_new("BytesIO", lf_bytesio));
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,408 @@
|
||||
/*
|
||||
* cat/stdlib/toml.c -- TOML subset parser/encoder.
|
||||
*
|
||||
* Naming rule: symbols here are cat_stdlib_toml_* / CAT_STDLIB_TOML_*.
|
||||
*
|
||||
* Supported: bare/quoted keys, [table], [[array of tables]], strings,
|
||||
* integers, floats, booleans, inline arrays, dotted keys. Returns a
|
||||
* table value so TOML documents are ordinary catscript objects.
|
||||
*/
|
||||
#include "cat/stdlib/toml.h"
|
||||
#include "cat/types/table.h"
|
||||
#include "cat/types/number.h"
|
||||
#include "cat/types/text.h"
|
||||
#include "cat/types/int.h"
|
||||
#include "cat/types/float.h"
|
||||
#include "cat/types/ascii_text.h"
|
||||
#include "cat/types/bool.h"
|
||||
#include "cat/types/nil.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct {
|
||||
const char *s;
|
||||
size_t pos;
|
||||
int line;
|
||||
CAT_ERROR_Error *err;
|
||||
} TParser;
|
||||
|
||||
static void skip_ws(TParser *p)
|
||||
{
|
||||
for (;;) {
|
||||
char c = p->s[p->pos];
|
||||
if (c == ' ' || c == '\t' || c == '\r') {
|
||||
p->pos++;
|
||||
} else if (c == '#' || (c == '\n')) {
|
||||
if (c == '#') {
|
||||
while (p->s[p->pos] != '\n' && p->s[p->pos] != '\0')
|
||||
p->pos++;
|
||||
} else {
|
||||
p->pos++;
|
||||
p->line++;
|
||||
}
|
||||
} else {
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void skip_inline_ws(TParser *p)
|
||||
{
|
||||
while (p->s[p->pos] == ' ' || p->s[p->pos] == '\t')
|
||||
p->pos++;
|
||||
}
|
||||
|
||||
static char *parse_key(TParser *p)
|
||||
{
|
||||
char buf[256];
|
||||
int n = 0;
|
||||
|
||||
skip_inline_ws(p);
|
||||
if (p->s[p->pos] == '"') {
|
||||
p->pos++;
|
||||
while (p->s[p->pos] != '"' && p->s[p->pos] != '\0') {
|
||||
if (n < (int)sizeof(buf) - 1)
|
||||
buf[n++] = p->s[p->pos];
|
||||
p->pos++;
|
||||
}
|
||||
if (p->s[p->pos] == '"')
|
||||
p->pos++;
|
||||
} else {
|
||||
while (isalnum((unsigned char)p->s[p->pos]) ||
|
||||
p->s[p->pos] == '_' || p->s[p->pos] == '-') {
|
||||
if (n < (int)sizeof(buf) - 1)
|
||||
buf[n++] = p->s[p->pos];
|
||||
p->pos++;
|
||||
}
|
||||
}
|
||||
buf[n] = '\0';
|
||||
return strdup(buf);
|
||||
}
|
||||
|
||||
static CAT_VALUE_Value *parse_value(TParser *p);
|
||||
|
||||
static CAT_VALUE_Value *parse_string(TParser *p)
|
||||
{
|
||||
char buf[1024];
|
||||
int n = 0;
|
||||
char quote = p->s[p->pos++];
|
||||
|
||||
while (p->s[p->pos] != quote && p->s[p->pos] != '\0') {
|
||||
char c = p->s[p->pos++];
|
||||
if (c == '\\') {
|
||||
char e = p->s[p->pos++];
|
||||
switch (e) {
|
||||
case 'n': c = '\n'; break;
|
||||
case 't': c = '\t'; break;
|
||||
case 'r': c = '\r'; break;
|
||||
case '\\': c = '\\'; break;
|
||||
case '"': c = '"'; break;
|
||||
default: c = e; break;
|
||||
}
|
||||
}
|
||||
if (n < (int)sizeof(buf) - 1)
|
||||
buf[n++] = c;
|
||||
}
|
||||
if (p->s[p->pos] == quote)
|
||||
p->pos++;
|
||||
buf[n] = '\0';
|
||||
return cat_types_ascii_text_new(buf);
|
||||
}
|
||||
|
||||
static CAT_VALUE_Value *parse_array(TParser *p)
|
||||
{
|
||||
CAT_VALUE_Value *arr = cat_types_table_new();
|
||||
int idx = 0;
|
||||
|
||||
p->pos++; /* [ */
|
||||
for (;;) {
|
||||
char key[32];
|
||||
CAT_VALUE_Value *v;
|
||||
CAT_ERROR_Error e;
|
||||
|
||||
skip_ws(p);
|
||||
if (p->s[p->pos] == ']') {
|
||||
p->pos++;
|
||||
break;
|
||||
}
|
||||
if (p->s[p->pos] == '\0')
|
||||
break;
|
||||
v = parse_value(p);
|
||||
snprintf(key, sizeof(key), "%d", idx++);
|
||||
cat_error_clear(&e);
|
||||
cat_types_table_set(arr, key, v, &e);
|
||||
skip_ws(p);
|
||||
if (p->s[p->pos] == ',')
|
||||
p->pos++;
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
static CAT_VALUE_Value *parse_value(TParser *p)
|
||||
{
|
||||
char c;
|
||||
|
||||
skip_inline_ws(p);
|
||||
c = p->s[p->pos];
|
||||
|
||||
if (c == '"' || c == '\'')
|
||||
return parse_string(p);
|
||||
if (c == '[')
|
||||
return parse_array(p);
|
||||
if (c == 't' && strncmp(p->s + p->pos, "true", 4) == 0) {
|
||||
p->pos += 4;
|
||||
return cat_types_bool_true();
|
||||
}
|
||||
if (c == 'f' && strncmp(p->s + p->pos, "false", 5) == 0) {
|
||||
p->pos += 5;
|
||||
return cat_types_bool_false();
|
||||
}
|
||||
if (isdigit((unsigned char)c) || c == '-' || c == '+') {
|
||||
char buf[64];
|
||||
int n = 0;
|
||||
int isfloat = 0;
|
||||
while ((isdigit((unsigned char)p->s[p->pos]) || p->s[p->pos] == '-' ||
|
||||
p->s[p->pos] == '+' || p->s[p->pos] == '.' ||
|
||||
p->s[p->pos] == 'e' || p->s[p->pos] == 'E') &&
|
||||
n < (int)sizeof(buf) - 1) {
|
||||
if (p->s[p->pos] == '.' || p->s[p->pos] == 'e' ||
|
||||
p->s[p->pos] == 'E')
|
||||
isfloat = 1;
|
||||
buf[n++] = p->s[p->pos++];
|
||||
}
|
||||
buf[n] = '\0';
|
||||
if (isfloat)
|
||||
return cat_types_float_new(strtod(buf, NULL));
|
||||
return cat_types_int_new(strtoll(buf, NULL, 10));
|
||||
}
|
||||
/* fallback: consume a bare word as a string */
|
||||
{
|
||||
char buf[256];
|
||||
int n = 0;
|
||||
while (p->s[p->pos] != '\0' && p->s[p->pos] != '\n' &&
|
||||
p->s[p->pos] != ',' && p->s[p->pos] != ']' &&
|
||||
n < (int)sizeof(buf) - 1)
|
||||
buf[n++] = p->s[p->pos++];
|
||||
buf[n] = '\0';
|
||||
return cat_types_ascii_text_new(buf);
|
||||
}
|
||||
}
|
||||
|
||||
/* Navigate/create a nested table by dotted path. */
|
||||
static CAT_VALUE_Value *descend(CAT_VALUE_Value *root, char *path)
|
||||
{
|
||||
char *save = NULL;
|
||||
char *part = strtok_r(path, ".", &save);
|
||||
CAT_VALUE_Value *cur = root;
|
||||
|
||||
while (part != NULL) {
|
||||
CAT_VALUE_Value *next = NULL;
|
||||
CAT_ERROR_Error e;
|
||||
cat_error_clear(&e);
|
||||
if (cat_types_table_get(cur, part, &next, &e) != 0) {
|
||||
next = cat_types_table_new();
|
||||
cat_error_clear(&e);
|
||||
cat_types_table_set(cur, part, next, &e);
|
||||
}
|
||||
cur = next;
|
||||
part = strtok_r(NULL, ".", &save);
|
||||
}
|
||||
return cur;
|
||||
}
|
||||
|
||||
int cat_stdlib_toml_parse(const char *text, CAT_VALUE_Value **out,
|
||||
CAT_ERROR_Error *err)
|
||||
{
|
||||
TParser p;
|
||||
CAT_VALUE_Value *root = cat_types_table_new();
|
||||
CAT_VALUE_Value *current = root;
|
||||
|
||||
p.s = text ? text : "";
|
||||
p.pos = 0;
|
||||
p.line = 1;
|
||||
p.err = err;
|
||||
|
||||
for (;;) {
|
||||
skip_ws(&p);
|
||||
if (p.s[p.pos] == '\0')
|
||||
break;
|
||||
|
||||
if (p.s[p.pos] == '[') {
|
||||
int array_table = 0;
|
||||
char path[256];
|
||||
int n = 0;
|
||||
p.pos++;
|
||||
if (p.s[p.pos] == '[') {
|
||||
array_table = 1;
|
||||
p.pos++;
|
||||
}
|
||||
while (p.s[p.pos] != ']' && p.s[p.pos] != '\0' &&
|
||||
n < (int)sizeof(path) - 1)
|
||||
path[n++] = p.s[p.pos++];
|
||||
path[n] = '\0';
|
||||
/* trim */
|
||||
while (n > 0 && (path[n - 1] == ' ' || path[n - 1] == '\t'))
|
||||
path[--n] = '\0';
|
||||
if (p.s[p.pos] == ']')
|
||||
p.pos++;
|
||||
if (p.s[p.pos] == ']')
|
||||
p.pos++;
|
||||
if (array_table) {
|
||||
CAT_VALUE_Value *tbl = cat_types_table_new();
|
||||
CAT_ERROR_Error e;
|
||||
char countkey[32];
|
||||
CAT_VALUE_Value *arr = NULL;
|
||||
int cnt;
|
||||
cat_error_clear(&e);
|
||||
if (cat_types_table_get(root, path, &arr, &e) != 0) {
|
||||
arr = cat_types_table_new();
|
||||
cat_error_clear(&e);
|
||||
cat_types_table_set(root, path, arr, &e);
|
||||
}
|
||||
cnt = cat_types_table_count(arr);
|
||||
snprintf(countkey, sizeof(countkey), "%d", cnt);
|
||||
cat_error_clear(&e);
|
||||
cat_types_table_set(arr, countkey, tbl, &e);
|
||||
current = tbl;
|
||||
} else {
|
||||
char copy[256];
|
||||
snprintf(copy, sizeof(copy), "%s", path);
|
||||
current = descend(root, copy);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
/* key = value */
|
||||
{
|
||||
char copy[256];
|
||||
char *key = parse_key(&p);
|
||||
CAT_VALUE_Value *val;
|
||||
CAT_VALUE_Value *target;
|
||||
CAT_ERROR_Error e;
|
||||
|
||||
snprintf(copy, sizeof(copy), "%s", key);
|
||||
free(key);
|
||||
skip_inline_ws(&p);
|
||||
if (p.s[p.pos] == '=') {
|
||||
p.pos++;
|
||||
} else {
|
||||
/* skip line */
|
||||
while (p.s[p.pos] != '\n' && p.s[p.pos] != '\0')
|
||||
p.pos++;
|
||||
continue;
|
||||
}
|
||||
val = parse_value(&p);
|
||||
target = descend(current, copy);
|
||||
cat_error_clear(&e);
|
||||
/* descend with a full path only makes sense for top-level;
|
||||
* here `copy` may contain dots for dotted keys. */
|
||||
(void)target;
|
||||
{
|
||||
char *dot = strrchr(copy, '.');
|
||||
if (dot != NULL) {
|
||||
*dot = '\0';
|
||||
descend(current, copy);
|
||||
cat_error_clear(&e);
|
||||
cat_types_table_set(descend(current, copy),
|
||||
dot + 1, val, &e);
|
||||
} else {
|
||||
cat_error_clear(&e);
|
||||
cat_types_table_set(current, copy, val, &e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*out = root;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ---- encoder --------------------------------------------------------- */
|
||||
|
||||
static void enc_value(FILE *f, CAT_VALUE_Value *v);
|
||||
|
||||
static int is_bare_key(const char *s)
|
||||
{
|
||||
if (*s == '\0')
|
||||
return 0;
|
||||
for (; *s != '\0'; s++)
|
||||
if (!(isalnum((unsigned char)*s) || *s == '_' || *s == '-'))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void enc_string(FILE *f, const char *s)
|
||||
{
|
||||
fputc('"', f);
|
||||
for (; *s != '\0'; s++) {
|
||||
switch (*s) {
|
||||
case '"': fputs("\\\"", f); break;
|
||||
case '\\': fputs("\\\\", f); break;
|
||||
case '\n': fputs("\\n", f); break;
|
||||
case '\t': fputs("\\t", f); break;
|
||||
default: fputc(*s, f); break;
|
||||
}
|
||||
}
|
||||
fputc('"', f);
|
||||
}
|
||||
|
||||
static void enc_value(FILE *f, CAT_VALUE_Value *v)
|
||||
{
|
||||
if (v == NULL || v == cat_types_nil_get()) {
|
||||
fputs("\"nil\"", 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) {
|
||||
CAT_TYPES_NUMBER_Number *n = (CAT_TYPES_NUMBER_Number *)v;
|
||||
if (n->is_float)
|
||||
fprintf(f, "%g", n->f);
|
||||
else
|
||||
fprintf(f, "%lld", n->i);
|
||||
} else if (v->vtable && v->vtable->kind == CAT_VALUE_KIND_BOOL) {
|
||||
fputs(((CAT_TYPES_BOOL_Bool *)v)->v ? "true" : "false", f);
|
||||
} else if (v->vtable && v->vtable->kind == CAT_VALUE_KIND_TABLE) {
|
||||
int i, n = cat_types_table_count(v);
|
||||
fputc('[', f);
|
||||
for (i = 0; i < n; i++) {
|
||||
if (i)
|
||||
fputs(", ", f);
|
||||
enc_value(f, cat_types_table_value_at(v, i));
|
||||
}
|
||||
fputc(']', f);
|
||||
} else {
|
||||
fputs("\"?\"", f);
|
||||
}
|
||||
}
|
||||
|
||||
char *cat_stdlib_toml_encode(CAT_VALUE_Value *tbl, CAT_ERROR_Error *err)
|
||||
{
|
||||
FILE *f;
|
||||
char *buf;
|
||||
long sz;
|
||||
int i, n;
|
||||
(void)err;
|
||||
|
||||
if (tbl == NULL || !tbl->vtable || tbl->vtable->kind != CAT_VALUE_KIND_TABLE)
|
||||
return strdup("");
|
||||
f = open_memstream(&buf, (size_t *)&sz);
|
||||
if (f == NULL)
|
||||
return strdup("");
|
||||
n = cat_types_table_count(tbl);
|
||||
for (i = 0; i < n; i++) {
|
||||
const char *key = cat_types_table_key_at(tbl, i);
|
||||
CAT_VALUE_Value *v = cat_types_table_value_at(tbl, i);
|
||||
if (is_bare_key(key))
|
||||
fputs(key, f);
|
||||
else
|
||||
enc_string(f, key);
|
||||
fputs(" = ", f);
|
||||
enc_value(f, v);
|
||||
fputc('\n', f);
|
||||
}
|
||||
fclose(f);
|
||||
return buf;
|
||||
}
|
||||
Reference in new issue
Block a user