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

+110
View File
@@ -0,0 +1,110 @@
/*
* cat/bytecode.h -- the catscript bytecode format.
*
* Naming rule: symbols here are cat_bytecode_* / CAT_BYTECODE_*.
*
* A compiled module is one CAT_BYTECODE_Function. Instructions are a
* flat array of CAT_BYTECODE_Instr. The VM (cat/vm.h) executes them.
*
* Operand conventions:
* - A/B/C are small integer operands (register-free, stack based).
* - constants live in `consts[]`; locals in the current frame;s slots.
*/
#ifndef CAT_BYTECODE_H
#define CAT_BYTECODE_H
#include "cat/value.h"
#include <stdio.h>
/* Opcodes (documented, stable). */
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_TRUE, /* push true */
CAT_BYTECODE_OP_FALSE, /* push false */
CAT_BYTECODE_OP_GETLOCAL, /* A: local slot -> stack */
CAT_BYTECODE_OP_SETLOCAL, /* A: pop -> local slot */
CAT_BYTECODE_OP_GETGLOBAL, /* A: const name -> stack */
CAT_BYTECODE_OP_SETGLOBAL, /* A: const name, pop value */
CAT_BYTECODE_OP_ADD, /* pop b, a -> push a+b */
CAT_BYTECODE_OP_SUB,
CAT_BYTECODE_OP_MUL,
CAT_BYTECODE_OP_DIV,
CAT_BYTECODE_OP_REM,
CAT_BYTECODE_OP_NEG, /* unary minus */
CAT_BYTECODE_OP_NOT,
CAT_BYTECODE_OP_EQ,
CAT_BYTECODE_OP_NE,
CAT_BYTECODE_OP_LT,
CAT_BYTECODE_OP_LE,
CAT_BYTECODE_OP_GT,
CAT_BYTECODE_OP_GE,
CAT_BYTECODE_OP_GETATTR, /* A: const name; pop obj -> stack */
CAT_BYTECODE_OP_SETATTR, /* A: const name; pop val, obj */
CAT_BYTECODE_OP_NEWTABLE, /* push empty table */
CAT_BYTECODE_OP_CALL, /* A: nargs; pop fn+args */
CAT_BYTECODE_OP_CALLMETHOD, /* A: nargs; B: const name */
CAT_BYTECODE_OP_JUMP, /* A: absolute target */
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_POP, /* discard top */
CAT_BYTECODE_OP_PRINT, /* pop, print */
CAT_BYTECODE_OP_CLOSURE, /* A: index into protos[] */
CAT_BYTECODE_OP_GETUPVAL, /* A: upvalue slot */
CAT_BYTECODE_OP_CONCAT, /* string concat fast path */
CAT_BYTECODE_OP_DUP, /* duplicate top of stack */
CAT_BYTECODE_OP_GETIDX, /* pop idx, obj -> push obj[idx] */
CAT_BYTECODE_OP_SETIDX, /* pop val, idx, obj -> obj[idx]=val */
};
/* One instruction: opcode + three integer operands. */
typedef struct CAT_BYTECODE_Instr {
unsigned char op; /* a CAT_BYTECODE_Op */
int a;
int b;
int c;
int line; /* source line for diagnostics */
} CAT_BYTECODE_Instr;
struct CAT_BYTECODE_Function;
/* A constant pool entry: a value plus (for names) a string. */
typedef struct CAT_BYTECODE_Constant {
CAT_VALUE_Value *value;
} CAT_BYTECODE_Constant;
/* CAT_BYTECODE_Function -- a compiled chunk.
* `protos` holds nested function prototypes (for `function` literals). */
typedef struct CAT_BYTECODE_Function {
char *name;
CAT_BYTECODE_Instr *code;
int ncode, capcode;
CAT_BYTECODE_Constant *consts;
int nconsts, capconsts;
struct CAT_BYTECODE_Function **protos;
int nprotos, capprotos;
int nlocals; /* number of local slots */
int nparams; /* number of parameters */
} CAT_BYTECODE_Function;
CAT_BYTECODE_Function *cat_bytecode_function_new(const char *name);
/* Emit one instruction, returning its index. */
int cat_bytecode_emit(CAT_BYTECODE_Function *fn, unsigned char op,
int a, int b, int c, int line);
/* Add a constant; returns its index. */
int cat_bytecode_add_const(CAT_BYTECODE_Function *fn, CAT_VALUE_Value *v);
/* Add a nested prototype; returns its index. */
int cat_bytecode_add_proto(CAT_BYTECODE_Function *fn, CAT_BYTECODE_Function *p);
/* Human-readable disassembly (used by `--dump`). */
void cat_bytecode_dump(CAT_BYTECODE_Function *fn, FILE *out);
void cat_bytecode_function_free(CAT_BYTECODE_Function *fn);
#endif /* CAT_BYTECODE_H */
+20
View File
@@ -0,0 +1,20 @@
/*
* cat/compiler.h -- source -> bytecode compiler.
*
* Naming rule: symbols here are cat_compiler_* / CAT_COMPILER_*.
*
* The compiler is a recursive-descent parser over the Lua-like syntax.
* It performs structural-typing checks only where cheap (nothing is
* nominal) and accepts optional `: type` hints, which are advisory.
*/
#ifndef CAT_COMPILER_H
#define CAT_COMPILER_H
#include "cat/bytecode.h"
#include "cat/error.h"
/* Compile `source` into a fresh top-level function. */
CAT_BYTECODE_Function *cat_compiler_compile(const char *source,
CAT_ERROR_Error *err);
#endif /* CAT_COMPILER_H */
+29
View File
@@ -0,0 +1,29 @@
/*
* cat/error.h -- runtime error object.
*
* Naming rule: a symbol living in `cat/error.h` (path without the
* leading include/ or src/ prefix) is named cat_error_* / CAT_ERROR_*.
*
* Catscript runtime errors are carried in a plain value struct so that
* any C-API call can report failure without exceptions or longjmp.
*/
#ifndef CAT_ERROR_H
#define CAT_ERROR_H
#include <stdbool.h>
/* CAT_ERROR_Error -- an error slot passed into API calls. */
/* `raised` becomes true when an operation fails; `message` is a */
/* human-readable description (always NUL terminated when raised). */
typedef struct CAT_ERROR_Error {
bool raised;
char message[256];
} CAT_ERROR_Error;
/* Clear an error slot back to the "no error" state. */
void cat_error_clear(CAT_ERROR_Error *err);
/* Raise (fill) an error slot with a printf-style message. */
void cat_error_raise(CAT_ERROR_Error *err, const char *fmt, ...);
#endif /* CAT_ERROR_H */
+66
View File
@@ -0,0 +1,66 @@
/*
* cat/lexer.h -- tokenizer for catscript source.
*
* Naming rule: symbols here are cat_lexer_* / CAT_LEXER_*.
*
* The lexer turns Lua-like source into a stream of CAT_LEXER_Token.
* Keywords follow Lua; type hints use `:` in a few documented spots.
*/
#ifndef CAT_LEXER_H
#define CAT_LEXER_H
#include <stdio.h>
/* Token kinds. */
enum CAT_LEXER_Kind {
CAT_LEXER_TOK_EOF = 0,
CAT_LEXER_TOK_NAME,
CAT_LEXER_TOK_INT,
CAT_LEXER_TOK_FLOAT,
CAT_LEXER_TOK_STRING,
/* keywords */
CAT_LEXER_TOK_AND, CAT_LEXER_TOK_BREAK, CAT_LEXER_TOK_DO,
CAT_LEXER_TOK_ELSE, CAT_LEXER_TOK_ELSEIF, CAT_LEXER_TOK_END,
CAT_LEXER_TOK_FALSE, CAT_LEXER_TOK_FOR, CAT_LEXER_TOK_FUNCTION,
CAT_LEXER_TOK_IF, CAT_LEXER_TOK_IN, CAT_LEXER_TOK_LOCAL,
CAT_LEXER_TOK_NIL, CAT_LEXER_TOK_NOT, CAT_LEXER_TOK_OR,
CAT_LEXER_TOK_REPEAT, CAT_LEXER_TOK_RETURN, CAT_LEXER_TOK_THEN,
CAT_LEXER_TOK_TRUE, CAT_LEXER_TOK_UNTIL, CAT_LEXER_TOK_WHILE,
/* symbols */
CAT_LEXER_TOK_PLUS, CAT_LEXER_TOK_MINUS, CAT_LEXER_TOK_STAR,
CAT_LEXER_TOK_SLASH, CAT_LEXER_TOK_PERCENT, CAT_LEXER_TOK_CARET,
CAT_LEXER_TOK_HASH, CAT_LEXER_TOK_EQ, CAT_LEXER_TOK_NE,
CAT_LEXER_TOK_LE, CAT_LEXER_TOK_GE, CAT_LEXER_TOK_LT,
CAT_LEXER_TOK_GT, CAT_LEXER_TOK_ASSIGN, CAT_LEXER_TOK_LPAREN,
CAT_LEXER_TOK_RPAREN, CAT_LEXER_TOK_LBRACE, CAT_LEXER_TOK_RBRACE,
CAT_LEXER_TOK_LBRACKET, CAT_LEXER_TOK_RBRACKET,
CAT_LEXER_TOK_SEMI, CAT_LEXER_TOK_COMMA, CAT_LEXER_TOK_DOT,
CAT_LEXER_TOK_COLON, CAT_LEXER_TOK_DCOLON, CAT_LEXER_TOK_CONCAT,
CAT_LEXER_TOK_ARROW
};
/* A lexed token: kind + payload + source line. */
typedef struct CAT_LEXER_Token {
int kind;
char text[256]; /* NAME / STRING payload */
long long ival; /* INT payload */
double fval; /* FLOAT payload */
int line;
} CAT_LEXER_Token;
/* CAT_LEXER_Lexer -- cursor over a source buffer. */
typedef struct CAT_LEXER_Lexer {
const char *src;
size_t pos;
int line;
} CAT_LEXER_Lexer;
void cat_lexer_init(CAT_LEXER_Lexer *lx, const char *src);
/* Produce the next token into *out; returns 0 on success. */
int cat_lexer_next(CAT_LEXER_Lexer *lx, CAT_LEXER_Token *out);
/* Peek-friendly variant used by the parser: one-token lookahead. */
const char *cat_lexer_kind_name(int kind);
#endif /* CAT_LEXER_H */
+39
View File
@@ -0,0 +1,39 @@
/*
* cat/module.h -- Cat_Module, a unit of catscript code.
*
* Naming rule: symbols declared here are cat_module_* / CAT_MODULE_*.
* (Path of this header below include/ is `cat/module`, so cat_module_*.)
*
* A module owns a name and a compiled entry prototype. Loading a module
* is how an embedder turns catscript source into something callable.
*/
#ifndef CAT_MODULE_H
#define CAT_MODULE_H
#include "cat/value.h"
#include "cat/error.h"
struct CAT_BYTECODE_Function;
/* CAT_MODULE_Module == Cat_Module.
* One compilable unit: a name plus its top-level function prototype. */
typedef struct CAT_MODULE_Module {
char *name;
struct CAT_BYTECODE_Function *main;
} CAT_MODULE_Module;
/* Create an empty module with the given name (name is copied). */
CAT_MODULE_Module *cat_module_new(const char *name);
/* Compile catscript source text into this module. */
int cat_module_compile(CAT_MODULE_Module *mod, const char *source,
CAT_ERROR_Error *err);
/* Execute the module's main prototype; `out` receives its return value. */
int cat_module_run(CAT_MODULE_Module *mod, CAT_VALUE_Value **out,
CAT_ERROR_Error *err);
/* Free a module. */
void cat_module_free(CAT_MODULE_Module *mod);
#endif /* CAT_MODULE_H */
+25
View File
@@ -0,0 +1,25 @@
/*
* cat/stdlib/build.h -- bytecode builder primitives for self-hosted tools.
*
* Naming rule: symbols here are cat_stdlib_build_* / CAT_STDLIB_BUILD_*.
*
* The lexer/parser/checker/compiler are written in catscript itself. To
* let them produce bytecode, a compiled chunk is represented as plain
* catscript data (a table) and this module turns that data into a real
* CAT_BYTECODE_Function the VM can run.
*
* A chunk table has the shape:
* { name=..., nparams=N, nlocals=N,
* consts={ v0, v1, ... }, -- constant pool (any values)
* protos={ chunk0, chunk1, ... }, -- nested chunk tables
* code={ {op=1,a=0,b=0,c=0,line=1}, ... } }
*/
#ifndef CAT_STDLIB_BUILD_H
#define CAT_STDLIB_BUILD_H
#include "cat/vm.h"
/* Install the builder natives (bc_run, bc_dump) into `vm`. */
int cat_stdlib_build_install(CAT_VM_State *vm);
#endif /* CAT_STDLIB_BUILD_H */
+18
View File
@@ -0,0 +1,18 @@
/*
* cat/stdlib/stdlib.h -- the catscript standard library bindings.
*
* Naming rule: symbols here are cat_stdlib_stdlib_* / CAT_STDLIB_STDLIB_*.
*
* The stdlib is exposed to catscript as native functions installed into
* a VM's globals. It is also part of the C API: an embedder may call
* these helpers directly.
*/
#ifndef CAT_STDLIB_STDLIB_H
#define CAT_STDLIB_STDLIB_H
#include "cat/vm.h"
/* Install the whole standard library into `vm`'s globals. */
int cat_stdlib_stdlib_install(CAT_VM_State *vm);
#endif /* CAT_STDLIB_STDLIB_H */
+25
View File
@@ -0,0 +1,25 @@
/*
* cat/stdlib/toml.h -- TOML parsing/encoding for the standard library.
*
* Naming rule: symbols here are cat_stdlib_toml_* / CAT_STDLIB_TOML_*.
*
* The parser supports the commonly used TOML subset: key/value pairs,
* [tables], [[array-of-tables]], strings, integers, floats, booleans,
* and inline arrays. It produces a CAT_TYPES_TABLE_Table value, so TOML
* documents are just objects to catscript. This is intentionally a
* focused subset, documented in the README.
*/
#ifndef CAT_STDLIB_TOML_H
#define CAT_STDLIB_TOML_H
#include "cat/value.h"
#include "cat/error.h"
/* Parse `text` as TOML; on success *out is a table value. */
int cat_stdlib_toml_parse(const char *text, CAT_VALUE_Value **out,
CAT_ERROR_Error *err);
/* Serialize a table value back to TOML text (heap string, caller frees). */
char *cat_stdlib_toml_encode(CAT_VALUE_Value *tbl, CAT_ERROR_Error *err);
#endif /* CAT_STDLIB_TOML_H */
+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 */
+147
View File
@@ -0,0 +1,147 @@
/*
* cat/value.h -- the object model at the heart of catscript.
*
* Naming rule: symbols declared here are cat_value_* / CAT_VALUE_*.
*
* Every catscript value is an object: a pointer to a CAT_VALUE_Value
* whose first member is a vtable pointer. The vtable (Cat_ValueType)
* carries one sub-interface per supported operation. A type that does
* not support an operation leaves the slot empty; the call helper then
* raises a structured error instead of crashing.
*
* The abstract interfaces requested for the language live here:
* Cat_ValueType, Cat_ImplementsAdd/Sub/Mul/Div/Rem/Call/Raise,
* Cat_ImplementsGetAttribute, Cat_ImplementsSetAttribute.
*/
#ifndef CAT_VALUE_H
#define CAT_VALUE_H
#include <stdbool.h>
struct CAT_ERROR_Error;
struct CAT_VALUE_Value;
struct CAT_VALUE_Vtable;
typedef struct CAT_VALUE_Value CAT_VALUE_Value;
typedef struct CAT_VALUE_Vtable CAT_VALUE_Vtable;
typedef struct CAT_ERROR_Error CAT_ERROR_Error;
/* ---- abstract interfaces (each is a single-slot method table) ---- */
/* Cat_ImplementsAdd -- binary + : self OP other -> *out. */
typedef struct CAT_IMPL_Add {
int (*fn)(CAT_VALUE_Value *self, CAT_VALUE_Value *other,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
} CAT_IMPL_Add;
/* Cat_ImplementsSub -- binary - . */
typedef struct CAT_IMPL_Sub {
int (*fn)(CAT_VALUE_Value *self, CAT_VALUE_Value *other,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
} CAT_IMPL_Sub;
/* Cat_ImplementsMul -- binary * . */
typedef struct CAT_IMPL_Mul {
int (*fn)(CAT_VALUE_Value *self, CAT_VALUE_Value *other,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
} CAT_IMPL_Mul;
/* Cat_ImplementsDiv -- binary / . */
typedef struct CAT_IMPL_Div {
int (*fn)(CAT_VALUE_Value *self, CAT_VALUE_Value *other,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
} CAT_IMPL_Div;
/* Cat_ImplementsRem -- binary % . */
typedef struct CAT_IMPL_Rem {
int (*fn)(CAT_VALUE_Value *self, CAT_VALUE_Value *other,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
} CAT_IMPL_Rem;
/* Cat_ImplementsCall -- self(args...) -> *out. */
typedef struct CAT_IMPL_Call {
int (*fn)(CAT_VALUE_Value *self, CAT_VALUE_Value **args, int nargs,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
} CAT_IMPL_Call;
/* Cat_ImplementsRaise -- raise a structured error from this object. */
typedef struct CAT_IMPL_Raise {
int (*fn)(CAT_VALUE_Value *self, CAT_VALUE_Value *message,
CAT_ERROR_Error *err);
} CAT_IMPL_Raise;
/* Cat_ImplementsGetAttribute -- self.name -> *out. */
typedef struct CAT_IMPL_GetAttribute {
int (*fn)(CAT_VALUE_Value *self, const char *name,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
} CAT_IMPL_GetAttribute;
/* Cat_ImplementsSetAttribute -- self.name = value. */
typedef struct CAT_IMPL_SetAttribute {
int (*fn)(CAT_VALUE_Value *self, const char *name, CAT_VALUE_Value *value,
CAT_ERROR_Error *err);
} CAT_IMPL_SetAttribute;
/* 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 */
CAT_VALUE_KIND_TABLE, /* table / object */
CAT_VALUE_KIND_FUNCTION /* closure */
};
/* ---- the vtable every value type fills in ---- */
/* CAT_VALUE_Vtable == Cat_ValueType.
* Slots left NULL mean "operation not supported by this type"; the
* dispatch helpers turn that into an error. */
struct CAT_VALUE_Vtable {
const char *type_name;
int kind; /* one of CAT_VALUE_KIND_* */
CAT_IMPL_Add add;
CAT_IMPL_Sub sub;
CAT_IMPL_Mul mul;
CAT_IMPL_Div div;
CAT_IMPL_Rem rem;
CAT_IMPL_Call call;
CAT_IMPL_Raise raise;
CAT_IMPL_GetAttribute get_attribute;
CAT_IMPL_SetAttribute set_attribute;
/* truthiness for control flow (NULL => everything is true) */
int (*truthy)(CAT_VALUE_Value *self, CAT_ERROR_Error *err);
/* release the object's own storage (shallow) */
void (*destroy)(CAT_VALUE_Value *self);
};
/* The common header shared by every concrete value. */
struct CAT_VALUE_Value {
CAT_VALUE_Vtable *vtable;
};
/* ---- dispatch helpers used by compiler/VM and embedders ---- */
int cat_value_add(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
int cat_value_sub(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
int cat_value_mul(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
int cat_value_div(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
int cat_value_rem(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
int cat_value_call(CAT_VALUE_Value *fn, CAT_VALUE_Value **args, int nargs,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
int cat_value_get_attribute(CAT_VALUE_Value *self, const char *name,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
int cat_value_set_attribute(CAT_VALUE_Value *self, const char *name,
CAT_VALUE_Value *value, CAT_ERROR_Error *err);
int cat_value_truthy(CAT_VALUE_Value *v, int *out, CAT_ERROR_Error *err);
int cat_value_equal(CAT_VALUE_Value *a, CAT_VALUE_Value *b, int *out,
CAT_ERROR_Error *err);
const char *cat_value_type_name(CAT_VALUE_Value *v);
#endif /* CAT_VALUE_H */
+59
View File
@@ -0,0 +1,59 @@
/*
* cat/vm.h -- the stack VM that executes catscript bytecode.
*
* Naming rule: symbols here are cat_vm_* / CAT_VM_*.
*/
#ifndef CAT_VM_H
#define CAT_VM_H
#include "cat/value.h"
#include "cat/error.h"
#include "cat/bytecode.h"
/* Forward declaration so the native-callback signature below refers to
* the real CAT_VM_State type, not a parameter-list-scoped struct. */
typedef struct CAT_VM_State CAT_VM_State;
/* One registered native function. Natives live in the VM (not in a
* process-global list) so distinct VMs never share mutable state and can
* run concurrently on different threads with no global lock (no GIL). */
typedef struct CAT_VM_Native {
char name[64];
int (*cb)(CAT_VM_State *, CAT_VALUE_Value **, int, CAT_VALUE_Value **);
} CAT_VM_Native;
#define CAT_VM_MAXNATIVE 128
/* CAT_VM_State -- globals + a call stack. Fully self-contained: two VMs
* share no mutable state, so they may execute on separate threads. */
typedef struct CAT_VM_State {
/* global table (name -> value) */ CAT_VALUE_Value *globals;
CAT_ERROR_Error err;
/* native functions installed into this VM */
CAT_VM_Native natives[CAT_VM_MAXNATIVE];
int nnatives;
} CAT_VM_State;
/* Create a VM with an empty globals table. */
CAT_VM_State *cat_vm_new(void);
/* Run a top-level function; `out` receives the return value. */
int cat_vm_run(CAT_VM_State *vm, CAT_BYTECODE_Function *fn,
CAT_VALUE_Value **out);
/* Register a native function (name, C callback) into globals. */
int cat_vm_register_native(CAT_VM_State *vm, const char *name,
int (*cb)(CAT_VM_State *, CAT_VALUE_Value **, int,
CAT_VALUE_Value **));
/* The VM currently executing on THIS thread (thread-local), used by
* closures/natives invoked through the object system. */
CAT_VM_State *cat_vm_current(void);
/* Convenience: get/set a global. */
CAT_VALUE_Value *cat_vm_get_global(CAT_VM_State *vm, const char *name);
int cat_vm_set_global(CAT_VM_State *vm, const char *name, CAT_VALUE_Value *v);
void cat_vm_free(CAT_VM_State *vm);
#endif /* CAT_VM_H */