diff --git a/include/tinyblocks/tb_block.h b/include/tinyblocks/tb_block.h index 868adf9..33ff9cf 100644 --- a/include/tinyblocks/tb_block.h +++ b/include/tinyblocks/tb_block.h @@ -73,6 +73,14 @@ TB_API bool tb_block_set_default(TinyBlocks_Block* target, uint32_t input_idx, /* Retorna o valor do input[idx]; resolve a conexão se necessário */ TB_API TinyBlocks_Value tb_block_get_input (TinyBlocks_Block* self, TinyBlocks* rt, uint32_t input_idx); + +/* Entradas ja convertidas (despacham pela classe do valor, tb_value_class.h). + * input_text: texto emprestado, valido ate a proxima chamada para o mesmo + * campo ou ate o bloco ser destruido ("" se vazio). */ +TB_API const char* tb_block_input_text (TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i); +TB_API double tb_block_input_number (TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i, double def); +TB_API int64_t tb_block_input_integer(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i, int64_t def); +TB_API bool tb_block_input_truthy (TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i); /* Escreve no output[idx] */ TB_API void tb_block_set_output(TinyBlocks_Block* self, uint32_t output_idx, TinyBlocks_Value v); diff --git a/include/tinyblocks/tb_defs.h b/include/tinyblocks/tb_defs.h index bd1175d..d6fa55b 100644 --- a/include/tinyblocks/tb_defs.h +++ b/include/tinyblocks/tb_defs.h @@ -97,7 +97,8 @@ struct TinyBlocks_VectorData { /* -- Descritor de slot (em TypeBlock - descreve a forma dos inputs/outputs) - */ /* Tracos de um slot (tb_type_block_set_input_flags / _output_flags) */ enum { - TB_SLOT_BOOLEAN = 1u << 0, /* entrada hexagonal / saida booleana */ + TB_SLOT_BOOLEAN = 1u << 0, /* entrada hexagonal / saida booleana (saida * + * com nome terminado em '?' ja nasce assim) */ TB_SLOT_VARIABLE = 1u << 1, /* campo com o NOME de uma variavel */ }; @@ -111,6 +112,7 @@ typedef struct TinyBlocks_Connection { TinyBlocks_Block* source; /* bloco de origem (NULL se não conectado) */ uint32_t output_idx; /* qual output slot da origem */ TinyBlocks_Value default_value; /* valor quando não há conexão */ + char* text_cache; /* tb_block_input_text de um não-texto */ } TinyBlocks_Connection; /* -- Alocador customizável --- */ diff --git a/libraries/tinyblocks-base64/src/tb_base64.c b/libraries/tinyblocks-base64/src/tb_base64.c index 137b352..769fb3a 100644 --- a/libraries/tinyblocks-base64/src/tb_base64.c +++ b/libraries/tinyblocks-base64/src/tb_base64.c @@ -90,20 +90,16 @@ char* tb_base64_decode_str(const char* s, size_t* out_len) { /* -- Blocos -------------------------------------------------------------- */ -static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : ""; -} static void eval_encode(TinyBlocks_Block* self, TinyBlocks* rt) { - char* s = tb_base64_encode_str(in_text(self, rt, 0)); + char* s = tb_base64_encode_str(tb_block_input_text(self, rt, 0)); tb_block_set_output(self, 0, tb_value_text(s ? s : "")); free(s); self->dirty = false; } static void eval_decode(TinyBlocks_Block* self, TinyBlocks* rt) { - char* s = tb_base64_decode_str(in_text(self, rt, 0), NULL); + char* s = tb_base64_decode_str(tb_block_input_text(self, rt, 0), NULL); tb_block_set_output(self, 0, tb_value_text(s ? s : "")); free(s); self->dirty = false; diff --git a/libraries/tinyblocks-bmp/src/tb_bmp.c b/libraries/tinyblocks-bmp/src/tb_bmp.c index 948a1cc..5d7ed90 100644 --- a/libraries/tinyblocks-bmp/src/tb_bmp.c +++ b/libraries/tinyblocks-bmp/src/tb_bmp.c @@ -123,17 +123,13 @@ bool tb_bmp_save(const TinyBlocks_Image* img, const char* path) { /* -- Blocos -------------------------------------------------------------- */ -static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : ""; -} static TinyBlocks_Image* in_img(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { TinyBlocks_Value v = tb_block_get_input(self, rt, i); return v.kind == TB_VALUE_IMAGE ? v.as.image : NULL; } static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) { - TinyBlocks_Image* img = tb_bmp_load(in_text(self, rt, 0)); + TinyBlocks_Image* img = tb_bmp_load(tb_block_input_text(self, rt, 0)); if (!img) { tinyblocks_set_error(rt, "%s", tb_bmp_last_error()); self->dirty = false; return; } tb_block_set_output(self, 0, tb_value_image(img)); tb_image_release(img); @@ -142,7 +138,7 @@ static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) { static void eval_save(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Image* img = in_img(self, rt, 0); - if (!img || !tb_bmp_save(img, in_text(self, rt, 1))) + if (!img || !tb_bmp_save(img, tb_block_input_text(self, rt, 1))) tinyblocks_set_error(rt, "%s", tb_bmp_last_error()); self->dirty = false; } diff --git a/libraries/tinyblocks-curves/src/tb_curves.c b/libraries/tinyblocks-curves/src/tb_curves.c index 645b090..d712c80 100644 --- a/libraries/tinyblocks-curves/src/tb_curves.c +++ b/libraries/tinyblocks-curves/src/tb_curves.c @@ -130,13 +130,6 @@ bool tb_curves_point(const TinyBlocks_JsonValue* pts, int i, double* x, double* /* -- Helpers de entrada -------------------------------------------------- */ -static double in_real(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - if (v.kind == TB_VALUE_REAL) return v.as.real; - if (v.kind == TB_VALUE_INTEGER) return (double)v.as.integer; - if (v.kind == TB_VALUE_TEXT && v.as.text) return atof(v.as.text); - return 0.0; -} static int64_t in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { TinyBlocks_Value v = tb_block_get_input(self, rt, i); @@ -159,31 +152,31 @@ static void out_pts(TinyBlocks_Block* self, TinyBlocks_JsonValue* pts) { /* -- Blocos -------------------------------------------------------------- */ static void eval_line(TinyBlocks_Block* self, TinyBlocks* rt) { - out_pts(self, tb_curves_line(in_real(self,rt,0), in_real(self,rt,1), - in_real(self,rt,2), in_real(self,rt,3), + out_pts(self, tb_curves_line(tb_block_input_number(self,rt,0, 0), tb_block_input_number(self,rt,1, 0), + tb_block_input_number(self,rt,2, 0), tb_block_input_number(self,rt,3, 0), (int)in_int(self,rt,4))); } static void eval_bezier(TinyBlocks_Block* self, TinyBlocks* rt) { - out_pts(self, tb_curves_bezier(in_real(self,rt,0), in_real(self,rt,1), - in_real(self,rt,2), in_real(self,rt,3), - in_real(self,rt,4), in_real(self,rt,5), - in_real(self,rt,6), in_real(self,rt,7), + out_pts(self, tb_curves_bezier(tb_block_input_number(self,rt,0, 0), tb_block_input_number(self,rt,1, 0), + tb_block_input_number(self,rt,2, 0), tb_block_input_number(self,rt,3, 0), + tb_block_input_number(self,rt,4, 0), tb_block_input_number(self,rt,5, 0), + tb_block_input_number(self,rt,6, 0), tb_block_input_number(self,rt,7, 0), (int)in_int(self,rt,8))); } static void eval_quadratic(TinyBlocks_Block* self, TinyBlocks* rt) { - out_pts(self, tb_curves_quadratic(in_real(self,rt,0), in_real(self,rt,1), - in_real(self,rt,2), in_real(self,rt,3), - in_real(self,rt,4), in_real(self,rt,5), + out_pts(self, tb_curves_quadratic(tb_block_input_number(self,rt,0, 0), tb_block_input_number(self,rt,1, 0), + tb_block_input_number(self,rt,2, 0), tb_block_input_number(self,rt,3, 0), + tb_block_input_number(self,rt,4, 0), tb_block_input_number(self,rt,5, 0), (int)in_int(self,rt,6))); } static void eval_arc(TinyBlocks_Block* self, TinyBlocks* rt) { - out_pts(self, tb_curves_arc(in_real(self,rt,0), in_real(self,rt,1), - in_real(self,rt,2), in_real(self,rt,3), - in_real(self,rt,4), (int)in_int(self,rt,5))); + out_pts(self, tb_curves_arc(tb_block_input_number(self,rt,0, 0), tb_block_input_number(self,rt,1, 0), + tb_block_input_number(self,rt,2, 0), tb_block_input_number(self,rt,3, 0), + tb_block_input_number(self,rt,4, 0), (int)in_int(self,rt,5))); } static void eval_circle(TinyBlocks_Block* self, TinyBlocks* rt) { - out_pts(self, tb_curves_circle(in_real(self,rt,0), in_real(self,rt,1), - in_real(self,rt,2), (int)in_int(self,rt,3))); + out_pts(self, tb_curves_circle(tb_block_input_number(self,rt,0, 0), tb_block_input_number(self,rt,1, 0), + tb_block_input_number(self,rt,2, 0), (int)in_int(self,rt,3))); } static void eval_count(TinyBlocks_Block* self, TinyBlocks* rt) { tb_block_set_output(self, 0, tb_value_integer(tb_curves_count(in_pts(self,rt,0)))); diff --git a/libraries/tinyblocks-editor/src/tb_editor.c b/libraries/tinyblocks-editor/src/tb_editor.c index 4242e2a..9516539 100644 --- a/libraries/tinyblocks-editor/src/tb_editor.c +++ b/libraries/tinyblocks-editor/src/tb_editor.c @@ -179,13 +179,7 @@ static Shape shape_of(const TinyBlocks_TypeBlock* t) { case TB_SHAPE_BOOLEAN: return SH_BOOL; default: break; } - if (t->output_desc_count == 1) { - if (tb_type_block_output_flags(t, 0) & TB_SLOT_BOOLEAN) return SH_BOOL; - /* convencao antiga das libraries: saida cujo nome termina em '?' */ - const char* on = t->output_descs && t->output_descs[0].name ? t->output_descs[0].name : ""; - size_t ol = strlen(on); - if (ol > 0 && on[ol - 1] == '?') return SH_BOOL; - } + if (t->output_desc_count == 1 && (tb_type_block_output_flags(t, 0) & TB_SLOT_BOOLEAN)) return SH_BOOL; return t->output_desc_count > 0 ? SH_REPORTER : SH_STACK; } @@ -1405,11 +1399,6 @@ static unsigned now_ms(void) { /* === * Portas de blocos compostos ("Meus Blocos") * === */ -static void input_name(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i, char* out, size_t cap) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - tb_value_to_text(v, out, cap); -} - /* == Blocos compostos ("Meus Blocos") === * A definição é uma aba com scripts; "parâmetro [x]" lê os inputs da chamada * e "devolva [y] - (v)" escreve os outputs. */ @@ -1428,7 +1417,7 @@ static void eval_port_in(TinyBlocks_Block* self, TinyBlocks* rt) { CallFrame* f = cur_frame(); TinyBlocks_Value out = tb_value_none(); if (f) { - char label[64]; input_name(self, rt, 0, label, sizeof(label)); + const char* label = tb_block_input_text(self, rt, 0); for (int i = 0; i < f->def->input_count; i++) if (strcmp(f->def->input_names[i], label) == 0) { out = tb_value_clone(f->in[i]); break; } } @@ -1439,7 +1428,7 @@ static void eval_port_in(TinyBlocks_Block* self, TinyBlocks* rt) { static void eval_port_out(TinyBlocks_Block* self, TinyBlocks* rt) { CallFrame* f = cur_frame(); if (f) { - char label[64]; input_name(self, rt, 0, label, sizeof(label)); + const char* label = tb_block_input_text(self, rt, 0); for (int i = 0; i < f->def->output_count; i++) { if (strcmp(f->def->output_names[i], label) != 0) continue; tb_value_free(&f->out[i]); diff --git a/libraries/tinyblocks-environ/src/tb_environ.c b/libraries/tinyblocks-environ/src/tb_environ.c index 1099532..6ef87db 100644 --- a/libraries/tinyblocks-environ/src/tb_environ.c +++ b/libraries/tinyblocks-environ/src/tb_environ.c @@ -202,28 +202,24 @@ const char* tb_environ_env_path(void) { /* -- Blocos -------------------------------------------------------------- */ -static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : ""; -} static void eval_get(TinyBlocks_Block* self, TinyBlocks* rt) { - tb_block_set_output(self, 0, tb_value_text(tb_environ_get(in_text(self, rt, 0)))); + tb_block_set_output(self, 0, tb_value_text(tb_environ_get(tb_block_input_text(self, rt, 0)))); self->dirty = false; } static void eval_set(TinyBlocks_Block* self, TinyBlocks* rt) { - tb_environ_set(in_text(self, rt, 0), in_text(self, rt, 1)); + tb_environ_set(tb_block_input_text(self, rt, 0), tb_block_input_text(self, rt, 1)); self->dirty = false; } static void eval_has(TinyBlocks_Block* self, TinyBlocks* rt) { - tb_block_set_output(self, 0, tb_value_integer(tb_environ_has(in_text(self, rt, 0)) ? 1 : 0)); + tb_block_set_output(self, 0, tb_value_integer(tb_environ_has(tb_block_input_text(self, rt, 0)) ? 1 : 0)); self->dirty = false; } static void eval_unset(TinyBlocks_Block* self, TinyBlocks* rt) { - tb_environ_unset(in_text(self, rt, 0)); + tb_environ_unset(tb_block_input_text(self, rt, 0)); self->dirty = false; } diff --git a/libraries/tinyblocks-fs/src/tb_fs.c b/libraries/tinyblocks-fs/src/tb_fs.c index d4e8db1..cfe2ba2 100644 --- a/libraries/tinyblocks-fs/src/tb_fs.c +++ b/libraries/tinyblocks-fs/src/tb_fs.c @@ -36,10 +36,6 @@ static void fs_close_slot(int i) { if (i >= 0 && i < FS_MAX_FILES && s_files[i]) { fclose(s_files[i]); s_files[i] = NULL; } } -static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : ""; -} static int64_t in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { TinyBlocks_Value v = tb_block_get_input(self, rt, i); if (v.kind == TB_VALUE_INTEGER) return v.as.integer; @@ -48,7 +44,7 @@ static int64_t in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { } static void eval_open(TinyBlocks_Block* self, TinyBlocks* rt) { - const char* path = in_text(self, rt, 0); + const char* path = tb_block_input_text(self, rt, 0); int slot = fs_slot(); int64_t handle = 0; if (slot >= 0 && path[0]) { @@ -84,14 +80,14 @@ static void eval_read(TinyBlocks_Block* self, TinyBlocks* rt) { static void eval_write(TinyBlocks_Block* self, TinyBlocks* rt) { FILE* f = fs_get(in_int(self, rt, 0)); - const char* text = in_text(self, rt, 1); + const char* text = tb_block_input_text(self, rt, 1); if (f) fwrite(text, 1, strlen(text), f); self->dirty = false; } static void eval_append(TinyBlocks_Block* self, TinyBlocks* rt) { - const char* path = in_text(self, rt, 0); - const char* text = in_text(self, rt, 1); + const char* path = tb_block_input_text(self, rt, 0); + const char* text = tb_block_input_text(self, rt, 1); if (path[0]) { FILE* f = fopen(path, "ab"); if (f) { fwrite(text, 1, strlen(text), f); fclose(f); } @@ -155,7 +151,7 @@ static void free_names(char** v, int n) { static void eval_listdir(TinyBlocks_Block* self, TinyBlocks* rt) { int n = 0; - char** names = list_names(in_text(self, rt, 0), &n); + char** names = list_names(tb_block_input_text(self, rt, 0), &n); TinyBlocks_JsonValue* arr = tb_json_array(); for (int i = 0; arr && i < n; i++) { TinyBlocks_JsonValue* s = tb_json_string(names[i]); @@ -169,7 +165,7 @@ static void eval_listdir(TinyBlocks_Block* self, TinyBlocks* rt) { static void eval_listcount(TinyBlocks_Block* self, TinyBlocks* rt) { int n = 0; - char** names = list_names(in_text(self, rt, 0), &n); + char** names = list_names(tb_block_input_text(self, rt, 0), &n); free_names(names, n); tb_block_set_output(self, 0, tb_value_integer(n)); self->dirty = false; @@ -178,7 +174,7 @@ static void eval_listcount(TinyBlocks_Block* self, TinyBlocks* rt) { /* item N (1..total) da pasta, em ordem alfabetica; fora da faixa = "" */ static void eval_listitem(TinyBlocks_Block* self, TinyBlocks* rt) { int n = 0; - char** names = list_names(in_text(self, rt, 0), &n); + char** names = list_names(tb_block_input_text(self, rt, 0), &n); int64_t k = in_int(self, rt, 1); const char* item = (k >= 1 && k <= n) ? names[k - 1] : ""; tb_block_set_output(self, 0, tb_value_text(item)); @@ -188,7 +184,7 @@ static void eval_listitem(TinyBlocks_Block* self, TinyBlocks* rt) { static void eval_isfile(TinyBlocks_Block* self, TinyBlocks* rt) { struct stat st; - const char* p = in_text(self, rt, 0); + const char* p = tb_block_input_text(self, rt, 0); bool ok = p[0] && stat(p, &st) == 0 && S_ISREG(st.st_mode); tb_block_set_output(self, 0, tb_value_integer(ok ? 1 : 0)); self->dirty = false; @@ -196,7 +192,7 @@ static void eval_isfile(TinyBlocks_Block* self, TinyBlocks* rt) { static void eval_isdir(TinyBlocks_Block* self, TinyBlocks* rt) { struct stat st; - const char* p = in_text(self, rt, 0); + const char* p = tb_block_input_text(self, rt, 0); bool ok = p[0] && stat(p, &st) == 0 && S_ISDIR(st.st_mode); tb_block_set_output(self, 0, tb_value_integer(ok ? 1 : 0)); self->dirty = false; @@ -204,7 +200,7 @@ static void eval_isdir(TinyBlocks_Block* self, TinyBlocks* rt) { static void eval_exists(TinyBlocks_Block* self, TinyBlocks* rt) { struct stat st; - const char* p = in_text(self, rt, 0); + const char* p = tb_block_input_text(self, rt, 0); bool ok = p[0] && stat(p, &st) == 0; tb_block_set_output(self, 0, tb_value_integer(ok ? 1 : 0)); self->dirty = false; diff --git a/libraries/tinyblocks-image/src/tb_imglib.c b/libraries/tinyblocks-image/src/tb_imglib.c index da231da..b577d47 100644 --- a/libraries/tinyblocks-image/src/tb_imglib.c +++ b/libraries/tinyblocks-image/src/tb_imglib.c @@ -79,18 +79,6 @@ TinyBlocks_Image* tb_imglib_scale(const TinyBlocks_Image* img, /* -- Helpers de entrada -------------------------------------------------- */ -static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : ""; -} -static double in_real(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - if (v.kind == TB_VALUE_REAL) return v.as.real; - if (v.kind == TB_VALUE_INTEGER) return (double)v.as.integer; - if (v.kind == TB_VALUE_TEXT && v.as.text) return atof(v.as.text); - return 0.0; -} -static int in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { return (int)in_real(self, rt, i); } static TinyBlocks_Image* in_img(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { TinyBlocks_Value v = tb_block_get_input(self, rt, i); return v.kind == TB_VALUE_IMAGE ? v.as.image : NULL; @@ -107,11 +95,11 @@ static void out_img(TinyBlocks_Block* self, TinyBlocks_Image* img) { /* -- Blocos -------------------------------------------------------------- */ static void eval_new(TinyBlocks_Block* self, TinyBlocks* rt) { - int w = in_int(self, rt, 0), h = in_int(self, rt, 1); + int w = (int)tb_block_input_number(self, rt, 0, 0), h = (int)tb_block_input_number(self, rt, 1, 0); TinyBlocks_Image* img = tb_image_new(w > 0 ? w : 1, h > 0 ? h : 1, 4); if (!img) { tinyblocks_set_error(rt, "sem memoria"); self->dirty = false; return; } - tb_imglib_fill(img, clamp8(in_real(self,rt,2)), clamp8(in_real(self,rt,3)), - clamp8(in_real(self,rt,4)), 255); + tb_imglib_fill(img, clamp8(tb_block_input_number(self,rt,2, 0)), clamp8(tb_block_input_number(self,rt,3, 0)), + clamp8(tb_block_input_number(self,rt,4, 0)), 255); out_img(self, img); } static void eval_width (TinyBlocks_Block* self, TinyBlocks* rt) { tb_block_set_output(self,0,tb_value_integer(tb_image_width (in_img(self,rt,0)))); self->dirty = false; } @@ -119,7 +107,7 @@ static void eval_height(TinyBlocks_Block* self, TinyBlocks* rt) { tb_block_set_o static void eval_getpixel(TinyBlocks_Block* self, TinyBlocks* rt) { uint8_t r=0,g=0,b=0,a=0; - tb_image_get_pixel(in_img(self,rt,0), in_int(self,rt,1), in_int(self,rt,2), &r,&g,&b,&a); + tb_image_get_pixel(in_img(self,rt,0), (int)tb_block_input_number(self,rt,1, 0), (int)tb_block_input_number(self,rt,2, 0), &r,&g,&b,&a); TinyBlocks_JsonValue* arr = tb_json_array(); tb_json_array_push(arr, tb_json_number(r)); tb_json_array_push(arr, tb_json_number(g)); @@ -131,26 +119,26 @@ static void eval_getpixel(TinyBlocks_Block* self, TinyBlocks* rt) { } static void eval_setpixel(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Image* img = in_img(self,rt,0); - if (img) tb_image_set_pixel(img, in_int(self,rt,1), in_int(self,rt,2), - clamp8(in_real(self,rt,3)), clamp8(in_real(self,rt,4)), - clamp8(in_real(self,rt,5)), 255); + if (img) tb_image_set_pixel(img, (int)tb_block_input_number(self,rt,1, 0), (int)tb_block_input_number(self,rt,2, 0), + clamp8(tb_block_input_number(self,rt,3, 0)), clamp8(tb_block_input_number(self,rt,4, 0)), + clamp8(tb_block_input_number(self,rt,5, 0)), 255); self->dirty = false; } static void eval_fill(TinyBlocks_Block* self, TinyBlocks* rt) { - tb_imglib_fill(in_img(self,rt,0), clamp8(in_real(self,rt,1)), - clamp8(in_real(self,rt,2)), clamp8(in_real(self,rt,3)), 255); + tb_imglib_fill(in_img(self,rt,0), clamp8(tb_block_input_number(self,rt,1, 0)), + clamp8(tb_block_input_number(self,rt,2, 0)), clamp8(tb_block_input_number(self,rt,3, 0)), 255); self->dirty = false; } static void eval_clone(TinyBlocks_Block* self, TinyBlocks* rt) { out_img(self, tb_image_clone(in_img(self,rt,0))); } static void eval_crop(TinyBlocks_Block* self, TinyBlocks* rt) { - out_img(self, tb_imglib_crop(in_img(self,rt,0), in_int(self,rt,1), in_int(self,rt,2), - in_int(self,rt,3), in_int(self,rt,4))); + out_img(self, tb_imglib_crop(in_img(self,rt,0), (int)tb_block_input_number(self,rt,1, 0), (int)tb_block_input_number(self,rt,2, 0), + (int)tb_block_input_number(self,rt,3, 0), (int)tb_block_input_number(self,rt,4, 0))); } static void eval_scale(TinyBlocks_Block* self, TinyBlocks* rt) { - out_img(self, tb_imglib_scale(in_img(self,rt,0), in_int(self,rt,1), in_int(self,rt,2), true)); + out_img(self, tb_imglib_scale(in_img(self,rt,0), (int)tb_block_input_number(self,rt,1, 0), (int)tb_block_input_number(self,rt,2, 0), true)); } static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) { - const char* path = in_text(self, rt, 0); + const char* path = tb_block_input_text(self, rt, 0); TinyBlocks_Image* img = tb_image_load(path); if (!img) { tinyblocks_set_error(rt, "nao carregou '%s' (formato?)", path); self->dirty = false; return; } /* A imagem carregada ganha nome (nome do arquivo) e numero no armazem, @@ -162,8 +150,8 @@ static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) { } static void eval_save(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Image* img = in_img(self,rt,0); - if (!img || !tb_image_save(img, in_text(self,rt,1))) - tinyblocks_set_error(rt, "nao gravou '%s' (formato?)", in_text(self,rt,1)); + if (!img || !tb_image_save(img, tb_block_input_text(self,rt,1))) + tinyblocks_set_error(rt, "nao gravou '%s' (formato?)", tb_block_input_text(self,rt,1)); self->dirty = false; } @@ -218,7 +206,7 @@ static TinyBlocks_JsonValue* in_json_list(TinyBlocks_Block* self, TinyBlocks* rt /* "imagem de largura (w) com os pixels (lista)": cada pixel e [r,g,b], [r,g,b,a] ou um numero (cinza); a altura sai do tamanho da lista */ static void eval_from_list(TinyBlocks_Block* self, TinyBlocks* rt) { - int w = (int)in_int(self, rt, 0); + int w = (int)(int)tb_block_input_number(self, rt, 0, 0); TinyBlocks_JsonValue* list = in_json_list(self, rt, 1); uint32_t n = list ? tb_json_array_length(list) : 0; if (w <= 0 || !n) { @@ -278,7 +266,7 @@ static void eval_gray(TinyBlocks_Block* self, TinyBlocks* rt) { static void eval_to_text(TinyBlocks_Block* self, TinyBlocks* rt) { static const char ramp[] = " .:-=+*#%@"; /* claro -> escuro */ TinyBlocks_Image* img = in_img(self, rt, 0); - int cols = (int)in_int(self, rt, 1); + int cols = (int)(int)tb_block_input_number(self, rt, 1, 0); int w = tb_image_width(img), h = tb_image_height(img); if (!img || w <= 0 || h <= 0) { tb_block_set_output(self, 0, tb_value_text("")); self->dirty = false; return; } if (cols <= 0 || cols > w) cols = w; diff --git a/libraries/tinyblocks-input/src/tb_input.c b/libraries/tinyblocks-input/src/tb_input.c index 40bcf3c..8e14c32 100644 --- a/libraries/tinyblocks-input/src/tb_input.c +++ b/libraries/tinyblocks-input/src/tb_input.c @@ -91,25 +91,11 @@ TB_API const char* tb_input_key_name(int key) { /* -- Entradas/saidas ------------------------------------------------------ */ -static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - static char num[32]; - if (v.kind == TB_VALUE_TEXT && v.as.text) return v.as.text; - if (v.kind == TB_VALUE_INTEGER) { snprintf(num, sizeof(num), "%lld", (long long)v.as.integer); return num; } - return ""; -} -static long long in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - if (v.kind == TB_VALUE_INTEGER) return v.as.integer; - if (v.kind == TB_VALUE_REAL) return (long long)llround(v.as.real); - if (v.kind == TB_VALUE_TEXT && v.as.text) return strtoll(v.as.text, NULL, 10); - return 0; -} /* Eixo "x"/"y"/"z" (ou 1/2/3) -> 0..2; -1 se invalido */ static int in_axis(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - const char* s = in_text(self, rt, i); + const char* s = tb_block_input_text(self, rt, i); while (isspace((unsigned char)*s)) s++; switch (tolower((unsigned char)*s)) { case 'x': case '1': return 0; @@ -141,7 +127,7 @@ static void eval_mouse_over(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; static void eval_wheel(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; out_num(self, tb_input_wheel()); } static void eval_key_pressed(TinyBlocks_Block* self, TinyBlocks* rt) { - const char* nm = in_text(self, rt, 0); + const char* nm = tb_block_input_text(self, rt, 0); int k = tb_input_key_from_name(nm); if (k == 0 && nm[0]) tinyblocks_set_error(rt, "tecla desconhecida: '%s'", nm); out_bool(self, k < 0 ? tb_input_any_key_held() : tb_input_key_held(k)); @@ -157,10 +143,10 @@ static void eval_touch_count(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; static void eval_touching(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; out_bool(self, tb_input_touch_count() > 0); } static void eval_touch_x(TinyBlocks_Block* self, TinyBlocks* rt) { - out_num(self, roundf(tb_input_touch_x((int)in_int(self, rt, 0) - 1))); + out_num(self, roundf(tb_input_touch_x((int)tb_block_input_integer(self, rt, 0, 0) - 1))); } static void eval_touch_y(TinyBlocks_Block* self, TinyBlocks* rt) { - out_num(self, roundf(tb_input_touch_y((int)in_int(self, rt, 0) - 1))); + out_num(self, roundf(tb_input_touch_y((int)tb_block_input_integer(self, rt, 0, 0) - 1))); } static void eval_has_accel(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; out_bool(self, tb_input_has_accel()); } diff --git a/libraries/tinyblocks-jpg/src/tb_jpg.c b/libraries/tinyblocks-jpg/src/tb_jpg.c index f940a87..48141f5 100644 --- a/libraries/tinyblocks-jpg/src/tb_jpg.c +++ b/libraries/tinyblocks-jpg/src/tb_jpg.c @@ -131,23 +131,13 @@ bool tb_jpg_save(const TinyBlocks_Image* img, const char* path, int quality) { /* -- Blocos -------------------------------------------------------------- */ -static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : ""; -} -static double in_real(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - if (v.kind == TB_VALUE_REAL) return v.as.real; - if (v.kind == TB_VALUE_INTEGER) return (double)v.as.integer; - return 0; -} static TinyBlocks_Image* in_img(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { TinyBlocks_Value v = tb_block_get_input(self, rt, i); return v.kind == TB_VALUE_IMAGE ? v.as.image : NULL; } static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) { - TinyBlocks_Image* img = tb_jpg_load(in_text(self, rt, 0)); + TinyBlocks_Image* img = tb_jpg_load(tb_block_input_text(self, rt, 0)); if (!img) { tinyblocks_set_error(rt, "%s", tb_jpg_last_error()); self->dirty = false; return; } tb_block_set_output(self, 0, tb_value_image(img)); tb_image_release(img); @@ -155,7 +145,7 @@ static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) { } static void eval_save(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Image* img = in_img(self, rt, 0); - if (!img || !tb_jpg_save(img, in_text(self, rt, 1), (int)in_real(self, rt, 2))) + if (!img || !tb_jpg_save(img, tb_block_input_text(self, rt, 1), (int)tb_block_input_number(self, rt, 2, 0))) tinyblocks_set_error(rt, "%s", tb_jpg_last_error()); self->dirty = false; } diff --git a/libraries/tinyblocks-markdown/src/tb_markdown.c b/libraries/tinyblocks-markdown/src/tb_markdown.c index 3bdc7c2..3d97db2 100644 --- a/libraries/tinyblocks-markdown/src/tb_markdown.c +++ b/libraries/tinyblocks-markdown/src/tb_markdown.c @@ -471,20 +471,16 @@ char* tb_markdown_to_svg_opts(const char* md, int width, int font_size) { char* tb_markdown_to_svg(const char* md) { return tb_markdown_to_svg_opts(md, 640, 16); } /* ---------------------------------------------------------------- Blocos */ -static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : ""; -} static void eval_html(TinyBlocks_Block* self, TinyBlocks* rt) { - char* s = tb_markdown_to_html(in_text(self, rt, 0)); + char* s = tb_markdown_to_html(tb_block_input_text(self, rt, 0)); tb_block_set_output(self, 0, tb_value_text(s ? s : "")); free(s); self->dirty = false; } static void eval_xml(TinyBlocks_Block* self, TinyBlocks* rt) { - char* s = tb_markdown_to_xml(in_text(self, rt, 0)); + char* s = tb_markdown_to_xml(tb_block_input_text(self, rt, 0)); tb_block_set_output(self, 0, tb_value_text(s ? s : "")); free(s); self->dirty = false; } static void eval_svg(TinyBlocks_Block* self, TinyBlocks* rt) { - char* s = tb_markdown_to_svg(in_text(self, rt, 0)); + char* s = tb_markdown_to_svg(tb_block_input_text(self, rt, 0)); tb_block_set_output(self, 0, tb_value_text(s ? s : "")); free(s); self->dirty = false; } diff --git a/libraries/tinyblocks-number/src/tb_number.c b/libraries/tinyblocks-number/src/tb_number.c index 12496b9..a5bd16c 100644 --- a/libraries/tinyblocks-number/src/tb_number.c +++ b/libraries/tinyblocks-number/src/tb_number.c @@ -62,13 +62,6 @@ TB_API void tb_number_format(double x, int places, char* out, size_t cap) { /* -- Entradas/saidas ------------------------------------------------------ */ -static double num(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - if (v.kind == TB_VALUE_INTEGER) return (double)v.as.integer; - if (v.kind == TB_VALUE_REAL) return v.as.real; - if (v.kind == TB_VALUE_TEXT && v.as.text) return strtod(v.as.text, NULL); - return 0.0; -} /* Inteiro quando o resultado for exato (e couber), senao real */ static void out_num(TinyBlocks_Block* self, double x) { @@ -89,7 +82,7 @@ static double trig(double x) { return tb_number_round_to(x, 10); } /* -- Blocos --------------------------------------------------------------- */ -#define N(i) num(self, rt, (i)) +#define N(i) tb_block_input_number(self, rt, (i), 0) static void eval_div(TinyBlocks_Block* self, TinyBlocks* rt) { double b = N(1); @@ -175,13 +168,13 @@ static void eval_from(TinyBlocks_Block* self, TinyBlocks* rt) { } static int in_base(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - int b = (int)llround(num(self, rt, i)); + int b = (int)llround(tb_block_input_number(self, rt, i, 0)); return b < 2 ? 2 : b > 36 ? 36 : b; } /* "(255) na base (16)" -> "ff" */ static void eval_to_base(TinyBlocks_Block* self, TinyBlocks* rt) { - long long x = llround(num(self, rt, 0)); + long long x = llround(tb_block_input_number(self, rt, 0, 0)); int b = in_base(self, rt, 1); char buf[72], *p = buf + sizeof(buf) - 1; *p = 0; @@ -198,7 +191,7 @@ static void eval_from_base(TinyBlocks_Block* self, TinyBlocks* rt) { char buf[72]; const char* s = buf; if (v.kind == TB_VALUE_TEXT) s = v.as.text ? v.as.text : ""; - else snprintf(buf, sizeof(buf), "%lld", llround(num(self, rt, 0))); + else snprintf(buf, sizeof(buf), "%lld", llround(tb_block_input_number(self, rt, 0, 0))); int b = in_base(self, rt, 1); while (isspace((unsigned char)*s)) s++; char* e; diff --git a/libraries/tinyblocks-opentype/src/tb_opentype.c b/libraries/tinyblocks-opentype/src/tb_opentype.c index 36b1f0f..8205a65 100644 --- a/libraries/tinyblocks-opentype/src/tb_opentype.c +++ b/libraries/tinyblocks-opentype/src/tb_opentype.c @@ -295,39 +295,29 @@ const char* tb_opentype_default_font(void) { } /* ---------------------------------------------------------------- Blocos */ -static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : ""; -} -static double in_real(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - if (v.kind == TB_VALUE_REAL) return v.as.real; - if (v.kind == TB_VALUE_INTEGER) return (double)v.as.integer; - return 0; -} static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) { - int h = tb_opentype_load(in_text(self, rt, 0)); + int h = tb_opentype_load(tb_block_input_text(self, rt, 0)); if (!h) tinyblocks_set_error(rt, "%s", tb_opentype_last_error()); tb_block_set_output(self, 0, tb_value_integer(h)); self->dirty = false; } static void eval_upem(TinyBlocks_Block* self, TinyBlocks* rt) { - tb_block_set_output(self, 0, tb_value_integer(tb_opentype_units_per_em((int)in_real(self, rt, 0)))); + tb_block_set_output(self, 0, tb_value_integer(tb_opentype_units_per_em((int)tb_block_input_number(self, rt, 0, 0)))); self->dirty = false; } static void eval_glyphs(TinyBlocks_Block* self, TinyBlocks* rt) { - tb_block_set_output(self, 0, tb_value_integer(tb_opentype_glyph_count((int)in_real(self, rt, 0)))); + tb_block_set_output(self, 0, tb_value_integer(tb_opentype_glyph_count((int)tb_block_input_number(self, rt, 0, 0)))); self->dirty = false; } static void eval_advance(TinyBlocks_Block* self, TinyBlocks* rt) { tb_block_set_output(self, 0, tb_value_real( - tb_opentype_advance((int)in_real(self, rt, 0), in_text(self, rt, 1), (int)in_real(self, rt, 2)))); + tb_opentype_advance((int)tb_block_input_number(self, rt, 0, 0), tb_block_input_text(self, rt, 1), (int)tb_block_input_number(self, rt, 2, 0)))); self->dirty = false; } static void eval_outline(TinyBlocks_Block* self, TinyBlocks* rt) { - TinyBlocks_JsonValue* j = tb_opentype_outline((int)in_real(self, rt, 0), in_text(self, rt, 1), - (int)in_real(self, rt, 2)); + TinyBlocks_JsonValue* j = tb_opentype_outline((int)tb_block_input_number(self, rt, 0, 0), tb_block_input_text(self, rt, 1), + (int)tb_block_input_number(self, rt, 2, 0)); if (j) { tb_block_set_output(self, 0, tb_value_json(j)); tb_json_release(j); @@ -337,10 +327,10 @@ static void eval_outline(TinyBlocks_Block* self, TinyBlocks* rt) { self->dirty = false; } static void eval_render(TinyBlocks_Block* self, TinyBlocks* rt) { - TinyBlocks_Image* img = tb_opentype_render((int)in_real(self, rt, 0), in_text(self, rt, 1), - (int)in_real(self, rt, 2), - (int)in_real(self, rt, 3), (int)in_real(self, rt, 4), - (int)in_real(self, rt, 5)); + TinyBlocks_Image* img = tb_opentype_render((int)tb_block_input_number(self, rt, 0, 0), tb_block_input_text(self, rt, 1), + (int)tb_block_input_number(self, rt, 2, 0), + (int)tb_block_input_number(self, rt, 3, 0), (int)tb_block_input_number(self, rt, 4, 0), + (int)tb_block_input_number(self, rt, 5, 0)); if (!img) { tinyblocks_set_error(rt, "%s", tb_opentype_last_error()); self->dirty = false; return; } tb_block_set_output(self, 0, tb_value_image(img)); tb_image_release(img); diff --git a/libraries/tinyblocks-operators/src/tb_operators.c b/libraries/tinyblocks-operators/src/tb_operators.c index 2b31e13..5282e91 100644 --- a/libraries/tinyblocks-operators/src/tb_operators.c +++ b/libraries/tinyblocks-operators/src/tb_operators.c @@ -11,41 +11,9 @@ #include #include "tb_operators.h" -/* == Conversões === */ -static bool value_num(const TinyBlocks_Value* v, double* d) { - if (v->kind == TB_VALUE_INTEGER) { *d = (double)v->as.integer; return true; } - if (v->kind == TB_VALUE_REAL) { *d = v->as.real; return true; } - if (v->kind == TB_VALUE_TEXT && v->as.text && v->as.text[0]) { - char* e; *d = strtod(v->as.text, &e); - return *e == 0; - } - return false; -} +/* == Conversões: classes de valor do core (tb_value_to_number/_truthy/_to_text) === */ -static bool value_truthy(const TinyBlocks_Value* v) { - switch (v->kind) { - case TB_VALUE_INTEGER: return v->as.integer != 0; - case TB_VALUE_REAL: return v->as.real != 0.0; - case TB_VALUE_TEXT: { - const char* t = v->as.text ? v->as.text : ""; - if (!t[0] || strcmp(t, "0") == 0) return false; - const char* f = "false"; - for (; *t && *f; t++, f++) - if ((*t | 32) != *f) return true; - return *t || *f; - } - default: return false; - } -} -static void value_to_str(const TinyBlocks_Value* v, char* buf, size_t cap) { - switch (v->kind) { - case TB_VALUE_INTEGER: snprintf(buf, cap, "%lld", (long long)v->as.integer); break; - case TB_VALUE_REAL: snprintf(buf, cap, "%g", v->as.real); break; - case TB_VALUE_TEXT: snprintf(buf, cap, "%s", v->as.text ? v->as.text : ""); break; - default: buf[0] = 0; break; - } -} static int str_icmp(const char* a, const char* b) { for (;; a++, b++) { @@ -58,10 +26,10 @@ static int cmp_inputs(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Value a = tb_block_get_input(self, rt, 0); TinyBlocks_Value b = tb_block_get_input(self, rt, 1); double x, y; - if (value_num(&a, &x) && value_num(&b, &y)) return (x > y) - (x < y); + if (tb_value_to_number(a, &x) && tb_value_to_number(b, &y)) return (x > y) - (x < y); char sa[256], sb[256]; - value_to_str(&a, sa, sizeof(sa)); - value_to_str(&b, sb, sizeof(sb)); + tb_value_to_text(a, sa, sizeof(sa)); + tb_value_to_text(b, sb, sizeof(sb)); return str_icmp(sa, sb); } @@ -76,18 +44,18 @@ static void eval_op_gt(TinyBlocks_Block* self, TinyBlocks* rt) static void eval_op_and(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Value a = tb_block_get_input(self, rt, 0); TinyBlocks_Value b = tb_block_get_input(self, rt, 1); - tb_block_set_output(self, 0, tb_value_integer(value_truthy(&a) && value_truthy(&b))); + tb_block_set_output(self, 0, tb_value_integer(tb_value_truthy(a) && tb_value_truthy(b))); self->dirty = false; } static void eval_op_or(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Value a = tb_block_get_input(self, rt, 0); TinyBlocks_Value b = tb_block_get_input(self, rt, 1); - tb_block_set_output(self, 0, tb_value_integer(value_truthy(&a) || value_truthy(&b))); + tb_block_set_output(self, 0, tb_value_integer(tb_value_truthy(a) || tb_value_truthy(b))); self->dirty = false; } static void eval_op_not(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Value a = tb_block_get_input(self, rt, 0); - tb_block_set_output(self, 0, tb_value_integer(!value_truthy(&a))); + tb_block_set_output(self, 0, tb_value_integer(!tb_value_truthy(a))); self->dirty = false; } @@ -95,7 +63,7 @@ static void eval_op_random(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Value a = tb_block_get_input(self, rt, 0); TinyBlocks_Value b = tb_block_get_input(self, rt, 1); double lo = 0, hi = 0; - value_num(&a, &lo); value_num(&b, &hi); + tb_value_to_number(a, &lo); tb_value_to_number(b, &hi); if (lo > hi) { double t = lo; lo = hi; hi = t; } if (lo == floor(lo) && hi == floor(hi)) { long long span = (long long)(hi - lo) + 1; diff --git a/libraries/tinyblocks-pen/src/tb_pen.c b/libraries/tinyblocks-pen/src/tb_pen.c index 305b3e9..15381f2 100644 --- a/libraries/tinyblocks-pen/src/tb_pen.c +++ b/libraries/tinyblocks-pen/src/tb_pen.c @@ -13,39 +13,32 @@ #include #include "tb_pen.h" -static float num(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - if (v.kind == TB_VALUE_REAL) return (float)v.as.real; - if (v.kind == TB_VALUE_INTEGER) return (float)v.as.integer; - if (v.kind == TB_VALUE_TEXT && v.as.text) return (float)strtod(v.as.text, NULL); - return 0.f; -} static void eval_clear(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; tb_stage_clear(); self->dirty = false; } static void eval_background(TinyBlocks_Block* self, TinyBlocks* rt) { - tb_stage_set_background(num(self, rt, 0), num(self, rt, 1), num(self, rt, 2)); + tb_stage_set_background((float)tb_block_input_number(self, rt, 0, 0), (float)tb_block_input_number(self, rt, 1, 0), (float)tb_block_input_number(self, rt, 2, 0)); self->dirty = false; } static void eval_pen_color(TinyBlocks_Block* self, TinyBlocks* rt) { - tb_stage_set_pen(num(self, rt, 0), num(self, rt, 1), num(self, rt, 2)); + tb_stage_set_pen((float)tb_block_input_number(self, rt, 0, 0), (float)tb_block_input_number(self, rt, 1, 0), (float)tb_block_input_number(self, rt, 2, 0)); self->dirty = false; } static void eval_pen_size(TinyBlocks_Block* self, TinyBlocks* rt) { - tb_stage_set_pen_size(num(self, rt, 0)); + tb_stage_set_pen_size((float)tb_block_input_number(self, rt, 0, 0)); self->dirty = false; } static void eval_line(TinyBlocks_Block* self, TinyBlocks* rt) { - tb_stage_line(num(self, rt, 0), num(self, rt, 1), num(self, rt, 2), num(self, rt, 3)); + tb_stage_line((float)tb_block_input_number(self, rt, 0, 0), (float)tb_block_input_number(self, rt, 1, 0), (float)tb_block_input_number(self, rt, 2, 0), (float)tb_block_input_number(self, rt, 3, 0)); self->dirty = false; } static void eval_rect(TinyBlocks_Block* self, TinyBlocks* rt) { - tb_stage_rect(num(self, rt, 0), num(self, rt, 1), num(self, rt, 2), num(self, rt, 3)); + tb_stage_rect((float)tb_block_input_number(self, rt, 0, 0), (float)tb_block_input_number(self, rt, 1, 0), (float)tb_block_input_number(self, rt, 2, 0), (float)tb_block_input_number(self, rt, 3, 0)); self->dirty = false; } static void eval_circle(TinyBlocks_Block* self, TinyBlocks* rt) { - tb_stage_circle(num(self, rt, 0), num(self, rt, 1), num(self, rt, 2)); + tb_stage_circle((float)tb_block_input_number(self, rt, 0, 0), (float)tb_block_input_number(self, rt, 1, 0), (float)tb_block_input_number(self, rt, 2, 0)); self->dirty = false; } static void eval_text(TinyBlocks_Block* self, TinyBlocks* rt) { @@ -54,20 +47,20 @@ static void eval_text(TinyBlocks_Block* self, TinyBlocks* rt) { if (v.kind == TB_VALUE_TEXT && v.as.text) snprintf(buf, sizeof(buf), "%s", v.as.text); else if (v.kind == TB_VALUE_INTEGER) snprintf(buf, sizeof(buf), "%lld", (long long)v.as.integer); else if (v.kind == TB_VALUE_REAL) snprintf(buf, sizeof(buf), "%g", v.as.real); - tb_stage_text(buf, num(self, rt, 1), num(self, rt, 2)); + tb_stage_text(buf, (float)tb_block_input_number(self, rt, 1, 0), (float)tb_block_input_number(self, rt, 2, 0)); self->dirty = false; } static void eval_image_name(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Value v = tb_block_get_input(self, rt, 0); const char* nm = (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : ""; - if (!tb_stage_image_named(nm, num(self, rt, 1), num(self, rt, 2))) + if (!tb_stage_image_named(nm, (float)tb_block_input_number(self, rt, 1, 0), (float)tb_block_input_number(self, rt, 2, 0))) tinyblocks_set_error(rt, "imagem '%s' nao encontrada", nm); self->dirty = false; } static void eval_image_number(TinyBlocks_Block* self, TinyBlocks* rt) { - int n = (int)num(self, rt, 0); - if (!tb_stage_image_number(n, num(self, rt, 1), num(self, rt, 2))) + int n = (int)(float)tb_block_input_number(self, rt, 0, 0); + if (!tb_stage_image_number(n, (float)tb_block_input_number(self, rt, 1, 0), (float)tb_block_input_number(self, rt, 2, 0))) tinyblocks_set_error(rt, "imagem numero %d nao existe", n); self->dirty = false; } @@ -94,7 +87,7 @@ static bool point_at(const TinyBlocks_JsonValue* arr, uint32_t i, float* x, floa imagem guardada. */ static void eval_draw(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Value v = tb_block_get_input(self, rt, 0); - float x = num(self, rt, 1), y = num(self, rt, 2); + float x = (float)tb_block_input_number(self, rt, 1, 0), y = (float)tb_block_input_number(self, rt, 2, 0); if (v.kind == TB_VALUE_IMAGE && v.as.image) { tb_stage_image(v.as.image, x, y); } else if (v.kind == TB_VALUE_JSON && v.as.json && tb_json_kind(v.as.json) == TB_JSON_ARRAY) { diff --git a/libraries/tinyblocks-png/src/tb_png.c b/libraries/tinyblocks-png/src/tb_png.c index 3a8daf7..b5affc8 100644 --- a/libraries/tinyblocks-png/src/tb_png.c +++ b/libraries/tinyblocks-png/src/tb_png.c @@ -74,17 +74,13 @@ bool tb_png_save(const TinyBlocks_Image* img, const char* path) { /* -- Blocos -------------------------------------------------------------- */ -static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : ""; -} static TinyBlocks_Image* in_img(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { TinyBlocks_Value v = tb_block_get_input(self, rt, i); return v.kind == TB_VALUE_IMAGE ? v.as.image : NULL; } static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) { - TinyBlocks_Image* img = tb_png_load(in_text(self, rt, 0)); + TinyBlocks_Image* img = tb_png_load(tb_block_input_text(self, rt, 0)); if (!img) { tinyblocks_set_error(rt, "%s", tb_png_last_error()); self->dirty = false; return; } tb_block_set_output(self, 0, tb_value_image(img)); tb_image_release(img); @@ -93,7 +89,7 @@ static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) { static void eval_save(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Image* img = in_img(self, rt, 0); - if (!img || !tb_png_save(img, in_text(self, rt, 1))) + if (!img || !tb_png_save(img, tb_block_input_text(self, rt, 1))) tinyblocks_set_error(rt, "%s", tb_png_last_error()); self->dirty = false; } diff --git a/libraries/tinyblocks-ppm/src/tb_ppm.c b/libraries/tinyblocks-ppm/src/tb_ppm.c index d6a8d24..7e607c6 100644 --- a/libraries/tinyblocks-ppm/src/tb_ppm.c +++ b/libraries/tinyblocks-ppm/src/tb_ppm.c @@ -112,20 +112,6 @@ bool tb_ppm_save(const TinyBlocks_Image* img, const char* path) { /* -- Helpers de entrada -------------------------------------------------- */ -static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : ""; -} -static double in_real(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - if (v.kind == TB_VALUE_REAL) return v.as.real; - if (v.kind == TB_VALUE_INTEGER) return (double)v.as.integer; - if (v.kind == TB_VALUE_TEXT && v.as.text) return atof(v.as.text); - return 0.0; -} -static int in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - return (int)in_real(self, rt, i); -} static TinyBlocks_Image* in_img(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { TinyBlocks_Value v = tb_block_get_input(self, rt, i); return v.kind == TB_VALUE_IMAGE ? v.as.image : NULL; @@ -140,7 +126,7 @@ static uint8_t clamp8(double v) { /* -- Blocos -------------------------------------------------------------- */ static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) { - TinyBlocks_Image* img = tb_ppm_load(in_text(self, rt, 0)); + TinyBlocks_Image* img = tb_ppm_load(tb_block_input_text(self, rt, 0)); if (!img) { tinyblocks_set_error(rt, "%s", tb_ppm_last_error()); self->dirty = false; return; } tb_block_set_output(self, 0, tb_value_image(img)); tb_image_release(img); @@ -149,18 +135,18 @@ static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) { static void eval_save(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Image* img = in_img(self, rt, 0); - if (!img || !tb_ppm_save(img, in_text(self, rt, 1))) + if (!img || !tb_ppm_save(img, tb_block_input_text(self, rt, 1))) tinyblocks_set_error(rt, "%s", tb_ppm_last_error()); self->dirty = false; } static void eval_new(TinyBlocks_Block* self, TinyBlocks* rt) { - int w = in_int(self, rt, 0), h = in_int(self, rt, 1); + int w = (int)tb_block_input_number(self, rt, 0, 0), h = (int)tb_block_input_number(self, rt, 1, 0); TinyBlocks_Image* img = tb_image_new(w > 0 ? w : 1, h > 0 ? h : 1, 3); if (!img) { tinyblocks_set_error(rt, "sem memoria"); self->dirty = false; return; } - uint8_t r = clamp8(in_real(self, rt, 2)); - uint8_t g = clamp8(in_real(self, rt, 3)); - uint8_t b = clamp8(in_real(self, rt, 4)); + uint8_t r = clamp8(tb_block_input_number(self, rt, 2, 0)); + uint8_t g = clamp8(tb_block_input_number(self, rt, 3, 0)); + uint8_t b = clamp8(tb_block_input_number(self, rt, 4, 0)); for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) tb_image_set_pixel(img, x, y, r, g, b, 255); @@ -181,16 +167,16 @@ static void eval_height(TinyBlocks_Block* self, TinyBlocks* rt) { static void eval_setpixel(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Image* img = in_img(self, rt, 0); if (!img) { tinyblocks_set_error(rt, "imagem invalida"); self->dirty = false; return; } - tb_image_set_pixel(img, in_int(self, rt, 1), in_int(self, rt, 2), - clamp8(in_real(self, rt, 3)), clamp8(in_real(self, rt, 4)), - clamp8(in_real(self, rt, 5)), 255); + tb_image_set_pixel(img, (int)tb_block_input_number(self, rt, 1, 0), (int)tb_block_input_number(self, rt, 2, 0), + clamp8(tb_block_input_number(self, rt, 3, 0)), clamp8(tb_block_input_number(self, rt, 4, 0)), + clamp8(tb_block_input_number(self, rt, 5, 0)), 255); self->dirty = false; } static void eval_getpixel(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Image* img = in_img(self, rt, 0); uint8_t r = 0, g = 0, b = 0, a = 0; - tb_image_get_pixel(img, in_int(self, rt, 1), in_int(self, rt, 2), &r, &g, &b, &a); + tb_image_get_pixel(img, (int)tb_block_input_number(self, rt, 1, 0), (int)tb_block_input_number(self, rt, 2, 0), &r, &g, &b, &a); TinyBlocks_JsonValue* arr = tb_json_array(); tb_json_array_push(arr, tb_json_number(r)); tb_json_array_push(arr, tb_json_number(g)); diff --git a/libraries/tinyblocks-regex/src/tb_regex.c b/libraries/tinyblocks-regex/src/tb_regex.c index 35d131b..20d7ed5 100644 --- a/libraries/tinyblocks-regex/src/tb_regex.c +++ b/libraries/tinyblocks-regex/src/tb_regex.c @@ -211,56 +211,46 @@ void tb_regex_free_list(char** list) { } /* ---------------------------------------------------------------- Blocos */ -static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : ""; -} -static double in_real(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - if (v.kind == TB_VALUE_REAL) return v.as.real; - if (v.kind == TB_VALUE_INTEGER) return (double)v.as.integer; - return 0; -} static void eval_test(TinyBlocks_Block* self, TinyBlocks* rt) { - bool ok = tb_regex_test(in_text(self, rt, 0), in_text(self, rt, 1)); + bool ok = tb_regex_test(tb_block_input_text(self, rt, 0), tb_block_input_text(self, rt, 1)); tb_block_set_output(self, 0, tb_value_integer(ok ? 1 : 0)); self->dirty = false; } static void eval_match(TinyBlocks_Block* self, TinyBlocks* rt) { - bool ok = tb_regex_match(in_text(self, rt, 0), in_text(self, rt, 1)); + bool ok = tb_regex_match(tb_block_input_text(self, rt, 0), tb_block_input_text(self, rt, 1)); tb_block_set_output(self, 0, tb_value_integer(ok ? 1 : 0)); self->dirty = false; } static void eval_find(TinyBlocks_Block* self, TinyBlocks* rt) { - char* s = tb_regex_find(in_text(self, rt, 0), in_text(self, rt, 1)); + char* s = tb_regex_find(tb_block_input_text(self, rt, 0), tb_block_input_text(self, rt, 1)); tb_block_set_output(self, 0, tb_value_text(s ? s : "")); free(s); self->dirty = false; } static void eval_group(TinyBlocks_Block* self, TinyBlocks* rt) { - char* s = tb_regex_group(in_text(self, rt, 0), in_text(self, rt, 1), (int)in_real(self, rt, 2)); + char* s = tb_regex_group(tb_block_input_text(self, rt, 0), tb_block_input_text(self, rt, 1), (int)tb_block_input_number(self, rt, 2, 0)); tb_block_set_output(self, 0, tb_value_text(s ? s : "")); free(s); self->dirty = false; } static void eval_count(TinyBlocks_Block* self, TinyBlocks* rt) { - tb_block_set_output(self, 0, tb_value_integer(tb_regex_count(in_text(self, rt, 0), in_text(self, rt, 1)))); + tb_block_set_output(self, 0, tb_value_integer(tb_regex_count(tb_block_input_text(self, rt, 0), tb_block_input_text(self, rt, 1)))); self->dirty = false; } static void eval_groups(TinyBlocks_Block* self, TinyBlocks* rt) { - tb_block_set_output(self, 0, tb_value_integer(tb_regex_group_count("", in_text(self, rt, 1)))); + tb_block_set_output(self, 0, tb_value_integer(tb_regex_group_count("", tb_block_input_text(self, rt, 1)))); self->dirty = false; } static void eval_replace(TinyBlocks_Block* self, TinyBlocks* rt) { - char* s = tb_regex_replace(in_text(self, rt, 0), in_text(self, rt, 1), in_text(self, rt, 2)); + char* s = tb_regex_replace(tb_block_input_text(self, rt, 0), tb_block_input_text(self, rt, 1), tb_block_input_text(self, rt, 2)); tb_block_set_output(self, 0, tb_value_text(s ? s : "")); free(s); self->dirty = false; } static void eval_split(TinyBlocks_Block* self, TinyBlocks* rt) { int n = 0; - char** parts = tb_regex_split(in_text(self, rt, 0), in_text(self, rt, 1), &n); + char** parts = tb_regex_split(tb_block_input_text(self, rt, 0), tb_block_input_text(self, rt, 1), &n); TinyBlocks_JsonValue* arr = tb_json_array(); if (arr) { for (int i = 0; parts && parts[i]; i++) { diff --git a/libraries/tinyblocks-string/src/tb_string.c b/libraries/tinyblocks-string/src/tb_string.c index 552a119..6af4cf0 100644 --- a/libraries/tinyblocks-string/src/tb_string.c +++ b/libraries/tinyblocks-string/src/tb_string.c @@ -159,23 +159,7 @@ TB_API bool tb_string_is_number(const char* s) { /* -- Entradas ------------------------------------------------------------- */ /* Texto da entrada i (numeros viram texto). Fica valido ate o proximo uso. */ -static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i, char* buf, size_t cap) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - switch (v.kind) { - case TB_VALUE_TEXT: return v.as.text ? v.as.text : ""; - case TB_VALUE_INTEGER: snprintf(buf, cap, "%lld", (long long)v.as.integer); return buf; - case TB_VALUE_REAL: snprintf(buf, cap, "%g", v.as.real); return buf; - default: return ""; - } -} -static long long in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - if (v.kind == TB_VALUE_INTEGER) return v.as.integer; - if (v.kind == TB_VALUE_REAL) return (long long)v.as.real; - if (v.kind == TB_VALUE_TEXT && v.as.text) return strtoll(v.as.text, NULL, 10); - return 0; -} static void out_owned(TinyBlocks_Block* self, char* s) { tb_block_set_output(self, 0, tb_value_text(s ? s : "")); @@ -188,47 +172,41 @@ static void out_bool(TinyBlocks_Block* self, bool b) { self->dirty = false; } -#define TXT(i) in_text(self, rt, (i), b##i, sizeof(b##i)) +#define TXT(i) tb_block_input_text(self, rt, (i)) /* -- Blocos --------------------------------------------------------------- */ -static void eval_upper(TinyBlocks_Block* self, TinyBlocks* rt) { char b0[64]; out_owned(self, tb_string_upper(TXT(0))); } -static void eval_lower(TinyBlocks_Block* self, TinyBlocks* rt) { char b0[64]; out_owned(self, tb_string_lower(TXT(0))); } -static void eval_trim(TinyBlocks_Block* self, TinyBlocks* rt) { char b0[64]; out_owned(self, tb_string_trim(TXT(0))); } -static void eval_reverse(TinyBlocks_Block* self, TinyBlocks* rt) { char b0[64]; out_owned(self, tb_string_reverse(TXT(0))); } +static void eval_upper(TinyBlocks_Block* self, TinyBlocks* rt) { out_owned(self, tb_string_upper(TXT(0))); } +static void eval_lower(TinyBlocks_Block* self, TinyBlocks* rt) { out_owned(self, tb_string_lower(TXT(0))); } +static void eval_trim(TinyBlocks_Block* self, TinyBlocks* rt) { out_owned(self, tb_string_trim(TXT(0))); } +static void eval_reverse(TinyBlocks_Block* self, TinyBlocks* rt) { out_owned(self, tb_string_reverse(TXT(0))); } static void eval_sub(TinyBlocks_Block* self, TinyBlocks* rt) { - char b0[64]; - out_owned(self, tb_string_sub(TXT(0), in_int(self, rt, 1), in_int(self, rt, 2))); + out_owned(self, tb_string_sub(TXT(0), (long long)tb_block_input_number(self, rt, 1, 0), (long long)tb_block_input_number(self, rt, 2, 0))); } static void eval_letter(TinyBlocks_Block* self, TinyBlocks* rt) { - char b0[64]; - long long k = in_int(self, rt, 1); + long long k = (long long)tb_block_input_number(self, rt, 1, 0); out_owned(self, tb_string_sub(TXT(0), k, k)); } static void eval_length(TinyBlocks_Block* self, TinyBlocks* rt) { - char b0[64]; - tb_block_set_output(self, 0, tb_value_integer((int64_t)tb_string_length(TXT(0)))); + tb_block_set_output(self, 0, tb_value_integer((int64_t)tb_string_length(TXT(0)))); self->dirty = false; } static void eval_find(TinyBlocks_Block* self, TinyBlocks* rt) { - char b0[64], b1[64]; - tb_block_set_output(self, 0, tb_value_integer(tb_string_find(TXT(0), TXT(1)))); + tb_block_set_output(self, 0, tb_value_integer(tb_string_find(TXT(0), TXT(1)))); self->dirty = false; } static void eval_replace(TinyBlocks_Block* self, TinyBlocks* rt) { - char b0[64], b1[64], b2[64]; - out_owned(self, tb_string_replace(TXT(0), TXT(1), TXT(2))); + out_owned(self, tb_string_replace(TXT(0), TXT(1), TXT(2))); } static void eval_repeat(TinyBlocks_Block* self, TinyBlocks* rt) { - char b0[64]; - const char* s = TXT(0); - long long k = in_int(self, rt, 1); + const char* s = TXT(0); + long long k = (long long)tb_block_input_number(self, rt, 1, 0); size_t n = strlen(s); if (k < 0) k = 0; if (n && (size_t)k > STR_MAX_OUT / n) k = (long long)(STR_MAX_OUT / n); @@ -250,8 +228,7 @@ static void push_piece(TinyBlocks_JsonValue* arr, const char* s, size_t n) { /* Divide em lista JSON; separador vazio = uma letra por item. "a,,b" -> ["a","","b"]; "a," -> ["a",""]; "" -> [] */ static void eval_split(TinyBlocks_Block* self, TinyBlocks* rt) { - char b0[64], b1[64]; - const char* s = TXT(0); + const char* s = TXT(0); const char* sep = TXT(1); size_t sl = strlen(sep), n = strlen(s); TinyBlocks_JsonValue* arr = tb_json_array(); @@ -279,36 +256,31 @@ static void eval_split(TinyBlocks_Block* self, TinyBlocks* rt) { } static void eval_contains(TinyBlocks_Block* self, TinyBlocks* rt) { - char b0[64], b1[64]; - char* a = tb_string_lower(TXT(0)); + char* a = tb_string_lower(TXT(0)); char* w = tb_string_lower(TXT(1)); out_bool(self, a && w && strstr(a, w) != NULL); free(a); free(w); } static void eval_starts(TinyBlocks_Block* self, TinyBlocks* rt) { - char b0[64], b1[64]; - const char* s = TXT(0); + const char* s = TXT(0); const char* w = TXT(1); out_bool(self, strncmp(s, w, strlen(w)) == 0); } static void eval_ends(TinyBlocks_Block* self, TinyBlocks* rt) { - char b0[64], b1[64]; - const char* s = TXT(0); + const char* s = TXT(0); const char* w = TXT(1); size_t n = strlen(s), m = strlen(w); out_bool(self, m <= n && strcmp(s + n - m, w) == 0); } static void eval_isnum(TinyBlocks_Block* self, TinyBlocks* rt) { - char b0[64]; - out_bool(self, tb_string_is_number(TXT(0))); + out_bool(self, tb_string_is_number(TXT(0))); } static void eval_isempty(TinyBlocks_Block* self, TinyBlocks* rt) { - char b0[64]; - char* t = tb_string_trim(TXT(0)); + char* t = tb_string_trim(TXT(0)); out_bool(self, !t || !t[0]); free(t); } @@ -347,15 +319,14 @@ static size_t u8_encode(long c, char* out) { /* "código da letra (A)" -> 65 (Unicode, "ç" = 231) */ static void eval_code(TinyBlocks_Block* self, TinyBlocks* rt) { - char b0[64]; - tb_block_set_output(self, 0, tb_value_integer(u8_decode(TXT(0)))); + tb_block_set_output(self, 0, tb_value_integer(u8_decode(TXT(0)))); self->dirty = false; } /* "letra do código (65)" -> "A" */ static void eval_from_code(TinyBlocks_Block* self, TinyBlocks* rt) { char buf[5]; - size_t n = u8_encode((long)in_int(self, rt, 0), buf); + size_t n = u8_encode((long)(long long)tb_block_input_number(self, rt, 0, 0), buf); out_owned(self, dup_n(buf, n)); } diff --git a/libraries/tinyblocks-svg/src/tb_svg.c b/libraries/tinyblocks-svg/src/tb_svg.c index 67ad2a9..c35c821 100644 --- a/libraries/tinyblocks-svg/src/tb_svg.c +++ b/libraries/tinyblocks-svg/src/tb_svg.c @@ -453,33 +453,22 @@ TinyBlocks_Image* tb_svg_render(const char* svg, int w, int h) { /* -- Blocos -------------------------------------------------------------- */ -static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : ""; -} -static double in_real(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - if (v.kind == TB_VALUE_REAL) return v.as.real; - if (v.kind == TB_VALUE_INTEGER) return (double)v.as.integer; - return 0.0; -} -static int in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { return (int)in_real(self, rt, i); } static void eval_parse(TinyBlocks_Block* self, TinyBlocks* rt) { - TinyBlocks_VectorData* v = tb_svg_parse(in_text(self, rt, 0), NULL, NULL); + TinyBlocks_VectorData* v = tb_svg_parse(tb_block_input_text(self, rt, 0), NULL, NULL); tb_block_set_output(self, 0, tb_value_vector(v)); tb_vector_data_release(v); self->dirty = false; } static void eval_render(TinyBlocks_Block* self, TinyBlocks* rt) { - TinyBlocks_Image* im = tb_svg_render(in_text(self, rt, 0), in_int(self, rt, 1), in_int(self, rt, 2)); + TinyBlocks_Image* im = tb_svg_render(tb_block_input_text(self, rt, 0), (int)tb_block_input_number(self, rt, 1, 0), (int)tb_block_input_number(self, rt, 2, 0)); if (!im) { tinyblocks_set_error(rt, "%s", tb_svg_last_error()); self->dirty = false; return; } tb_block_set_output(self, 0, tb_value_image(im)); tb_image_release(im); self->dirty = false; } static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) { - const char* path = in_text(self, rt, 0); + const char* path = tb_block_input_text(self, rt, 0); FILE* f = path && path[0] ? fopen(path, "rb") : NULL; if (!f) { tinyblocks_set_error(rt, "nao abriu '%s'", path); self->dirty = false; return; } fseek(f, 0, SEEK_END); long n = ftell(f); fseek(f, 0, SEEK_SET); diff --git a/libraries/tinyblocks-syntax/src/tb_syntax.c b/libraries/tinyblocks-syntax/src/tb_syntax.c index 3bcc8a2..ff9de05 100644 --- a/libraries/tinyblocks-syntax/src/tb_syntax.c +++ b/libraries/tinyblocks-syntax/src/tb_syntax.c @@ -436,20 +436,16 @@ char* tb_syntax_highlight(const char* text, const char* lang) { } /* ---------------------------------------------------------------- Blocos */ -static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : ""; -} static void eval_highlight(TinyBlocks_Block* self, TinyBlocks* rt) { - char* x = tb_syntax_highlight(in_text(self, rt, 0), in_text(self, rt, 1)); + char* x = tb_syntax_highlight(tb_block_input_text(self, rt, 0), tb_block_input_text(self, rt, 1)); tb_block_set_output(self, 0, tb_value_text(x ? x : "")); free(x); self->dirty = false; } static void eval_inline(TinyBlocks_Block* self, TinyBlocks* rt) { - char* x = tb_syntax_highlight_inline(in_text(self, rt, 0), in_text(self, rt, 1)); + char* x = tb_syntax_highlight_inline(tb_block_input_text(self, rt, 0), tb_block_input_text(self, rt, 1)); tb_block_set_output(self, 0, tb_value_text(x ? x : "")); free(x); self->dirty = false; diff --git a/libraries/tinyblocks-variables/src/tb_variables.c b/libraries/tinyblocks-variables/src/tb_variables.c index 70bdf9a..4659d33 100644 --- a/libraries/tinyblocks-variables/src/tb_variables.c +++ b/libraries/tinyblocks-variables/src/tb_variables.c @@ -17,43 +17,29 @@ #include #include "tb_variables.h" -static void input_name(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i, - char* out, size_t cap) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - /* value_to_str reduzido */ - switch (v.kind) { - case TB_VALUE_INTEGER: snprintf(out, cap, "%lld", (long long)v.as.integer); break; - case TB_VALUE_REAL: snprintf(out, cap, "%g", v.as.real); break; - case TB_VALUE_TEXT: snprintf(out, cap, "%s", v.as.text ? v.as.text : ""); break; - default: out[0] = 0; break; - } -} static void eval_var_get(TinyBlocks_Block* self, TinyBlocks* rt) { - char name[64]; input_name(self, rt, 0, name, sizeof(name)); + const char* name = tb_block_input_text(self, rt, 0); const TinyBlocks_Value* v = tb_vars_get(name); tb_block_set_output(self, 0, v ? tb_value_clone(*v) : tb_value_integer(0)); self->dirty = false; } static void eval_var_set(TinyBlocks_Block* self, TinyBlocks* rt) { - char name[64]; input_name(self, rt, 0, name, sizeof(name)); + const char* name = tb_block_input_text(self, rt, 0); tb_vars_set(name, tb_block_get_input(self, rt, 1)); self->dirty = false; } +/* Numero de uma variavel (NULL = nao existe = 0) */ static double as_num(const TinyBlocks_Value* v) { - if (!v) return 0; - if (v->kind == TB_VALUE_INTEGER) return (double)v->as.integer; - if (v->kind == TB_VALUE_REAL) return v->as.real; - if (v->kind == TB_VALUE_TEXT && v->as.text) return strtod(v->as.text, NULL); - return 0; + return v ? tb_value_as_number(*v, 0) : 0; } static void eval_var_change(TinyBlocks_Block* self, TinyBlocks* rt) { - char name[64]; input_name(self, rt, 0, name, sizeof(name)); + const char* name = tb_block_input_text(self, rt, 0); TinyBlocks_Value by = tb_block_get_input(self, rt, 1); - double a = as_num(tb_vars_get(name)), d = as_num(&by); + double a = as_num(tb_vars_get(name)), d = tb_value_as_number(by, 0); TinyBlocks_Value nv = (a == floor(a) && d == floor(d)) ? tb_value_integer((long long)llround(a + d)) : tb_value_real(a + d); @@ -64,7 +50,7 @@ static void eval_var_change(TinyBlocks_Block* self, TinyBlocks* rt) { /* "crie variável local [x] com valor (v)": so deste script/chamada */ static void eval_var_local(TinyBlocks_Block* self, TinyBlocks* rt) { - char name[64]; input_name(self, rt, 0, name, sizeof(name)); + const char* name = tb_block_input_text(self, rt, 0); TinyBlocks_Value v = tb_block_get_input(self, rt, 1); tb_vars_declare_local(name, v.kind == TB_VALUE_NONE ? tb_value_integer(0) : v); self->dirty = false; @@ -72,7 +58,7 @@ static void eval_var_local(TinyBlocks_Block* self, TinyBlocks* rt) { /* "crie variável global [x] com valor (v)": visivel em todo lugar */ static void eval_var_global(TinyBlocks_Block* self, TinyBlocks* rt) { - char name[64]; input_name(self, rt, 0, name, sizeof(name)); + const char* name = tb_block_input_text(self, rt, 0); TinyBlocks_Value v = tb_block_get_input(self, rt, 1); tb_vars_declare_global(name, v.kind == TB_VALUE_NONE ? tb_value_integer(0) : v); self->dirty = false; @@ -80,7 +66,7 @@ static void eval_var_global(TinyBlocks_Block* self, TinyBlocks* rt) { /* "apague a variável [x]": a mais interna com esse nome */ static void eval_var_delete(TinyBlocks_Block* self, TinyBlocks* rt) { - char name[64]; input_name(self, rt, 0, name, sizeof(name)); + const char* name = tb_block_input_text(self, rt, 0); if (!tb_vars_delete(name) && name[0]) tinyblocks_set_error(rt, "variável '%s' não existe", name); self->dirty = false; diff --git a/libraries/tinyblocks-vector/src/tb_vector.c b/libraries/tinyblocks-vector/src/tb_vector.c index a16dfe6..2d29b15 100644 --- a/libraries/tinyblocks-vector/src/tb_vector.c +++ b/libraries/tinyblocks-vector/src/tb_vector.c @@ -79,26 +79,10 @@ static TinyBlocks_Value value_from_item(const TinyBlocks_JsonValue* j) { } } -/* Texto do item (para juntar/comparar) */ +/* Texto do item (para juntar/comparar): pela classe de valor do JSON */ static void item_text(const TinyBlocks_JsonValue* j, char* out, size_t cap) { - out[0] = 0; - if (!j) return; - switch (tb_json_kind(j)) { - case TB_JSON_NUMBER: { - double d = tb_json_as_number(j, 0); - if (d == floor(d) && fabs(d) < 1e15) snprintf(out, cap, "%lld", (long long)d); - else snprintf(out, cap, "%g", d); - break; - } - case TB_JSON_STRING: snprintf(out, cap, "%s", tb_json_as_string(j)); break; - case TB_JSON_BOOL: snprintf(out, cap, "%s", tb_json_as_bool(j, false) ? "1" : "0"); break; - case TB_JSON_NULL: break; - default: { - char* s = tb_json_stringify(j); - snprintf(out, cap, "%s", s ? s : ""); - free(s); - } - } + TinyBlocks_Value v = { .kind = TB_VALUE_JSON, .as.json = (TinyBlocks_JsonValue*)j }; /* emprestado */ + tb_value_to_text(v, out, cap); } static bool item_number(const TinyBlocks_JsonValue* j, double* d) { @@ -185,13 +169,6 @@ static TinyBlocks_JsonValue* in_list(TinyBlocks_Block* self, TinyBlocks* rt, uin return tb_vector_from_value(tb_block_get_input(self, rt, i)); } -static long long in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - if (v.kind == TB_VALUE_INTEGER) return v.as.integer; - if (v.kind == TB_VALUE_REAL) return (long long)llround(v.as.real); - if (v.kind == TB_VALUE_TEXT && v.as.text) return strtoll(v.as.text, NULL, 10); - return 0; -} static TinyBlocks_JsonValue* in_item(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { TinyBlocks_JsonValue* it = item_from_value(tb_block_get_input(self, rt, i)); @@ -232,7 +209,7 @@ static void eval_of(TinyBlocks_Block* self, TinyBlocks* rt) { /* "números de A até B" (inclusivo, passo 1 ou -1) */ static void eval_range(TinyBlocks_Block* self, TinyBlocks* rt) { - long long a = in_int(self, rt, 0), b = in_int(self, rt, 1); + long long a = tb_block_input_integer(self, rt, 0, 0), b = tb_block_input_integer(self, rt, 1, 0); long long step = a <= b ? 1 : -1; long long n = (b - a) * step + 1; if (n > LIST_MAX_RANGE) n = LIST_MAX_RANGE; @@ -253,7 +230,7 @@ static void eval_add(TinyBlocks_Block* self, TinyBlocks* rt) { static void eval_insert(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_JsonValue* src = in_list(self, rt, 0); uint32_t n = src ? tb_json_array_length(src) : 0; - long long pos = in_int(self, rt, 2); + long long pos = tb_block_input_integer(self, rt, 2, 0); if (pos < 1) pos = 1; if (pos > (long long)n + 1) pos = (long long)n + 1; TinyBlocks_JsonValue* arr = list_copy(src, 0, (uint32_t)(pos - 1)); @@ -268,7 +245,7 @@ static void eval_insert(TinyBlocks_Block* self, TinyBlocks* rt) { static void eval_remove(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_JsonValue* src = in_list(self, rt, 0); uint32_t n = src ? tb_json_array_length(src) : 0; - long long k = in_int(self, rt, 1); + long long k = tb_block_input_integer(self, rt, 1, 0); TinyBlocks_JsonValue* arr = tb_json_array(); for (uint32_t i = 0; arr && i < n; i++) if ((long long)i + 1 != k) push_owned(arr, tb_json_array_get(src, i)); @@ -279,7 +256,7 @@ static void eval_remove(TinyBlocks_Block* self, TinyBlocks* rt) { static void eval_replace(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_JsonValue* src = in_list(self, rt, 0); uint32_t n = src ? tb_json_array_length(src) : 0; - long long k = in_int(self, rt, 1); + long long k = tb_block_input_integer(self, rt, 1, 0); TinyBlocks_JsonValue* arr = tb_json_array(); for (uint32_t i = 0; arr && i < n; i++) { if ((long long)i + 1 == k) push_owned(arr, in_item(self, rt, 2)); @@ -291,7 +268,7 @@ static void eval_replace(TinyBlocks_Block* self, TinyBlocks* rt) { static void eval_item(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_JsonValue* src = in_list(self, rt, 0); - long long k = in_int(self, rt, 1); + long long k = tb_block_input_integer(self, rt, 1, 0); uint32_t n = src ? tb_json_array_length(src) : 0; TinyBlocks_JsonValue* it = (k >= 1 && k <= (long long)n) ? tb_json_array_get(src, (uint32_t)(k - 1)) : NULL; tb_block_set_output(self, 0, value_from_item(it)); @@ -337,7 +314,7 @@ static void eval_contains(TinyBlocks_Block* self, TinyBlocks* rt) { /* "itens A a B de L" (inclusivo, cortado aos limites) */ static void eval_slice(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_JsonValue* src = in_list(self, rt, 0); - long long a = in_int(self, rt, 1), b = in_int(self, rt, 2); + long long a = tb_block_input_integer(self, rt, 1, 0), b = tb_block_input_integer(self, rt, 2, 0); if (a < 1) a = 1; TinyBlocks_JsonValue* arr = (b >= a) ? list_copy(src, (uint32_t)(a - 1), (uint32_t)(b > UINT32_MAX ? UINT32_MAX : b)) : tb_json_array(); @@ -472,7 +449,7 @@ static long long in_pos(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i, uint if (v.kind == TB_VALUE_TEXT && v.as.text && (!strcasecmp(v.as.text, "ultimo") || !strcasecmp(v.as.text, "último") || !strcasecmp(v.as.text, "last"))) return allow_end ? (long long)n + 1 : (long long)n; - long long k = in_int(self, rt, i); + long long k = tb_block_input_integer(self, rt, i, 0); long long hi = allow_end ? (long long)n + 1 : (long long)n; return (k >= 1 && k <= hi) ? k : 0; } diff --git a/libraries/tinyblocks-vg/src/tb_vg.c b/libraries/tinyblocks-vg/src/tb_vg.c index c6ab537..e522466 100644 --- a/libraries/tinyblocks-vg/src/tb_vg.c +++ b/libraries/tinyblocks-vg/src/tb_vg.c @@ -198,13 +198,6 @@ bool tb_vg_polygon(TinyBlocks_VectorData* v, const double* xy, int n, bool close /* -- Blocos -------------------------------------------------------------- */ -static double in_real(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { - TinyBlocks_Value v = tb_block_get_input(self, rt, i); - if (v.kind == TB_VALUE_REAL) return v.as.real; - if (v.kind == TB_VALUE_INTEGER) return (double)v.as.integer; - return 0.0; -} -static int in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { return (int)in_real(self, rt, i); } static TinyBlocks_VectorData* in_vec(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { TinyBlocks_Value v = tb_block_get_input(self, rt, i); return v.kind == TB_VALUE_VECTOR ? v.as.vector : NULL; @@ -225,27 +218,27 @@ static void eval_new(TinyBlocks_Block* self, TinyBlocks* rt) { } static void eval_line(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_VectorData* v = in_vec(self, rt, 0); - if (v) tb_vg_line(v, in_real(self,rt,1), in_real(self,rt,2), in_real(self,rt,3), in_real(self,rt,4), - f01(in_real(self,rt,5)), f01(in_real(self,rt,6)), f01(in_real(self,rt,7)), 1.f, - in_real(self,rt,8)); + if (v) tb_vg_line(v, tb_block_input_number(self,rt,1, 0), tb_block_input_number(self,rt,2, 0), tb_block_input_number(self,rt,3, 0), tb_block_input_number(self,rt,4, 0), + f01(tb_block_input_number(self,rt,5, 0)), f01(tb_block_input_number(self,rt,6, 0)), f01(tb_block_input_number(self,rt,7, 0)), 1.f, + tb_block_input_number(self,rt,8, 0)); out_vec(self, v); } static void eval_rect(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_VectorData* v = in_vec(self, rt, 0); - if (v) tb_vg_rect(v, in_real(self,rt,1), in_real(self,rt,2), in_real(self,rt,3), in_real(self,rt,4), - f01(in_real(self,rt,5)), f01(in_real(self,rt,6)), f01(in_real(self,rt,7)), 1.f); + if (v) tb_vg_rect(v, tb_block_input_number(self,rt,1, 0), tb_block_input_number(self,rt,2, 0), tb_block_input_number(self,rt,3, 0), tb_block_input_number(self,rt,4, 0), + f01(tb_block_input_number(self,rt,5, 0)), f01(tb_block_input_number(self,rt,6, 0)), f01(tb_block_input_number(self,rt,7, 0)), 1.f); out_vec(self, v); } static void eval_circle(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_VectorData* v = in_vec(self, rt, 0); - if (v) tb_vg_circle(v, in_real(self,rt,1), in_real(self,rt,2), in_real(self,rt,3), - f01(in_real(self,rt,4)), f01(in_real(self,rt,5)), f01(in_real(self,rt,6)), 1.f); + if (v) tb_vg_circle(v, tb_block_input_number(self,rt,1, 0), tb_block_input_number(self,rt,2, 0), tb_block_input_number(self,rt,3, 0), + f01(tb_block_input_number(self,rt,4, 0)), f01(tb_block_input_number(self,rt,5, 0)), f01(tb_block_input_number(self,rt,6, 0)), 1.f); out_vec(self, v); } /* VG.Render (vetor, largura, altura, escala) -> imagem */ static void eval_render(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_VectorData* v = in_vec(self, rt, 0); - TinyBlocks_Image* im = tb_vg_render(v, in_int(self,rt,1), in_int(self,rt,2), (float)in_real(self,rt,3)); + TinyBlocks_Image* im = tb_vg_render(v, (int)tb_block_input_number(self,rt,1, 0), (int)tb_block_input_number(self,rt,2, 0), (float)tb_block_input_number(self,rt,3, 0)); if (!im) { self->dirty = false; return; } tb_block_set_output(self, 0, tb_value_image(im)); tb_image_release(im); @@ -271,8 +264,8 @@ static void eval_polygon(TinyBlocks_Block* self, TinyBlocks* rt) { if (p) tb_json_release(p); } if (m >= 4) - tb_vg_polygon(v, xy, m/2, in_int(self, rt, 2) != 0, - f01(in_real(self,rt,3)), f01(in_real(self,rt,4)), f01(in_real(self,rt,5)), 1.f); + tb_vg_polygon(v, xy, m/2, (int)tb_block_input_number(self, rt, 2, 0) != 0, + f01(tb_block_input_number(self,rt,3, 0)), f01(tb_block_input_number(self,rt,4, 0)), f01(tb_block_input_number(self,rt,5, 0)), 1.f); free(xy); } out_vec(self, v); diff --git a/src/tb_block.c b/src/tb_block.c index e834ade..a04cce9 100644 --- a/src/tb_block.c +++ b/src/tb_block.c @@ -1,3 +1,4 @@ +#include #include #include #include "tinyblocks/tinyblocks.h" @@ -127,8 +128,10 @@ void tb_block_destroy(TinyBlocks_Block* block, TinyBlocks* rt) tb_free(rt, block->name); /* Libera valores de default dos inputs */ - for (uint32_t i = 0; i < block->input_count; ++i) + for (uint32_t i = 0; i < block->input_count; ++i) { tb_value_free(&block->inputs[i].default_value); + free(block->inputs[i].text_cache); + } tb_free(rt, block->inputs); for (uint32_t i = 0; i < block->output_count; ++i) @@ -230,6 +233,33 @@ TinyBlocks_Value tb_block_get_input(TinyBlocks_Block* self, TinyBlocks* rt, return conn->default_value; } +/* -- Entradas convertidas (polimorficas pela classe do valor) --- */ +const char* tb_block_input_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) +{ + TinyBlocks_Value v = tb_block_get_input(self, rt, i); + if (v.kind == TB_VALUE_TEXT) return v.as.text ? v.as.text : ""; + if (!self || i >= self->input_count) return ""; + TinyBlocks_Connection* c = &self->inputs[i]; + free(c->text_cache); + c->text_cache = tb_value_to_string(v); + return c->text_cache ? c->text_cache : ""; +} + +double tb_block_input_number(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i, double def) +{ + return tb_value_as_number(tb_block_get_input(self, rt, i), def); +} + +int64_t tb_block_input_integer(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i, int64_t def) +{ + return tb_value_as_integer(tb_block_get_input(self, rt, i), def); +} + +bool tb_block_input_truthy(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) +{ + return tb_value_truthy(tb_block_get_input(self, rt, i)); +} + /* Toma ownership de v - o caller NÃO deve liberar v depois desta chamada. Se precisar manter v, passe tb_value_clone(v) em vez de v. */ void tb_block_set_output(TinyBlocks_Block* self, uint32_t output_idx, diff --git a/src/tb_type_block.c b/src/tb_type_block.c index 41f12e8..f4fc8a3 100644 --- a/src/tb_type_block.c +++ b/src/tb_type_block.c @@ -139,6 +139,13 @@ TinyBlocks_TypeBlock* tb_type_block_create( } tb_rt_register_type(rt, tb); + /* Convencao das libraries: saida cujo nome termina em '?' ("é pasta?") + e booleana. Vira o traco TB_SLOT_BOOLEAN aqui, uma vez so. */ + for (uint32_t i = 0; i < output_count; ++i) { + const char* n = tb->output_descs[i].name; + size_t len = n ? strlen(n) : 0; + if (len && n[len - 1] == '?') tb->output_flags[i] |= TB_SLOT_BOOLEAN; + } return tb; fail: