- 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)
61 lines
1.3 KiB
C
61 lines
1.3 KiB
C
/*
|
|
* cat/module.c -- Cat_Module implementation.
|
|
*
|
|
* Naming rule: symbols here are cat_module_* / CAT_MODULE_*.
|
|
*/
|
|
#include "cat/module.h"
|
|
#include "cat/compiler.h"
|
|
#include "cat/bytecode.h"
|
|
#include "cat/vm.h"
|
|
#include "cat/stdlib/stdlib.h"
|
|
#include "cat/stdlib/build.h"
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
CAT_MODULE_Module *cat_module_new(const char *name)
|
|
{
|
|
CAT_MODULE_Module *m = calloc(1, sizeof(*m));
|
|
|
|
if (m == NULL)
|
|
return NULL;
|
|
m->name = strdup(name ? name : "module");
|
|
return m;
|
|
}
|
|
|
|
int cat_module_compile(CAT_MODULE_Module *mod, const char *source,
|
|
CAT_ERROR_Error *err)
|
|
{
|
|
mod->main = cat_compiler_compile(source, err);
|
|
return mod->main ? 0 : -1;
|
|
}
|
|
|
|
int cat_module_run(CAT_MODULE_Module *mod, CAT_VALUE_Value **out,
|
|
CAT_ERROR_Error *err)
|
|
{
|
|
CAT_VM_State *vm;
|
|
int rc;
|
|
|
|
if (mod->main == NULL)
|
|
return (cat_error_raise(err, "module has no compiled code"), -1);
|
|
vm = cat_vm_new();
|
|
if (vm == NULL)
|
|
return -1;
|
|
cat_stdlib_stdlib_install(vm);
|
|
cat_stdlib_build_install(vm);
|
|
rc = cat_vm_run(vm, mod->main, out);
|
|
if (rc != 0)
|
|
cat_error_raise(err, "%s", vm->err.message);
|
|
cat_vm_free(vm);
|
|
return rc;
|
|
}
|
|
|
|
void cat_module_free(CAT_MODULE_Module *mod)
|
|
{
|
|
if (mod == NULL)
|
|
return;
|
|
cat_bytecode_function_free(mod->main);
|
|
free(mod->name);
|
|
free(mod);
|
|
}
|