#include "uscript.h" #include #include "dyn_arr.h" #include "lex.h" #include "val.h" #include "vm.h" #include "parser.h" void core_print(int argc) { (void)argc; char *str = val_to_str(vm_peek(), NULL); olog(str); mem_free(str); vm_push(create_zilch()); } void core_len(int argc) { (void)argc; struct us_val val = vm_peek(); switch (val.type) { case VAL_ARR: vm_push(create_num(da_len(get_arr(val)->e))); break; case VAL_STR: vm_push(create_num(get_str(val)->len)); break; default: vm_push(create_zilch()); } } void us_init(void) { init_vm(); us_set_cfunc("core:len", core_len, 1); us_set_cfunc("core:log", core_print, 1); } void us_deinit(void) { deinit_vm(); } void us_load_file(const char *file_path) { char *file = read_file(file_path, NULL); us_load_src(file); mem_free(file); } void us_load_src(const char *src) { struct us_proto *proto = compile("main", src); if (!proto) return; us_exec(create_func(proto)); } int us_declare_global(const char *name) { return declare_global(copy_str(name, -1)); } void us_set_cfunc(const char *c_name, us_cfunc_sig c, int argc) { struct us_str *name = copy_str(c_name, -1); int global = declare_global(name); set_global(global, wrap_cfunc(create_cfunc(name, c, argc))); }