Encaixes de bloco: o bloco encaixado vai como codigo (JSON)
Uma entrada TB_SLOT_CODE recebe o CODIGO do bloco encaixado, sem
executa-lo; TB_SLOT_STACK recebe uma pilha de comandos. No editor o
encaixe e uma cavidade mais escura com a forma do bloco que cabe nela
(pilula, hexagono ou comando, como a sombra do prototipo no "defina"); o
de pilha fica numa faixa abaixo do rotulo e cresce com a pilha, e os
comandos encaixam no topo dele ao arrastar.
Formato (tb_defs.h): {"type", "inputs": [...], "mouths": [...]}; uma
pilha e uma lista de blocos; campos digitados viram texto ou numero.
Controle ganha "codigo de (...)" e "codigo da pilha (...)". O MCP poe
comandos num encaixe de pilha (into + input). Na notacao do forum o
encaixe de pilha de um comando vem nas linhas de baixo, indentado e
fechado com "end", como um bloco C.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5bae31edf3
commit
a3bae660fb
8 files changed
+311
-18
No files matched your search
@@ -1,6 +1,8 @@
|
||||
/*
|
||||
* TinyBlocks - tb_control.c
|
||||
* Categoria "Controle": repita / repita até que / sempre / se / se-senão.
|
||||
* Categoria "Controle": repita / repita até que / sempre / se / se-senão,
|
||||
* e os repórteres de código ("código de", "código da pilha"), que mostram o
|
||||
* que um encaixe de bloco (TB_SLOT_CODE / TB_SLOT_STACK) entrega.
|
||||
* Cada bloco C diz como roda por uma TinyBlocks_FlowClass (tb_flow.h); quem
|
||||
* executa os scripts so segue as bocas que ela escolhe.
|
||||
*/
|
||||
@@ -53,6 +55,26 @@ static const TinyBlocks_FlowClass s_forever = { 1, { NULL }, true, forever_n
|
||||
static const TinyBlocks_FlowClass s_if = { 1, { NULL }, false, if_next, NULL };
|
||||
static const TinyBlocks_FlowClass s_if_else = { 2, { "senão" }, false, if_else_next, NULL };
|
||||
|
||||
/* -- Codigo como valor ---
|
||||
* A entrada e um encaixe: o editor entrega o codigo (JSON) do que estiver
|
||||
* encaixado, sem executar; o bloco so o devolve. */
|
||||
static void eval_code(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
|
||||
tb_block_set_output(self, 0, tb_value_clone(v));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static bool reg_code(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||
const char* label, uint32_t flags) {
|
||||
static const TinyBlocks_SlotDesc in[] = {{"bloco", TB_VALUE_NONE}};
|
||||
static const TinyBlocks_SlotDesc out[] = {{"código", TB_VALUE_JSON}};
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, 1, out, 1, eval_code);
|
||||
if (!t) return false;
|
||||
tb_type_block_set_input_flags(t, 0, flags);
|
||||
tb_type_block_set_label(t, label, NULL);
|
||||
return tb_library_add(lib, &t->base);
|
||||
}
|
||||
|
||||
/* -- Registro --- */
|
||||
|
||||
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||
@@ -84,6 +106,10 @@ static bool control_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
"Executa o corpo se a condicao for verdadeira");
|
||||
TB_BLOCK_META("Control.IfElse", "if_else_next",
|
||||
"Executa o corpo 'entao' ou 'senao' conforme a condicao");
|
||||
TB_BLOCK_META("Control.Code", "eval_code",
|
||||
"O codigo (JSON) do bloco encaixado, sem executa-lo");
|
||||
TB_BLOCK_META("Control.StackCode", "eval_code",
|
||||
"O codigo (JSON) da pilha encaixada, sem executa-la");
|
||||
|
||||
static const TinyBlocks_SlotDesc times_in[] = {{"vezes", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc cond_in[] = {{"condição", TB_VALUE_INTEGER}};
|
||||
@@ -91,7 +117,9 @@ static bool control_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
&& reg(rt, lib, "Control.RepeatUntil", cond_in, 1, &s_until, true)
|
||||
&& reg(rt, lib, "Control.Forever", NULL, 0, &s_forever, false)
|
||||
&& reg(rt, lib, "Control.If", cond_in, 1, &s_if, true)
|
||||
&& reg(rt, lib, "Control.IfElse", cond_in, 1, &s_if_else, true);
|
||||
&& reg(rt, lib, "Control.IfElse", cond_in, 1, &s_if_else, true)
|
||||
&& reg_code(rt, lib, "Control.Code", "código de %1", TB_SLOT_CODE)
|
||||
&& reg_code(rt, lib, "Control.StackCode", "código da pilha %1", TB_SLOT_STACK);
|
||||
}
|
||||
|
||||
static void control_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
|
||||
@@ -174,6 +174,14 @@ enum { CV_CODE, CV_STAGE };
|
||||
#define BOT_H 22 /* braço inferior dos blocos C */
|
||||
#define MID_H 26 /* braço "senão" */
|
||||
#define MOUTH_MIN 22
|
||||
/* Encaixes de bloco (TB_SLOT_CODE / TB_SLOT_STACK) */
|
||||
#define SOCK_PAD_X 4 /* folga da cavidade em volta do repórter */
|
||||
#define SOCK_PAD_Y 3
|
||||
#define SOCK_EMPTY_H 24
|
||||
#define SOCK_SHADE 0.80f /* cavidade: a cor do bloco, mais escura */
|
||||
#define STK_PAD 6 /* folga da cavidade de pilha */
|
||||
#define STK_MIN_W 120
|
||||
#define STK_MIN_H 26
|
||||
#define NOTCH_X 12 /* encaixe (macho) sob os comandos */
|
||||
#define NOTCH_W 18
|
||||
#define NOTCH_D 4
|
||||
@@ -666,7 +674,7 @@ typedef enum { FI_HEADER, FI_BUTTON, FI_BLOCK } FlyKind;
|
||||
typedef struct { FlyKind kind; int cat; int bi; float y, h; } FlyItem;
|
||||
|
||||
typedef enum { ES_IDLE, ES_PRESS, ES_DRAG, ES_PAN, ES_FLYPRESS, ES_FLYPAN } EdSt;
|
||||
typedef enum { SN_NONE, SN_NEXT, SN_SUB, SN_ABOVE, SN_ARG } SnapKind;
|
||||
typedef enum { SN_NONE, SN_NEXT, SN_SUB, SN_ABOVE, SN_ARG, SN_STACK } SnapKind;
|
||||
enum { CM_DUPLICATE, CM_DELETE, CM_EDIT_DEF, CM_CLEANUP, CM_NEW_COMPOSITE,
|
||||
CM_EXP_JSON, CM_EXP_XML, CM_EXP_CSV, CM_EXP_SB, CM_PASTE_SB,
|
||||
CM_GROUP_NEW, CM_GROUP_STACK, CM_GROUP_RENAME, CM_GROUP_COLOR, CM_GROUP_DELETE,
|
||||
@@ -1108,6 +1116,8 @@ bool is_cap(const TinyBlocks_TypeBlock* t);
|
||||
const char* mouth_label(const TinyBlocks_TypeBlock* t, int k);
|
||||
int input_count(const TinyBlocks_TypeBlock* t);
|
||||
bool slot_is_bool(const TinyBlocks_TypeBlock* t, int i);
|
||||
bool slot_is_code(const TinyBlocks_TypeBlock* t, int i);
|
||||
bool slot_is_stack(const TinyBlocks_TypeBlock* t, int i);
|
||||
const char* slot_src(const EdBlock* b, int s);
|
||||
|
||||
/* -- Executor: arvore do runtime e compilador de expressoes (tb_editor_jit.c) -- */
|
||||
|
||||
@@ -232,6 +232,21 @@ bool slot_is_bool(const TinyBlocks_TypeBlock* t, int i) {
|
||||
return i >= 0 && (tb_type_block_input_flags(t, (uint32_t)i) & TB_SLOT_BOOLEAN);
|
||||
}
|
||||
|
||||
/* Encaixe de bloco (TB_SLOT_CODE/STACK): o bloco encaixado vai como codigo */
|
||||
bool slot_is_code(const TinyBlocks_TypeBlock* t, int i) {
|
||||
return t && i >= 0 && (tb_type_block_input_flags(t, (uint32_t)i) & (TB_SLOT_CODE | TB_SLOT_STACK));
|
||||
}
|
||||
|
||||
/* Encaixe de pilha: cavidade em forma de comando numa faixa abaixo do rotulo */
|
||||
bool slot_is_stack(const TinyBlocks_TypeBlock* t, int i) {
|
||||
return t && i >= 0 && (tb_type_block_input_flags(t, (uint32_t)i) & TB_SLOT_STACK);
|
||||
}
|
||||
|
||||
static bool has_stack_slot(const TinyBlocks_TypeBlock* t) {
|
||||
for (int i = 0; t && i < (int)t->input_desc_count; i++) if (slot_is_stack(t, i)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool is_lit_text(const TinyBlocks_TypeBlock* t) {
|
||||
return t && t->type_name && strcmp(t->type_name, "Literal.Text") == 0;
|
||||
}
|
||||
@@ -526,7 +541,7 @@ static int copy_tree(Tab* S, int si, Tab* D, bool with_next) {
|
||||
for (int s = 0; s < MAX_INPUTS; s++) n->args[s] = -1;
|
||||
for (int s = 0; s < MAX_INPUTS; s++) {
|
||||
if (o->args[s] < 0) continue;
|
||||
int k = copy_tree(S, o->args[s], D, false);
|
||||
int k = copy_tree(S, o->args[s], D, slot_is_stack(o->type, s));
|
||||
if (k >= 0) blk_link(D, ni, REL_ARG0 + s, k);
|
||||
}
|
||||
for (int k = 0; k < MAX_MOUTHS; k++) {
|
||||
@@ -661,7 +676,8 @@ static void build_parts(EdBlock* b) {
|
||||
if (p[0] == '%' && p[1] >= '1' && p[1] <= '9') {
|
||||
add_part(b, -1, seg, (int)(p - seg));
|
||||
int a = p[1] - '1';
|
||||
if (a < n) { add_part(b, a, NULL, 0); used[a] = true; }
|
||||
if (a < n && slot_is_stack(t, a)) used[a] = true; /* vai abaixo do rotulo */
|
||||
else if (a < n) { add_part(b, a, NULL, 0); used[a] = true; }
|
||||
p += 2; seg = p;
|
||||
} else p++;
|
||||
}
|
||||
@@ -690,7 +706,7 @@ static void build_parts(EdBlock* b) {
|
||||
}
|
||||
/* Campos que o rótulo não cita: "nome [campo]" ao final */
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (used[i]) continue;
|
||||
if (used[i] || slot_is_stack(t, i)) continue;
|
||||
const char* sn = t->input_descs[i].name;
|
||||
if (sn) add_part(b, -1, sn, (int)strlen(sn));
|
||||
add_part(b, i, NULL, 0);
|
||||
@@ -782,7 +798,12 @@ static void measure(Tab* T, int bi) {
|
||||
p->w = tw(part_str(p, buf, sizeof(buf)), FS);
|
||||
} else {
|
||||
int s = p->arg; float aw, ah;
|
||||
if (b->args[s] >= 0) {
|
||||
if (slot_is_code(b->type, s)) { /* cavidade: o filho com folga */
|
||||
if (b->args[s] >= 0) {
|
||||
measure(T, b->args[s]);
|
||||
aw = T->ws[b->args[s]].w + 2 * SOCK_PAD_X; ah = T->ws[b->args[s]].h + 2 * SOCK_PAD_Y;
|
||||
} else { aw = slot_is_bool(b->type, s) ? 44.f : 40.f; ah = SOCK_EMPTY_H; }
|
||||
} else if (b->args[s] >= 0) {
|
||||
measure(T, b->args[s]);
|
||||
aw = T->ws[b->args[s]].w; ah = T->ws[b->args[s]].h;
|
||||
} else slot_size(b, s, &aw, &ah);
|
||||
@@ -801,6 +822,22 @@ static void measure(Tab* T, int bi) {
|
||||
}
|
||||
|
||||
float h = b->row_y + row_h;
|
||||
/* encaixes de pilha: uma faixa cada, abaixo do rotulo */
|
||||
for (int s = 0; s < input_count(b->type); s++) {
|
||||
if (!slot_is_stack(b->type, s)) continue;
|
||||
float iw = STK_MIN_W, ih = STK_MIN_H;
|
||||
if (b->args[s] >= 0) {
|
||||
ih = measure_chain(T, b->args[s]);
|
||||
iw = 0;
|
||||
for (int c = b->args[s]; c >= 0; c = T->ws[c].next) if (T->ws[c].w > iw) iw = T->ws[c].w;
|
||||
}
|
||||
b->slot_x[s] = BLK_PAD;
|
||||
b->slot_y[s] = h;
|
||||
b->slot_w[s] = iw + 2 * STK_PAD;
|
||||
b->slot_h[s] = ih + 2 * STK_PAD;
|
||||
h += b->slot_h[s] + STK_PAD;
|
||||
if (b->slot_x[s] + b->slot_w[s] + BLK_PAD > w) w = b->slot_x[s] + b->slot_w[s] + BLK_PAD;
|
||||
}
|
||||
switch (sh) {
|
||||
case SH_HAT: if (w < HAT_CAP_W + 16) w = HAT_CAP_W + 16; break;
|
||||
case SH_STACK: if (w < 64) w = 64; break;
|
||||
@@ -833,8 +870,15 @@ static void place_chain(Tab* T, int head, float x, float y);
|
||||
static void place(Tab* T, int bi, float x, float y) {
|
||||
EdBlock* b = &T->ws[bi];
|
||||
b->ax = x; b->ay = y;
|
||||
for (int s = 0; s < MAX_INPUTS; s++)
|
||||
if (b->args[s] >= 0) place(T, b->args[s], x + b->slot_x[s], y + b->slot_y[s]);
|
||||
for (int s = 0; s < MAX_INPUTS; s++) {
|
||||
if (b->args[s] < 0) continue;
|
||||
if (slot_is_stack(b->type, s))
|
||||
place_chain(T, b->args[s], x + b->slot_x[s] + STK_PAD, y + b->slot_y[s] + STK_PAD);
|
||||
else if (slot_is_code(b->type, s))
|
||||
place(T, b->args[s], x + b->slot_x[s] + SOCK_PAD_X, y + b->slot_y[s] + SOCK_PAD_Y);
|
||||
else
|
||||
place(T, b->args[s], x + b->slot_x[s], y + b->slot_y[s]);
|
||||
}
|
||||
for (int k = 0; k < MAX_MOUTHS; k++)
|
||||
if (b->sub[k] >= 0) place_chain(T, b->sub[k], x + ARM_W, y + b->sub_y[k]);
|
||||
}
|
||||
@@ -875,7 +919,7 @@ static int hit_one(Tab* T, int bi, float x, float y) {
|
||||
EdBlock* b = &T->ws[bi];
|
||||
for (int s = 0; s < MAX_INPUTS; s++) {
|
||||
if (b->args[s] < 0) continue;
|
||||
int r = hit_one(T, b->args[s], x, y);
|
||||
int r = slot_is_stack(b->type, s) ? hit_chain(T, b->args[s], x, y) : hit_one(T, b->args[s], x, y);
|
||||
if (r >= 0) return r;
|
||||
}
|
||||
for (int k = 0; k < MAX_MOUTHS; k++) {
|
||||
@@ -906,7 +950,7 @@ static int hit_ws(Tab* T, float x, float y) {
|
||||
static int hit_slot(Tab* T, int bi, float x, float y) {
|
||||
EdBlock* b = &T->ws[bi];
|
||||
for (int s = 0; s < input_count(b->type); s++) {
|
||||
if (b->args[s] >= 0) continue;
|
||||
if (b->args[s] >= 0 || slot_is_code(b->type, s)) continue; /* encaixe nao se digita */
|
||||
if (in_rect(x, y, b->ax + b->slot_x[s], b->ay + b->slot_y[s], b->slot_w[s], b->slot_h[s]))
|
||||
return s;
|
||||
}
|
||||
@@ -922,6 +966,26 @@ static bool slot_is_var_name(const TinyBlocks_TypeBlock* t, int s) {
|
||||
return s >= 0 && (tb_type_block_input_flags(t, (uint32_t)s) & TB_SLOT_VARIABLE);
|
||||
}
|
||||
|
||||
/* Encaixe de bloco: uma cavidade mais escura com a forma do bloco que cabe
|
||||
* nela (como a sombra do prototipo no "defina"): pilula, hexagono ou, para
|
||||
* uma pilha, um comando com o encaixe do bloco aparecendo no alto. */
|
||||
static void draw_socket(TinyBlocks_RenderableBlock* rb, const EdBlock* b, int s,
|
||||
float x, float y, Col c, Col d) {
|
||||
float sx = x + b->slot_x[s], sy = y + b->slot_y[s], sw = b->slot_w[s], sh = b->slot_h[s];
|
||||
Col fill = col_mul(c, SOCK_SHADE);
|
||||
if (slot_is_stack(b->type, s)) {
|
||||
rb_fill_rrect(rb, sx, sy, sw, sh, 4, d);
|
||||
rb_fill_rrect(rb, sx + 1, sy + 1, sw - 2, sh - 2, 3, fill);
|
||||
draw_bump(rb, sx + STK_PAD, sy + 1, c, d); /* onde o comando encaixa */
|
||||
} else if (slot_is_bool(b->type, s)) {
|
||||
draw_hex(rb, sx, sy, sw, sh, d);
|
||||
draw_hex(rb, sx + 1, sy + 1, sw - 2, sh - 2, fill);
|
||||
} else {
|
||||
rb_fill_rrect(rb, sx, sy, sw, sh, sh * 0.5f, d);
|
||||
rb_fill_rrect(rb, sx + 1, sy + 1, sw - 2, sh - 2, sh * 0.5f - 1, fill);
|
||||
}
|
||||
}
|
||||
|
||||
static void draw_slot(TinyBlocks_RenderableBlock* rb, const EdBlock* b, int s,
|
||||
float x, float y, Col dark, bool editing) {
|
||||
float sx = x + b->slot_x[s], sy = y + b->slot_y[s];
|
||||
@@ -1072,6 +1136,13 @@ static void ax_block(Ed* ed, Tab* T, int bi, float ox, float oy, uint32_t acts)
|
||||
if (p->arg < 0) continue;
|
||||
int s = p->arg;
|
||||
if (b->args[s] >= 0) { ax_block(ed, T, b->args[s], ox, oy, acts); continue; }
|
||||
if (slot_is_code(b->type, s)) {
|
||||
const char* nm = b->type->input_descs[s].name ? b->type->input_descs[s].name : "";
|
||||
int q = tb_a11y_add(t, TB_A11Y_SLOT, nm, x + b->slot_x[s], y + b->slot_y[s], b->slot_w[s], b->slot_h[s]);
|
||||
tb_a11y_set_description(t, q, "encaixe de bloco: o bloco encaixado vai como código");
|
||||
tb_a11y_set_actions(t, q, TB_A11Y_ACT_DROP);
|
||||
continue;
|
||||
}
|
||||
const char* sname = b->type->input_descs[s].name ? b->type->input_descs[s].name : "";
|
||||
float sx = x + b->slot_x[s], sy = y + b->slot_y[s];
|
||||
if (slot_is_bool(b->type, s)) {
|
||||
@@ -1144,7 +1215,7 @@ static void render_block(Ed* ed, TinyBlocks_RenderableBlock* rb, Tab* T, int bi,
|
||||
break;
|
||||
}
|
||||
case SH_REPORTER: {
|
||||
float rad = b->prev_y > 0 ? 8.f : h*0.5f;
|
||||
float rad = b->prev_y > 0 || has_stack_slot(b->type) ? 8.f : h*0.5f;
|
||||
rb_fill_rrect(rb, x, y, w, h, rad, d);
|
||||
rb_fill_rrect(rb, x+1, y+1, w-2, h-2, rad-1, c);
|
||||
break;
|
||||
@@ -1184,9 +1255,13 @@ static void render_block(Ed* ed, TinyBlocks_RenderableBlock* rb, Tab* T, int bi,
|
||||
}
|
||||
} else if (p->arg < 0)
|
||||
rb_text_c(rb, x + p->x, cy, part_str(p, buf, sizeof(buf)), COL_WHITE, FS);
|
||||
else if (slot_is_code(b->type, p->arg))
|
||||
draw_socket(rb, b, p->arg, x, y, c, d);
|
||||
else if (b->args[p->arg] < 0)
|
||||
draw_slot(rb, b, p->arg, x, y, d, can_edit && ed->edit_in == p->arg);
|
||||
}
|
||||
for (int s = 0; s < input_count(b->type); s++)
|
||||
if (slot_is_stack(b->type, s)) draw_socket(rb, b, s, x, y, c, d);
|
||||
for (int k = 0; k + 1 < shape_mouths(sh); k++)
|
||||
rb_text_c(rb, x + BLK_PAD, y + b->sub_y[k] + b->sub_h[k] + MID_H*0.5f,
|
||||
mouth_label(b->type, k), COL_WHITE, FS);
|
||||
@@ -1198,9 +1273,12 @@ static void render_block(Ed* ed, TinyBlocks_RenderableBlock* rb, Tab* T, int bi,
|
||||
draw_bump(rb, x + ARM_W, y + b->sub_y[k], c, d);
|
||||
}
|
||||
|
||||
/* == Repórteres encaixados === */
|
||||
for (int s = 0; s < MAX_INPUTS; s++)
|
||||
if (b->args[s] >= 0) render_block(ed, rb, T, b->args[s], ox, oy);
|
||||
/* == Repórteres (e pilhas) encaixados === */
|
||||
for (int s = 0; s < MAX_INPUTS; s++) {
|
||||
if (b->args[s] < 0) continue;
|
||||
if (slot_is_stack(b->type, s)) render_chain(ed, rb, T, b->args[s], ox, oy);
|
||||
else render_block(ed, rb, T, b->args[s], ox, oy);
|
||||
}
|
||||
|
||||
/* == Área de on_draw === */
|
||||
if (b->prev_y > 0) {
|
||||
@@ -1287,6 +1365,63 @@ TinyBlocks_TypeBlock* blk_type(Ed* ed, EdBlock* b) {
|
||||
return b->type;
|
||||
}
|
||||
|
||||
/* == Codigo de um encaixe (formato em tb_defs.h, perto de TB_SLOT_CODE) == */
|
||||
static TinyBlocks_JsonValue* code_block(Ed* ed, Tab* T, int bi);
|
||||
|
||||
/* push/set do JSON retem o item: estes entregam a referencia de quem criou */
|
||||
static void code_push(TinyBlocks_JsonValue* arr, TinyBlocks_JsonValue* item) {
|
||||
if (item) { tb_json_array_push(arr, item); tb_json_release(item); }
|
||||
}
|
||||
static void code_set(TinyBlocks_JsonValue* obj, const char* key, TinyBlocks_JsonValue* val) {
|
||||
if (val) { tb_json_object_set(obj, key, val); tb_json_release(val); }
|
||||
}
|
||||
|
||||
static TinyBlocks_JsonValue* code_chain(Ed* ed, Tab* T, int head) {
|
||||
TinyBlocks_JsonValue* arr = tb_json_array();
|
||||
for (int c = head; arr && c >= 0; c = T->ws[c].next) code_push(arr, code_block(ed, T, c));
|
||||
return arr;
|
||||
}
|
||||
|
||||
static TinyBlocks_JsonValue* code_input(Ed* ed, Tab* T, int bi, int s) {
|
||||
EdBlock* b = &T->ws[bi];
|
||||
if (slot_is_stack(b->type, s)) return code_chain(ed, T, b->args[s]);
|
||||
if (b->args[s] >= 0) return code_block(ed, T, b->args[s]);
|
||||
const char* raw = slot_src(b, s);
|
||||
if (!raw[0]) return tb_json_null();
|
||||
TinyBlocks_Value v = value_from_text(raw, b->type->input_descs[s].expected_kind);
|
||||
TinyBlocks_JsonValue* j = v.kind == TB_VALUE_INTEGER ? tb_json_number((double)v.as.integer)
|
||||
: v.kind == TB_VALUE_REAL ? tb_json_number(v.as.real)
|
||||
: tb_json_string(raw);
|
||||
tb_value_free(&v);
|
||||
return j;
|
||||
}
|
||||
|
||||
static TinyBlocks_JsonValue* code_block(Ed* ed, Tab* T, int bi) {
|
||||
EdBlock* b = &T->ws[bi];
|
||||
TinyBlocks_TypeBlock* t = blk_type(ed, b);
|
||||
TinyBlocks_JsonValue* o = tb_json_object();
|
||||
if (!o) return NULL;
|
||||
code_set(o, "type", tb_json_string(b->type_name));
|
||||
TinyBlocks_JsonValue* ins = tb_json_array();
|
||||
for (int s = 0; t && ins && s < input_count(t); s++) code_push(ins, code_input(ed, T, bi, s));
|
||||
code_set(o, "inputs", ins);
|
||||
int mouths = shape_mouths(shape_of(t));
|
||||
if (mouths > 0) {
|
||||
TinyBlocks_JsonValue* m = tb_json_array();
|
||||
for (int k = 0; m && k < mouths; k++) code_push(m, code_chain(ed, T, b->sub[k]));
|
||||
code_set(o, "mouths", m);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
/* Valor de um encaixe: o codigo do que esta encaixado (bloco, pilha ou null) */
|
||||
static TinyBlocks_Value code_value(Ed* ed, Tab* T, int bi, int s) {
|
||||
TinyBlocks_JsonValue* j = code_input(ed, T, bi, s);
|
||||
TinyBlocks_Value v = j ? tb_value_json(j) : tb_value_none();
|
||||
if (j) tb_json_release(j);
|
||||
return v;
|
||||
}
|
||||
|
||||
/* Converte o bloco bi (e seus repórteres) em uma árvore de blocos do runtime */
|
||||
TinyBlocks_Block* build_rt(Ed* ed, Tab* T, int bi) {
|
||||
EdBlock* b = &T->ws[bi];
|
||||
@@ -1305,6 +1440,12 @@ TinyBlocks_Block* build_rt(Ed* ed, Tab* T, int bi) {
|
||||
|
||||
bool lit = is_lit_text(t);
|
||||
for (int s = 0; s < input_count(t); s++) {
|
||||
if (slot_is_code(t, s)) { /* encaixe: o codigo, nao o valor */
|
||||
TinyBlocks_Value v = code_value(ed, T, bi, s);
|
||||
tb_block_set_default(r, (uint32_t)s, v);
|
||||
tb_value_free(&v);
|
||||
continue;
|
||||
}
|
||||
if (b->args[s] >= 0) {
|
||||
TinyBlocks_Block* c = build_rt(ed, T, b->args[s]);
|
||||
if (c && c->output_count > 0) tb_block_connect(r, (uint32_t)s, c, 0);
|
||||
@@ -1384,6 +1525,7 @@ static TinyBlocks_Value eval_reporter(Ed* ed, Tab* T, int bi) {
|
||||
|
||||
static TinyBlocks_Value eval_arg(Ed* ed, Tab* T, int bi, int s) {
|
||||
EdBlock* b = &T->ws[bi];
|
||||
if (slot_is_code(b->type, s)) return code_value(ed, T, bi, s);
|
||||
if (b->args[s] >= 0) return eval_reporter(ed, T, b->args[s]);
|
||||
const char* raw = slot_src(b, s);
|
||||
if (!raw[0] || !b->type) return tb_value_none();
|
||||
@@ -3242,6 +3384,7 @@ static int sb_slot_kind(const char* type, int slot, void* ud) {
|
||||
Ed* ed = (Ed*)ud;
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_find(ed->rt, type);
|
||||
if (!t || slot < 0 || slot >= input_count(t)) return TB_SB_SLOT_TEXT;
|
||||
if (slot_is_stack(t, slot)) return TB_SB_SLOT_STACK;
|
||||
if (slot_is_bool(t, slot)) return TB_SB_SLOT_BOOL;
|
||||
if (!strncmp(type, "User.", 5)) return TB_SB_SLOT_NUM; /* entrada de Meus Blocos: ( ) */
|
||||
TinyBlocks_ValueKind k = t->input_descs[slot].expected_kind;
|
||||
@@ -4638,6 +4781,10 @@ static void render_snap(Ed* ed, TinyBlocks_RenderableBlock* rb, float ox, float
|
||||
float gh = chain_h(T, ed->drag_bi), gx = t->ax, gy;
|
||||
if (ed->snap_kind == SN_NEXT) gy = t->ay + t->h;
|
||||
else if (ed->snap_kind == SN_SUB) { gx = t->ax + ARM_W; gy = t->ay + t->sub_y[ed->snap_idx]; }
|
||||
else if (ed->snap_kind == SN_STACK) {
|
||||
gx = t->ax + t->slot_x[ed->snap_idx] + STK_PAD;
|
||||
gy = t->ay + t->slot_y[ed->snap_idx] + STK_PAD;
|
||||
}
|
||||
else gy = t->ay - gh;
|
||||
rb_fill_rrect(rb, gx + ox, gy + oy, d->w, gh, 4, COLA(0,0,0,45));
|
||||
}
|
||||
@@ -6325,6 +6472,7 @@ static void compute_snap(Ed* ed) {
|
||||
if (!B->used || B->dragging || !B->type) continue;
|
||||
for (int s = 0; s < input_count(B->type); s++) {
|
||||
if (ds == SH_REPORTER && slot_is_bool(B->type, s)) continue;
|
||||
if (slot_is_stack(B->type, s)) continue; /* pilha: so comandos */
|
||||
consider(ed, &best, SN_ARG, i, s, px, py,
|
||||
B->ax + B->slot_x[s], B->ay + B->slot_y[s] + B->slot_h[s]*0.5f);
|
||||
}
|
||||
@@ -6353,6 +6501,15 @@ static void compute_snap(Ed* ed) {
|
||||
if (B->parent < 0 && bs != SH_HAT && !drag_cap)
|
||||
consider(ed, &best, SN_ABOVE, i, 0, d->x, d->y + dh, B->ax, B->ay);
|
||||
}
|
||||
/* topo dos encaixes de pilha (de qualquer bloco, ate de um repórter) */
|
||||
for (int i = 0; ds != SH_HAT && i < T->ws_n; i++) {
|
||||
EdBlock* B = &T->ws[i];
|
||||
if (!B->used || B->dragging || !B->type) continue;
|
||||
for (int s = 0; s < input_count(B->type); s++)
|
||||
if (slot_is_stack(B->type, s) && !(drag_cap && B->args[s] >= 0))
|
||||
consider(ed, &best, SN_STACK, i, s, d->x, d->y,
|
||||
B->ax + B->slot_x[s] + STK_PAD, B->ay + B->slot_y[s] + STK_PAD);
|
||||
}
|
||||
}
|
||||
|
||||
static void apply_snap(Ed* ed) {
|
||||
@@ -6363,6 +6520,7 @@ static void apply_snap(Ed* ed) {
|
||||
case SN_SUB: attach_insert(T, t, REL_SUB0 + ed->snap_idx, bi); break;
|
||||
case SN_ABOVE: attach_above(T, t, bi); break;
|
||||
case SN_ARG: attach_arg(T, t, ed->snap_idx, bi); break;
|
||||
case SN_STACK: attach_insert(T, t, REL_ARG0 + ed->snap_idx, bi); break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -441,7 +441,8 @@ static void t_move_stack(void* user, TbMcpRequest* rq) {
|
||||
|
||||
/* Coloca a pilha solta bi (ja de topo) conforme os argumentos:
|
||||
* below=id embaixo do bloco id (o que estava embaixo vai depois)
|
||||
* into=id, input=n no campo n do bloco id (bi precisa ser reporter)
|
||||
* into=id, input=n no campo n do bloco id (bi precisa ser reporter; num
|
||||
* encaixe de pilha, um comando: vai no topo da pilha)
|
||||
* into=id, mouth=k no comeco da boca k do bloco C id
|
||||
* x, y solta na area (padrao: um lugar livre) */
|
||||
static bool place(TbMcpRequest* rq, Tab* T, int bi) {
|
||||
@@ -481,6 +482,11 @@ static bool place(TbMcpRequest* rq, Tab* T, int bi) {
|
||||
at, input_names(tt, names, sizeof(names)));
|
||||
return false;
|
||||
}
|
||||
if (slot_is_stack(tt, s)) { /* encaixe de pilha: so comandos */
|
||||
if (shape_is_value(sh) || sh == SH_HAT) { tb_mcp_req_error(rq, "Nesse encaixe so entram comandos"); return false; }
|
||||
attach_insert(T, at, REL_ARG0 + s, bi);
|
||||
return true;
|
||||
}
|
||||
if (!shape_is_value(sh)) { tb_mcp_req_error(rq, "So reporteres e booleanos entram em campos"); return false; }
|
||||
if (slot_is_bool(tt, s) != (sh == SH_BOOL)) {
|
||||
tb_mcp_req_error(rq, slot_is_bool(tt, s) ? "Esse campo e booleano: so blocos <...> entram"
|
||||
|
||||
@@ -40,7 +40,9 @@ enum {
|
||||
};
|
||||
|
||||
/* Tipo de um campo de entrada (escolhe ( ), [ ] ou < >) */
|
||||
enum { TB_SB_SLOT_TEXT = 0, TB_SB_SLOT_NUM, TB_SB_SLOT_BOOL };
|
||||
enum { TB_SB_SLOT_TEXT = 0, TB_SB_SLOT_NUM, TB_SB_SLOT_BOOL,
|
||||
TB_SB_SLOT_STACK }; /* encaixe de pilha: a pilha vem nas linhas de baixo,
|
||||
indentada e fechada com "end" (como um bloco C) */
|
||||
|
||||
/* Rotulo extra: "%1".."%9" sao os campos (ex.: "mude %1 para %2") */
|
||||
typedef struct {
|
||||
|
||||
@@ -237,6 +237,7 @@ typedef struct {
|
||||
Tok tok[TOK_MAX];
|
||||
int slot[TOK_MAX]; /* campo (0-based) do token; -1 = fixo/palavra */
|
||||
bool listslot[TOK_MAX]; /* %LN */
|
||||
int stack_slot; /* encaixe de pilha (fora da linha); -1 = nenhum */
|
||||
char cat[24]; /* "list" de "... :: list" (modelos internos) */
|
||||
} Model;
|
||||
|
||||
@@ -280,6 +281,7 @@ static void loose_punct(char* buf, size_t cap) {
|
||||
|
||||
static bool model_build(Model* m, const char* type, const char* text, char shape, bool loose) {
|
||||
memset(m, 0, sizeof(*m));
|
||||
m->stack_slot = -1;
|
||||
m->type = type;
|
||||
m->shape = shape;
|
||||
char buf[512];
|
||||
@@ -308,6 +310,27 @@ typedef struct {
|
||||
const TbSbContext* cx;
|
||||
} Models;
|
||||
|
||||
/* O encaixe de pilha nao fica na linha do bloco: tira o "%k" do modelo e
|
||||
lembra qual e (a pilha vem nas linhas de baixo, ate o "end") */
|
||||
static void model_take_stack(Model* m, const TbSbContext* cx) {
|
||||
if (!cx || !cx->slot_kind) return;
|
||||
for (int i = 0; i < m->n; i++) {
|
||||
if (m->slot[i] < 0 || cx->slot_kind(m->type, m->slot[i], cx->ud) != TB_SB_SLOT_STACK) continue;
|
||||
if (m->stack_slot < 0) m->stack_slot = m->slot[i];
|
||||
memmove(&m->tok[i], &m->tok[i + 1], (size_t)(m->n - i - 1) * sizeof(m->tok[0]));
|
||||
memmove(&m->slot[i], &m->slot[i + 1], (size_t)(m->n - i - 1) * sizeof(m->slot[0]));
|
||||
memmove(&m->listslot[i], &m->listslot[i + 1], (size_t)(m->n - i - 1) * sizeof(m->listslot[0]));
|
||||
m->n--; i--;
|
||||
}
|
||||
}
|
||||
|
||||
/* O modelo do tipo que tem encaixe de pilha (NULL se o tipo nao tem) */
|
||||
static const Model* model_of(const Models* M, const char* type) {
|
||||
for (int i = 0; type && i < M->n; i++)
|
||||
if (M->m[i].stack_slot >= 0 && !strcmp(M->m[i].type, type)) return &M->m[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* "define nome (a) rotulo <b>": um bloco do usuario achado no texto */
|
||||
#define PROTO_MAX 64
|
||||
typedef struct {
|
||||
@@ -385,6 +408,7 @@ static Models models_load_with(const TbSbContext* cx, const Proto* protos, int n
|
||||
int sh = cx_shape(cx, L->type);
|
||||
Model* m = &M.m[M.n];
|
||||
if (!model_build(m, L->type, L->label, (char)shape_char(sh < 0 ? TB_SB_STACK : sh), true)) continue;
|
||||
model_take_stack(m, cx);
|
||||
/* "%1" sozinho (ex.: valor de variavel) casaria com qualquer coisa */
|
||||
if (m->n == 1 && m->slot[0] >= 0) continue;
|
||||
M.n++;
|
||||
@@ -457,6 +481,7 @@ static bool model_fits(const Model* m, const TbBpTab* tab, int bi) {
|
||||
if (m->import_only) return false;
|
||||
const TbBpBlock* b = &tab->blocks[bi];
|
||||
bool covered[TB_BP_MAX_INPUTS] = {0};
|
||||
if (m->stack_slot >= 0 && m->stack_slot < TB_BP_MAX_INPUTS) covered[m->stack_slot] = true;
|
||||
for (int i = 0; i < m->n; i++) {
|
||||
int s = m->slot[i];
|
||||
if (s < 0 || s >= TB_BP_MAX_INPUTS) continue;
|
||||
@@ -584,6 +609,11 @@ static void render_chain(const Models* M, const TbBpTab* tab, int bi, int depth,
|
||||
indent(out, depth);
|
||||
render_block(M, tab, c, out, true);
|
||||
sb_str(out, "\n");
|
||||
const Model* md = model_of(M, b->type);
|
||||
if (md && md->stack_slot >= 0 && md->stack_slot < TB_BP_MAX_INPUTS) {
|
||||
render_chain(M, tab, b->args[md->stack_slot], depth + 1, out);
|
||||
indent(out, depth); sb_str(out, "end\n");
|
||||
}
|
||||
if (sh == TB_SB_C1 || sh == TB_SB_C2 || sh == TB_SB_CAP) {
|
||||
render_chain(M, tab, b->sub[0], depth + 1, out);
|
||||
if (sh == TB_SB_C2) {
|
||||
@@ -886,10 +916,15 @@ int tb_scratchblocks_import(const char* text, TbBpTab* out, const TbSbContext* c
|
||||
} else {
|
||||
Level* L = &lv[depth];
|
||||
if (L->last >= 0) tb_blueprint_link(out, L->last, TB_BP_REL_NEXT, bi);
|
||||
else if (depth > 0 && L->sub < 0) tb_blueprint_link(out, L->cblock, TB_BP_REL_ARG0 + (-1 - L->sub), bi);
|
||||
else if (depth > 0) tb_blueprint_link(out, L->cblock, TB_BP_REL_SUB0 + L->sub, bi);
|
||||
L->last = bi;
|
||||
}
|
||||
if (!outer && (sh == TB_SB_C1 || sh == TB_SB_C2 || sh == TB_SB_CAP) && depth + 1 < LEVEL_MAX) {
|
||||
const Model* md = outer ? NULL : model_of(&M, out->blocks[bi].type);
|
||||
if (md && md->stack_slot >= 0 && depth + 1 < LEVEL_MAX) { /* encaixe de pilha: sub < 0 */
|
||||
depth++;
|
||||
lv[depth].cblock = bi; lv[depth].sub = -1 - md->stack_slot; lv[depth].last = -1;
|
||||
} else if (!outer && (sh == TB_SB_C1 || sh == TB_SB_C2 || sh == TB_SB_CAP) && depth + 1 < LEVEL_MAX) {
|
||||
depth++;
|
||||
lv[depth].cblock = bi; lv[depth].sub = 0; lv[depth].last = -1;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user