Comportamento dos blocos no próprio tipo e executor único (fluxo como interface)
Core: - TinyBlocks_FlowClass (tb_flow.h): um bloco C diz como roda (bocas, rótulos das faixas, tampa, next/error). next escolhe a boca, sai ou pede pausa (TB_FLOW_YIELD); error trata um erro que subiu de dentro. - TypeBlock: shape (chapéu...), flags (TB_TYPE_HIDDEN, TB_TYPE_REDRAW), flow, event (TB_EVENT_FLAG) e traços por campo (TB_SLOT_BOOLEAN, TB_SLOT_VARIABLE) via tb_type_block_set_input/output_flags. Libraries declaram o próprio comportamento: - control: repita / repita até que / sempre / se / se-senão como fluxos; errors: tente / se der erro / finalmente como fluxos. - events: chapéu da bandeira; operators: booleanos e campos hexagonais, literais numéricos ocultos; variables: campo de nome de variável; pen: redesenha o palco; graphics/editor: tipos de infraestrutura ocultos. Editor: - Forma, bocas, rótulos, tampa, campos booleanos/de variável, ocultos e "redesenhe" vêm dos traços (sem strcmp em nomes de outras libraries). - run_chain e vm_step viraram um executor só, que segue a classe de fluxo de cada bloco C; o corpo de Meus Blocos roda nele sem pausas. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
351e85bb14
commit
a5e36b3eed
15 files changed
+456
-251
No files matched your search
@@ -1,8 +1,8 @@
|
||||
/*
|
||||
* TinyBlocks - tb_control.c
|
||||
* Categoria "Controle": repita / repita até que / se / se-senão.
|
||||
* A semântica (bocas e sequência) é interpretada pelo editor; aqui só
|
||||
* registramos os tipos e a categoria.
|
||||
* Categoria "Controle": repita / repita até que / sempre / se / se-senão.
|
||||
* Cada bloco C diz como roda por uma TinyBlocks_FlowClass (tb_flow.h); quem
|
||||
* executa os scripts so segue as bocas que ela escolhe.
|
||||
*/
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include "tb_control.h"
|
||||
@@ -12,10 +12,57 @@ static void eval_noop(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* -- Fluxos ------------------------------------------------------------------
|
||||
* step: 0 = entrando, 1 = vai testar/entrar na volta, 2 = a volta acabou
|
||||
* (pausa um quadro, como no Scratch, e testa de novo). */
|
||||
|
||||
static int repeat_next(TinyBlocks_Flow* f) {
|
||||
if (f->step == 0) { f->count = tb_flow_arg_integer(f, 0); f->step = 1; }
|
||||
if (f->step == 2) { f->step = 1; return TB_FLOW_YIELD; }
|
||||
if (f->count-- <= 0) return TB_FLOW_DONE;
|
||||
f->step = 2;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int until_next(TinyBlocks_Flow* f) {
|
||||
if (f->step == 2) { f->step = 1; return TB_FLOW_YIELD; }
|
||||
if (tb_flow_arg_truthy(f, 0)) return TB_FLOW_DONE;
|
||||
f->step = 2;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int forever_next(TinyBlocks_Flow* f) {
|
||||
if (f->step == 2) { f->step = 1; return TB_FLOW_YIELD; }
|
||||
f->step = 2;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int if_next(TinyBlocks_Flow* f) {
|
||||
if (f->step++ > 0) return TB_FLOW_DONE;
|
||||
return tb_flow_arg_truthy(f, 0) ? 0 : TB_FLOW_DONE;
|
||||
}
|
||||
|
||||
static int if_else_next(TinyBlocks_Flow* f) {
|
||||
if (f->step++ > 0) return TB_FLOW_DONE;
|
||||
return tb_flow_arg_truthy(f, 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
static const TinyBlocks_FlowClass s_repeat = { 1, { NULL }, false, repeat_next, NULL };
|
||||
static const TinyBlocks_FlowClass s_until = { 1, { NULL }, false, until_next, NULL };
|
||||
static const TinyBlocks_FlowClass s_forever = { 1, { NULL }, true, forever_next, NULL };
|
||||
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 };
|
||||
|
||||
/* -- Registro ------------------------------------------------------------- */
|
||||
|
||||
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||
const TinyBlocks_SlotDesc* in, uint32_t ni) {
|
||||
const TinyBlocks_SlotDesc* in, uint32_t ni,
|
||||
const TinyBlocks_FlowClass* flow, bool cond) {
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, NULL, 0, eval_noop);
|
||||
return t && tb_library_add(lib, &t->base);
|
||||
if (!t) return false;
|
||||
tb_type_block_set_flow(t, flow);
|
||||
if (cond) tb_type_block_set_input_flags(t, 0, TB_SLOT_BOOLEAN); /* campo hexagonal */
|
||||
return tb_library_add(lib, &t->base);
|
||||
}
|
||||
|
||||
static bool control_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
@@ -27,24 +74,24 @@ static bool control_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
tb_library_set_category(lib, &cat);
|
||||
|
||||
/* Metadados dos blocos (tooltip do editor: Alt + hover). */
|
||||
TB_BLOCK_META("Control.Repeat", "eval_noop",
|
||||
TB_BLOCK_META("Control.Repeat", "repeat_next",
|
||||
"Repete o corpo do bloco o numero de vezes indicado");
|
||||
TB_BLOCK_META("Control.RepeatUntil", "eval_noop",
|
||||
TB_BLOCK_META("Control.RepeatUntil", "until_next",
|
||||
"Repete o corpo enquanto a condicao for falsa");
|
||||
TB_BLOCK_META("Control.Forever", "eval_noop",
|
||||
TB_BLOCK_META("Control.Forever", "forever_next",
|
||||
"Repete o corpo para sempre (ate clicar no botao vermelho)");
|
||||
TB_BLOCK_META("Control.If", "eval_noop",
|
||||
TB_BLOCK_META("Control.If", "if_next",
|
||||
"Executa o corpo se a condicao for verdadeira");
|
||||
TB_BLOCK_META("Control.IfElse", "eval_noop",
|
||||
TB_BLOCK_META("Control.IfElse", "if_else_next",
|
||||
"Executa o corpo 'entao' ou 'senao' conforme a condicao");
|
||||
|
||||
static const TinyBlocks_SlotDesc times_in[] = {{"vezes", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc cond_in[] = {{"condição", TB_VALUE_INTEGER}};
|
||||
return reg(rt, lib, "Control.Repeat", times_in, 1)
|
||||
&& reg(rt, lib, "Control.RepeatUntil", cond_in, 1)
|
||||
&& reg(rt, lib, "Control.Forever", NULL, 0)
|
||||
&& reg(rt, lib, "Control.If", cond_in, 1)
|
||||
&& reg(rt, lib, "Control.IfElse", cond_in, 1);
|
||||
return reg(rt, lib, "Control.Repeat", times_in, 1, &s_repeat, false)
|
||||
&& 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);
|
||||
}
|
||||
|
||||
static void control_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
|
||||
@@ -682,16 +682,12 @@ typedef struct {
|
||||
/* Execucao em "threads" (estilo Scratch 3): cada script da bandeira tem uma
|
||||
pilha explicita e pausa no fim de cada volta de laco; o editor avanca as
|
||||
threads a cada quadro (ver vm_frame em tb_editor.c). */
|
||||
enum { VF_CHAIN, VF_REPEAT, VF_UNTIL, VF_FOREVER, VF_TRY };
|
||||
enum { VF_CHAIN, VF_FLOW };
|
||||
typedef struct {
|
||||
Tab* T;
|
||||
int bi; /* CHAIN: proximo bloco; laco: o bloco do laco */
|
||||
int kind; /* VF_* */
|
||||
long long n; /* REPEAT: voltas que faltam */
|
||||
bool in_iter; /* laco: uma volta esta em andamento */
|
||||
int phase; /* TRY: 0 tente, 1 se der erro, 2 finalmente */
|
||||
char pend[200]; /* TRY: erro a relancar depois do finalmente */
|
||||
char saved[200]; /* TRY: "erro capturado" de fora (restaura) */
|
||||
Tab* T;
|
||||
int bi; /* CHAIN: proximo bloco; FLOW: o bloco C */
|
||||
int kind; /* VF_* */
|
||||
TinyBlocks_Flow flow; /* FLOW: estado do bloco C (a classe dele manda) */
|
||||
} VmFrame;
|
||||
#define VM_STACK 64
|
||||
#define VM_THREADS 32
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
@@ -166,25 +167,22 @@ static int input_count(const TinyBlocks_TypeBlock* t) {
|
||||
return t->input_desc_count < MAX_INPUTS ? (int)t->input_desc_count : MAX_INPUTS;
|
||||
}
|
||||
|
||||
/* A forma vem do tipo (a library declara): bloco C = classe de fluxo,
|
||||
chapeu/booleano = shape ou saida TB_SLOT_BOOLEAN */
|
||||
static Shape shape_of(const TinyBlocks_TypeBlock* t) {
|
||||
if (!t || !t->type_name) return SH_STACK;
|
||||
const char* n = t->type_name;
|
||||
if (strncmp(n, "Events.", 7) == 0 ||
|
||||
strcmp(n, "Port.Define") == 0) return SH_HAT;
|
||||
if (strcmp(n, "Control.If") == 0 || strcmp(n, "Control.Repeat") == 0 ||
|
||||
strcmp(n, "Control.RepeatUntil") == 0 ||
|
||||
strcmp(n, "Control.Forever") == 0) return SH_C1;
|
||||
if (strcmp(n, "Control.IfElse") == 0 ||
|
||||
strcmp(n, "Errors.TryCatch") == 0) return SH_C2;
|
||||
if (strcmp(n, "Errors.Try") == 0) return SH_C3;
|
||||
if (strcmp(n, "Operators.Lt") == 0 || strcmp(n, "Operators.Eq") == 0 ||
|
||||
strcmp(n, "Operators.Gt") == 0 || strcmp(n, "Operators.And") == 0 ||
|
||||
strcmp(n, "Operators.Or") == 0 || strcmp(n, "Operators.Not") == 0)
|
||||
return SH_BOOL;
|
||||
/* Convencao para as libraries: uma unica saida cujo nome termina em '?'
|
||||
(ex.: "é pasta?") e um bloco booleano. */
|
||||
if (t->output_desc_count == 1 && t->output_descs && t->output_descs[0].name) {
|
||||
const char* on = t->output_descs[0].name;
|
||||
if (!t) return SH_STACK;
|
||||
if (t->flow) return t->flow->mouths >= 3 ? SH_C3 : t->flow->mouths == 2 ? SH_C2 : SH_C1;
|
||||
switch (t->shape) {
|
||||
case TB_SHAPE_HAT: return SH_HAT;
|
||||
case TB_SHAPE_STACK: return SH_STACK;
|
||||
case TB_SHAPE_REPORTER: return SH_REPORTER;
|
||||
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;
|
||||
}
|
||||
@@ -198,14 +196,13 @@ static int shape_mouths(Shape s) { return s == SH_C1 ? 1 : s == SH_C2 ? 2 : s ==
|
||||
|
||||
/* Texto da faixa entre a boca k e a k+1 ("senão", "se der erro"...) */
|
||||
static const char* mouth_label(const TinyBlocks_TypeBlock* t, int k) {
|
||||
const char* n = t && t->type_name ? t->type_name : "";
|
||||
if (!strncmp(n, "Errors.Try", 10)) return k == 0 ? "se der erro" : "finalmente";
|
||||
return "senão";
|
||||
if (!t || !t->flow || k < 0 || k >= TB_FLOW_MAX_MOUTHS - 1) return "";
|
||||
return t->flow->labels[k] ? t->flow->labels[k] : "";
|
||||
}
|
||||
|
||||
/* Bloco "tampa" (como o "sempre" do Scratch): nada encaixa embaixo dele */
|
||||
static bool is_cap(const TinyBlocks_TypeBlock* t) {
|
||||
return t && t->type_name && strcmp(t->type_name, "Control.Forever") == 0;
|
||||
return t && t->flow && t->flow->cap;
|
||||
}
|
||||
|
||||
static UserBlockDef* user_def_find(Ed* ed, const char* name);
|
||||
@@ -222,18 +219,9 @@ static const UserParam* user_param_at(const UserBlockDef* def, int idx) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Campo hexagonal (recebe booleano): traco do campo, declarado no tipo */
|
||||
static bool slot_is_bool(const TinyBlocks_TypeBlock* t, int i) {
|
||||
if (!t || !t->type_name) return false;
|
||||
const char* n = t->type_name;
|
||||
if (strncmp(n, "User.", 5) == 0) {
|
||||
UserBlockDef* def = g_active_ed ? user_def_find(g_active_ed, n + 5) : NULL;
|
||||
const UserParam* p = user_param_at(def, i);
|
||||
return p && p->kind == PARAM_BOOL;
|
||||
}
|
||||
if (i == 0 && (strcmp(n,"Control.If") == 0 || strcmp(n,"Control.IfElse") == 0 ||
|
||||
strcmp(n,"Control.RepeatUntil") == 0)) return true;
|
||||
return strcmp(n,"Operators.And") == 0 || strcmp(n,"Operators.Or") == 0 ||
|
||||
strcmp(n,"Operators.Not") == 0;
|
||||
return i >= 0 && (tb_type_block_input_flags(t, (uint32_t)i) & TB_SLOT_BOOLEAN);
|
||||
}
|
||||
|
||||
static bool is_lit_text(const TinyBlocks_TypeBlock* t) {
|
||||
@@ -776,17 +764,10 @@ static int hit_slot(Tab* T, int bi, float x, float y) {
|
||||
/* ===
|
||||
* Renderização de blocos
|
||||
* === */
|
||||
/* Campo de nome de variavel (slot 0 de Variables.Get/Set/Change):
|
||||
* desenhado laranja (cor da categoria), como no Scratch. */
|
||||
/* Campo com o nome de uma variavel (traco TB_SLOT_VARIABLE): desenhado
|
||||
* laranja (cor da categoria), como no Scratch. */
|
||||
static bool slot_is_var_name(const TinyBlocks_TypeBlock* t, int s) {
|
||||
if (!t || !t->type_name || s != 0) return false;
|
||||
const char* n = t->type_name;
|
||||
return strcmp(n, "Variables.Get") == 0 ||
|
||||
strcmp(n, "Variables.Set") == 0 ||
|
||||
strcmp(n, "Variables.Change") == 0 ||
|
||||
strcmp(n, "Variables.DeclareLocal") == 0 ||
|
||||
strcmp(n, "Variables.DeclareGlobal") == 0 ||
|
||||
strcmp(n, "Variables.Delete") == 0;
|
||||
return s >= 0 && (tb_type_block_input_flags(t, (uint32_t)s) & TB_SLOT_VARIABLE);
|
||||
}
|
||||
|
||||
static void draw_slot(TinyBlocks_RenderableBlock* rb, const EdBlock* b, int s,
|
||||
@@ -1083,156 +1064,77 @@ static bool step(Ed* ed) {
|
||||
return !ed->abort;
|
||||
}
|
||||
|
||||
/* Roda uma pilha de uma vez (corpo de Meus Blocos). Um erro para a pilha e
|
||||
fica pendente para quem chamou (o "tente" mais proximo trata). */
|
||||
static void run_chain(Ed* ed, Tab* T, int head) {
|
||||
for (int bi = head; bi >= 0 && !err_pending(ed) && step(ed); bi = T->ws[bi].next) {
|
||||
EdBlock* b = &T->ws[bi];
|
||||
TinyBlocks_TypeBlock* t = blk_type(ed, b);
|
||||
if (!t) continue;
|
||||
const char* tn = t->type_name;
|
||||
Shape sh = shape_of(t);
|
||||
if (sh == SH_HAT || shape_is_value(sh)) continue;
|
||||
|
||||
if (strcmp(tn, "Control.Repeat") == 0) {
|
||||
TinyBlocks_Value v = eval_arg(ed, T, bi, 0);
|
||||
long long n = tb_value_as_integer(v, 0);
|
||||
tb_value_free(&v);
|
||||
for (long long k = 0; k < n && !err_pending(ed) && step(ed); k++) run_chain(ed, T, b->sub[0]);
|
||||
} else if (strcmp(tn, "Control.RepeatUntil") == 0) {
|
||||
for (;;) {
|
||||
TinyBlocks_Value v = eval_arg(ed, T, bi, 0);
|
||||
bool done = tb_value_truthy(v);
|
||||
tb_value_free(&v);
|
||||
if (done || err_pending(ed) || !step(ed)) break;
|
||||
run_chain(ed, T, b->sub[0]);
|
||||
}
|
||||
} else if (strcmp(tn, "Control.Forever") == 0) {
|
||||
/* dentro de Meus Blocos roda sem pausar: so o limite de passos para */
|
||||
while (!err_pending(ed) && step(ed)) run_chain(ed, T, b->sub[0]);
|
||||
} else if (strcmp(tn, "Control.If") == 0 || strcmp(tn, "Control.IfElse") == 0) {
|
||||
TinyBlocks_Value v = eval_arg(ed, T, bi, 0);
|
||||
bool cond = tb_value_truthy(v);
|
||||
tb_value_free(&v);
|
||||
if (err_pending(ed)) return;
|
||||
if (cond) run_chain(ed, T, b->sub[0]);
|
||||
else if (sh == SH_C2) run_chain(ed, T, b->sub[1]);
|
||||
} else if (strcmp(tn, "Errors.TryCatch") == 0 || strcmp(tn, "Errors.Try") == 0) {
|
||||
run_chain(ed, T, b->sub[0]);
|
||||
if (ed->abort) return;
|
||||
if (err_pending(ed)) { /* se der erro */
|
||||
char msg[200], saved[200];
|
||||
err_take(ed, msg, sizeof(msg));
|
||||
snprintf(saved, sizeof(saved), "%s", tinyblocks_caught_error(ed->rt));
|
||||
tinyblocks_set_caught_error(ed->rt, msg);
|
||||
run_chain(ed, T, b->sub[1]);
|
||||
tinyblocks_set_caught_error(ed->rt, saved);
|
||||
}
|
||||
if (sh == SH_C3 && !ed->abort) { /* finalmente: sempre */
|
||||
char pend[200] = "";
|
||||
if (err_pending(ed)) err_take(ed, pend, sizeof(pend));
|
||||
run_chain(ed, T, b->sub[2]);
|
||||
if (pend[0] && !err_pending(ed)) tinyblocks_set_error(ed->rt, "%s", pend);
|
||||
}
|
||||
} else {
|
||||
TinyBlocks_Block* r = get_rt(ed, T, bi);
|
||||
if (r) tb_block_eval(r, ed->rt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* == Threads (scripts rodando quadro a quadro, estilo Scratch 3) ===
|
||||
* Cada script e uma pilha explicita de VmFrame. Um laco pausa no fim de
|
||||
* cada volta; vm_frame avanca as threads varias vezes no mesmo quadro ate
|
||||
* todas pausarem/terminarem, um bloco do palco pedir redesenho ou acabar o
|
||||
* tempo do quadro. Assim contas terminam na hora e desenhos animam. */
|
||||
/* == Executor (scripts quadro a quadro, estilo Scratch 3) ===
|
||||
* Cada script e uma pilha explicita de VmFrame. Um bloco C nao e conhecido
|
||||
* pelo nome: a classe de fluxo dele (TinyBlocks_FlowClass) escolhe a boca a
|
||||
* rodar, pede pausa (lacos) ou trata um erro que subiu ("tente"). vm_frame
|
||||
* avanca as threads varias vezes no mesmo quadro ate todas pausarem ou
|
||||
* terminarem. O corpo de Meus Blocos roda no mesmo executor, sem pausas. */
|
||||
#define VM_FRAME_MS 12u
|
||||
static unsigned now_ms(void);
|
||||
|
||||
static void vm_push(Ed* ed, VmThread* th, Tab* T, int bi, int kind, long long n) {
|
||||
static const TinyBlocks_FlowClass* frame_flow(Ed* ed, VmFrame* f) {
|
||||
EdBlock* B = &f->T->ws[f->bi];
|
||||
TinyBlocks_TypeBlock* t = B->used ? blk_type(ed, B) : NULL;
|
||||
return t ? t->flow : NULL;
|
||||
}
|
||||
|
||||
/* TinyBlocks_Flow.arg: avalia uma entrada do bloco C deste frame */
|
||||
static TinyBlocks_Value flow_arg(TinyBlocks_Flow* fl, uint32_t slot) {
|
||||
VmFrame* f = (VmFrame*)((char*)fl - offsetof(VmFrame, flow));
|
||||
return g_active_ed ? eval_arg(g_active_ed, f->T, f->bi, (int)slot) : tb_value_none();
|
||||
}
|
||||
|
||||
static void vm_push(Ed* ed, VmThread* th, Tab* T, int bi, int kind) {
|
||||
if (th->sp >= VM_STACK) { run_abort(ed, "blocos aninhados demais"); return; }
|
||||
VmFrame* f = &th->st[th->sp++];
|
||||
f->T = T; f->bi = bi; f->kind = kind; f->n = n; f->in_iter = false;
|
||||
f->phase = 0; f->pend[0] = f->saved[0] = 0;
|
||||
f->T = T; f->bi = bi; f->kind = kind;
|
||||
memset(&f->flow, 0, sizeof(f->flow));
|
||||
f->flow.rt = ed->rt;
|
||||
f->flow.arg = flow_arg;
|
||||
}
|
||||
|
||||
static void vm_start(Ed* ed, Tab* T, int head) {
|
||||
if (ed->vm_n >= VM_THREADS) return;
|
||||
VmThread* th = &ed->vm[ed->vm_n++];
|
||||
th->sp = 0;
|
||||
th->scope = tb_vars_scope_new(NULL); /* variaveis locais deste script */
|
||||
vm_push(ed, th, T, head, VF_CHAIN, 0);
|
||||
ed->vm_running = true;
|
||||
}
|
||||
|
||||
/* Um erro na thread: desempilha ate o "tente" mais proximo e entra no
|
||||
"se der erro" (ou no "finalmente", guardando o erro para depois).
|
||||
false = ninguem tratou (a thread acaba). */
|
||||
static bool vm_throw(Ed* ed, VmThread* th) {
|
||||
char msg[200];
|
||||
err_take(ed, msg, sizeof(msg));
|
||||
/* Um erro na thread: desempilha ate um bloco C cuja classe trate o erro e
|
||||
entra na boca que ela escolher. false = ninguem tratou (msg = o erro). */
|
||||
static bool vm_throw(Ed* ed, VmThread* th, char* msg, size_t cap) {
|
||||
err_take(ed, msg, cap);
|
||||
while (th->sp > 0) {
|
||||
VmFrame* f = &th->st[th->sp - 1];
|
||||
if (f->kind != VF_TRY) { th->sp--; continue; }
|
||||
EdBlock* B = &f->T->ws[f->bi];
|
||||
bool has_finally = shape_of(B->type) == SH_C3;
|
||||
if (f->phase == 0) { /* no tente: vai para o se der erro */
|
||||
f->phase = 1;
|
||||
snprintf(f->saved, sizeof(f->saved), "%s", tinyblocks_caught_error(ed->rt));
|
||||
tinyblocks_set_caught_error(ed->rt, msg);
|
||||
vm_push(ed, th, f->T, B->sub[1], VF_CHAIN, 0);
|
||||
return true;
|
||||
if (f->kind == VF_FLOW) {
|
||||
const TinyBlocks_FlowClass* fc = frame_flow(ed, f);
|
||||
int m = fc && fc->error ? fc->error(&f->flow, msg) : TB_FLOW_DONE;
|
||||
if (m >= 0 && m < fc->mouths) {
|
||||
vm_push(ed, th, f->T, f->T->ws[f->bi].sub[m], VF_CHAIN);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (f->phase == 1) tinyblocks_set_caught_error(ed->rt, f->saved);
|
||||
if (f->phase == 1 && has_finally) { /* erro no se der erro: finalmente e relanca */
|
||||
f->phase = 2;
|
||||
snprintf(f->pend, sizeof(f->pend), "%s", msg);
|
||||
vm_push(ed, th, f->T, B->sub[2], VF_CHAIN, 0);
|
||||
return true;
|
||||
}
|
||||
th->sp--; /* erro no finalmente: sobe */
|
||||
th->sp--;
|
||||
}
|
||||
err_uncaught(ed, msg);
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Executa a thread ate a proxima pausa (true) ou ate terminar (false) */
|
||||
static bool vm_step(Ed* ed, VmThread* th) {
|
||||
/* Avanca a thread ate a proxima pausa (true) ou ate terminar (false).
|
||||
sync: roda de uma vez (ignora pausas) e deixa um erro sem tratamento
|
||||
pendente para quem chamou. */
|
||||
static bool vm_step(Ed* ed, VmThread* th, bool sync) {
|
||||
while (th->sp > 0 && !ed->abort) {
|
||||
if (err_pending(ed) && !vm_throw(ed, th)) break;
|
||||
if (th->sp <= 0) break;
|
||||
if (err_pending(ed)) {
|
||||
char msg[200];
|
||||
if (vm_throw(ed, th, msg, sizeof(msg))) continue;
|
||||
if (sync) tinyblocks_set_error(ed->rt, "%s", msg); /* sobe para quem chamou */
|
||||
else err_uncaught(ed, msg);
|
||||
break;
|
||||
}
|
||||
VmFrame* f = &th->st[th->sp - 1];
|
||||
Tab* T = f->T;
|
||||
if (f->kind == VF_TRY) { /* uma parte do tente acabou bem */
|
||||
EdBlock* B = &T->ws[f->bi];
|
||||
bool has_finally = B->used && shape_of(B->type) == SH_C3;
|
||||
if (f->phase == 1) tinyblocks_set_caught_error(ed->rt, f->saved);
|
||||
if (f->phase < 2 && has_finally) {
|
||||
f->phase = 2;
|
||||
vm_push(ed, th, T, B->sub[2], VF_CHAIN, 0);
|
||||
continue;
|
||||
}
|
||||
char pend[200];
|
||||
snprintf(pend, sizeof(pend), "%s", f->pend);
|
||||
th->sp--;
|
||||
if (pend[0]) tinyblocks_set_error(ed->rt, "%s", pend); /* relanca */
|
||||
continue;
|
||||
}
|
||||
if (f->kind != VF_CHAIN) {
|
||||
EdBlock* L = &T->ws[f->bi];
|
||||
if (!L->used) { th->sp--; continue; } /* laco apagado com o programa rodando */
|
||||
if (f->in_iter) { f->in_iter = false; return true; } /* fim de uma volta: pausa */
|
||||
if (f->kind == VF_REPEAT) {
|
||||
if (f->n <= 0) { th->sp--; continue; }
|
||||
f->n--;
|
||||
} else if (f->kind == VF_UNTIL) {
|
||||
TinyBlocks_Value v = eval_arg(ed, T, f->bi, 0);
|
||||
bool done = tb_value_truthy(v);
|
||||
tb_value_free(&v);
|
||||
if (done) { th->sp--; continue; }
|
||||
}
|
||||
f->in_iter = true;
|
||||
vm_push(ed, th, T, L->sub[0], VF_CHAIN, 0);
|
||||
if (f->kind == VF_FLOW) { /* bloco C: a classe escolhe */
|
||||
const TinyBlocks_FlowClass* fc = frame_flow(ed, f);
|
||||
if (!fc) { th->sp--; continue; } /* apagado com o programa rodando */
|
||||
if (!step(ed)) break;
|
||||
int m = fc->next(&f->flow);
|
||||
if (m == TB_FLOW_YIELD) { if (sync) continue; return true; }
|
||||
if (m < 0 || m >= fc->mouths) { th->sp--; continue; }
|
||||
vm_push(ed, th, T, T->ws[f->bi].sub[m], VF_CHAIN);
|
||||
continue;
|
||||
}
|
||||
if (f->bi < 0 || !T->ws[f->bi].used) { th->sp--; continue; }
|
||||
@@ -1242,38 +1144,37 @@ static bool vm_step(Ed* ed, VmThread* th) {
|
||||
if (!step(ed)) break;
|
||||
TinyBlocks_TypeBlock* t = blk_type(ed, b);
|
||||
if (!t) continue;
|
||||
const char* tn = t->type_name;
|
||||
Shape sh = shape_of(t);
|
||||
if (sh == SH_HAT || shape_is_value(sh)) continue;
|
||||
if (strcmp(tn, "Control.Repeat") == 0) {
|
||||
TinyBlocks_Value v = eval_arg(ed, T, bi, 0);
|
||||
long long n = tb_value_as_integer(v, 0);
|
||||
tb_value_free(&v);
|
||||
if (n > 0) vm_push(ed, th, T, bi, VF_REPEAT, n);
|
||||
} else if (strcmp(tn, "Control.RepeatUntil") == 0) {
|
||||
vm_push(ed, th, T, bi, VF_UNTIL, 0);
|
||||
} else if (strcmp(tn, "Control.Forever") == 0) {
|
||||
vm_push(ed, th, T, bi, VF_FOREVER, 0);
|
||||
} else if (strcmp(tn, "Control.If") == 0 || strcmp(tn, "Control.IfElse") == 0) {
|
||||
TinyBlocks_Value v = eval_arg(ed, T, bi, 0);
|
||||
bool cond = tb_value_truthy(v);
|
||||
tb_value_free(&v);
|
||||
if (err_pending(ed)) continue;
|
||||
if (cond) vm_push(ed, th, T, b->sub[0], VF_CHAIN, 0);
|
||||
else if (sh == SH_C2) vm_push(ed, th, T, b->sub[1], VF_CHAIN, 0);
|
||||
} else if (strcmp(tn, "Errors.TryCatch") == 0 || strcmp(tn, "Errors.Try") == 0) {
|
||||
vm_push(ed, th, T, bi, VF_TRY, 0);
|
||||
vm_push(ed, th, T, b->sub[0], VF_CHAIN, 0);
|
||||
} else {
|
||||
TinyBlocks_Block* r = get_rt(ed, T, bi);
|
||||
if (r) tb_block_eval(r, ed->rt);
|
||||
if (strncmp(tn, "Stage.", 6) == 0) ed->redraw_req = true; /* caneta: mostra este quadro */
|
||||
}
|
||||
if (t->flow) { vm_push(ed, th, T, bi, VF_FLOW); continue; }
|
||||
TinyBlocks_Block* r = get_rt(ed, T, bi);
|
||||
if (r) tb_block_eval(r, ed->rt);
|
||||
if (t->flags & TB_TYPE_REDRAW) ed->redraw_req = true; /* caneta: mostra este quadro */
|
||||
}
|
||||
th->sp = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Roda uma pilha de uma vez (corpo de Meus Blocos), no mesmo executor */
|
||||
static void run_chain(Ed* ed, Tab* T, int head) {
|
||||
VmThread* th = malloc(sizeof(*th));
|
||||
if (!th) { run_abort(ed, "sem memória para rodar o bloco"); return; }
|
||||
th->sp = 0;
|
||||
th->scope = NULL;
|
||||
vm_push(ed, th, T, head, VF_CHAIN);
|
||||
vm_step(ed, th, true);
|
||||
free(th);
|
||||
}
|
||||
|
||||
static void vm_start(Ed* ed, Tab* T, int head) {
|
||||
if (ed->vm_n >= VM_THREADS) return;
|
||||
VmThread* th = &ed->vm[ed->vm_n++];
|
||||
th->sp = 0;
|
||||
th->scope = tb_vars_scope_new(NULL); /* variaveis locais deste script */
|
||||
vm_push(ed, th, T, head, VF_CHAIN);
|
||||
ed->vm_running = true;
|
||||
}
|
||||
|
||||
/* Captura o stdout (blocos "escreva") enquanto os blocos rodam */
|
||||
static void cap_begin(Ed* ed) {
|
||||
fflush(stdout);
|
||||
@@ -1350,7 +1251,7 @@ static void vm_frame(Ed* ed) {
|
||||
VmThread* th = &ed->vm[i];
|
||||
if (th->sp <= 0) continue;
|
||||
tb_vars_scope_set(th->scope);
|
||||
if (vm_step(ed, th)) alive = true;
|
||||
if (vm_step(ed, th, false)) alive = true;
|
||||
else { tb_vars_scope_set(NULL); tb_vars_scope_free(th->scope); th->scope = NULL; }
|
||||
tb_vars_scope_set(NULL);
|
||||
}
|
||||
@@ -1384,7 +1285,8 @@ static void run_flag(Ed* ed) {
|
||||
int started = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
EdBlock* b = &CT.ws[heads[i]];
|
||||
if (blk_type(ed, b) && strcmp(b->type->type_name, "Events.WhenRun") == 0) {
|
||||
TinyBlocks_TypeBlock* t = blk_type(ed, b);
|
||||
if (t && t->event && strcmp(t->event, TB_EVENT_FLAG) == 0) { /* chapeus da bandeira */
|
||||
vm_start(ed, &CT, heads[i]);
|
||||
started++;
|
||||
}
|
||||
@@ -1744,9 +1646,7 @@ static void retype_all(Ed* ed) {
|
||||
static bool type_hidden(Ed* ed, const TinyBlocks_TypeBlock* t) {
|
||||
const char* n = t->type_name;
|
||||
if (!n || n[0] == '_' || strstr(n, "._")) return true;
|
||||
if (strncmp(n, "Editor.", 7) == 0 || strncmp(n, "Graphics.", 9) == 0) return true;
|
||||
if (strcmp(n, "Literal.Integer") == 0 || strcmp(n, "Literal.Real") == 0) return true;
|
||||
if (strcmp(n, "Port.Define") == 0) return true; /* nasce pelo "Criar um Bloco" */
|
||||
if (t->flags & TB_TYPE_HIDDEN) return true; /* infraestrutura / "defina" */
|
||||
if (strncmp(n, "Port.", 5) == 0) return !CT.is_composite && !tab_has_define(&CT);
|
||||
return false;
|
||||
}
|
||||
@@ -2512,6 +2412,10 @@ static void comp_make_type(Ed* ed, UserBlockDef* def) {
|
||||
def->input_count ? in_s : NULL, (uint32_t)def->input_count,
|
||||
def->output_count ? out_s : NULL, (uint32_t)def->output_count,
|
||||
eval_composite);
|
||||
for (int i = 0; def->reg_type && i < def->input_count; i++) {
|
||||
const UserParam* p = user_param_at(def, i);
|
||||
if (p && p->kind == PARAM_BOOL) tb_type_block_set_input_flags(def->reg_type, (uint32_t)i, TB_SLOT_BOOLEAN);
|
||||
}
|
||||
retype_all(ed);
|
||||
}
|
||||
|
||||
@@ -6781,6 +6685,9 @@ static bool editor_lib_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_SlotDesc port_out_in[] = {{"nome", TB_VALUE_TEXT}, {"valor", TB_VALUE_NONE}};
|
||||
static const TinyBlocks_SlotDesc any_out[] = {{"valor", TB_VALUE_NONE}};
|
||||
reg(rt, lib, "Port.Define", port_label, 1, NULL, 0, eval_port_define);
|
||||
TinyBlocks_TypeBlock* tdef = tb_type_block_find(rt, "Port.Define");
|
||||
tb_type_block_set_shape(tdef, TB_SHAPE_HAT);
|
||||
tb_type_block_set_flags(tdef, TB_TYPE_HIDDEN); /* nasce pelo "Criar um Bloco" */
|
||||
reg(rt, lib, "Port.In", port_label, 1, any_out, 1, eval_port_in);
|
||||
reg(rt, lib, "Port.Out", port_out_in, 2, NULL, 0, eval_port_out);
|
||||
|
||||
@@ -6793,6 +6700,7 @@ static bool editor_lib_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
scene_in, 1, NULL, 0, editor_eval);
|
||||
if (!t) return false;
|
||||
tb_type_block_set_instance_hooks(t, sizeof(Ed*), editor_instance_init, editor_instance_deinit);
|
||||
tb_type_block_set_flags(t, TB_TYPE_HIDDEN);
|
||||
t->on_draw = editor_on_draw;
|
||||
tb_library_add(lib, &t->base);
|
||||
|
||||
@@ -6801,6 +6709,7 @@ static bool editor_lib_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
scene_in, 1, NULL, 0, area_eval);
|
||||
if (!ta) return false;
|
||||
tb_type_block_set_instance_hooks(ta, sizeof(AreaScene), area_instance_init, area_instance_deinit);
|
||||
tb_type_block_set_flags(ta, TB_TYPE_HIDDEN);
|
||||
ta->on_draw = area_on_draw;
|
||||
tb_library_add(lib, &ta->base);
|
||||
return true;
|
||||
|
||||
@@ -46,6 +46,7 @@ bool tb_stage_register(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, "Editor.Stage", stage_in, 1, NULL, 0, eval_stage);
|
||||
if (!t) return false;
|
||||
tb_type_block_set_instance_hooks(t, 1, stage_instance_init, stage_instance_deinit);
|
||||
tb_type_block_set_flags(t, TB_TYPE_HIDDEN);
|
||||
t->on_draw = stage_on_draw;
|
||||
return tb_library_add(lib, &t->base);
|
||||
}
|
||||
@@ -7,9 +7,9 @@
|
||||
* tinyblocks/tb_result.h). "tente obter (x)" transforma o erro de um
|
||||
* reporter num Erro; "desembrulhe" faz o caminho de volta.
|
||||
*
|
||||
* Tratamento de erros: os blocos "tente / se der erro" e "tente / se der
|
||||
* erro / finalmente" sao interpretados por quem executa (o editor); aqui so
|
||||
* ficam os tipos. "lance o erro" levanta um erro e "erro capturado" le a
|
||||
* Tratamento de erros: "tente / se der erro" e "tente / se der erro /
|
||||
* finalmente" sao blocos C com classe de fluxo propria (quem executa so
|
||||
* segue as bocas). "lance o erro" levanta um erro e "erro capturado" le a
|
||||
* mensagem dentro do "se der erro".
|
||||
*/
|
||||
#ifndef TINYBLOCKS_ERRORS_H
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
/*
|
||||
* TinyBlocks - tb_errors.c
|
||||
* Categoria "Erros": resultados (TinyBlocks_Result: Ok | Erro, como o Result
|
||||
* do Rust) e os tipos dos blocos de tratamento de erro (ver tb_errors.h).
|
||||
* do Rust) e os blocos de tratamento de erro, que dizem como rodam por
|
||||
* classes de fluxo (TinyBlocks_FlowClass, tb_flow.h).
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -117,12 +118,70 @@ static void eval_caught(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* Os blocos C sao executados por quem roda os scripts */
|
||||
/* Os blocos C rodam pelas classes de fluxo abaixo */
|
||||
static void eval_nop(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* -- Fluxos: tente / se der erro / finalmente -------------------------------
|
||||
* step: 0 entrando, 1 no "tente", 2 no "se der erro", 3 no "finalmente". */
|
||||
|
||||
static void catch_enter(TinyBlocks_Flow* f, const char* msg) {
|
||||
snprintf(f->saved, sizeof(f->saved), "%s", tinyblocks_caught_error(f->rt));
|
||||
tinyblocks_set_caught_error(f->rt, msg);
|
||||
f->step = 2;
|
||||
}
|
||||
|
||||
static void catch_leave(TinyBlocks_Flow* f) {
|
||||
tinyblocks_set_caught_error(f->rt, f->saved);
|
||||
}
|
||||
|
||||
/* tente / se der erro */
|
||||
static int try_catch_next(TinyBlocks_Flow* f) {
|
||||
if (f->step == 0) { f->step = 1; return 0; }
|
||||
if (f->step == 2) catch_leave(f);
|
||||
return TB_FLOW_DONE;
|
||||
}
|
||||
|
||||
static int try_catch_error(TinyBlocks_Flow* f, const char* msg) {
|
||||
if (f->step == 1) { catch_enter(f, msg); return 1; }
|
||||
if (f->step == 2) catch_leave(f);
|
||||
return TB_FLOW_DONE; /* erro no tratamento: sobe */
|
||||
}
|
||||
|
||||
/* tente / se der erro / finalmente: o finalmente roda sempre; um erro no
|
||||
"se der erro" espera o finalmente e depois continua subindo */
|
||||
static int try_next(TinyBlocks_Flow* f) {
|
||||
switch (f->step) {
|
||||
case 0: f->step = 1; return 0;
|
||||
case 2: catch_leave(f); /* fallthrough */
|
||||
case 1: f->step = 3; return 2;
|
||||
default:
|
||||
if (f->pending[0]) { /* relanca */
|
||||
tinyblocks_set_error(f->rt, "%s", f->pending);
|
||||
f->pending[0] = 0;
|
||||
}
|
||||
return TB_FLOW_DONE;
|
||||
}
|
||||
}
|
||||
|
||||
static int try_error(TinyBlocks_Flow* f, const char* msg) {
|
||||
if (f->step == 1) { catch_enter(f, msg); return 1; }
|
||||
if (f->step == 2) {
|
||||
catch_leave(f);
|
||||
snprintf(f->pending, sizeof(f->pending), "%s", msg);
|
||||
f->step = 3;
|
||||
return 2;
|
||||
}
|
||||
return TB_FLOW_DONE; /* erro no finalmente: sobe */
|
||||
}
|
||||
|
||||
static const TinyBlocks_FlowClass s_try_catch = {
|
||||
2, { "se der erro" }, false, try_catch_next, try_catch_error };
|
||||
static const TinyBlocks_FlowClass s_try = {
|
||||
3, { "se der erro", "finalmente" }, false, try_next, try_error };
|
||||
|
||||
/* -- Registro ------------------------------------------------------------- */
|
||||
|
||||
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||
@@ -141,9 +200,9 @@ static bool errors_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
};
|
||||
tb_library_set_category(lib, &cat);
|
||||
|
||||
TB_BLOCK_META("Errors.Try", "eval_nop",
|
||||
TB_BLOCK_META("Errors.Try", "try_next",
|
||||
"Roda o primeiro bloco; se der erro, o segundo; e sempre o terceiro no fim");
|
||||
TB_BLOCK_META("Errors.TryCatch", "eval_nop",
|
||||
TB_BLOCK_META("Errors.TryCatch", "try_catch_next",
|
||||
"Roda o primeiro bloco; se der erro, roda o segundo em vez de parar o script");
|
||||
TB_BLOCK_META("Errors.Raise", "eval_raise", "Levanta um erro (para o script, ou vai para o \"se der erro\")");
|
||||
TB_BLOCK_META("Errors.Caught", "eval_caught", "Mensagem do erro dentro do \"se der erro\"");
|
||||
@@ -164,23 +223,30 @@ static bool errors_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_SlotDesc res_out[] = {{"resultado", TB_VALUE_RESULT}};
|
||||
static const TinyBlocks_SlotDesc any_out[] = {{"valor", TB_VALUE_NONE}};
|
||||
static const TinyBlocks_SlotDesc text_out[] = {{"mensagem", TB_VALUE_TEXT}};
|
||||
/* saidas terminadas em '?' = blocos booleanos (hexagonais no editor) */
|
||||
static const TinyBlocks_SlotDesc ok_out[] = {{"é ok?", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc err_out[] = {{"é erro?", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc ok_out[] = {{"é ok", TB_VALUE_INTEGER}}; /* booleanos: TB_SLOT_BOOLEAN */
|
||||
static const TinyBlocks_SlotDesc err_out[] = {{"é erro", TB_VALUE_INTEGER}};
|
||||
|
||||
if (!(reg(rt, lib, "Errors.TryCatch", NULL, 0, NULL, 0, eval_nop)
|
||||
&& reg(rt, lib, "Errors.Try", NULL, 0, NULL, 0, eval_nop)
|
||||
&& reg(rt, lib, "Errors.Raise", msg_in, 1, NULL, 0, eval_raise)
|
||||
&& reg(rt, lib, "Errors.Try", NULL, 0, NULL, 0, eval_nop)))
|
||||
return false;
|
||||
tb_type_block_set_flow(tb_type_block_find(rt, "Errors.TryCatch"), &s_try_catch);
|
||||
tb_type_block_set_flow(tb_type_block_find(rt, "Errors.Try"), &s_try);
|
||||
|
||||
if (!(reg(rt, lib, "Errors.Raise", msg_in, 1, NULL, 0, eval_raise)
|
||||
&& reg(rt, lib, "Errors.Caught", NULL, 0, text_out, 1, eval_caught)
|
||||
&& reg(rt, lib, "Errors.Attempt", any_in, 1, res_out, 1, eval_attempt)))
|
||||
return false;
|
||||
tb_type_block_find(rt, "Errors.Attempt")->lazy_inputs = true; /* ve o erro de dentro */
|
||||
|
||||
return reg(rt, lib, "Errors.Ok", any_in, 1, res_out, 1, eval_ok)
|
||||
&& reg(rt, lib, "Errors.Err", err_in, 1, res_out, 1, eval_err)
|
||||
&& reg(rt, lib, "Errors.IsOk", any_in, 1, ok_out, 1, eval_is_ok)
|
||||
&& reg(rt, lib, "Errors.IsErr", any_in, 1, err_out, 1, eval_is_err)
|
||||
&& reg(rt, lib, "Errors.UnwrapOr", or_in, 2, any_out, 1, eval_unwrap_or)
|
||||
if (!(reg(rt, lib, "Errors.Ok", any_in, 1, res_out, 1, eval_ok)
|
||||
&& reg(rt, lib, "Errors.Err", err_in, 1, res_out, 1, eval_err)
|
||||
&& reg(rt, lib, "Errors.IsOk", any_in, 1, ok_out, 1, eval_is_ok)
|
||||
&& reg(rt, lib, "Errors.IsErr", any_in, 1, err_out, 1, eval_is_err)))
|
||||
return false;
|
||||
tb_type_block_set_output_flags(tb_type_block_find(rt, "Errors.IsOk"), 0, TB_SLOT_BOOLEAN);
|
||||
tb_type_block_set_output_flags(tb_type_block_find(rt, "Errors.IsErr"), 0, TB_SLOT_BOOLEAN);
|
||||
|
||||
return reg(rt, lib, "Errors.UnwrapOr", or_in, 2, any_out, 1, eval_unwrap_or)
|
||||
&& reg(rt, lib, "Errors.Unwrap", any_in, 1, any_out, 1, eval_unwrap)
|
||||
&& reg(rt, lib, "Errors.Message", any_in, 1, text_out, 1, eval_message);
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ static bool events_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, "Events.WhenRun",
|
||||
NULL, 0, NULL, 0, eval_noop);
|
||||
if (!t) return false;
|
||||
tb_type_block_set_event(t, TB_EVENT_FLAG); /* chapeu da bandeira verde */
|
||||
return tb_library_add(lib, &t->base);
|
||||
}
|
||||
|
||||
|
||||
@@ -423,8 +423,10 @@ static void eval_run(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
static bool graphics_lib_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
TinyBlocks_TypeBlock* t;
|
||||
|
||||
/* tipos de infraestrutura: nao aparecem na paleta */
|
||||
t = tb_type_block_create(rt, "Graphics.Input", NULL, 0, s_input_outs, GFX_IN_COUNT, eval_input);
|
||||
if (!t) return false;
|
||||
tb_type_block_set_flags(t, TB_TYPE_HIDDEN);
|
||||
tb_library_add(lib, &t->base);
|
||||
|
||||
static const TinyBlocks_SlotDesc win_in[] = {
|
||||
@@ -435,10 +437,12 @@ static bool graphics_lib_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
t = tb_type_block_create(rt, "Graphics.Window", win_in, 6, win_out, 1, eval_window);
|
||||
if (!t) return false;
|
||||
tb_type_block_set_instance_hooks(t, sizeof(WinState), win_instance_init, win_instance_deinit);
|
||||
tb_type_block_set_flags(t, TB_TYPE_HIDDEN);
|
||||
tb_library_add(lib, &t->base);
|
||||
|
||||
t = tb_type_block_create(rt, "Graphics.Run", NULL, 0, NULL, 0, eval_run);
|
||||
if (!t) return false;
|
||||
tb_type_block_set_flags(t, TB_TYPE_HIDDEN);
|
||||
tb_library_add(lib, &t->base);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -242,7 +242,7 @@ static bool operators_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_SlotDesc lit_txt_in[] = {{"value", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc lit_txt_out[] = {{"out", TB_VALUE_TEXT}};
|
||||
|
||||
return reg(rt, lib, "Operators.Lt", any2, 2, bool_out, 1, eval_op_lt)
|
||||
if (!(reg(rt, lib, "Operators.Lt", any2, 2, bool_out, 1, eval_op_lt)
|
||||
&& reg(rt, lib, "Operators.Eq", any2, 2, bool_out, 1, eval_op_eq)
|
||||
&& reg(rt, lib, "Operators.Gt", any2, 2, bool_out, 1, eval_op_gt)
|
||||
&& reg(rt, lib, "Operators.And", bool2, 2, bool_out, 1, eval_op_and)
|
||||
@@ -261,7 +261,23 @@ static bool operators_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
&& reg(rt, lib, "Text.Length", text_in, 1, int_out, 1, eval_len)
|
||||
&& reg(rt, lib, "Literal.Integer", lit_int_in, 1, lit_int_out, 1, eval_lit_int)
|
||||
&& reg(rt, lib, "Literal.Real", lit_real_in, 1, lit_real_out, 1, eval_lit_real)
|
||||
&& reg(rt, lib, "Literal.Text", lit_txt_in, 1, lit_txt_out, 1, eval_lit_text);
|
||||
&& reg(rt, lib, "Literal.Text", lit_txt_in, 1, lit_txt_out, 1, eval_lit_text)))
|
||||
return false;
|
||||
|
||||
/* comparacoes e logica sao booleanas (hexagonais); e/ou/nao recebem booleanos */
|
||||
static const char* const bools[] = { "Operators.Lt", "Operators.Eq", "Operators.Gt",
|
||||
"Operators.And", "Operators.Or", "Operators.Not" };
|
||||
for (size_t k = 0; k < sizeof(bools) / sizeof(bools[0]); k++)
|
||||
tb_type_block_set_output_flags(tb_type_block_find(rt, bools[k]), 0, TB_SLOT_BOOLEAN);
|
||||
for (uint32_t k = 0; k < 2; k++) {
|
||||
tb_type_block_set_input_flags(tb_type_block_find(rt, "Operators.And"), k, TB_SLOT_BOOLEAN);
|
||||
tb_type_block_set_input_flags(tb_type_block_find(rt, "Operators.Or"), k, TB_SLOT_BOOLEAN);
|
||||
}
|
||||
tb_type_block_set_input_flags(tb_type_block_find(rt, "Operators.Not"), 0, TB_SLOT_BOOLEAN);
|
||||
/* literais numericos so existem por baixo dos campos */
|
||||
tb_type_block_set_flags(tb_type_block_find(rt, "Literal.Integer"), TB_TYPE_HIDDEN);
|
||||
tb_type_block_set_flags(tb_type_block_find(rt, "Literal.Real"), TB_TYPE_HIDDEN);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void operators_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
|
||||
@@ -124,7 +124,9 @@ static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||
const TinyBlocks_SlotDesc* in, uint32_t ni,
|
||||
void (*fn)(TinyBlocks_Block*, TinyBlocks*)) {
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, NULL, 0, fn);
|
||||
return t && tb_library_add(lib, &t->base);
|
||||
if (!t) return false;
|
||||
tb_type_block_set_flags(t, TB_TYPE_REDRAW); /* desenha no palco: mostra ja */
|
||||
return tb_library_add(lib, &t->base);
|
||||
}
|
||||
|
||||
static bool pen_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
|
||||
@@ -148,6 +148,12 @@ static bool variables_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
if (!t || !tb_library_add(lib, &t->base)) return false;
|
||||
t = tb_type_block_create(rt, "Variables.Delete", var_in, 1, NULL, 0, eval_var_delete);
|
||||
if (!t || !tb_library_add(lib, &t->base)) return false;
|
||||
|
||||
/* o 1o campo de todos e o NOME de uma variavel (campo laranja no editor) */
|
||||
static const char* const named[] = { "Variables.Set", "Variables.Change", "Variables.Get",
|
||||
"Variables.DeclareLocal", "Variables.DeclareGlobal", "Variables.Delete" };
|
||||
for (size_t k = 0; k < sizeof(named) / sizeof(named[0]); k++)
|
||||
tb_type_block_set_input_flags(tb_type_block_find(rt, named[k]), 0, TB_SLOT_VARIABLE);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user