/* * cat/stdlib/stdlib.c -- standard library native functions. * * Naming rule: symbols here are cat_stdlib_stdlib_*. * * Native signature: int cb(vm, args, nargs, out). Installed names: * print, tostring, tonumber, type, len, toml_parse, toml_encode. */ #include "cat/stdlib/stdlib.h" #include "cat/stdlib/toml.h" #include "cat/compiler.h" #include "cat/bytecode.h" #include "cat/types/nil.h" #include "cat/types/bool.h" #include "cat/types/number.h" #include "cat/types/int.h" #include "cat/types/float.h" #include "cat/types/text.h" #include "cat/types/ascii_text.h" #include "cat/types/table.h" #include "cat/types/native.h" #include "cat/types/file.h" #include #include #include /* toString for any value (heap string, caller does not free). */ static const char *value_to_text(CAT_VALUE_Value *v) { static char buf[512]; if (v == NULL || v == cat_types_nil_get()) return "nil"; if (v->vtable && v->vtable->kind == CAT_VALUE_KIND_TEXT) return cat_types_text_bytes(v); if (v->vtable && v->vtable->kind == CAT_VALUE_KIND_NUMBER) { if (((CAT_TYPES_NUMBER_Number *)v)->is_float) snprintf(buf, sizeof(buf), "%g", cat_types_number_as_double(v)); else snprintf(buf, sizeof(buf), "%lld", cat_types_number_as_int(v)); return buf; } if (v->vtable && v->vtable->kind == CAT_VALUE_KIND_BOOL) return ((CAT_TYPES_BOOL_Bool *)v)->v ? "true" : "false"; snprintf(buf, sizeof(buf), "<%s>", cat_value_type_name(v)); return buf; } static int lf_print(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { int i; (void)vm; for (i = 0; i < nargs; i++) { if (i) putchar(' '); fputs(value_to_text(args[i]), stdout); } putchar('\n'); *out = cat_types_nil_get(); return 0; } static int lf_tostring(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { (void)vm; *out = cat_types_ascii_text_new(nargs > 0 ? value_to_text(args[0]) : ""); return 0; } static int lf_tonumber(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { const char *s; char *end; (void)vm; if (nargs < 1) { *out = cat_types_nil_get(); return 0; } if (args[0]->vtable && args[0]->vtable->kind == CAT_VALUE_KIND_NUMBER) { *out = args[0]; return 0; } s = cat_types_text_bytes(args[0]); if (strchr(s, '.') != NULL) { double d = strtod(s, &end); *out = (*end == '\0') ? cat_types_float_new(d) : cat_types_nil_get(); } else { long long n = strtoll(s, &end, 10); *out = (*end == '\0') ? cat_types_int_new(n) : cat_types_nil_get(); } return 0; } static int lf_type(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { (void)vm; *out = cat_types_ascii_text_new(nargs > 0 ? cat_value_type_name(args[0]) : "nil"); return 0; } static int lf_len(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { (void)vm; if (nargs < 1) { *out = cat_types_int_new(0); return 0; } if (args[0]->vtable && args[0]->vtable->kind == CAT_VALUE_KIND_TEXT) *out = cat_types_int_new(cat_types_text_length(args[0])); else if (args[0]->vtable && args[0]->vtable->kind == CAT_VALUE_KIND_TABLE) *out = cat_types_int_new(cat_types_table_count(args[0])); else *out = cat_types_int_new(0); return 0; } static int lf_toml_parse(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { CAT_VALUE_Value *tbl = NULL; CAT_ERROR_Error e; (void)vm; cat_error_clear(&e); if (nargs < 1) { *out = cat_types_nil_get(); return 0; } if (cat_stdlib_toml_parse(cat_types_text_bytes(args[0]), &tbl, &e) != 0) { fprintf(stderr, "toml_parse: %s\n", e.message); *out = cat_types_nil_get(); return 0; } *out = tbl; return 0; } static int lf_toml_encode(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { CAT_ERROR_Error e; char *s; (void)vm; cat_error_clear(&e); if (nargs < 1) { *out = cat_types_ascii_text_new(""); return 0; } s = cat_stdlib_toml_encode(args[0], &e); *out = cat_types_ascii_text_new(s ? s : ""); free(s); return 0; } static int lf_read_file(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { FILE *f; long sz; char *buf = NULL; char *path; (void)vm; if (nargs < 1) { *out = cat_types_nil_get(); return 0; } path = (char *)cat_types_text_bytes(args[0]); f = fopen(path, "rb"); if (f == NULL) { *out = cat_types_nil_get(); return 0; } fseek(f, 0, SEEK_END); sz = ftell(f); fseek(f, 0, SEEK_SET); buf = malloc((size_t)sz + 1); if (buf == NULL) { fclose(f); *out = cat_types_nil_get(); return 0; } if (fread(buf, 1, (size_t)sz, f) != (size_t)sz) { /* short read ok */ } buf[sz] = '\0'; fclose(f); *out = cat_types_ascii_text_new(buf); free(buf); return 0; } static int lf_write_file(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { FILE *f; (void)vm; if (nargs < 2) { *out = cat_types_nil_get(); return 0; } f = fopen(cat_types_text_bytes(args[0]), "wb"); if (f != NULL) { const char *s = cat_types_text_bytes(args[1]); fwrite(s, 1, strlen(s), f); fclose(f); } *out = cat_types_nil_get(); return 0; } static int lf_ord(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { const char *s; long long i; (void)vm; if (nargs < 2) { *out = cat_types_int_new(-1); return 0; } s = cat_types_text_bytes(args[0]); i = cat_types_number_as_int(args[1]); if (i < 0 || i >= (long long)strlen(s)) *out = cat_types_int_new(-1); else *out = cat_types_int_new((unsigned char)s[i]); return 0; } static int lf_chr(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { char buf[2]; (void)vm; buf[0] = (char)(nargs > 0 ? cat_types_number_as_int(args[0]) : 0); buf[1] = '\0'; *out = cat_types_ascii_text_new(buf); return 0; } static int lf_substr(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { const char *s; long long i, n; char *buf; (void)vm; if (nargs < 3) { *out = cat_types_ascii_text_new(""); return 0; } s = cat_types_text_bytes(args[0]); i = cat_types_number_as_int(args[1]); n = cat_types_number_as_int(args[2]); if (i < 0 || i > (long long)strlen(s) || n < 0) { *out = cat_types_ascii_text_new(""); return 0; } buf = malloc((size_t)n + 1); if (buf == NULL) { *out = cat_types_ascii_text_new(""); return 0; } memcpy(buf, s + i, (size_t)n); buf[n] = '\0'; *out = cat_types_ascii_text_new(buf); free(buf); return 0; } static int lf_dofile(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { FILE *f; long sz; char *src; CAT_BYTECODE_Function *fn; CAT_ERROR_Error e; if (nargs < 1) { *out = cat_types_nil_get(); return 0; } f = fopen(cat_types_text_bytes(args[0]), "rb"); if (f == NULL) { *out = cat_types_nil_get(); return 0; } fseek(f, 0, SEEK_END); sz = ftell(f); fseek(f, 0, SEEK_SET); src = malloc((size_t)sz + 1); if (src == NULL) { fclose(f); *out = cat_types_nil_get(); return 0; } if (fread(src, 1, (size_t)sz, f) != (size_t)sz) { /* short read ok */ } src[sz] = '\0'; fclose(f); cat_error_clear(&e); fn = cat_compiler_compile(src, &e); free(src); if (fn == NULL) { fprintf(stderr, "dofile: %s\n", e.message); *out = cat_types_nil_get(); return 0; } cat_error_clear(&e); cat_vm_run(vm, fn, out); /* The compiled chunk is intentionally NOT freed: any `function` * definitions in it became globals whose closures point into this * bytecode tree. The chunk lives as long as the VM does. */ return 0; } /* `use expr` -- import another catscript source file (alias of dofile), * so `use "std/io/file.cat"` pulls its definitions into this unit. */ static int lf_use(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { return lf_dofile(vm, args, nargs, out); } /* `__class_new(cls, ...)` -- default constructor installed on every * class as `new`. Copies the class into a fresh instance and runs the * class's `init` method (if any) with the given arguments. */ static int lf_class_new(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { CAT_VALUE_Value *cls; CAT_VALUE_Value *inst; CAT_VALUE_Value *init = NULL; CAT_ERROR_Error e; int i; (void)vm; if (nargs < 1) { *out = cat_types_nil_get(); return 0; } cls = args[0]; inst = cat_types_table_new(); /* share the class's methods/layout with the instance */ cat_error_clear(&e); cat_types_table_set(inst, "__index", cls, &e); cat_error_clear(&e); cat_types_table_set(inst, "__class", cls, &e); /* run init(args...) if the class defines it */ cat_error_clear(&e); if (cat_value_get_attribute(cls, "init", &init, &e) == 0 && init != NULL) { CAT_VALUE_Value *r = NULL; CAT_VALUE_Value *callargs[258]; callargs[0] = inst; for (i = 1; i < nargs && i < 257; i++) callargs[i] = args[i]; cat_error_clear(&e); cat_value_call(init, callargs, nargs, &r, &e); } *out = inst; return 0; } static int lf_abstractfile(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { /* AbstractFile cannot be instantiated: it is the abstract base of the * stream family (File/StringIO/BytesIO). */ (void)vm; (void)args; (void)nargs; fprintf(stderr, "AbstractFile is abstract; use File::open, " "StringIO or BytesIO\n"); *out = cat_types_nil_get(); return 0; } static int lf_stringio(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { (void)vm; *out = cat_types_file_stringio(nargs > 0 ? cat_types_text_bytes(args[0]) : ""); return 0; } static int lf_bytesio(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { (void)vm; (void)args; (void)nargs; *out = cat_types_file_bytesio(); return 0; } int cat_stdlib_stdlib_install(CAT_VM_State *vm) { cat_vm_register_native(vm, "print", lf_print); cat_vm_register_native(vm, "tostring", lf_tostring); cat_vm_register_native(vm, "tonumber", lf_tonumber); cat_vm_register_native(vm, "type", lf_type); cat_vm_register_native(vm, "len", lf_len); cat_vm_register_native(vm, "toml_parse", lf_toml_parse); cat_vm_register_native(vm, "toml_encode", lf_toml_encode); cat_vm_register_native(vm, "read_file", lf_read_file); cat_vm_register_native(vm, "write_file", lf_write_file); cat_vm_register_native(vm, "ord", lf_ord); cat_vm_register_native(vm, "chr", lf_chr); cat_vm_register_native(vm, "substr", lf_substr); cat_vm_register_native(vm, "dofile", lf_dofile); cat_vm_register_native(vm, "use", lf_use); cat_vm_register_native(vm, "__class_new", lf_class_new); /* the file family: AbstractFile (abstract base), File, StringIO, BytesIO */ cat_vm_set_global(vm, "AbstractFile", cat_types_native_new("AbstractFile", lf_abstractfile)); cat_vm_set_global(vm, "File", cat_types_file_class_table()); cat_vm_set_global(vm, "StringIO", cat_types_native_new("StringIO", lf_stringio)); cat_vm_set_global(vm, "BytesIO", cat_types_native_new("BytesIO", lf_bytesio)); return 0; }