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
+138
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* main.c -- catscript CLI.
|
||||
*
|
||||
* Naming rule: this file lives at the project root, so its symbols are
|
||||
* main_* (top-level program entry). We keep helpers static and named
|
||||
* after their file, e.g. main_run_file().
|
||||
*
|
||||
* Modes:
|
||||
* catscript <file.cat> run a script
|
||||
* catscript --dump <file> print bytecode instead of running
|
||||
* catscript --help usage
|
||||
* catscript REPL (until EOF)
|
||||
*/
|
||||
#include "cat/compiler.h"
|
||||
#include "cat/bytecode.h"
|
||||
#include "cat/vm.h"
|
||||
#include "cat/module.h"
|
||||
#include "cat/stdlib/stdlib.h"
|
||||
#include "cat/stdlib/build.h"
|
||||
#include "cat/error.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static char *main_read_file(const char *path)
|
||||
{
|
||||
FILE *f = fopen(path, "rb");
|
||||
long sz;
|
||||
char *buf;
|
||||
|
||||
if (f == NULL)
|
||||
return NULL;
|
||||
fseek(f, 0, SEEK_END);
|
||||
sz = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
buf = malloc((size_t)sz + 1);
|
||||
if (buf == NULL) {
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
if (fread(buf, 1, (size_t)sz, f) != (size_t)sz) {
|
||||
/* short read is fine for text */
|
||||
}
|
||||
buf[sz] = '\0';
|
||||
fclose(f);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static int main_run_source(const char *source, int dump)
|
||||
{
|
||||
CAT_ERROR_Error err;
|
||||
CAT_BYTECODE_Function *fn;
|
||||
CAT_VM_State *vm;
|
||||
CAT_VALUE_Value *out = NULL;
|
||||
int rc;
|
||||
|
||||
cat_error_clear(&err);
|
||||
fn = cat_compiler_compile(source, &err);
|
||||
if (fn == NULL) {
|
||||
fprintf(stderr, "catscript: %s\n", err.message);
|
||||
return 1;
|
||||
}
|
||||
if (dump) {
|
||||
cat_bytecode_dump(fn, stdout);
|
||||
cat_bytecode_function_free(fn);
|
||||
return 0;
|
||||
}
|
||||
vm = cat_vm_new();
|
||||
if (vm == NULL) {
|
||||
cat_bytecode_function_free(fn);
|
||||
return 1;
|
||||
}
|
||||
cat_stdlib_stdlib_install(vm);
|
||||
cat_stdlib_build_install(vm);
|
||||
rc = cat_vm_run(vm, fn, &out);
|
||||
if (rc != 0)
|
||||
fprintf(stderr, "catscript: %s\n", vm->err.message);
|
||||
cat_vm_free(vm);
|
||||
cat_bytecode_function_free(fn);
|
||||
return rc != 0;
|
||||
}
|
||||
|
||||
static void main_repl(void)
|
||||
{
|
||||
char line[1024];
|
||||
|
||||
printf("catscript REPL -- type an expression; EOF to exit.\n");
|
||||
for (;;) {
|
||||
printf("> ");
|
||||
fflush(stdout);
|
||||
if (fgets(line, sizeof(line), stdin) == NULL)
|
||||
break;
|
||||
main_run_source(line, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void main_usage(void)
|
||||
{
|
||||
printf("catscript -- a small Lua-like scripting language\n\n");
|
||||
printf("usage:\n");
|
||||
printf(" catscript <file.cat> run a script\n");
|
||||
printf(" catscript --dump <file> print compiled bytecode\n");
|
||||
printf(" catscript --help show this help\n");
|
||||
printf(" catscript start a REPL\n");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc >= 2 && strcmp(argv[1], "--help") == 0) {
|
||||
main_usage();
|
||||
return 0;
|
||||
}
|
||||
if (argc >= 3 && strcmp(argv[1], "--dump") == 0) {
|
||||
char *src = main_read_file(argv[2]);
|
||||
int rc;
|
||||
if (src == NULL) {
|
||||
fprintf(stderr, "catscript: cannot read %s\n", argv[2]);
|
||||
return 1;
|
||||
}
|
||||
rc = main_run_source(src, 1);
|
||||
free(src);
|
||||
return rc;
|
||||
}
|
||||
if (argc >= 2) {
|
||||
char *src = main_read_file(argv[1]);
|
||||
int rc;
|
||||
if (src == NULL) {
|
||||
fprintf(stderr, "catscript: cannot read %s\n", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
rc = main_run_source(src, 0);
|
||||
free(src);
|
||||
return rc;
|
||||
}
|
||||
main_repl();
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user