MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rename cprintf->crprintf

+101 -101
-47
include/cli/cprintf.h
··· 1 - #ifndef CPRINTF_H 2 - #define CPRINTF_H 3 - 4 - #include <stdio.h> 5 - #include <stddef.h> 6 - #include <stdbool.h> 7 - 8 - extern bool io_no_color; 9 - extern bool cprintf_debug; 10 - extern bool cprintf_debug_hex; 11 - 12 - typedef struct program_t program_t; 13 - program_t *cprintf_compile(const char *fmt); 14 - 15 - void cprintf_var(const char *name, const char *value); 16 - void cprintf_hexdump(program_t *prog, FILE *out); 17 - void cprintf_disasm(program_t *prog, FILE *out); 18 - 19 - int cprintf_exec(program_t *prog, FILE *stream, ...); 20 - int csprintf_inner(program_t *prog, char *buf, size_t size, ...); 21 - 22 - #define _CPRINTF_INIT(prog, fmt) \ 23 - if (!prog) { \ 24 - prog = cprintf_compile(fmt); \ 25 - if (cprintf_debug) cprintf_disasm(prog, stderr); \ 26 - if (cprintf_debug_hex) cprintf_hexdump(prog, stderr); \ 27 - } 28 - 29 - #define cprintf(fmt, ...) ({ \ 30 - static program_t *_cp_prog_ = NULL; \ 31 - _CPRINTF_INIT(_cp_prog_, fmt); \ 32 - cprintf_exec(_cp_prog_, stdout, ##__VA_ARGS__); \ 33 - }) 34 - 35 - #define cfprintf(stream, fmt, ...) ({ \ 36 - static program_t *_cp_prog_ = NULL; \ 37 - _CPRINTF_INIT(_cp_prog_, fmt); \ 38 - cprintf_exec(_cp_prog_, stream, ##__VA_ARGS__); \ 39 - }) 40 - 41 - #define csprintf(buf, size, fmt, ...) ({ \ 42 - static program_t *_cp_prog_ = NULL; \ 43 - _CPRINTF_INIT(_cp_prog_, fmt); \ 44 - csprintf_inner(_cp_prog_, buf, size, ##__VA_ARGS__); \ 45 - }) 46 - 47 - #endif
+47
include/cli/crprintf.h
··· 1 + #ifndef CRPRINTF_H 2 + #define CRPRINTF_H 3 + 4 + #include <stdio.h> 5 + #include <stddef.h> 6 + #include <stdbool.h> 7 + 8 + extern bool io_no_color; 9 + extern bool crprintf_debug; 10 + extern bool crprintf_debug_hex; 11 + 12 + typedef struct program_t program_t; 13 + program_t *crprintf_compile(const char *fmt); 14 + 15 + void crprintf_var(const char *name, const char *value); 16 + void crprintf_hexdump(program_t *prog, FILE *out); 17 + void crprintf_disasm(program_t *prog, FILE *out); 18 + 19 + int crprintf_exec(program_t *prog, FILE *stream, ...); 20 + int crsprintf_inner(program_t *prog, char *buf, size_t size, ...); 21 + 22 + #define _CRPRINTF_INIT(prog, fmt) \ 23 + if (!prog) { \ 24 + prog = crprintf_compile(fmt); \ 25 + if (crprintf_debug) crprintf_disasm(prog, stderr); \ 26 + if (crprintf_debug_hex) crprintf_hexdump(prog, stderr); \ 27 + } 28 + 29 + #define crprintf(fmt, ...) ({ \ 30 + static program_t *_cp_prog_ = NULL; \ 31 + _CRPRINTF_INIT(_cp_prog_, fmt); \ 32 + crprintf_exec(_cp_prog_, stdout, ##__VA_ARGS__); \ 33 + }) 34 + 35 + #define crfprintf(stream, fmt, ...) ({ \ 36 + static program_t *_cp_prog_ = NULL; \ 37 + _CRPRINTF_INIT(_cp_prog_, fmt); \ 38 + crprintf_exec(_cp_prog_, stream, ##__VA_ARGS__); \ 39 + }) 40 + 41 + #define crsprintf(buf, size, fmt, ...) ({ \ 42 + static program_t *_cp_prog_ = NULL; \ 43 + _CRPRINTF_INIT(_cp_prog_, fmt); \ 44 + crsprintf_inner(_cp_prog_, buf, size, ##__VA_ARGS__); \ 45 + }) 46 + 47 + #endif
+1 -1
libant/meson.build
··· 36 36 '../src/runtime.c', 37 37 '../src/snapshot.c', 38 38 '../src/esm/remote.c', 39 - '../src/cli/cprintf.c', 39 + '../src/cli/crprintf.c', 40 40 ) + files(module_files) 41 41 42 42 include = include_directories('../include')
+1 -1
meson.build
··· 38 38 'src/cli/pkg.c', 39 39 'src/cli/misc.c', 40 40 'src/cli/version.c', 41 - 'src/cli/cprintf.c', 41 + 'src/cli/crprintf.c', 42 42 ) + files(module_files) 43 43 44 44 include = include_directories('include')
+27 -27
src/cli/cprintf.c src/cli/crprintf.c
··· 1 1 /* 2 - * cprintf - printf with inline color tags, powered by a register-based VM 2 + * crprintf - printf with inline color tags, powered by a register-based VM 3 3 * 4 4 * usage: 5 - * cprintf("<red>error:</red> something went wrong\n"); 6 - * cprintf("<bold><cyan>info:</cyan></bold> hello %s\n", name); 7 - * cprintf("<#ff8800>orange text</#ff8800>\n"); 8 - * cprintf(" <pad=18><green>%s</green></pad> %s\n", cmd->name, cmd->desc); 5 + * crprintf("<red>error:</red> something went wrong\n"); 6 + * crprintf("<bold><cyan>info:</cyan></bold> hello %s\n", name); 7 + * crprintf("<#ff8800>orange text</#ff8800>\n"); 8 + * crprintf(" <pad=18><green>%s</green></pad> %s\n", cmd->name, cmd->desc); 9 9 * 10 10 * supported tags: 11 11 * <red> <green> <yellow> <blue> <magenta> <cyan> <white> <black> ··· 34 34 */ 35 35 36 36 #include "utils.h" 37 - #include "cli/cprintf.h" 37 + #include "cli/crprintf.h" 38 38 39 39 #include <ctype.h> 40 40 #include <stdio.h> ··· 44 44 #include <stdint.h> 45 45 #include <wchar.h> 46 46 47 - bool cprintf_debug = false; 48 - bool cprintf_debug_hex = false; 47 + bool crprintf_debug = false; 48 + bool crprintf_debug_hex = false; 49 49 50 50 typedef enum { 51 51 OP_NOP = 0, ··· 133 133 char name[MAX_VAR_NAME]; 134 134 char value[MAX_VAR_VALUE]; 135 135 int nlen; int vlen; int is_fmt; 136 - } cprintf_var_t; 136 + } crprintf_var_t; 137 137 138 138 typedef struct { 139 - cprintf_var_t vars[MAX_VARS]; 139 + crprintf_var_t vars[MAX_VARS]; 140 140 int count; 141 141 } var_table_t; 142 142 ··· 372 372 if (nlen <= 0 || nlen >= MAX_VAR_NAME || vlen < 0 || vlen >= MAX_VAR_VALUE) return 0; 373 373 if (vars->count >= MAX_VARS) return 0; 374 374 375 - cprintf_var_t *v = &vars->vars[vars->count++]; 375 + crprintf_var_t *v = &vars->vars[vars->count++]; 376 376 memcpy(v->name, p, nlen); 377 377 378 378 v->name[nlen] = '\0'; v->nlen = nlen; ··· 396 396 if (nlen <= 0 || nlen >= MAX_VAR_NAME || vlen <= 0 || vlen >= MAX_VAR_VALUE) return 0; 397 397 if (vars->count >= MAX_VARS) return 0; 398 398 399 - cprintf_var_t *v = &vars->vars[vars->count++]; 399 + crprintf_var_t *v = &vars->vars[vars->count++]; 400 400 memcpy(v->name, p, nlen); 401 401 v->name[nlen] = '\0'; v->nlen = nlen; 402 402 ··· 408 408 return 1; 409 409 } 410 410 411 - void cprintf_var(const char *name, const char *value) { 411 + void crprintf_var(const char *name, const char *value) { 412 412 if (global_vars.count >= MAX_VARS) return; 413 413 int nlen = (int)strlen(name); 414 414 int vlen = (int)strlen(value); 415 415 if (nlen <= 0 || nlen >= MAX_VAR_NAME || vlen <= 0 || vlen >= MAX_VAR_VALUE) return; 416 416 417 417 for (int i = 0; i < global_vars.count; i++) { 418 - cprintf_var_t *v = &global_vars.vars[i]; 418 + crprintf_var_t *v = &global_vars.vars[i]; 419 419 if (v->nlen == nlen && memcmp(v->name, name, nlen) == 0) { 420 420 memcpy(v->value, value, vlen); 421 421 v->value[vlen] = '\0'; v->vlen = vlen; return; 422 422 } 423 423 } 424 424 425 - cprintf_var_t *v = &global_vars.vars[global_vars.count++]; 425 + crprintf_var_t *v = &global_vars.vars[global_vars.count++]; 426 426 memcpy(v->name, name, nlen); 427 427 v->name[nlen] = '\0'; v->nlen = nlen; 428 428 ··· 438 438 int var_nlen = plus ? (int)(plus - name) : nlen; 439 439 440 440 for (int i = 0; i < vars->count; i++) { 441 - cprintf_var_t *v = &vars->vars[i]; 441 + crprintf_var_t *v = &vars->vars[i]; 442 442 if (var_nlen != v->nlen || memcmp(name, v->name, var_nlen) != 0) continue; 443 443 444 444 emit_op(p, OP_STYLE_PUSH, 0); ··· 744 744 } 745 745 746 746 for (int i = 0; i < vars->count; i++) { 747 - cprintf_var_t *v = &vars->vars[i]; 747 + crprintf_var_t *v = &vars->vars[i]; 748 748 if (nlen != v->nlen || memcmp(name, v->name, nlen) != 0) continue; 749 749 750 750 const char *val = v->value; ··· 781 781 return *lit; 782 782 } 783 783 784 - program_t *cprintf_compile(const char *fmt) { 784 + program_t *crprintf_compile(const char *fmt) { 785 785 program_t *p = program_new(); 786 786 var_table_t vars = global_vars; 787 787 ··· 804 804 size_t len; 805 805 } vm_output_t; 806 806 807 - static vm_output_t cprintf_vm_run(program_t *prog, va_list ap) { 807 + static vm_output_t crprintf_vm_run(program_t *prog, va_list ap) { 808 808 vm_regs_t regs = {0}; 809 809 810 810 size_t cap = 512, pos = 0; ··· 1061 1061 #undef OUT_CSTR 1062 1062 } 1063 1063 1064 - int cprintf_exec(program_t *prog, FILE *stream, ...) { 1064 + int crprintf_exec(program_t *prog, FILE *stream, ...) { 1065 1065 va_list ap; va_start(ap, stream); 1066 - vm_output_t o = cprintf_vm_run(prog, ap); 1066 + vm_output_t o = crprintf_vm_run(prog, ap); 1067 1067 va_end(ap); if (!o.data) return -1; 1068 1068 1069 1069 int ret = (int)fwrite(o.data, 1, o.len, stream); ··· 1072 1072 return ret; 1073 1073 } 1074 1074 1075 - int csprintf_inner(program_t *prog, char *buf, size_t size, ...) { 1075 + int crsprintf_inner(program_t *prog, char *buf, size_t size, ...) { 1076 1076 va_list ap; va_start(ap, size); 1077 - vm_output_t o = cprintf_vm_run(prog, ap); 1077 + vm_output_t o = crprintf_vm_run(prog, ap); 1078 1078 va_end(ap); if (!o.data) return -1; 1079 1079 1080 1080 size_t copy = (o.len < size) ? o.len : size - 1; ··· 1226 1226 } 1227 1227 } 1228 1228 1229 - void cprintf_disasm(program_t *prog, FILE *out) { 1230 - fprintf(out, "; cprintf bytecode โ€” %zu instructions, %zu bytes literal pool\n", prog->code_len, prog->lit_len); 1229 + void crprintf_disasm(program_t *prog, FILE *out) { 1230 + fprintf(out, "; crprintf bytecode โ€” %zu instructions, %zu bytes literal pool\n", prog->code_len, prog->lit_len); 1231 1231 fprintf(out, "; %-4s %-16s %s\n", "addr", "opcode", "operand"); 1232 1232 fprintf(out, "; ---- ---------------- -------\n"); 1233 1233 ··· 1241 1241 } 1242 1242 } 1243 1243 1244 - void cprintf_hexdump(program_t *prog, FILE *out) { 1245 - fprintf(out, "; cprintf hex dump โ€” %zu instructions, %zu bytes literal pool\n", prog->code_len, prog->lit_len); 1244 + void crprintf_hexdump(program_t *prog, FILE *out) { 1245 + fprintf(out, "; crprintf hex dump โ€” %zu instructions, %zu bytes literal pool\n", prog->code_len, prog->lit_len); 1246 1246 fprintf(out, "; %-4s %-26s %s\n", "addr", "bytes", "decoded"); 1247 1247 fprintf(out, "; ---- ------------------------- --------\n"); 1248 1248
+3 -3
src/cli/misc.c
··· 2 2 #include <argtable3.h> 3 3 4 4 #include "cli/misc.h" 5 - #include "cli/cprintf.h" 5 + #include "cli/crprintf.h" 6 6 7 7 void print_flag(FILE *fp, flag_help_t f) { 8 8 const char *s = f.s, *l = f.l, *d = f.d, *g = f.g; 9 9 int opt = f.opt; 10 10 11 11 char syn[200]; 12 - csprintf(syn, sizeof(syn), 12 + crsprintf(syn, sizeof(syn), 13 13 "<cyan>%s</cyan>%s<cyan>%s%s%s%s</cyan>", 14 14 s ? (char[]){'-', s[0], '\0'} : (l ? " " : ""), 15 15 s && l ? ", " : "", ··· 20 20 d && s ? " " : "", 21 21 d ? d : ""); 22 22 23 - cfprintf(fp, "<space=2/><pad=32>%s</pad> %s</>\n", syn, g); 23 + crfprintf(fp, "<space=2/><pad=32>%s</pad> %s</>\n", syn, g); 24 24 } 25 25 26 26 void print_flags_help(FILE *fp, void **argtable) {
+20 -20
src/main.c
··· 22 22 #include "cli/pkg.h" 23 23 #include "cli/misc.h" 24 24 #include "cli/version.h" 25 - #include "cli/cprintf.h" 25 + #include "cli/crprintf.h" 26 26 27 27 #include "modules/builtin.h" 28 28 #include "modules/buffer.h" ··· 81 81 }; 82 82 83 83 static void parse_ant_debug(const char *flag) { 84 - if (strncmp(flag, "dump-cprintf=", 13) == 0) { 84 + if (strncmp(flag, "dump-crprintf=", 13) == 0) { 85 85 const char *mode = flag + 13; 86 - if (strcmp(mode, "bytecode") == 0 || strcmp(mode, "all") == 0) cprintf_debug = true; 87 - if (strcmp(mode, "hex") == 0 || strcmp(mode, "all") == 0) cprintf_debug_hex = true; 86 + if (strcmp(mode, "bytecode") == 0 || strcmp(mode, "all") == 0) crprintf_debug = true; 87 + if (strcmp(mode, "hex") == 0 || strcmp(mode, "all") == 0) crprintf_debug_hex = true; 88 88 } 89 89 90 - else cfprintf(stderr, "{warn}: <bold>Unknown ANT_DEBUG flag: \"%s\"</>\n", flag); 90 + else crfprintf(stderr, "{warn}: <bold>Unknown ANT_DEBUG flag: \"%s\"</>\n", flag); 91 91 } 92 92 93 93 static const subcommand_t *find_subcommand(const char *name) { ··· 99 99 } 100 100 101 101 static void print_subcommands(void) { 102 - cprintf("<bold>Commands:</>\n"); 102 + crprintf("<bold>Commands:</>\n"); 103 103 for (const subcommand_t *cmd = subcommands; cmd->name; cmd++) { 104 - cprintf(" <pad=18>%s</pad> %s\n", cmd->name, cmd->desc); 104 + crprintf(" <pad=18>%s</pad> %s\n", cmd->name, cmd->desc); 105 105 } 106 - cprintf("\n <pad=18><command> <bold+cyan>--help</></pad> Print help text for command.\n"); 106 + crprintf("\n <pad=18><command> <bold+cyan>--help</></pad> Print help text for command.\n"); 107 107 printf("\n"); 108 108 } 109 109 ··· 187 187 buffer = esm_fetch_url(filename, &len, &error); 188 188 189 189 if (!buffer) { 190 - cfprintf(stderr, "{error}: <bold>Could not fetch \"%s\"</>: %s\n", filename, error ? error : "unknown error"); 190 + crfprintf(stderr, "{error}: <bold>Could not fetch \"%s\"</>: %s\n", filename, error ? error : "unknown error"); 191 191 free(error); return EXIT_FAILURE; 192 192 } 193 193 ··· 195 195 } else { 196 196 buffer = read_file(filename, &len); 197 197 if (!buffer) { 198 - cfprintf(stderr, "{error}: <bold>Module not found: \"%s\"</>\n", filename); 198 + crfprintf(stderr, "{error}: <bold>Module not found: \"%s\"</>\n", filename); 199 199 return EXIT_FAILURE; 200 200 } 201 201 ··· 213 213 if (is_typescript_file(filename)) { 214 214 int result = OXC_strip_types(buffer, filename, buffer, len + 1); 215 215 if (result < 0) { 216 - cfprintf(stderr, "{error}: <bold>Type stripping failed (%d)</>\n", result); 216 + crfprintf(stderr, "{error}: <bold>Type stripping failed (%d)</>\n", result); 217 217 free(buffer); 218 218 return EXIT_FAILURE; 219 219 } ··· 243 243 const char *binary_name = strrchr(argv[0], '/'); 244 244 binary_name = binary_name ? binary_name + 1 : argv[0]; 245 245 246 - cprintf_var("version", ANT_VERSION); 247 - cprintf_var("fatal", "<bold+red>FATAL</bold>"); 248 - cprintf_var("error", "<red>Error</red>"); 249 - cprintf_var("warn", "<yellow>Warning</yellow>"); 246 + crprintf_var("version", ANT_VERSION); 247 + crprintf_var("fatal", "<bold+red>FATAL</bold>"); 248 + crprintf_var("error", "<red>Error</red>"); 249 + crprintf_var("warn", "<yellow>Warning</yellow>"); 250 250 251 251 if (strcmp(binary_name, "antx") == 0) { 252 252 char **exec_argv = try_oom(sizeof(char*) * (argc + 2)); ··· 306 306 int nerrors = arg_parse(argc, argv, argtable); 307 307 308 308 if (help->count > 0) { 309 - cprintf( 309 + crprintf( 310 310 "{let h=bold, arg=cyan, name='Ant'}" 311 311 "<$h+red>{name}</> is a tiny JavaScript runtime and package manager ({version})<br=2/>" 312 312 "<$h>Usage: {~name} <yellow>[module.js]</yellow> <$arg>[...flags]<reset/><br/>" ··· 315 315 ); 316 316 317 317 print_subcommands(); 318 - cprintf("<bold>Flags:</>\n"); 318 + crprintf("<bold>Flags:</>\n"); 319 319 320 320 print_flags_help(stdout, argtable); 321 321 print_flag(stdout, (flag_help_t){ .l = "verbose", .g = "enable verbose output" }); ··· 356 356 volatile char stack_base; 357 357 358 358 if (!(js = js_create_dynamic())) { 359 - cfprintf(stderr, "{fatal}: Failed to allocate for Ant.</>\n"); 359 + crfprintf(stderr, "{fatal}: Failed to allocate for Ant.</>\n"); 360 360 arg_freetable(argtable, ARGTABLE_COUNT); free(filtered_argv); 361 361 return EXIT_FAILURE; 362 362 } ··· 402 402 403 403 jsval_t snapshot_result = ant_load_snapshot(js); 404 404 if (vtype(snapshot_result) == T_ERR) { 405 - cfprintf(stderr, "{warn} <bold>Failed to load snapshot:</> %s\n", js_str(js, snapshot_result)); 405 + crfprintf(stderr, "{warn} <bold>Failed to load snapshot:</> %s\n", js_str(js, snapshot_result)); 406 406 } 407 407 408 408 if (eval->count > 0) { ··· 417 417 else if (has_stdin && file->count == 0) { 418 418 size_t len = 0; char *buf = read_stdin(&len); 419 419 if (!buf) { 420 - cfprintf(stderr, "{fatal}: Failed to allocate for Ant.</>\n"); 420 + crfprintf(stderr, "{fatal}: Failed to allocate for Ant.</>\n"); 421 421 js_result = EXIT_FAILURE; goto cleanup; 422 422 } 423 423 eval_code(js, buf, len, "[stdin]", print->count > 0); free(buf);
+2 -2
src/utils.c
··· 1 1 #include "utils.h" 2 - #include "cli/cprintf.h" 2 + #include "cli/crprintf.h" 3 3 4 4 #include <stdio.h> 5 5 #include <stdbool.h> ··· 89 89 void *try_oom(size_t size) { 90 90 void *p = malloc(size); 91 91 if (!p) { 92 - cfprintf(stderr, "<bold+red>FATAL</bold>: Out of memory\n"); 92 + crfprintf(stderr, "<bold+red>FATAL</bold>: Out of memory\n"); 93 93 exit(EXIT_FAILURE); 94 94 } return p; 95 95 }