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:
Minus committed 2026-11-16 01:27:44 +00:00
commit 52d118211f
69 files changed
+6938

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
/*
* cat/types/ascii_text.h -- the concrete `ascii_text` type.
*
* Naming rule: symbols here are cat_types_ascii_text_*.
* A heap-owned, NUL-terminated ASCII string; a Cat_Text.
*/
#ifndef CAT_TYPES_ASCII_TEXT_H
#define CAT_TYPES_ASCII_TEXT_H
#include "cat/types/text.h"
/* CAT_TYPES_ASCII_TEXT_AsciiText -- concrete ASCII text value. */
typedef struct CAT_TYPES_ASCII_TEXT_AsciiText {
CAT_TYPES_TEXT_Text text; /* base (must be first) */
char *owned; /* heap storage of `len`+1 bytes */
} CAT_TYPES_ASCII_TEXT_AsciiText;
CAT_VALUE_Value *cat_types_ascii_text_new(const char *s);
CAT_VALUE_Vtable *cat_types_ascii_text_vtable(void);
#endif /* CAT_TYPES_ASCII_TEXT_H */
+21
View File
@@ -0,0 +1,21 @@
/*
* cat/types/bool.h -- the boolean type.
*
* Naming rule: symbols here are cat_types_bool_* / CAT_TYPES_BOOL_*.
*/
#ifndef CAT_TYPES_BOOL_H
#define CAT_TYPES_BOOL_H
#include "cat/value.h"
typedef struct CAT_TYPES_BOOL_Bool {
CAT_VALUE_Value base;
int v; /* 0 or 1 */
} CAT_TYPES_BOOL_Bool;
/* Shared singletons for true/false. */
CAT_VALUE_Value *cat_types_bool_true(void);
CAT_VALUE_Value *cat_types_bool_false(void);
CAT_VALUE_Vtable *cat_types_bool_vtable(void);
#endif /* CAT_TYPES_BOOL_H */
+21
View File
@@ -0,0 +1,21 @@
/*
* cat/types/byte.h -- the concrete `byte` type.
*
* Naming rule: symbols here are cat_types_byte_* / CAT_TYPES_BYTE_*.
* `byte` is an unsigned 8-bit number; it participates in arithmetic as
* an integral Cat_Number and wraps at 256 when stored back.
*/
#ifndef CAT_TYPES_BYTE_H
#define CAT_TYPES_BYTE_H
#include "cat/types/number.h"
/* CAT_TYPES_BYTE_Byte -- concrete 8-bit unsigned number. */
typedef struct CAT_TYPES_BYTE_Byte {
CAT_TYPES_NUMBER_Number number; /* base (must be first) */
} CAT_TYPES_BYTE_Byte;
CAT_VALUE_Value *cat_types_byte_new(unsigned int v);
CAT_VALUE_Vtable *cat_types_byte_vtable(void);
#endif /* CAT_TYPES_BYTE_H */
+25
View File
@@ -0,0 +1,25 @@
/*
* cat/types/char.h -- the fixed-width text types char1..char4 (Cat_Text).
*
* Naming rule: symbols here are cat_types_char_* / CAT_TYPES_CHAR_*.
* Each charN holds exactly N bytes; the width is part of the type name.
*/
#ifndef CAT_TYPES_CHAR_H
#define CAT_TYPES_CHAR_H
#include "cat/types/text.h"
/* CAT_TYPES_CHAR_Char -- concrete fixed-width text. `width` is 1..4. */
typedef struct CAT_TYPES_CHAR_Char {
CAT_TYPES_TEXT_Text text; /* base (must be first) */
int width; /* 1, 2, 3 or 4 */
char buf[5]; /* storage, NUL padded */
} CAT_TYPES_CHAR_Char;
/* Create a charN value (width 1..4); `s` is truncated/NUL-padded. */
CAT_VALUE_Value *cat_types_char_new(int width, const char *s);
/* Vtable selector for each width. */
CAT_VALUE_Vtable *cat_types_char_vtable(int width);
#endif /* CAT_TYPES_CHAR_H */
+64
View File
@@ -0,0 +1,64 @@
/*
* cat/types/file.h -- the file family.
*
* Naming rule: symbols here are cat_types_file_* / CAT_TYPES_FILE_*.
*
* AbstractFile is the abstract base for every byte stream. Concrete
* subclasses share the same method set (read/write/close/name) through
* the base vtable, so they are interchangeable:
*
* AbstractFile -- abstract base: read/write/close/name
* File -- a real filesystem file; constructed by File::open()
* StringIO -- an in-memory text buffer (read as text)
* BytesIO -- an in-memory byte buffer (read as bytes)
*
* `File` is also exposed to catscript as a class table so that
* `File::open(path)` / `File:open(path)` produce a `File` value.
*/
#ifndef CAT_TYPES_FILE_H
#define CAT_TYPES_FILE_H
#include "cat/value.h"
/* How a stream was opened. */
enum {
CAT_TYPES_FILE_MODE_READ = 1,
CAT_TYPES_FILE_MODE_WRITE = 2,
CAT_TYPES_FILE_MODE_APPEND = 4
};
/* CAT_TYPES_FILE_AbstractFile -- common header for every stream. */
typedef struct CAT_TYPES_FILE_AbstractFile {
CAT_VALUE_Value base; /* vtable header (must be first) */
int mode; /* CAT_TYPES_FILE_MODE_* */
int closed;
int is_bytes; /* StringIO:0, BytesIO:1 */
} CAT_TYPES_FILE_AbstractFile;
/* A real file stream. */
typedef struct CAT_TYPES_FILE_File {
CAT_TYPES_FILE_AbstractFile af;
void *fp; /* FILE * (kept opaque here) */
char *path;
} CAT_TYPES_FILE_File;
/* An in-memory stream (StringIO/BytesIO). */
typedef struct CAT_TYPES_FILE_MemFile {
CAT_TYPES_FILE_AbstractFile af;
char *buf;
long len, cap, pos;
} CAT_TYPES_FILE_MemFile;
/* Constructors. */
CAT_VALUE_Value *cat_types_file_open(const char *path, const char *mode);
CAT_VALUE_Value *cat_types_file_stringio(const char *initial);
CAT_VALUE_Value *cat_types_file_bytesio(void);
/* The abstract base vtable (all streams share its methods). */
CAT_VALUE_Vtable *cat_types_file_abstract_vtable(void);
/* Build the `File` class table (with the `open` classmethod) into a new
* value; used by the stdlib to expose `File`/`AbstractFile`/`StringIO`/`BytesIO`. */
CAT_VALUE_Value *cat_types_file_class_table(void);
#endif /* CAT_TYPES_FILE_H */
+20
View File
@@ -0,0 +1,20 @@
/*
* cat/types/float.h -- the concrete `float` type.
*
* Naming rule: symbols here are cat_types_float_* / CAT_TYPES_FLOAT_*.
* `float` is a Cat_Number (floating point).
*/
#ifndef CAT_TYPES_FLOAT_H
#define CAT_TYPES_FLOAT_H
#include "cat/types/number.h"
/* CAT_TYPES_FLOAT_Float -- concrete floating point number. */
typedef struct CAT_TYPES_FLOAT_Float {
CAT_TYPES_NUMBER_Number number; /* base (must be first) */
} CAT_TYPES_FLOAT_Float;
CAT_VALUE_Value *cat_types_float_new(double v);
CAT_VALUE_Vtable *cat_types_float_vtable(void);
#endif /* CAT_TYPES_FLOAT_H */
+27
View File
@@ -0,0 +1,27 @@
/*
* cat/types/function.h -- closures (Cat_ImplementsCall).
*
* Naming rule: symbols here are cat_types_function_*.
*/
#ifndef CAT_TYPES_FUNCTION_H
#define CAT_TYPES_FUNCTION_H
#include "cat/value.h"
#include "cat/bytecode.h"
struct CAT_VM_State;
/* CAT_TYPES_FUNCTION_Closure -- prototype + captured upvalues. */
typedef struct CAT_TYPES_FUNCTION_Closure {
CAT_VALUE_Value base;
struct CAT_BYTECODE_Function *proto;
CAT_VALUE_Value **upvals; /* captured values */
int nupvals;
} CAT_TYPES_FUNCTION_Closure;
/* Wrap a prototype as a callable closure. */
CAT_VALUE_Value *cat_types_function_new(struct CAT_BYTECODE_Function *proto);
CAT_VALUE_Vtable *cat_types_function_vtable(void);
#endif /* CAT_TYPES_FUNCTION_H */
+23
View File
@@ -0,0 +1,23 @@
/*
* cat/types/int.h -- the concrete `int` type.
*
* Naming rule: symbols here are cat_types_int_* / CAT_TYPES_INT_*.
* `int` is a Cat_Number (integral).
*/
#ifndef CAT_TYPES_INT_H
#define CAT_TYPES_INT_H
#include "cat/types/number.h"
/* CAT_TYPES_INT_Int -- concrete integral number. */
typedef struct CAT_TYPES_INT_Int {
CAT_TYPES_NUMBER_Number number; /* base (must be first) */
} CAT_TYPES_INT_Int;
/* Allocate an int value. */
CAT_VALUE_Value *cat_types_int_new(long long v);
/* Shared int vtable (exposed for the compiler's constant folding). */
CAT_VALUE_Vtable *cat_types_int_vtable(void);
#endif /* CAT_TYPES_INT_H */
+35
View File
@@ -0,0 +1,35 @@
/*
* cat/types/native.h -- native (C-implemented) functions as values.
*
* Naming rule: symbols here are cat_types_native_* / CAT_TYPES_NATIVE_*.
*
* A native function is a first-class value (Cat_ImplementsCall) backed by
* a C callback. Making natives ordinary values means they can be stored
* anywhere -- globals, table attributes, class methods -- and the VM
* dispatches them through the same cat_value_call path as closures, with
* no process-global registry.
*/
#ifndef CAT_TYPES_NATIVE_H
#define CAT_TYPES_NATIVE_H
#include "cat/value.h"
/* Forward declaration matching cat/vm.h. */
typedef struct CAT_VM_State CAT_VM_State;
/* CAT_TYPES_NATIVE_Native -- a C callback wrapped as a callable value. */
typedef struct CAT_TYPES_NATIVE_Native {
CAT_VALUE_Value base;
char *name;
int (*cb)(CAT_VM_State *, CAT_VALUE_Value **, int, CAT_VALUE_Value **);
} CAT_TYPES_NATIVE_Native;
CAT_VALUE_Value *cat_types_native_new(const char *name,
int (*cb)(CAT_VM_State *, CAT_VALUE_Value **, int, CAT_VALUE_Value **));
CAT_VALUE_Vtable *cat_types_native_vtable(void);
/* True if v is a native function value. */
int cat_types_native_is(CAT_VALUE_Value *v);
#endif /* CAT_TYPES_NATIVE_H */
+20
View File
@@ -0,0 +1,20 @@
/*
* 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 */
+51
View File
@@ -0,0 +1,51 @@
/*
* cat/types/number.h -- Cat_Number, the abstract numeric class.
*
* Naming rule: symbols declared here are cat_types_number_* /
* CAT_TYPES_NUMBER_*.
*
* Cat_Number is abstract: it exists only as a base that concrete numeric
* types (int, float, byte) embed as their first member. Arithmetic is
* implemented once here and reused by every concrete number via their
* vtable slots, so the C API -- not the compiler -- owns numeric semantics.
*/
#ifndef CAT_TYPES_NUMBER_H
#define CAT_TYPES_NUMBER_H
#include "cat/value.h"
#include "cat/error.h"
/* CAT_TYPES_NUMBER_Number == Cat_Number (abstract).
* Concrete numbers embed this as their first field so a pointer to a
* CAT_TYPES_NUMBER_Number can be reinterpreted as CAT_VALUE_Value*. */
typedef struct CAT_TYPES_NUMBER_Number {
CAT_VALUE_Value base; /* vtable header (must be first) */
int is_float; /* 0 => integral, 1 => floating point */
long long i; /* valid when is_float == 0 */
double f; /* valid when is_float == 1 */
} CAT_TYPES_NUMBER_Number;
/* Read a number object as a double (works for int, float and byte). */
double cat_types_number_as_double(CAT_VALUE_Value *v);
/* Read a number object as a signed integer (truncating floats). */
long long cat_types_number_as_int(CAT_VALUE_Value *v);
/* True if the value's vtable derives from Cat_Number. */
int cat_types_number_is_number(CAT_VALUE_Value *v);
/* Shared binary-op helpers: implement +,-,*,/,% over any two numbers,
* promoting to floating point when either operand is floating point.
* Concrete type vtables point their add/sub/... slots at these. */
int cat_types_number_add(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
int cat_types_number_sub(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
int cat_types_number_mul(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
int cat_types_number_div(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
int cat_types_number_rem(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
#endif /* CAT_TYPES_NUMBER_H */
+42
View File
@@ -0,0 +1,42 @@
/*
* cat/types/table.h -- the table/object type (the OOP backbone).
*
* Naming rule: symbols here are cat_types_table_* / CAT_TYPES_TABLE_*.
*
* A table is an ordered map of string keys to values. It is the value
* used for Lua-like `{ key = value }` literals and for objects with
* methods, so `Cat_ImplementsGetAttribute`/`SetAttribute` are its main
* interfaces.
*/
#ifndef CAT_TYPES_TABLE_H
#define CAT_TYPES_TABLE_H
#include "cat/value.h"
typedef struct CAT_TYPES_TABLE_Pair {
char *key;
CAT_VALUE_Value *value;
} CAT_TYPES_TABLE_Pair;
/* CAT_TYPES_TABLE_Table -- concrete map/object. */
typedef struct CAT_TYPES_TABLE_Table {
CAT_VALUE_Value base; /* vtable header (must be first) */
CAT_TYPES_TABLE_Pair *pairs;
int npairs, cappairs;
} CAT_TYPES_TABLE_Table;
CAT_VALUE_Value *cat_types_table_new(void);
int cat_types_table_set(CAT_VALUE_Value *tbl, const char *key,
CAT_VALUE_Value *value, CAT_ERROR_Error *err);
int cat_types_table_get(CAT_VALUE_Value *tbl, const char *key,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
/* Number of entries, and key-at-index accessors (insertion order). */
int cat_types_table_count(CAT_VALUE_Value *tbl);
const char *cat_types_table_key_at(CAT_VALUE_Value *tbl, int i);
CAT_VALUE_Value *cat_types_table_value_at(CAT_VALUE_Value *tbl, int i);
CAT_VALUE_Vtable *cat_types_table_vtable(void);
#endif /* CAT_TYPES_TABLE_H */
+41
View File
@@ -0,0 +1,41 @@
/*
* cat/types/text.h -- Cat_Text, the abstract text class.
*
* Naming rule: symbols declared here are cat_types_text_* /
* CAT_TYPES_TEXT_*.
*
* Cat_Text is abstract. ascii_text, utf8_text and the fixed charN types
* embed this base (as their first field) and share the text semantics
* defined here: concatenation and a `length` attribute.
*/
#ifndef CAT_TYPES_TEXT_H
#define CAT_TYPES_TEXT_H
#include "cat/value.h"
#include "cat/error.h"
/* CAT_TYPES_TEXT_Text == Cat_Text (abstract). */
typedef struct CAT_TYPES_TEXT_Text {
CAT_VALUE_Value base; /* vtable header (must be first) */
const char *bytes; /* not owned; concrete type owns storage */
int len; /* byte length */
} CAT_TYPES_TEXT_Text;
/* Pointer to the bytes of a text value (never NULL for valid text). */
const char *cat_types_text_bytes(CAT_VALUE_Value *v);
/* Byte length of a text value. */
int cat_types_text_length(CAT_VALUE_Value *v);
/* True if the value's vtable derives from Cat_Text. */
int cat_types_text_is_text(CAT_VALUE_Value *v);
/* Shared concatenation used by every concrete text type's add slot. */
int cat_types_text_concat(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
/* Shared `length` attribute getter. */
int cat_types_text_get_attribute(CAT_VALUE_Value *self, const char *name,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
#endif /* CAT_TYPES_TEXT_H */
+21
View File
@@ -0,0 +1,21 @@
/*
* cat/types/utf8_text.h -- the concrete `utf8_text` type.
*
* Naming rule: symbols here are cat_types_utf8_text_*.
* A heap-owned UTF-8 string; a Cat_Text whose `length` counts code points.
*/
#ifndef CAT_TYPES_UTF8_TEXT_H
#define CAT_TYPES_UTF8_TEXT_H
#include "cat/types/text.h"
/* CAT_TYPES_UTF8_TEXT_Utf8Text -- concrete UTF-8 text value. */
typedef struct CAT_TYPES_UTF8_TEXT_Utf8Text {
CAT_TYPES_TEXT_Text text; /* base (must be first) */
char *owned; /* heap storage */
} CAT_TYPES_UTF8_TEXT_Utf8Text;
CAT_VALUE_Value *cat_types_utf8_text_new(const char *s);
CAT_VALUE_Vtable *cat_types_utf8_text_vtable(void);
#endif /* CAT_TYPES_UTF8_TEXT_H */