/* * cat/stdlib/build.c -- bytecode builder primitives (see build.h). * * Naming rule: symbols here are cat_stdlib_build_* / CAT_STDLIB_BUILD_*. * * bc_run(chunk) -- build a CAT_BYTECODE_Function from a chunk table and * execute it, returning the chunk's return value. * bc_dump(chunk) -- print a disassembly of a chunk table. */ #include "cat/stdlib/build.h" #include "cat/bytecode.h" #include "cat/types/table.h" #include "cat/types/int.h" #include "cat/types/empty.h" #include "cat/types/ascii_text.h" #include "cat/error.h" #include /* Read an integer field from a table (0 if absent). */ static long long tbl_int(CAT_VALUE_Value *t, const char *key) { CAT_VALUE_Value *v = NULL; CAT_ERROR_Error e; cat_error_clear(&e); if (t == NULL || cat_types_table_get(t, key, &v, &e) != 0 || v == NULL) return 0; return cat_types_number_as_int(v); } static const char *tbl_str(CAT_VALUE_Value *t, const char *key) { CAT_VALUE_Value *v = NULL; CAT_ERROR_Error e; cat_error_clear(&e); if (t == NULL || cat_types_table_get(t, key, &v, &e) != 0 || v == NULL) return NULL; return cat_types_text_bytes(v); } /* Recursively convert a chunk table into a CAT_BYTECODE_Function. */ static CAT_BYTECODE_Function *build_function(CAT_VALUE_Value *chunk) { CAT_BYTECODE_Function *fn = cat_bytecode_function_new(tbl_str(chunk, "name")); CAT_VALUE_Value *arr = NULL; CAT_ERROR_Error e; int i, n; fn->nparams = (int)tbl_int(chunk, "nparams"); fn->nlocals = (int)tbl_int(chunk, "nlocals"); /* constants */ cat_error_clear(&e); if (cat_types_table_get(chunk, "consts", &arr, &e) == 0) { n = cat_types_table_count(arr); for (i = 0; i < n; i++) cat_bytecode_add_const(fn, cat_types_table_value_at(arr, i)); } /* nested prototypes */ cat_error_clear(&e); if (cat_types_table_get(chunk, "protos", &arr, &e) == 0) { n = cat_types_table_count(arr); for (i = 0; i < n; i++) cat_bytecode_add_proto(fn, build_function(cat_types_table_value_at(arr, i))); } /* instructions */ cat_error_clear(&e); if (cat_types_table_get(chunk, "code", &arr, &e) == 0) { n = cat_types_table_count(arr); for (i = 0; i < n; i++) { CAT_VALUE_Value *in = cat_types_table_value_at(arr, i); cat_bytecode_emit(fn, (unsigned char)tbl_int(in, "op"), (int)tbl_int(in, "a"), (int)tbl_int(in, "b"), (int)tbl_int(in, "c"), (int)tbl_int(in, "line")); } } return fn; } static int lf_bc_run(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { CAT_BYTECODE_Function *fn; CAT_ERROR_Error e; int rc; (void)vm; cat_error_clear(&e); if (nargs < 1 || !cat_types_table_count(args[0])) { /* not a chunk */ } fn = build_function(args[0]); if (fn == NULL) { *out = cat_types_empty(); return 0; } rc = cat_vm_run(vm, fn, out); cat_bytecode_function_free(fn); return rc; } static int lf_bc_dump(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { CAT_BYTECODE_Function *fn; (void)vm; if (nargs < 1) { *out = cat_types_empty(); return 0; } fn = build_function(args[0]); if (fn != NULL) { cat_bytecode_dump(fn, stdout); cat_bytecode_function_free(fn); } *out = cat_types_empty(); return 0; } int cat_stdlib_build_install(CAT_VM_State *vm) { cat_vm_register_native(vm, "bc_run", lf_bc_run); cat_vm_register_native(vm, "bc_dump", lf_bc_dump); return 0; }