catblocks: linguagem visual com plugins wasm
This commit is contained in:
commit
95d7e3f800
35 files changed
+2294
No files matched your search
@@ -0,0 +1,35 @@
|
||||
/* prova: blocos .wasm carregam e assinam BlockLike/Renderable. */
|
||||
#include "catblocks/plugin.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(void) {
|
||||
const char *blocks[] = {
|
||||
"add","subtract","multiply","divide","and","or","not",
|
||||
"if","else","while","for","when_start"
|
||||
};
|
||||
int n = sizeof(blocks)/sizeof(blocks[0]), fail = 0;
|
||||
printf("%-12s %-8s %-8s %-9s %-5s %s\n", "bloco","BlockLike","Render","cor","cat","nome");
|
||||
for (int i = 0; i < n; i++) {
|
||||
char path[128]; snprintf(path, sizeof path, "build/plugins/blocks/%s.wasm", blocks[i]);
|
||||
char err[256];
|
||||
cb_plugin *p = cb_plugin_load_file(path, err, sizeof err);
|
||||
if (!p) { printf("FAIL %s: %s\n", blocks[i], err); fail = 1; continue; }
|
||||
int bl = cb_plugin_has_blocklike(p);
|
||||
int rd = cb_plugin_has_renderable(p);
|
||||
int col = cb_plugin_export_i32(p, "block_color", 0);
|
||||
int cat = cb_plugin_export_i32(p, "block_cat", 0);
|
||||
int nl = cb_plugin_export_i32(p, "block_name", 0);
|
||||
char name[64] = "?";
|
||||
if (nl > 0 && nl < 63) {
|
||||
int oa = 0; uint32_t msz = 0;
|
||||
char *m = cb_plugin_outbuf(p, &oa, &msz);
|
||||
if (m && oa >= 0 && oa + nl <= (int)msz) { memcpy(name, m + oa, nl); name[nl] = 0; }
|
||||
}
|
||||
printf("%-12s %-8d %-8d 0x%06X %-5d %s\n", blocks[i], bl, rd, col, cat, name);
|
||||
if (!bl) { printf(" AVISO: %s sem BlockLike\n", blocks[i]); fail = 1; }
|
||||
cb_plugin_free(p);
|
||||
}
|
||||
printf(fail ? "BLOCKS: FALHOU\n" : "BLOCKS: OK\n");
|
||||
return fail;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/* prova ponta-a-ponta: interpreta o IR, gera .wat e .c, compila o .c com gcc. */
|
||||
#include "catblocks/ir.h"
|
||||
#include "catblocks/programs.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char *cb_interp_run(cb_node *root, int nvars, char **out, size_t *outlen);
|
||||
|
||||
int main(void) {
|
||||
cb_node *root = cb_program_sum();
|
||||
|
||||
/* 1) interpretar */
|
||||
char *out = NULL; size_t olen = 0;
|
||||
cb_interp_run(root, 4, &out, &olen);
|
||||
printf("== interpretado ==\n%s", out ? out : "(vazio)");
|
||||
int interp_ok = out && strstr(out, "5050") != NULL;
|
||||
|
||||
/* 2) gerar .wat e .c */
|
||||
cb_str wat, c;
|
||||
cb_str_init(&wat); cb_emit_wat(root, &wat);
|
||||
cb_str_init(&c); cb_emit_c(root, &c);
|
||||
FILE *f = fopen("build/test_out.wat", "w"); fputs(wat.buf, f); fclose(f);
|
||||
f = fopen("build/test_out.c", "w"); fputs(c.buf, f); fclose(f);
|
||||
printf("== wat (%zu bytes) e c (%zu bytes) escritos ==\n", wat.len, c.len);
|
||||
|
||||
cb_str_free(&wat); cb_str_free(&c);
|
||||
free(out);
|
||||
cb_node_free_all(NULL);
|
||||
printf(interp_ok ? "IR: OK\n" : "IR: FALHOU\n");
|
||||
return interp_ok ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/* prova: plugins .wasm compiladores produzem wat/c/wasm a partir do IR serializado. */
|
||||
#include "catblocks/plugin.h"
|
||||
#include "catblocks/ir.h"
|
||||
#include "catblocks/programs.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char *cb_interp_run(cb_node *root, int nvars, char **out, size_t *outlen);
|
||||
|
||||
/* acumula a saida do "print" do wasm gerado */
|
||||
static char g_print[4096]; static size_t g_plen;
|
||||
static int host_print(wasm_inst *in, const wval *args, wval *ret, void *ud) {
|
||||
(void)in; (void)ret; (void)ud;
|
||||
char b[32]; int n = snprintf(b, sizeof b, "%d\n", (int)(int32_t)args[0]);
|
||||
if (g_plen + n < sizeof g_print) { memcpy(g_print+g_plen, b, n); g_plen += n; g_print[g_plen]=0; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
cb_node *root = cb_program_sum();
|
||||
cb_str ir; cb_str_init(&ir); cb_ir_serialize(root, &ir);
|
||||
printf("== IR serializado (%zu bytes) ==\n%s", ir.len, ir.buf);
|
||||
|
||||
char err[256];
|
||||
/* 1) plugin wat */
|
||||
cb_plugin *pw = cb_plugin_load_file("build/plugins/compiler_wat.wasm", err, sizeof err);
|
||||
if (!pw) { fprintf(stderr, "wat: %s\n", err); return 1; }
|
||||
size_t n; char *wat = cb_plugin_run(pw, ir.buf, ir.len, &n);
|
||||
printf("== WAT (%zu bytes) ==\n%s", n, wat ? wat : "(erro)");
|
||||
FILE *f = fopen("build/plugin_out.wat","w"); fputs(wat,f); fclose(f);
|
||||
free(wat); cb_plugin_free(pw);
|
||||
|
||||
/* 2) plugin c */
|
||||
cb_plugin *pc = cb_plugin_load_file("build/plugins/compiler_c.wasm", err, sizeof err);
|
||||
if (!pc) { fprintf(stderr, "c: %s\n", err); return 1; }
|
||||
char *c = cb_plugin_run(pc, ir.buf, ir.len, &n);
|
||||
printf("== C (%zu bytes) ==\n%s", n, c ? c : "(erro)");
|
||||
f = fopen("build/plugin_out.c","w"); fputs(c,f); fclose(f);
|
||||
free(c); cb_plugin_free(pc);
|
||||
|
||||
/* 3) plugin wasm -> binario; corre-o no interpretador com o print do host */
|
||||
cb_plugin *pwm = cb_plugin_load_file("build/plugins/compiler_wasm.wasm", err, sizeof err);
|
||||
if (!pwm) { fprintf(stderr, "wasm: %s\n", err); return 1; }
|
||||
char *wasmb = cb_plugin_run(pwm, ir.buf, ir.len, &n);
|
||||
printf("== WASM gerado: %zu bytes ==\n", n);
|
||||
f = fopen("build/plugin_out.wasm","wb"); fwrite(wasmb,1,n,f); fclose(f);
|
||||
|
||||
char e2[256];
|
||||
wasm_module *m = wasm_load((uint8_t*)wasmb, n, e2, sizeof e2);
|
||||
if (!m) { fprintf(stderr, "load gerado: %s\n", e2); return 1; }
|
||||
wasm_import_def imp[1] = { {"catblocks","print",host_print,NULL} };
|
||||
wasm_inst *in = wasm_instantiate(m, imp, 1, 4, e2, sizeof e2);
|
||||
if (!in) { fprintf(stderr, "inst gerado: %s\n", e2); return 1; }
|
||||
wval r = 0;
|
||||
if (wasm_call(in, "main", NULL, 0, &r)) { fprintf(stderr, "run gerado: %s\n", wasm_error(in)); return 1; }
|
||||
printf("== saida do wasm gerado ==\n%s", g_print);
|
||||
int ok = strstr(g_print, "5050") != NULL;
|
||||
free(wasmb); cb_plugin_free(pwm);
|
||||
wasm_free(in); wasm_module_free(m);
|
||||
cb_str_free(&ir); cb_node_free_all(NULL);
|
||||
printf(ok ? "PLUGINS: OK\n" : "PLUGINS: FALHOU\n");
|
||||
return ok ? 0 : 1;
|
||||
}
|
||||
Executable
+46
@@ -0,0 +1,46 @@
|
||||
#!/bin/sh
|
||||
# catblocks - prova ponta-a-ponta: interpretar + gerar .wat + gerar .c +
|
||||
# compilar o .c com gcc e correr + gerar .wasm e correr no interpretador.
|
||||
set -e
|
||||
cd "$(dirname "$0")/.."
|
||||
BIN=build/linux/x86_64/bin/catblocks
|
||||
TMP=$(mktemp -d)
|
||||
trap 'rm -rf "$TMP"' EXIT
|
||||
|
||||
echo "== 1) interpretador WASM (smoke) =="
|
||||
NDK=${NDK:-/home/ovelha/android-sdk/ndk/android-ndk-r26d}
|
||||
export PATH=/home/ovelha/wasmbin:$PATH
|
||||
"$NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/clang" --target=wasm32 -nostdlib -O2 \
|
||||
-Wl,--no-entry -Wl,--export-all -o "$TMP/demo.wasm" tests/wasm_demo.c
|
||||
gcc -std=c11 -O2 -Iinclude src/catblocks/wasm.c tests/wasm_smoke.c -o "$TMP/wasm_smoke"
|
||||
"$TMP/wasm_smoke" "$TMP/demo.wasm"
|
||||
|
||||
echo "== 2) interpretador de IR + emissores internos =="
|
||||
gcc -std=c11 -O2 -Iinclude src/catblocks/ir.c src/catblocks/interp.c src/catblocks/programs.c \
|
||||
tests/ir_test.c -o "$TMP/ir_test"
|
||||
"$TMP/ir_test"
|
||||
|
||||
echo "== 3) plugins .wasm (IR->wat/c/wasm) =="
|
||||
gcc -std=c11 -O2 -Iinclude src/catblocks/ir.c src/catblocks/wasm.c src/catblocks/plugin.c \
|
||||
src/catblocks/programs.c tests/plugins_test.c -o "$TMP/plugins_test"
|
||||
"$TMP/plugins_test"
|
||||
|
||||
echo "== 4) blocos .wasm (BlockLike/Renderable) =="
|
||||
gcc -std=c11 -O2 -Iinclude src/catblocks/wasm.c src/catblocks/plugin.c tests/blocks_test.c \
|
||||
-o "$TMP/blocks_test"
|
||||
"$TMP/blocks_test"
|
||||
|
||||
echo "== 5) CLI: run / ir / wat / c / wasm / blocks =="
|
||||
"$BIN" run
|
||||
"$BIN" wat
|
||||
"$BIN" c
|
||||
"$BIN" wasm
|
||||
"$BIN" blocks
|
||||
|
||||
echo "== 6) compilar o .c gerado com gcc e correr =="
|
||||
gcc build/catblocks_out.c -o "$TMP/gen_c"
|
||||
OUT=$("$TMP/gen_c")
|
||||
echo "saida C gerada: $OUT"
|
||||
[ "$OUT" = "5050" ] || { echo "FALHOU: esperado 5050"; exit 1; }
|
||||
|
||||
echo "== TUDO OK =="
|
||||
@@ -0,0 +1,13 @@
|
||||
/* modulo de exemplo compilado para wasm32 (sem libc) */
|
||||
__attribute__((visibility("default"))) int add(int a, int b) { return a + b; }
|
||||
__attribute__((visibility("default"))) int sum_to(int n) {
|
||||
int s = 0;
|
||||
for (int i = 1; i <= n; i++) s += i;
|
||||
return s;
|
||||
}
|
||||
__attribute__((visibility("default"))) int fact(int n) { return n <= 1 ? 1 : n * fact(n - 1); }
|
||||
__attribute__((visibility("default"))) int fib(int n) {
|
||||
int a = 0, b = 1;
|
||||
for (int i = 0; i < n; i++) { int t = a + b; a = b; b = t; }
|
||||
return a;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/* prova que o interpretador carrega e chama exports de um .wasm real */
|
||||
#include "catblocks/wasm.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static int chk(wasm_inst *in, const char *name, int nargs, int a0, int a1, int want) {
|
||||
wval args[2], r = 0;
|
||||
args[0] = wv_i32(a0); args[1] = wv_i32(a1);
|
||||
if (wasm_call(in, name, args, nargs, &r)) {
|
||||
printf("FAIL %s: %s\n", name, wasm_error(in)); return 1;
|
||||
}
|
||||
int got = (int)(int32_t)r;
|
||||
printf("%-8s(%-4d%s) = %-6d esperado %-6d %s\n", name, a0,
|
||||
nargs==2 ? ",2" : "", got, want, got==want ? "OK" : "FAIL");
|
||||
return got != want;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 2) { fprintf(stderr, "uso: %s arquivo.wasm\n", argv[0]); return 2; }
|
||||
FILE *f = fopen(argv[1], "rb");
|
||||
if (!f) { perror("fopen"); return 1; }
|
||||
fseek(f, 0, SEEK_END); long n = ftell(f); fseek(f, 0, SEEK_SET);
|
||||
uint8_t *buf = malloc(n);
|
||||
if (fread(buf, 1, n, f) != (size_t)n) { fprintf(stderr, "read falhou\n"); return 1; }
|
||||
fclose(f);
|
||||
char err[256];
|
||||
wasm_module *m = wasm_load(buf, n, err, sizeof(err));
|
||||
if (!m) { fprintf(stderr, "load: %s\n", err); return 1; }
|
||||
printf("imports=%d\n", wasm_import_count(m));
|
||||
wasm_inst *in = wasm_instantiate(m, NULL, 0, 16, err, sizeof(err));
|
||||
if (!in) { fprintf(stderr, "instantiate: %s\n", err); return 1; }
|
||||
int fail = 0;
|
||||
fail |= chk(in, "add", 2, 40, 2, 42);
|
||||
fail |= chk(in, "sum_to", 1, 10, 0, 55);
|
||||
fail |= chk(in, "sum_to", 1, 100, 0, 5050);
|
||||
fail |= chk(in, "fact", 1, 5, 0, 120);
|
||||
fail |= chk(in, "fact", 1, 7, 0, 5040);
|
||||
fail |= chk(in, "fib", 1, 10, 0, 55);
|
||||
fail |= chk(in, "fib", 1, 12, 0, 144);
|
||||
wasm_free(in); wasm_module_free(m); free(buf);
|
||||
printf(fail ? "SMOKE: FALHOU\n" : "SMOKE: OK\n");
|
||||
return fail;
|
||||
}
|
||||
Reference in new issue
Block a user