-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlvgl_renderer.c
More file actions
810 lines (713 loc) · 36.9 KB
/
lvgl_renderer.c
File metadata and controls
810 lines (713 loc) · 36.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
#include "lvgl_renderer.h"
#include "data_binding.h"
#include "debug_log.h"
#include "registry.h"
#include "lvgl_ui_utils.h"
#include "generator.h"
#include "viewer/view_inspector.h"
#include "ui_sim.h" // ADDED: For UI-Sim lifecycle management
#include "obj_registry.h"
#include <stdlib.h>
#include <string.h>
// --- ADDED: Static registry to hold allocated data across reloads ---
static Registry* g_renderer_registry = NULL;
// --- Render Context ---
// This struct is passed through the recursive render functions to manage state
// and gracefully handle errors without crashing.
typedef struct {
ApiSpec* spec;
Registry* registry;
bool error_occurred;
// Runtime ifdef defines set (populated by IR_NODE_DEFINE nodes)
char** defines;
int defines_count;
int defines_capacity;
} RenderContext;
// --- Defines helpers ---
static void render_ctx_add_define(RenderContext* ctx, const char* symbol) {
if (!symbol) return;
// Grow if needed
if (ctx->defines_count >= ctx->defines_capacity) {
int new_cap = ctx->defines_capacity ? ctx->defines_capacity * 2 : 8;
char** new_arr = realloc(ctx->defines, new_cap * sizeof(char*));
if (!new_arr) return;
ctx->defines = new_arr;
ctx->defines_capacity = new_cap;
}
ctx->defines[ctx->defines_count++] = strdup(symbol);
}
static bool render_ctx_has_define(const RenderContext* ctx, const char* symbol) {
for (int i = 0; i < ctx->defines_count; i++) {
if (strcmp(ctx->defines[i], symbol) == 0) return true;
}
return false;
}
static void render_ctx_free_defines(RenderContext* ctx) {
for (int i = 0; i < ctx->defines_count; i++) free(ctx->defines[i]);
free(ctx->defines);
ctx->defines = NULL;
ctx->defines_count = 0;
ctx->defines_capacity = 0;
}
// --- Forward Declarations ---
static void render_object_list(RenderContext* ctx, IRObject* head);
static void render_single_object(RenderContext* ctx, IRObject* current_obj);
static void evaluate_expression(RenderContext* ctx, IRExpr* expr, RenderValue* out_val);
static void render_ifdef_node(RenderContext* ctx, IRIfdef* ifn, lv_obj_t* c_obj);
// --- Main Backend Entry Point ---
void lvgl_render_backend(IRRoot* root, ApiSpec* api_spec, lv_obj_t* parent, Registry* registry) {
if (!root || !api_spec || !parent || !registry) {
DEBUG_LOG(LOG_MODULE_RENDERER, "Error: lvgl_render_backend called with NULL arguments.");
return;
}
obj_registry_init();
registry_add_pointer(registry, parent, "parent", "obj", "lv_obj_t*");
obj_registry_add("parent", parent);
/* Ensure the default LVGL font is available in both registries. */
registry_add_pointer(registry, (void*)LV_FONT_DEFAULT, "LV_FONT_DEFAULT", "font", "lv_font_t*");
registry_add_pointer(registry, (void*)LV_FONT_DEFAULT, "lv_font_default", "font", "lv_font_t*");
obj_registry_add("LV_FONT_DEFAULT", (void*)LV_FONT_DEFAULT);
obj_registry_add("lv_font_default", (void*)LV_FONT_DEFAULT);
DEBUG_LOG(LOG_MODULE_RENDERER, "Starting LVGL render backend.");
RenderContext ctx = { .spec = api_spec, .registry = registry, .error_occurred = false,
.defines = NULL, .defines_count = 0, .defines_capacity = 0 };
if (root->root_ops) {
// Use root_ops to honour interleaved ordering of define/ifdef/objects.
for (IROperationNode* op = root->root_ops; op; op = op->next) {
if (ctx.error_occurred) break;
IRNode* n = op->op_node;
if (n->type == IR_NODE_OBJECT) {
render_single_object(&ctx, (IRObject*)n);
} else if (n->type == IR_NODE_DEFINE) {
render_ctx_add_define(&ctx, ((IRDefine*)n)->symbol);
} else if (n->type == IR_NODE_IFDEF) {
render_ifdef_node(&ctx, (IRIfdef*)n, NULL);
}
}
} else {
render_object_list(&ctx, root->root_objects);
}
render_ctx_free_defines(&ctx);
DEBUG_LOG(LOG_MODULE_RENDERER, "LVGL render backend finished.");
lv_obj_update_layout(parent);
DEBUG_LOG(LOG_MODULE_RENDERER, "Forcing layout update on parent container.");
}
void lvgl_renderer_reload_ui_from_string(const char* ui_spec_string, ApiSpec* api_spec, lv_obj_t* preview_panel, lv_obj_t* inspector_panel) {
DEBUG_LOG(LOG_MODULE_RENDERER, "Reloading UI from string");
// --- State Reset ---
// Clean up memory from the *previous* render cycle first.
if (g_renderer_registry) {
registry_free(g_renderer_registry);
g_renderer_registry = NULL;
}
// Unconditionally clean the UI and reset global registries at the beginning of every reload.
// This ensures that we always start from a known-good state, even if the previous render failed.
lv_obj_clean(preview_panel);
if (inspector_panel) {
lv_obj_clean(inspector_panel);
}
obj_registry_deinit();
data_binding_init();
ui_sim_init(); // ADDED: Reset the UI Simulator
// --- IR Generation ---
// This will also process any `data-binding` block and populate the UI-Sim model
IRRoot* ir_root = generate_ir_from_string(ui_spec_string, api_spec);
// --- Handle Generation Result ---
if (!ir_root) {
// Generation failed. The generator already logged the error via render_abort().
// The screen is already clean, so just display an error message.
lv_obj_t* label = lv_label_create(preview_panel);
lv_label_set_text(label, "#f04040 Error generating UI.\nSee VSCode console for details.#");
lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_center(label);
// Do not proceed. The UI will show this error until the next successful render.
return;
}
// --- ADDED: Unconditional Hint for Empty UI ---
if (ir_root->root_objects == NULL) {
fprintf(stderr, "[HINT] UI specification is empty or contains no renderable objects. The preview will be blank.\n");
fflush(stderr);
}
// Validate IR for dynamic-dispatch: ensure enum/constant expressions carry numeric values
if (!ir_validate_for_dynamic_dispatch(ir_root)) {
// Generator already logged details via render_abort equivalents; surface a short message
lv_obj_t* label = lv_label_create(preview_panel);
lv_label_set_text(label, "#f04040 Error: IR validation failed for dynamic dispatch.#");
lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_center(label);
ir_free((IRNode*)ir_root);
return;
}
// --- Render a valid IR ---
// Create the new registry for this render cycle. It will be freed on the next reload.
g_renderer_registry = registry_create();
if (g_renderer_registry) {
lvgl_render_backend(ir_root, api_spec, preview_panel, g_renderer_registry);
// DO NOT free the registry here. Its data (grid arrays) must persist for LVGL.
}
if (inspector_panel) {
view_inspector_init(inspector_panel, ir_root, api_spec);
}
// Free the IR, it's not needed anymore for this cycle
ir_free((IRNode*)ir_root);
// ADDED: Start the UI Simulator *after* the UI has been rendered.
ui_sim_start();
DEBUG_LOG(LOG_MODULE_RENDERER, "UI reload complete.");
}
void lvgl_renderer_reload_ui_from_string_with_base_path(const char* ui_spec_string, const char* base_path, ApiSpec* api_spec, lv_obj_t* preview_panel, lv_obj_t* inspector_panel) {
DEBUG_LOG(LOG_MODULE_RENDERER, "Reloading UI from string with base path: %s", base_path ? base_path : "(null)");
if (g_renderer_registry) {
registry_free(g_renderer_registry);
g_renderer_registry = NULL;
}
lv_obj_clean(preview_panel);
if (inspector_panel) {
lv_obj_clean(inspector_panel);
}
obj_registry_deinit();
data_binding_init();
ui_sim_init();
IRRoot* ir_root = generate_ir_from_string_with_base_path(ui_spec_string, base_path ? base_path : ".", api_spec);
if (!ir_root) {
lv_obj_t* label = lv_label_create(preview_panel);
lv_label_set_text(label, "#f04040 Error generating UI.\nSee VSCode console for details.#");
lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_center(label);
return;
}
if (ir_root->root_objects == NULL) {
fprintf(stderr, "[HINT] UI specification is empty or contains no renderable objects. The preview will be blank.\n");
fflush(stderr);
}
if (!ir_validate_for_dynamic_dispatch(ir_root)) {
lv_obj_t* label = lv_label_create(preview_panel);
lv_label_set_text(label, "#f04040 Error: IR validation failed for dynamic dispatch.#");
lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_center(label);
ir_free((IRNode*)ir_root);
return;
}
g_renderer_registry = registry_create();
if (g_renderer_registry) {
lvgl_render_backend(ir_root, api_spec, preview_panel, g_renderer_registry);
}
if (inspector_panel) {
view_inspector_init(inspector_panel, ir_root, api_spec);
}
ir_free((IRNode*)ir_root);
ui_sim_start();
DEBUG_LOG(LOG_MODULE_RENDERER, "UI reload complete (with base path).");
}
void lvgl_renderer_reload_ui(const char* ui_spec_path, ApiSpec* api_spec, lv_obj_t* preview_panel, lv_obj_t* inspector_panel) {
DEBUG_LOG(LOG_MODULE_RENDERER, "Loading UI spec from file: %s", ui_spec_path);
char* content = read_file(ui_spec_path);
if (!content) {
print_warning("Failed to read UI spec file: %s", ui_spec_path);
// Display an error message on the screen
lv_obj_clean(preview_panel);
lv_obj_t* label = lv_label_create(preview_panel);
lv_label_set_text_fmt(label, "#f04040 Error reading file:\n%s#", ui_spec_path);
lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_center(label);
return;
}
lvgl_renderer_reload_ui_from_string(content, api_spec, preview_panel, inspector_panel);
free(content);
}
// --- Core Rendering Logic ---
static binding_value_t* evaluate_binding_array_expr(RenderContext* ctx, IRExprArray* arr, uint32_t* out_count);
static void render_single_object(RenderContext* ctx, IRObject* current_obj) {
if (ctx->error_occurred) return;
DEBUG_LOG(LOG_MODULE_RENDERER, "Rendering object: c_name='%s', json_type='%s'", current_obj->c_name, current_obj->json_type);
RenderValue constructor_result = { .type = RENDER_VAL_TYPE_NULL, .as.p_val = NULL };
void* c_obj = NULL;
if (current_obj->constructor_expr) {
bool is_malloc = (current_obj->constructor_expr->base.type == IR_EXPR_FUNCTION_CALL &&
strcmp(((IRExprFunctionCall*)current_obj->constructor_expr)->func_name, "malloc") == 0);
if (is_malloc) {
if (strcmp(current_obj->c_type, "lv_style_t*") == 0) {
c_obj = malloc(sizeof(lv_style_t));
} else {
render_abort("Renderer Error: Unknown object type, cannot malloc.");
}
} else {
evaluate_expression(ctx, current_obj->constructor_expr, &constructor_result);
if (ctx->error_occurred) return; // Unwind if evaluation failed
if(constructor_result.type == RENDER_VAL_TYPE_POINTER) {
c_obj = constructor_result.as.p_val;
}
}
}
if (c_obj) view_inspector_set_object_pointer((IRNode*)current_obj, c_obj);
registry_add_pointer(ctx->registry, c_obj, current_obj->c_name, current_obj->json_type, current_obj->c_type);
obj_registry_add(current_obj->c_name, c_obj);
if (current_obj->registered_id) {
registry_add_pointer(ctx->registry, c_obj, current_obj->registered_id, current_obj->json_type, current_obj->c_type);
obj_registry_add(current_obj->registered_id, c_obj);
}
if (current_obj->operations) {
// NOTE: `deferred_fn_name` is intentionally ignored by the lvgl_renderer backend.
// The renderer always inlines all children so that the live preview works correctly.
// Only the c_code backend uses deferred_fn_name to extract children into a separate
// static function for lazy/on-demand loading on resource-constrained targets.
for (IROperationNode* op_node = current_obj->operations; op_node; op_node = op_node->next) {
if (ctx->error_occurred) break; // Stop processing operations if a prior one failed
IRNode* node = op_node->op_node;
if (node->type == IR_NODE_OBJECT) {
render_single_object(ctx, (IRObject*)node);
} else if (node->type == IR_NODE_WARNING) {
print_hint("%s", ((IRWarning*)node)->message);
} else if (node->type == IR_NODE_OBSERVER) {
IRObserver* obs = (IRObserver*)node;
void* config_ptr = NULL;
size_t config_len = 0;
void* default_ptr = NULL;
RenderValue val;
evaluate_expression(ctx, obs->config_expr, &val);
if (ctx->error_occurred) continue;
if (obs->update_type == OBSERVER_TYPE_VALUE) {
lv_anim_enable_t anim_flag = LV_ANIM_ON;
if (obs->config_expr->base.type == IR_EXPR_ARRAY) {
IRExprArray* arr = (IRExprArray*)obs->config_expr;
if (arr->elements) {
RenderValue anim_val;
evaluate_expression(ctx, arr->elements->expr, &anim_val);
if (!ctx->error_occurred && anim_val.type == RENDER_VAL_TYPE_INT) {
anim_flag = (lv_anim_enable_t)anim_val.as.i_val;
}
}
} else if (obs->config_expr->base.type == IR_EXPR_LITERAL) {
// Support for observes: { state: "value" } -> value: [LV_ANIM_ON]
// Do nothing, anim_flag is already LV_ANIM_ON
}
config_ptr = &anim_flag;
config_len = sizeof(lv_anim_enable_t);
} else if (obs->config_expr->base.type == IR_EXPR_LITERAL) {
if (((IRExprLiteral*)obs->config_expr)->is_string) {
config_ptr = (void*)val.as.s_val; // Format string
} else {
config_ptr = &val.as.b_val; // bool for direct mapping
}
} else if (obs->config_expr->base.type == IR_EXPR_ARRAY) { // Map
IRExprArray* map_arr = (IRExprArray*)obs->config_expr;
config_len = 0;
for (IRExprNode* n = map_arr->elements; n; n = n->next) config_len++;
binding_map_entry_t* map = calloc(config_len, sizeof(binding_map_entry_t));
int i = 0;
int final_count = 0;
for (IRExprNode* n = map_arr->elements; n; n = n->next, i++) {
IRExprArray* pair = (IRExprArray*)n->expr;
RenderValue key, value;
evaluate_expression(ctx, pair->elements->expr, &key);
if(ctx->error_occurred) break;
evaluate_expression(ctx, pair->elements->next->expr, &value);
if(ctx->error_occurred) break;
if(key.type == RENDER_VAL_TYPE_STRING && strcmp(key.as.s_val, "default") == 0) {
if (obs->update_type == OBSERVER_TYPE_STYLE) default_ptr = value.as.p_val;
else default_ptr = &value.as.b_val;
} else {
if (key.type == RENDER_VAL_TYPE_STRING) map[final_count].key = (binding_value_t){.type=BINDING_TYPE_STRING, .as.s_val=key.as.s_val};
else if (key.type == RENDER_VAL_TYPE_BOOL) map[final_count].key = (binding_value_t){.type=BINDING_TYPE_BOOL, .as.b_val=key.as.b_val};
else map[final_count].key = (binding_value_t){.type=BINDING_TYPE_FLOAT, .as.f_val=(float)key.as.i_val};
if (obs->update_type == OBSERVER_TYPE_STYLE) map[final_count].value.p_val = value.as.p_val;
else map[final_count].value.b_val = value.as.b_val;
final_count++;
}
}
if(ctx->error_occurred) {
free(map);
continue;
}
config_ptr = map;
config_len = final_count;
}
data_binding_add_observer(obs->state_name, c_obj, obs->update_type, config_ptr, config_len, default_ptr);
// Free map if it was allocated
if (obs->update_type != OBSERVER_TYPE_VALUE && obs->config_expr->base.type == IR_EXPR_ARRAY) {
free(config_ptr);
}
} else if (node->type == IR_NODE_ACTION) {
IRAction* act = (IRAction*)node;
binding_value_t* cycle_values = NULL;
uint32_t cycle_count = 0;
void* config_data = NULL;
if (act->action_type == ACTION_TYPE_CYCLE && act->data_expr && act->data_expr->base.type == IR_EXPR_ARRAY) {
cycle_values = evaluate_binding_array_expr(ctx, (IRExprArray*)act->data_expr, &cycle_count);
} else if (act->action_type == ACTION_TYPE_NUMERIC_DIALOG && act->data_expr && act->data_expr->base.type == IR_EXPR_ARRAY) {
// This is a temporary struct passed on the stack. data_binding_add_action will copy it.
struct { float min_val, max_val, initial_val; const char* format_str, *text; } dialog_cfg = {
.min_val = 0, .max_val = 100, .initial_val = 0, .format_str = "%g", .text = "Input value:"
};
IRExprArray* map_arr = (IRExprArray*)act->data_expr;
for (IRExprNode* n = map_arr->elements; n; n = n->next) {
IRExprArray* pair = (IRExprArray*)n->expr;
RenderValue key, value;
evaluate_expression(ctx, pair->elements->expr, &key);
if(ctx->error_occurred) break;
evaluate_expression(ctx, pair->elements->next->expr, &value);
if(ctx->error_occurred) break;
if (key.type == RENDER_VAL_TYPE_STRING) {
if (strcmp(key.as.s_val, "min") == 0 && value.type == RENDER_VAL_TYPE_INT) dialog_cfg.min_val = (float)value.as.i_val;
else if (strcmp(key.as.s_val, "max") == 0 && value.type == RENDER_VAL_TYPE_INT) dialog_cfg.max_val = (float)value.as.i_val;
else if (strcmp(key.as.s_val, "initial") == 0 && value.type == RENDER_VAL_TYPE_INT) dialog_cfg.initial_val = (float)value.as.i_val;
else if (strcmp(key.as.s_val, "format") == 0 && value.type == RENDER_VAL_TYPE_STRING) dialog_cfg.format_str = value.as.s_val;
else if (strcmp(key.as.s_val, "text") == 0 && value.type == RENDER_VAL_TYPE_STRING) dialog_cfg.text = value.as.s_val;
}
}
if (ctx->error_occurred) continue;
config_data = &dialog_cfg;
}
if (ctx->error_occurred) {
free(cycle_values);
continue;
}
data_binding_add_action(c_obj, act->action_name, act->action_type, cycle_values, cycle_count, config_data);
if (cycle_values) free(cycle_values);
} else if (node->type == IR_NODE_DEFINE) {
render_ctx_add_define(ctx, ((IRDefine*)node)->symbol);
} else if (node->type == IR_NODE_IFDEF) {
render_ifdef_node(ctx, (IRIfdef*)node, c_obj);
} else {
RenderValue ignored;
evaluate_expression(ctx, (IRExpr*)node, &ignored);
}
}
}
}
// Evaluate an ifdef node and render the first matching branch.
// c_obj is the LVGL object that owns this operation scope (may be NULL for root-level).
static void render_ifdef_node(RenderContext* ctx, IRIfdef* ifn, lv_obj_t* c_obj) {
IRIfdefBranch* else_br = NULL;
for (IRIfdefBranch* br = ifn->branches; br; br = br->next) {
if (!br->condition) { else_br = br; continue; }
if (render_ctx_has_define(ctx, br->condition)) {
// Render this branch's ops
for (IROperationNode* op = br->ops; op; op = op->next) {
if (ctx->error_occurred) return;
IRNode* n = op->op_node;
if (n->type == IR_NODE_OBJECT) {
render_single_object(ctx, (IRObject*)n);
} else if (n->type == IR_NODE_DEFINE) {
render_ctx_add_define(ctx, ((IRDefine*)n)->symbol);
} else if (n->type == IR_NODE_IFDEF) {
render_ifdef_node(ctx, (IRIfdef*)n, c_obj);
} else if (n->type == IR_NODE_WARNING) {
print_hint("%s", ((IRWarning*)n)->message);
} else {
// observers, actions and function calls — these need c_obj
// (same handle as outer scope). Re-evaluate via the same
// path as render_single_object's operation loop:
if (c_obj) {
if (n->type == IR_NODE_OBSERVER) {
// Observer handling mirrors render_single_object;
// for correctness a full copy would be needed.
// For now emit a hint about the limitation.
print_hint("Observer inside ifdef branch: partial renderer support.");
} else if (n->type == IR_NODE_ACTION) {
print_hint("Action inside ifdef branch: partial renderer support.");
} else {
RenderValue ignored;
evaluate_expression(ctx, (IRExpr*)n, &ignored);
}
}
}
}
return;
}
}
// No named branch matched — render the else branch if present.
if (else_br) {
for (IROperationNode* op = else_br->ops; op; op = op->next) {
if (ctx->error_occurred) return;
IRNode* n = op->op_node;
if (n->type == IR_NODE_OBJECT) {
render_single_object(ctx, (IRObject*)n);
} else if (n->type == IR_NODE_DEFINE) {
render_ctx_add_define(ctx, ((IRDefine*)n)->symbol);
} else if (n->type == IR_NODE_IFDEF) {
render_ifdef_node(ctx, (IRIfdef*)n, c_obj);
} else if (n->type == IR_NODE_WARNING) {
print_hint("%s", ((IRWarning*)n)->message);
} else {
RenderValue ignored;
evaluate_expression(ctx, (IRExpr*)n, &ignored);
}
}
}
}
static void render_object_list(RenderContext* ctx, IRObject* head) {
for (IRObject* current_obj = head; current_obj; current_obj = current_obj->next) {
if (ctx->error_occurred) break;
render_single_object(ctx, current_obj);
}
}
// --- Recursive Expression Evaluator ---
static binding_value_t* evaluate_binding_array_expr(RenderContext* ctx, IRExprArray* arr, uint32_t* out_count) {
int count = 0;
for (IRExprNode* n = arr->elements; n; n = n->next) count++;
*out_count = count;
if (count == 0) return NULL;
binding_value_t* result_array = calloc(count, sizeof(binding_value_t));
if (!result_array) {
render_abort("Failed to allocate binding_value_t array");
ctx->error_occurred = true;
return NULL;
}
int i = 0;
for (IRExprNode* n = arr->elements; n; n = n->next, i++) {
RenderValue val;
evaluate_expression(ctx, n->expr, &val);
if(ctx->error_occurred) {
free(result_array);
return NULL;
}
switch(val.type) {
case RENDER_VAL_TYPE_STRING: result_array[i] = (binding_value_t){.type=BINDING_TYPE_STRING, .as.s_val=val.as.s_val}; break;
case RENDER_VAL_TYPE_BOOL: result_array[i] = (binding_value_t){.type=BINDING_TYPE_BOOL, .as.b_val=val.as.b_val}; break;
case RENDER_VAL_TYPE_INT: result_array[i] = (binding_value_t){.type=BINDING_TYPE_FLOAT, .as.f_val=(float)val.as.i_val}; break;
default: result_array[i].type = BINDING_TYPE_NULL;
}
}
return result_array;
}
static void evaluate_expression(RenderContext* ctx, IRExpr* expr, RenderValue* out_val) {
if (ctx->error_occurred) return;
if (!expr) {
out_val->type = RENDER_VAL_TYPE_NULL;
out_val->as.p_val = NULL;
return;
}
switch (expr->base.type) {
case IR_EXPR_IF_BACKEND: {
IRIfBackend* if_node = (IRIfBackend*)expr;
evaluate_expression(ctx, if_node->dynamic_expr, out_val);
return;
}
case IR_EXPR_LITERAL: {
IRExprLiteral* lit = (IRExprLiteral*)expr;
// If the literal text is exactly "NULL" and the declared C type
// is a pointer type (but not a char/string pointer), treat this
// as a genuine NULL pointer value rather than a string.
if (lit->value && strcmp(lit->value, "NULL") == 0 && lit->base.c_type && strchr(lit->base.c_type, '*') != NULL && strstr(lit->base.c_type, "char") == NULL) {
out_val->type = RENDER_VAL_TYPE_NULL;
out_val->as.p_val = NULL;
return;
}
if (lit->is_string) {
// Special-case: if the literal is the exact string "NULL" and
// the declared C type for this literal is a pointer type but
// not a char/string pointer, treat it as a genuine NULL
// pointer instead of the address of the string "NULL".
if (lit->value && strcmp(lit->value, "NULL") == 0 && lit->base.c_type && strchr(lit->base.c_type, '*') != NULL && strstr(lit->base.c_type, "char") == NULL) {
out_val->type = RENDER_VAL_TYPE_NULL;
out_val->as.p_val = NULL;
} else {
out_val->type = RENDER_VAL_TYPE_STRING;
out_val->as.s_val = lit->value;
}
} else {
if(strcmp(lit->base.c_type, "bool") == 0) {
out_val->type = RENDER_VAL_TYPE_BOOL;
out_val->as.b_val = (strcmp(lit->value, "true") == 0);
} else { // It's a number, treat as int internally for renderer
out_val->type = RENDER_VAL_TYPE_INT;
out_val->as.i_val = strtol(lit->value, NULL, 0);
}
}
return;
}
case IR_EXPR_STATIC_STRING: {
out_val->type = RENDER_VAL_TYPE_STRING;
out_val->as.s_val = ((IRExprStaticString*)expr)->value;
return;
}
case IR_EXPR_ENUM: {
out_val->type = RENDER_VAL_TYPE_INT;
out_val->as.i_val = ((IRExprEnum*)expr)->value;
return;
}
case IR_EXPR_REGISTRY_REF: {
const char* name = ((IRExprRegistryRef*)expr)->name;
out_val->type = RENDER_VAL_TYPE_POINTER;
out_val->as.p_val = registry_get_pointer(ctx->registry, name, NULL);
if (!out_val->as.p_val) {
const char* key = (name[0] == '@') ? name + 1 : name;
if (strcmp(key, "NULL") != 0) { // It's okay to not find "NULL", it means we want a null pointer.
char error_buf[256];
snprintf(error_buf, sizeof(error_buf), "Reference Error: Object with ID '%s' not found in the registry.", name);
#if RENDERER_ABORT_ON_UNRESOLVED_REFERENCE == 1
print_warning("%s Aborting...", error_buf);
registry_dump(ctx->registry);
registry_dump_suggestions(ctx->registry, key);
ctx->error_occurred = true;
#else
DEBUG_LOG(LOG_MODULE_RENDERER, "Warning: %s", error_buf);
registry_dump(ctx->registry);
registry_dump_suggestions(ctx->registry, key);
#endif
}
}
return;
}
case IR_EXPR_ARRAY: {
IRExprArray* arr = (IRExprArray*)expr;
if (arr->static_array_ptr) {
out_val->type = RENDER_VAL_TYPE_POINTER;
out_val->as.p_val = arr->static_array_ptr;
return;
}
int element_count = 0;
for (IRExprNode* n = arr->elements; n; n=n->next) element_count++;
char* base_type = get_array_base_type(arr->base.c_type);
size_t element_size = 0;
if(strcmp(base_type, "lv_coord_t") == 0) element_size = sizeof(lv_coord_t);
else if(strcmp(base_type, "int32_t") == 0) element_size = sizeof(int32_t);
else if(strcmp(base_type, "int") == 0) element_size = sizeof(int);
else if(strcmp(base_type, "void*") == 0) element_size = sizeof(void*);
// lv_style_prop_t is typedef uint8_t, but its enum values fit in int32_t;
// treat _lv_style_id_t and similar enum types as int32_t for safe evaluation.
else if(strcmp(base_type, "lv_style_prop_t") == 0) element_size = sizeof(int32_t);
else if(strcmp(base_type, "_lv_style_id_t") == 0) element_size = sizeof(int32_t);
else if(strcmp(base_type, "uint8_t") == 0) element_size = sizeof(uint8_t);
else if(strcmp(base_type, "uint16_t") == 0) element_size = sizeof(uint16_t);
else if(strcmp(base_type, "uint32_t") == 0) element_size = sizeof(uint32_t);
else {
// Unknown array element type: emit a warning and skip (returns NULL).
// This mirrors the c_code backend's "/* UNMAPPED_ARRAY */ NULL" output.
print_warning("Renderer: unsupported array base type '%s' — skipping array (will pass NULL).", base_type);
free(base_type);
out_val->type = RENDER_VAL_TYPE_POINTER;
out_val->as.p_val = NULL;
return;
}
free(base_type);
void* c_array = malloc(element_count * element_size);
if (!c_array) {
render_abort("Failed to allocate memory for static array.");
ctx->error_occurred = true;
return;
}
int i = 0;
for (IRExprNode* n = arr->elements; n; n=n->next) {
RenderValue elem_val;
evaluate_expression(ctx, n->expr, &elem_val);
if (ctx->error_occurred) {
free(c_array);
return;
}
if (elem_val.type == RENDER_VAL_TYPE_INT) {
if (element_size == sizeof(lv_coord_t)) ((lv_coord_t*)c_array)[i] = (lv_coord_t)elem_val.as.i_val;
else if (element_size == sizeof(int32_t)) ((int32_t*)c_array)[i] = (int32_t)elem_val.as.i_val;
else if (element_size == sizeof(int)) ((int*)c_array)[i] = (int)elem_val.as.i_val;
else if (element_size == sizeof(uint16_t)) ((uint16_t*)c_array)[i] = (uint16_t)elem_val.as.i_val;
else if (element_size == sizeof(uint8_t)) ((uint8_t*)c_array)[i] = (uint8_t)elem_val.as.i_val;
} else if (elem_val.type == RENDER_VAL_TYPE_POINTER) {
if (element_size == sizeof(void*)) ((void**)c_array)[i] = elem_val.as.p_val;
}
i++;
}
arr->static_array_ptr = c_array;
registry_add_static_array(ctx->registry, c_array);
out_val->type = RENDER_VAL_TYPE_POINTER;
out_val->as.p_val = c_array;
return;
}
case IR_EXPR_RUNTIME_REG_ADD: {
IRExprRuntimeRegAdd* reg = (IRExprRuntimeRegAdd*)expr;
RenderValue obj_to_reg;
evaluate_expression(ctx, reg->object_expr, &obj_to_reg);
if (ctx->error_occurred) return;
if (obj_to_reg.type == RENDER_VAL_TYPE_POINTER) {
obj_registry_add(reg->id, obj_to_reg.as.p_val);
}
out_val->type = RENDER_VAL_TYPE_NULL;
return;
}
case IR_EXPR_FUNCTION_CALL: {
IRExprFunctionCall* call = (IRExprFunctionCall*)expr;
DEBUG_LOG(LOG_MODULE_DISPATCH, "Evaluating call: %s", call->func_name);
int arg_count = 0;
for (IRExprNode* n = call->args; n; n = n->next) arg_count++;
RenderValue* evaluated_args = arg_count > 0 ? calloc(arg_count, sizeof(RenderValue)) : NULL;
if (arg_count > 0 && !evaluated_args) {
render_abort("Failed to allocate evaluated_args array");
ctx->error_occurred = true;
return;
}
int i = 0;
for (IRExprNode* n = call->args; n; n = n->next) {
evaluate_expression(ctx, n->expr, &evaluated_args[i++]);
if (ctx->error_occurred) {
free(evaluated_args);
return; // Unwind
}
}
IRExprNode* temp_ir_args_head = NULL;
for (i = 0; i < arg_count; i++) {
IRExpr* temp_expr = NULL;
switch (evaluated_args[i].type) {
case RENDER_VAL_TYPE_INT: {
char buf[32]; snprintf(buf, sizeof(buf), "%ld", (long)evaluated_args[i].as.i_val);
temp_expr = ir_new_expr_literal(buf, "int"); break;
}
case RENDER_VAL_TYPE_BOOL:
temp_expr = ir_new_expr_literal(evaluated_args[i].as.b_val ? "true" : "false", "bool"); break;
case RENDER_VAL_TYPE_COLOR: {
char buf[32]; snprintf(buf, sizeof(buf), "%u", lv_color_to_u32(evaluated_args[i].as.color_val));
temp_expr = ir_new_expr_literal(buf, "lv_color_t"); break;
}
case RENDER_VAL_TYPE_STRING:
temp_expr = ir_new_expr_literal(evaluated_args[i].as.s_val, "const char*");
((IRExprLiteral*)temp_expr)->is_string = true; break;
case RENDER_VAL_TYPE_POINTER:
case RENDER_VAL_TYPE_NULL: {
void* ptr = evaluated_args[i].as.p_val;
const char* id = registry_get_id_from_pointer(ctx->registry, ptr);
if (id) {
temp_expr = ir_new_expr_literal(id, "void*");
((IRExprLiteral*)temp_expr)->is_string = true;
} else {
temp_expr = ir_new_expr_raw_pointer(ptr, "void*");
}
break;
}
}
if (temp_expr) ir_expr_list_add(&temp_ir_args_head, temp_expr);
}
IRNode** final_ir_args_array = arg_count > 0 ? calloc(arg_count, sizeof(IRNode*)) : NULL;
if (arg_count > 0 && !final_ir_args_array) {
render_abort("Failed to alloc final_ir_args_array");
ctx->error_occurred = true;
free(evaluated_args);
ir_free((IRNode*)temp_ir_args_head);
return;
}
i = 0;
for (IRExprNode* n = temp_ir_args_head; n; n = n->next) final_ir_args_array[i++] = (IRNode*)n->expr;
void* target_obj = NULL;
IRNode** dispatcher_args = NULL;
int dispatcher_arg_count = 0;
const FunctionArg* f_args = api_spec_get_function_args_by_name(ctx->spec, call->func_name);
bool first_arg_is_target = f_args && f_args->type && (strstr(f_args->type, "_t*") != NULL);
if (first_arg_is_target && arg_count > 0) {
target_obj = evaluated_args[0].as.p_val;
dispatcher_args = (arg_count > 1) ? &final_ir_args_array[1] : NULL;
dispatcher_arg_count = arg_count - 1;
} else {
target_obj = NULL;
dispatcher_args = final_ir_args_array;
dispatcher_arg_count = arg_count;
}
*out_val = dynamic_lvgl_call_ir(call->func_name, target_obj, dispatcher_args, dispatcher_arg_count, ctx->spec);
free(evaluated_args);
ir_free((IRNode*)temp_ir_args_head);
free(final_ir_args_array);
return;
}
default:
DEBUG_LOG(LOG_MODULE_RENDERER, "Warning: evaluate_expression called on un-evaluatable node type %d", expr->base.type);
out_val->type = RENDER_VAL_TYPE_NULL;
out_val->as.p_val = NULL;
return;
}
}