-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembed.c
More file actions
1276 lines (1191 loc) · 61.3 KB
/
Copy pathembed.c
File metadata and controls
1276 lines (1191 loc) · 61.3 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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file embed.c is part of L1vm.
*
* (c) Copyright Stefan Pietzonke (info@midnight-coding.de), 2026
*
* L1vm is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* L1vm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with L1vm. If not, see <http://www.gnu.org/licenses/>.
*/
#include "brackets-code.h"
// ==================== TINY LLM INFERENCE ENGINE ====================
WordEmbedding word_embeddings[VOCAB_SIZE];
float attention_weights[NUM_EMITTERS];
const char *EMITTER_NAMES[NUM_EMITTERS] = {"math","input_loop","loop","for_sum","print_even","find_max","countdown","fib_seq","input_sort","median","string_cat","string_compare","array_assign","array_reverse","array_find","input_fact","array_vmath","read_file","write_file","string_to_num","timer","factorial","fizzbuzz","primes","even_odd","power","mult_table","guess","gcd","hello_name","random","array_min_max","bool_demo","bit_check","fann_create","fann_train","fann_run","average","selection_sort","palindrome","lcm","collatz","sum_of_digits","reverse_string","armstrong","perfect_number","count_vowels","anagram_check","string_to_upper","string_to_lower","caesar_cipher","palindrome_string","bubble_sort","binary_search","square_root","prime_factorization","standard_deviation","compound_interest","decimal_to_binary","dice_roll","double_math","double_circle_area","double_average","double_compound_interest","double_pythagoras","double_temp_convert","double_sqrt","function","string_length","stack","queue","insertion_sort","calculator","unit_converter","rock_paper_scissors","pyramid","temp_converter_menu","sort_stats","string_analyzer","number_analyzer","filter_numbers","random_generator","math_menu","quiz_game","bmi_calculator","statistics_suite","linked_list","binary_search_tree","tree_traversal","graph_bfs_dfs","n_queens","sudoku","levenshtein_distance","maze_generator","maze_solver","monte_carlo_pi","matrix_multiplication","matrix_transpose","numerical_integration","complex_numbers","linear_regression","base_converter","freq_analysis","shuffle","weighted_random","ascii_table","bignum_math","password_card","chess_problem","shell_repl","webserver","sdl_window","sdl_button","thread","scheduler","shell_exec","json","crypto","bluetooth_ble","serial_rs232","gpio","gps","timer_date","sdl_sound","sdl_joystick","sdl_mouse","fractal","cluster_3x1","reload","coordinate_grid","turmite","crossword","linter","double_power","double_volume_sphere","double_discount","double_simple_interest","double_bmi","double_standard_deviation","double_kinetic_energy","add","sub","mul","div","double_add","double_sub","double_mul","double_div",
"hello_world", "string_find", "string_split", "switch_demo",
"type_convert", "iterative_factorial", "random_walk", "bar_chart",
"hanoi_tower", "ascii_art", "number_to_words", "temperature_table",
"loop_demo", "pointer_demo", "struct_demo", "hex_binary",
"shell_args", "time_demo"};
/* compile-time assert: NUM_EMITTERS must match actual array count */
typedef int EMITTER_COUNT_CHECK[(sizeof(EMITTER_NAMES)/sizeof(EMITTER_NAMES[0])) == NUM_EMITTERS ? 1 : -1];
int vs_boost_tokens[64];
int vs_boost_count = 0;
// Vector search data structures
const char *example_subdirs[EXAMPLE_SUBDIRS] = {"prog", "include", "lib"};
static const char *example_exts[] = {".l1com", ".l1h", ".l1asm"};
ExampleDoc example_docs[MAX_EXAMPLES];
int num_examples = 0;
int examples_indexed = 0;
void index_examples(void);
int search_examples(const char *query, int top_k, int *indices, float *scores);
static unsigned long hash_word(const char *s) {
unsigned long hash = 5381;
int c;
while ((c = *s++))
hash = ((hash << 5) + hash) + c;
return hash;
}
float idf_weights[VOCAB_SIZE];
// Vocabulary token IDs
#define TOK_SUM 0
#define TOK_ADD 1
#define TOK_SUB 2
#define TOK_MUL 3
#define TOK_DIV 4
#define TOK_MOD 5
#define TOK_INPUT 6
#define TOK_PRINT 7
#define TOK_LOOP 8
#define TOK_FOR 9
#define TOK_WHILE 10
#define TOK_IF 11
#define TOK_ARRAY 12
#define TOK_SORT 13
#define TOK_MAX 14
#define TOK_MIN 15
#define TOK_AVERAGE 16
#define TOK_MEDIAN 17
#define TOK_COUNTDOWN 18
#define TOK_FIBONACCI 19
#define TOK_FACTORIAL 20
#define TOK_PRIME 21
#define TOK_FIZZBUZZ 22
#define TOK_POWER 23
#define TOK_GCD 24
#define TOK_TIME 25
#define TOK_POINTER 26
#define TOK_STRUCT 27
#define TOK_STRING 28
#define TOK_CONCAT 29
#define TOK_COMPARE 30
#define TOK_REVERSE 31
#define TOK_FIND 32
#define TOK_ASSIGN 33
#define TOK_FILE 34
#define TOK_READ 35
#define TOK_WRITE 36
#define TOK_CONVERT 37
#define TOK_TIMER 38
#define TOK_SHELL 39
#define TOK_HELLO 40
#define TOK_FUNCTION 41
#define TOK_EVEN 42
#define TOK_ODD 43
#define TOK_GUESS 44
#define TOK_TABLE 45
#define TOK_HEX 46
#define TOK_BINARY 47
#define TOK_RANGE 48
#define TOK_EXIT 49
#define TOK_ZERO 50
#define TOK_ONE 51
#define TOK_TWO 52
#define TOK_NUM 53
#define TOK_VALUE 54
#define TOK_DATA 55
#define TOK_CODE 56
#define TOK_GENERATE 57
#define TOK_CREATE 58
#define TOK_MAKE 59
#define TOK_SHOW 60
#define TOK_START 61
#define TOK_STOP 62
#define TOK_RUN 63
#define TOK_FANN 64
#define TOK_TRAIN 65
#define TOK_NEURAL 66
#define TOK_NETWORK 67
#define TOK_MODEL 68
#define TOK_LAYER 69
#define TOK_LEARN 70
#define TOK_PREDICT 71
const char *vocab[VOCAB_SIZE] = {
"sum", "add", "sub", "mul", "div", "mod", "input", "print", "loop", "for",
"while", "if", "array", "sort", "max", "min", "average", "median", "countdown",
"fibonacci", "factorial", "prime", "fizzbuzz", "power", "gcd", "time", "pointer",
"struct", "string", "concat", "compare", "reverse", "find", "assign", "file",
"read", "write", "convert", "timer", "shell", "hello", "function", "even",
"odd", "guess", "table", "hex", "binary", "range", "exit", "zero", "one",
"two", "num", "value", "data", "code", "generate", "create", "make", "show",
"start", "stop", "run",
"fann", "train", "neural", "network", "model", "layer", "learn", "predict"
};
int embeddings_initialized = 0;
void init_embeddings(void) {
if (embeddings_initialized) return;
embeddings_initialized = 1;
for (int i = 0; i < VOCAB_SIZE; i++) {
const char *w = vocab[i];
int len = strlen(w);
// Build embedding from character trigrams so similar words get similar vectors
int ngram_count = 0;
for (int ci = 0; ci + 2 < len; ci++) {
char trigram[4] = {w[ci], w[ci+1], w[ci+2], '\0'};
unsigned long th = hash_word(trigram);
srand(th);
for (int j = 0; j < EMBED_DIM; j++)
word_embeddings[i].embed[j] += ((float)rand() / (float)RAND_MAX) * 2.0f - 1.0f;
ngram_count++;
}
// Fallback for short words: use bigrams and character-position hash
if (ngram_count == 0) {
for (int ci = 0; ci + 1 < len; ci++) {
char bigram[3] = {w[ci], w[ci+1], '\0'};
unsigned long th = hash_word(bigram);
srand(th);
for (int j = 0; j < EMBED_DIM; j++)
word_embeddings[i].embed[j] += ((float)rand() / (float)RAND_MAX) * 2.0f - 1.0f;
ngram_count++;
}
}
// Single-char fallback
if (ngram_count == 0) {
unsigned long th = hash_word(w);
srand(th);
for (int j = 0; j < EMBED_DIM; j++)
word_embeddings[i].embed[j] = ((float)rand() / (float)RAND_MAX) * 2.0f - 1.0f;
ngram_count = 1;
}
// Average
float inv = 1.0f / ngram_count;
float sum = 0;
for (int j = 0; j < EMBED_DIM; j++) {
word_embeddings[i].embed[j] *= inv;
sum += word_embeddings[i].embed[j] * word_embeddings[i].embed[j];
}
float norm = sqrtf(sum);
if (norm > 0)
for (int j = 0; j < EMBED_DIM; j++) word_embeddings[i].embed[j] /= norm;
}
for (int i = 0; i < VOCAB_SIZE; i++) idf_weights[i] = 1.0f;
}
int tokenize(const char *text, int *tokens, int max_tokens) {
char buf[MAX_PROMPT];
SNPRINTF_CHECK(buf, sizeof(buf), "%s", text);
to_lowercase(buf);
int count = 0;
char *p = buf;
while (*p && count < max_tokens) {
while (*p && !isalpha((unsigned char)*p)) p++;
if (!*p) break;
char *start = p;
while (*p && isalpha((unsigned char)*p)) p++;
char saved = *p;
*p = '\0';
int found = -1;
for (int i = 0; i < VOCAB_SIZE; i++) {
if (strcmp(start, vocab[i]) == 0) { found = i; break; }
}
if (found < 0) {
const char *syn = resolve_synonym(start);
if (syn) {
for (int i = 0; i < VOCAB_SIZE; i++) {
if (strcmp(syn, vocab[i]) == 0) { found = i; break; }
}
}
}
if (found >= 0 && count < max_tokens) tokens[count++] = found;
*p = saved;
}
return count;
}
static void softmax(float *x, int n) {
if (n <= 0) return;
float max = x[0], sum = 0;
for (int i = 1; i < n; i++) if (x[i] > max) max = x[i];
for (int i = 0; i < n; i++) { x[i] = expf(x[i] - max); sum += x[i]; }
for (int i = 0; i < n; i++) x[i] /= sum;
}
static void apply_token_boosts(float *scores, const int *tokens, int count,
float fann_34, float fann_35, float fann_36,
float train_34, float train_35,
float predict_36,
float p, float s,
float power_0, float power_25)
{
for (int ti = 0; ti < count && ti < 32; ti++) {
int tok_id = tokens[ti];
if (tok_id == TOK_FANN || tok_id == TOK_NEURAL || tok_id == TOK_NETWORK) {
scores[34] += fann_34; scores[35] += fann_35; scores[36] += fann_36; }
if (tok_id == TOK_TRAIN || tok_id == TOK_LEARN) { scores[34] += train_34; scores[35] += train_35; }
if (tok_id == TOK_PREDICT) { scores[36] += predict_36; }
if (tok_id == TOK_SUM || tok_id == TOK_DIV) scores[0] += p;
if (tok_id == TOK_MOD || tok_id == TOK_ADD) scores[1] += p;
if (tok_id == TOK_LOOP || tok_id == TOK_FOR) scores[2] += p;
if (tok_id == TOK_MEDIAN) scores[9] += s;
if (tok_id == TOK_FIBONACCI) scores[7] += s;
if (tok_id == TOK_FACTORIAL) scores[15] += s;
if (tok_id == TOK_SORT) scores[8] += s;
if (tok_id == TOK_MAX) scores[5] += s;
if (tok_id == TOK_MIN) scores[16] += p;
if (tok_id == TOK_STRING) { scores[10] += s; scores[11] += s; }
if (tok_id == TOK_FIND) scores[13] += s;
if (tok_id == TOK_ASSIGN) scores[14] += s;
if (tok_id == TOK_WRITE || tok_id == TOK_READ) { scores[17] += s; scores[18] += s; }
if (tok_id == TOK_CONVERT) scores[19] += s;
if (tok_id == TOK_TIMER) scores[20] += s;
if (tok_id == TOK_STRUCT) scores[6] += s;
if (tok_id == TOK_POWER) scores[0] += power_0;
if (tok_id == TOK_FACTORIAL) scores[21] += s;
if (tok_id == TOK_FIZZBUZZ) scores[22] += s;
if (tok_id == TOK_PRIME) scores[23] += s;
if (tok_id == TOK_EVEN || tok_id == TOK_ODD) scores[24] += s;
if (tok_id == TOK_POWER) scores[25] += power_25;
if (tok_id == TOK_HEX) scores[26] += s;
if (tok_id == TOK_GUESS) scores[27] += s;
if (tok_id == TOK_GCD) scores[28] += s;
if (tok_id == TOK_HELLO) scores[29] += s;
if (tok_id == TOK_NUM) scores[30] += p;
}
}
int llm_select_emitter(const char *prompt, TaskProfile *task) {
int tokens[64], num_tokens = tokenize(prompt, tokens, 64);
float emitter_scores[NUM_EMITTERS];
for (int i = 0; i < NUM_EMITTERS; i++) emitter_scores[i] = 0;
// Macro-based table: SET(idx, flag, score) — sets emitter score if flag is true
#define SET(idx, flag, score) do { if (TF_ISSET(task, FLAG_ ## flag)) emitter_scores[idx] = (score); } while(0)
// Conditional scores (depend on multiple flags)
if (TF_ISSET(task, FLAG_operation) && TF_ISSET(task, FLAG_literals)) emitter_scores[0] = 1.5f;
if (TF_ISSET(task, FLAG_input) && !TF_ISSET(task, FLAG_operation)) emitter_scores[1] = 1.5f;
if (TF_ISSET(task, FLAG_input) && TF_ISSET(task, FLAG_operation)) emitter_scores[1] += 1.0f;
if (TF_ISSET(task, FLAG_loop) && !TF_ISSET(task, FLAG_operation) && !TF_ISSET(task, FLAG_input)) emitter_scores[2] = 1.5f;
if (TF_ISSET(task, FLAG_even_odd) && !TF_ISSET(task, FLAG_print_even)) emitter_scores[24] = 2.0f;
if (TF_ISSET(task, FLAG_sort) && !TF_ISSET(task, FLAG_input)) emitter_scores[38] = 2.0f;
if (TF_ISSET(task, FLAG_average) && TF_ISSET(task, FLAG_input)) emitter_scores[37] = 2.0f;
if (TF_ISSET(task, FLAG_factorial) && !TF_ISSET(task, FLAG_input)) emitter_scores[21] = 2.0f;
if (TF_ISSET(task, FLAG_fann_create) && TF_ISSET(task, FLAG_fann_train)) emitter_scores[35] = 2.5f;
if (TF_ISSET(task, FLAG_fann_create) && !TF_ISSET(task, FLAG_fann_train)) emitter_scores[34] = 2.0f;
if (TF_ISSET(task, FLAG_fann_run)) emitter_scores[36] = 2.0f;
if (TF_ISSET(task, FLAG_add) && TF_ISSET(task, FLAG_literals) && !TF_ISSET(task, FLAG_input)) emitter_scores[140] = 2.0f;
if (TF_ISSET(task, FLAG_sub) && TF_ISSET(task, FLAG_literals) && !TF_ISSET(task, FLAG_input)) emitter_scores[141] = 2.0f;
if (TF_ISSET(task, FLAG_mul) && TF_ISSET(task, FLAG_literals) && !TF_ISSET(task, FLAG_input)) emitter_scores[142] = 2.0f;
if (TF_ISSET(task, FLAG_div) && TF_ISSET(task, FLAG_literals) && !TF_ISSET(task, FLAG_input)) emitter_scores[143] = 2.0f;
if (TF_ISSET(task, FLAG_add) && TF_ISSET(task, FLAG_literals) && !TF_ISSET(task, FLAG_input) && strcmp(task->type, "double") == 0) emitter_scores[144] = 2.5f;
if (TF_ISSET(task, FLAG_sub) && TF_ISSET(task, FLAG_literals) && !TF_ISSET(task, FLAG_input) && strcmp(task->type, "double") == 0) emitter_scores[145] = 2.5f;
if (TF_ISSET(task, FLAG_mul) && TF_ISSET(task, FLAG_literals) && !TF_ISSET(task, FLAG_input) && strcmp(task->type, "double") == 0) emitter_scores[146] = 2.5f;
if (TF_ISSET(task, FLAG_div) && TF_ISSET(task, FLAG_literals) && !TF_ISSET(task, FLAG_input) && strcmp(task->type, "double") == 0) emitter_scores[147] = 2.5f;
// Simple flag-to-score mappings (each flag maps to exactly one emitter)
SET(3, sum_range, 2.0f); SET(4, print_even, 2.0f);
SET(5, find_max, 2.0f); SET(6, countdown_from, 2.0f);
SET(7, fib_seq, 2.0f); SET(8, input_sort, 2.0f);
SET(9, median, 2.0f); SET(10, string_cat, 2.0f);
SET(11, string_compare, 2.0f); SET(12, array_assign, 2.0f);
SET(13, array_reverse, 2.0f); SET(14, array_find, 2.0f);
SET(14, array_access, 2.0f); SET(12, array_write, 2.0f);
SET(15, input_fact, 2.0f); SET(16, array_vmath, 2.0f);
SET(17, read_file, 2.0f); SET(18, write_file, 2.0f);
SET(19, string_to_num, 2.0f); SET(20, timer, 2.0f);
SET(22, fizzbuzz, 2.0f); SET(23, primes, 2.0f);
SET(25, power, 2.0f); SET(26, mult_table, 2.0f);
SET(27, guess, 2.0f); SET(28, gcd, 2.0f);
SET(29, hello_name, 2.0f); SET(30, random, 2.0f);
SET(31, array_min_max, 2.0f); SET(32, bool_demo, 2.0f);
SET(33, bit_check, 2.0f);
SET(39, palindrome, 2.0f); SET(40, lcm, 2.0f);
SET(41, collatz, 2.0f); SET(42, sum_of_digits, 2.0f);
SET(43, reverse_string, 2.0f); SET(44, armstrong, 2.0f);
SET(45, perfect_number, 2.0f); SET(46, count_vowels, 2.0f);
SET(47, anagram_check, 2.0f); SET(48, string_to_upper, 2.0f);
SET(49, string_to_lower, 2.0f); SET(50, caesar_cipher, 2.0f);
SET(51, palindrome_string, 2.0f); SET(52, bubble_sort, 2.0f);
SET(53, binary_search, 2.0f); SET(54, square_root, 2.0f);
SET(55, prime_factorization, 2.0f); SET(56, standard_deviation, 2.0f);
SET(57, compound_interest, 2.0f); SET(58, decimal_to_binary, 2.0f);
SET(59, dice_roll, 2.0f); SET(60, double_math, 2.0f);
SET(61, double_circle_area, 2.0f); SET(62, double_average, 2.0f);
SET(63, double_compound_interest, 2.0f); SET(64, double_pythagoras, 2.0f);
SET(65, double_temp_convert, 2.0f); SET(66, double_sqrt, 2.0f);
SET(67, function, 2.0f); SET(68, string_length, 2.0f);
SET(69, stack, 2.0f); SET(70, queue, 2.0f);
SET(71, insertion_sort, 2.0f); SET(72, calculator, 2.0f);
SET(73, unit_converter, 2.0f); SET(74, rock_paper_scissors, 2.0f);
SET(75, pyramid, 2.0f); SET(76, temp_converter_menu, 2.0f);
SET(77, sort_stats, 2.0f); SET(78, string_analyzer, 2.0f);
SET(79, number_analyzer, 2.0f); SET(80, filter_numbers, 2.0f);
SET(81, random_generator, 2.0f); SET(82, math_menu, 2.0f);
SET(83, quiz_game, 2.0f); SET(84, bmi_calculator, 2.0f);
SET(85, statistics_suite, 2.0f); SET(86, linked_list, 2.0f);
SET(87, binary_search_tree, 2.0f); SET(88, tree_traversal, 2.0f);
SET(89, graph_bfs_dfs, 2.0f); SET(90, n_queens, 2.0f);
SET(91, sudoku, 2.0f); SET(92, levenshtein, 2.0f);
SET(93, maze_generator, 2.0f); SET(94, maze_solver, 2.0f);
SET(95, monte_carlo, 2.0f); SET(96, matrix_mul, 2.0f);
SET(97, matrix_transpose, 2.0f); SET(98, numerical_integration, 2.0f);
SET(99, complex_numbers, 2.0f); SET(100, linear_regression, 2.0f);
SET(101, base_converter, 2.0f); SET(102, freq_analysis, 2.0f);
SET(103, shuffle, 2.0f); SET(104, weighted_random, 2.0f);
SET(105, ascii_table, 3.0f); SET(106, bignum_math, 2.5f);
SET(107, password_card, 2.5f); SET(108, chess_problem, 2.5f);
SET(109, shell_repl, 2.5f); SET(110, webserver, 2.5f);
SET(111, sdl_window, 2.5f); SET(112, sdl_button, 2.5f);
SET(113, thread, 2.0f); SET(114, scheduler, 2.0f);
SET(115, shell_exec, 2.5f); SET(116, json, 2.5f);
SET(117, crypto, 2.5f); SET(118, bluetooth_ble, 2.5f);
SET(119, serial_rs232, 2.5f); SET(120, gpio, 2.5f);
SET(121, gps, 2.5f); SET(122, timer_date, 2.0f);
SET(123, sdl_sound, 2.5f); SET(124, sdl_joystick, 2.5f);
SET(125, sdl_mouse, 2.5f); SET(126, fractal, 2.5f);
SET(127, cluster_3x1, 2.5f); SET(128, reload, 2.0f);
SET(129, coordinate_grid, 2.0f); SET(130, turmite, 2.5f);
SET(131, crossword, 2.5f); SET(132, linter, 2.0f);
SET(133, double_power, 2.0f); SET(134, double_volume_sphere, 2.0f);
SET(135, double_discount, 2.0f); SET(136, double_simple_interest, 2.0f);
SET(137, double_bmi, 2.0f); SET(138, double_standard_deviation, 2.0f);
SET(139, double_kinetic_energy, 2.0f);
SET(148, hello_world, 2.0f); SET(149, string_find, 2.0f);
SET(150, string_split, 2.0f); SET(151, switch_demo, 2.0f);
SET(152, type_convert, 2.0f); SET(153, iterative_factorial, 2.0f);
SET(154, random_walk, 2.0f); SET(155, bar_chart, 2.0f);
SET(156, hanoi_tower, 2.0f); SET(157, ascii_art, 2.0f);
SET(158, number_to_words, 2.0f); SET(159, temperature_table, 2.0f);
SET(160, loop_demo, 2.0f); SET(161, pointer, 2.0f);
SET(162, struct, 2.0f); SET(163, hex_binary, 2.0f);
SET(164, shell_args, 2.0f); SET(165, time, 2.0f);
#undef SET
// Phase 2a: prompt token boosts
apply_token_boosts(emitter_scores, tokens, num_tokens,
1.0f, 1.5f, 1.0f,
0.5f, 1.5f,
1.5f,
0.5f, 0.8f,
0.3f, 0.5f);
// Phase 2b: Vector search boost tokens from matched examples
apply_token_boosts(emitter_scores, vs_boost_tokens, vs_boost_count,
0.5f, 0.7f, 0.5f,
0.3f, 0.7f,
0.7f,
0.3f, 0.4f,
0.2f, 0.3f);
float temp = TEMPERATURE;
for (int i = 0; i < NUM_EMITTERS; i++) emitter_scores[i] /= temp;
softmax(emitter_scores, NUM_EMITTERS);
for (int i = 0; i < NUM_EMITTERS; i++) attention_weights[i] = emitter_scores[i];
// Rank emitters by score, storing top alternatives for retry
int ranked[NUM_EMITTERS];
for (int i = 0; i < NUM_EMITTERS; i++) ranked[i] = i;
// Simple bubble sort (NUM_EMITTERS is small, 133)
for (int i = 0; i < NUM_EMITTERS - 1; i++) {
for (int j = 0; j < NUM_EMITTERS - i - 1; j++) {
if (emitter_scores[ranked[j]] < emitter_scores[ranked[j + 1]]) {
int tmp = ranked[j]; ranked[j] = ranked[j + 1]; ranked[j + 1] = tmp;
}
}
}
int best = 0;
float best_p = emitter_scores[0];
for (int i = 1; i < NUM_EMITTERS; i++) {
if (emitter_scores[i] > best_p) { best_p = emitter_scores[i]; best = i; }
}
// Use retry_seed to select alternative emitter (for validation retry)
if (retry_seed > 0) {
int alt_idx = retry_seed < NUM_EMITTERS ? retry_seed : NUM_EMITTERS - 1;
best = ranked[alt_idx];
best_p = emitter_scores[best];
}
if (verbose_flag) {
printf("\nEmitter scores:\n");
for (int i = 0; i < NUM_EMITTERS; i++)
printf(" %2d %-16s %.3f\n", i, EMITTER_NAMES[i], emitter_scores[i]);
printf(" -> best: %s (%.3f)\n", EMITTER_NAMES[best], best_p);
if (retry_seed > 0) printf(" (retry %d, using alternative rank %d)\n", retry_seed, retry_seed);
}
// Store ranked alternatives in extra_emitters for retry fallback
// (used when retry_seed changes which primary emitter is selected)
// Multi-emitter composition: add strongly-scored secondary emitters
// Dynamic threshold based on prompt length: longer prompts more likely multi-step
float pfactor = (float)strlen(prompt) / 80.0f;
if (pfactor > 2.0f) pfactor = 2.0f;
if (pfactor < 0.5f) pfactor = 0.5f;
task->num_extra_emitters = 0;
float threshold = best_p * (0.5f / pfactor);
float hard_min = 0.2f;
if (threshold < hard_min) threshold = hard_min;
// Emitter domain groups: avoid selecting redundant emitters from same domain
static const char *const sort_domain[] = {"input_sort", "bubble_sort", "selection_sort", "insertion_sort", NULL};
static const char *const math_domain[] = {"math", "input_loop", "loop", "for_sum", NULL};
static const char *const string_domain[] = {"string_cat", "reverse_string", "count_vowels", "anagram_check", "string_to_upper", "string_to_lower", "caesar_cipher", "palindrome_string", "string_compare", "string_length", "string_analyzer", NULL};
static const char *const fib_domain[] = {"fibonacci", "fib_seq", NULL};
static const char *const fann_domain[] = {"fann_create", "fann_train", "fann_run", NULL};
// Input-reading emitters: mutual exclusion - if primary reads input, skip other input-readers
static const char *const input_readers[] = {"math", "input_loop", "for_sum", "print_even",
"input_sort", "average", "selection_sort", "bubble_sort",
"standard_deviation", "insertion_sort", NULL};
int best_reads_input = 0;
for (int ri = 0; input_readers[ri]; ri++)
if (strcmp(EMITTER_NAMES[best], input_readers[ri]) == 0) { best_reads_input = 1; break; }
// Determine which domain the best emitter belongs to
int in_domain = 0;
const char *best_name = EMITTER_NAMES[best];
const char *const *domains[] = {sort_domain, math_domain, string_domain, fib_domain, fann_domain, NULL};
for (int d = 0; domains[d]; d++) {
for (int di = 0; domains[d][di]; di++) {
if (strcmp(best_name, domains[d][di]) == 0) {
in_domain = 1;
break;
}
}
if (in_domain) {
for (int i = 0; i < NUM_EMITTERS && task->num_extra_emitters < 32; i++) {
if (i == best) continue;
// Skip emitters in the same domain as the primary
int same_domain = 0;
for (int di = 0; domains[d][di]; di++) {
if (strcmp(EMITTER_NAMES[i], domains[d][di]) == 0) { same_domain = 1; break; }
}
if (same_domain) continue;
// Skip input-reading emitters if primary also reads input
if (best_reads_input) {
int is_input_reader = 0;
for (int ri = 0; input_readers[ri]; ri++)
if (strcmp(EMITTER_NAMES[i], input_readers[ri]) == 0) { is_input_reader = 1; break; }
if (is_input_reader) continue;
}
if (emitter_scores[i] >= threshold && emitter_scores[i] >= 0.5f) {
task->extra_emitters[task->num_extra_emitters++] = i;
}
}
break;
}
}
if (!in_domain) {
// Fallback: original behavior for undomained emitters
for (int i = 0; i < NUM_EMITTERS && task->num_extra_emitters < 32; i++) {
if (i == best) continue;
// Skip input-reading emitters if primary also reads input
if (best_reads_input) {
int is_input_reader = 0;
for (int ri = 0; input_readers[ri]; ri++)
if (strcmp(EMITTER_NAMES[i], input_readers[ri]) == 0) { is_input_reader = 1; break; }
if (is_input_reader) continue;
}
if (emitter_scores[i] >= threshold && emitter_scores[i] >= 0.5f) {
task->extra_emitters[task->num_extra_emitters++] = i;
}
}
}
if (verbose_flag && task->num_extra_emitters > 0) {
printf(" extra emitters:");
for (int ei = 0; ei < task->num_extra_emitters; ei++)
printf(" %s", EMITTER_NAMES[task->extra_emitters[ei]]);
printf("\n");
}
return best;
}
static int has_actionable_keyword(const char *text) {
// arithmetic
if (has_word(text, "add") || has_word(text, "sum") || has_word(text, "summe")
|| has_word(text, "plus") || has_word(text, "berechne") || has_word(text, "ermittle")
|| has_word(text, "calculate") || has_word(text, "compute") || has_word(text, "count")
|| has_word(text, "zähle") || has_word(text, "anzahl"))
return 1;
if (has_word(text, "sub") || has_word(text, "subtract") || has_word(text, "minus")
|| has_word(text, "difference") || has_word(text, "differenz"))
return 1;
if (has_word(text, "mul") || has_word(text, "multiply") || has_word(text, "mal")
|| has_word(text, "times") || has_word(text, "product") || has_word(text, "produkt"))
return 1;
if (has_word(text, "div") || has_word(text, "divide") || has_word(text, "geteilt")
|| has_word(text, "quotient"))
return 1;
if (has_word(text, "mod") || has_word(text, "modulo") || has_word(text, "remainder")
|| has_word(text, "rest"))
return 1;
if (has_word(text, "power") || has_word(text, "potenz") || has_word(text, "exponent")
|| has_word(text, "hoch") || has_word(text, "square") || has_word(text, "quadrat")
|| has_word(text, "cube") || has_word(text, "kubik") || has_word(text, "sqrt")
|| has_word(text, "wurzel") || has_word(text, "root"))
return 1;
// comparison
if (has_word(text, "max") || has_word(text, "größt") || has_word(text, "largest")
|| has_word(text, "greatest") || has_word(text, "groesst") || has_word(text, "maximum")
|| has_word(text, "minimum") || has_word(text, "min") || has_word(text, "kleinste")
|| has_word(text, "smallest") || has_word(text, "compare") || has_word(text, "vergleich"))
return 1;
// io
if (has_word(text, "lies") || has_word(text, "read") || has_word(text, "input")
|| has_word(text, "gib") || has_word(text, "enter") || has_word(text, "erfasse")
|| has_word(text, "collect") || has_word(text, "eingabe") || has_word(text, "einlesen"))
return 1;
if (has_word(text, "print") || has_word(text, "druck") || has_word(text, "ausg")
|| has_word(text, "show") || has_word(text, "display") || has_word(text, "zeig")
|| has_word(text, "output") || has_word(text, "schreib") || has_word(text, "write")
|| has_word(text, "ausgeben"))
return 1;
// sorting / searching
if (has_word(text, "sort") || has_word(text, "sortiere") || has_word(text, "bubble")
|| has_word(text, "sorted") || has_word(text, "sortiert") || has_word(text, "order")
|| has_word(text, "ordne") || has_word(text, "selection") || has_word(text, "insertion"))
return 1;
if (has_word(text, "find") || has_word(text, "search") || has_word(text, "suche")
|| has_word(text, "lookup") || has_word(text, "filter") || has_word(text, "select")
|| has_word(text, "choose") || has_word(text, "wähle") || has_word(text, "auswählen"))
return 1;
// sequence transformation
if (has_word(text, "reverse") || has_word(text, "umkehr") || has_word(text, "swap")
|| has_word(text, "tausche") || has_word(text, "rotate") || has_word(text, "shift")
|| has_word(text, "verschieb") || has_word(text, "shuffle") || has_word(text, "misch"))
return 1;
// aggregate
if (has_word(text, "average") || has_word(text, "durchschnitt") || has_word(text, "mean")
|| has_word(text, "median") || has_word(text, "standard") || has_word(text, "deviation"))
return 1;
// number theory
if (has_word(text, "factorial") || has_word(text, "fakult") || has_word(text, "fibo")
|| has_word(text, "fibonacci") || has_word(text, "prime") || has_word(text, "prim")
|| has_word(text, "fizzbuzz") || has_word(text, "gcd") || has_word(text, "ggt")
|| has_word(text, "gcm") || has_word(text, "lcm") || has_word(text, "kgv")
|| has_word(text, "collatz") || has_word(text, "palindrome") || has_word(text, "palindrom")
|| has_word(text, "armstrong") || has_word(text, "perfect") || has_word(text, "perfekt"))
return 1;
// even/odd / boolean
if (has_word(text, "even") || has_word(text, "odd") || has_word(text, "gerade")
|| has_word(text, "ungerade") || has_word(text, "leap") || has_word(text, "schalt"))
return 1;
// loops / conditions
if (has_word(text, "for") || has_word(text, "loop") || has_word(text, "schleife")
|| has_word(text, "while") || has_word(text, "switch") || has_word(text, "case")
|| has_word(text, "if") || has_word(text, "bedingung") || has_word(text, "wenn"))
return 1;
// data structures
if (has_word(text, "array") || has_word(text, "feld") || has_word(text, "liste")
|| has_word(text, "list") || has_word(text, "stack") || has_word(text, "queue")
|| has_word(text, "pointer") || has_word(text, "zeiger") || has_word(text, "struct")
|| has_word(text, "tree") || has_word(text, "baum") || has_word(text, "graph")
|| has_word(text, "linked") || has_word(text, "verkettet"))
return 1;
// string operations
if (has_word(text, "string") || has_word(text, "zeichen") || has_word(text, "text")
|| has_word(text, "cat") || has_word(text, "concat") || has_word(text, "vowel")
|| has_word(text, "vokal") || has_word(text, "anagram") || has_word(text, "upper")
|| has_word(text, "lower") || has_word(text, "groß") || has_word(text, "klein")
|| has_word(text, "caesar") || has_word(text, "cipher") || has_word(text, "chiffre")
|| has_word(text, "length") || has_word(text, "länge") || has_word(text, "len"))
return 1;
// conversion
if (has_word(text, "convert") || has_word(text, "parse") || has_word(text, "umwand")
|| has_word(text, "decimal") || has_word(text, "dezimal") || has_word(text, "binary")
|| has_word(text, "binär") || has_word(text, "hex") || has_word(text, "hexadezimal")
|| has_word(text, "celsius") || has_word(text, "fahrenheit") || has_word(text, "temp"))
return 1;
// games / simulation
if (has_word(text, "guess") || has_word(text, "rate") || has_word(text, "raten")
|| has_word(text, "dice") || has_word(text, "würfel") || has_word(text, "wuerfel")
|| has_word(text, "random") || has_word(text, "zufall") || has_word(text, "game")
|| has_word(text, "spiel") || has_word(text, "rock") || has_word(text, "paper")
|| has_word(text, "scissors") || has_word(text, "schere") || has_word(text, "stein"))
return 1;
// math utilities
if (has_word(text, "table") || has_word(text, "einmaleins") || has_word(text, "time")
|| has_word(text, "zeit") || has_word(text, "clock") || has_word(text, "countdown")
|| has_word(text, "function") || has_word(text, "funktion") || has_word(text, "area")
|| has_word(text, "fläche") || has_word(text, "circle") || has_word(text, "kreis")
|| has_word(text, "bmi") || has_word(text, "interest") || has_word(text, "zins")
|| has_word(text, "pyramid") || has_word(text, "pyramide") || has_word(text, "matrix"))
return 1;
// file / system
if (has_word(text, "file") || has_word(text, "datei") || has_word(text, "shell")
|| has_word(text, "argument") || has_word(text, "parameter") || has_word(text, "env")
|| has_word(text, "timer") || has_word(text, "benchmark") || has_word(text, "measure")
|| has_word(text, "mess") || has_word(text, "dauer") || has_word(text, "execution")
|| has_word(text, "ausführ") || has_word(text, "lauf"))
return 1;
// general action verbs
if (has_word(text, "generate") || has_word(text, "create") || has_word(text, "erzeuge")
|| has_word(text, "erstelle") || has_word(text, "bau") || has_word(text, "build")
|| has_word(text, "check") || has_word(text, "prüfe") || has_word(text, "teste")
|| has_word(text, "verify") || has_word(text, "überprüf") || has_word(text, "solve")
|| has_word(text, "löse") || has_word(text, "set") || has_word(text, "initialize")
|| has_word(text, "initialisiere") || has_word(text, "setze") || has_word(text, "analyze")
|| has_word(text, "analyse") || has_word(text, "untersuche") || has_word(text, "determine")
|| has_word(text, "bestimme") || has_word(text, "evaluate") || has_word(text, "auswerten"))
return 1;
// nouns as context
if (has_word(text, "ergebnis") || has_word(text, "result") || has_word(text, "value")
|| has_word(text, "wert") || has_word(text, "number") || has_word(text, "zahl")
|| has_word(text, "element") || has_word(text, "index") || has_word(text, "bis")
|| has_word(text, "to") || has_word(text, "von") || has_word(text, "from")
|| has_word(text, "data") || has_word(text, "daten") || has_word(text, "sequence")
|| has_word(text, "folge") || has_word(text, "reihe") || has_word(text, "series"))
return 1;
// boolean
if (has_word(text, "fann") || has_word(text, "neural") || has_word(text, "network")
|| has_word(text, "train") || has_word(text, "learn") || has_word(text, "predict")
|| has_word(text, "infer"))
return 1;
return 0;
}
int has_sequential_pattern(const char *text) {
// Check if text contains numbered steps like "1)...2)..." or "step 1...step 2..."
int count_enumerated = 0;
const char *p = text;
while (*p && count_enumerated < 2) {
if ((p[0] >= '1' && p[0] <= '9') && (p[1] == ')' || (p[1] == '.' && p[2] != '.' && !(p[2] >= '0' && p[2] <= '9')))) { count_enumerated++; p += 2; }
else if ((p[0] == '(') && (p[1] >= '1' && p[1] <= '9') && p[2] == ')') { count_enumerated++; p += 3; }
else if (strncmp(p, "step ", 5) == 0 && p[5] >= '1' && p[5] <= '9') { count_enumerated++; p += 6; }
else p++;
}
if (count_enumerated >= 2) return 1;
// Check for bullet points: lines starting with "-", "*", "+"
int bullet_count = 0;
const char *bp = text;
while (*bp) {
while (*bp && *bp == ' ') bp++;
if (*bp == '-' || *bp == '*' || *bp == '+') { bullet_count++; if (bullet_count >= 2) return 1; }
while (*bp && *bp != '\n') bp++;
if (*bp == '\n') bp++;
}
// Check for multi-step sequential adverbs: at least 2 of "first, then, finally"
// Count multiple occurrences of "then" (e.g. "input numbers then sort then print")
int seq_count = 0;
if (has_word(text, "first") || has_word(text, "zuerst") || has_word(text, "erstens")) seq_count++;
if (has_word(text, "second") || has_word(text, "zweitens") || has_word(text, "zweit")) seq_count++;
if (has_word(text, "third") || has_word(text, "drittens") || has_word(text, "dritt")) seq_count++;
if (has_word(text, "fourth") || has_word(text, "viertens") || has_word(text, "viert")) seq_count++;
if (has_word(text, "fifth") || has_word(text, "fünftens") || has_word(text, "fuenft")) seq_count++;
if (has_word(text, "finally") || has_word(text, "schließlich") || has_word(text, "abschließend")) seq_count++;
{ const char *tp = text; while ((tp = strstr(tp, "then")) != NULL) { seq_count++; tp++; } }
{ const char *tp = text; while ((tp = strstr(tp, "dann")) != NULL) { seq_count++; tp++; } }
if (has_word(text, "next") || has_word(text, "als nächstes") || has_word(text, "danach")) seq_count++;
// Compound conjunctions ("und danach", "and then") inherently imply multi-step
if (strstr(text, "und dann")) seq_count += 2;
if (strstr(text, "und danach")) seq_count += 2;
if (strstr(text, "und anschließend")) seq_count += 2;
if (strstr(text, "and then")) seq_count += 2;
if (strstr(text, "and after")) seq_count += 2;
if (seq_count >= 2) return 1;
return 0;
}
int split_prompt_steps(const char *prompt, char steps[MAX_STEPS][MAX_PROMPT]) {
char buf[MAX_PROMPT];
SNPRINTF_CHECK(buf, sizeof(buf), "%s", prompt);
to_lowercase(buf);
int num_steps = 0;
char *remaining = buf;
// Phase 1: Newline-based splitting (bullet points, numbered lists, line-separated items)
// Detect if prompt contains explicit line breaks with list markers
{
int newline_count = 0;
char *nlp = buf;
while (*nlp) { if (*nlp == '\n') newline_count++; nlp++; }
if (newline_count >= 2) {
char *line = buf;
char *next;
while (line && num_steps < MAX_STEPS) {
next = strchr(line, '\n');
if (next) *next = '\0';
trim(line);
// Strip leading bullet/number markers
char *content = line;
while (*content == ' ' || *content == '\t') content++;
if (*content == '-' || *content == '*' || *content == '+') { content++; while (*content == ' ') content++; }
else {
while ((*content >= '0' && *content <= '9') || *content == '(' || *content == ')' || *content == '.') content++;
while (*content == ' ') content++;
}
if (strlen(content) > 0 && has_actionable_keyword(content)) {
SNPRINTF_CHECK(steps[num_steps], MAX_PROMPT, "%s", content);
num_steps++;
}
if (next) { *next = '\n'; line = next + 1; }
else break;
}
if (num_steps >= 2) return num_steps;
num_steps = 0;
}
}
// Phase 2: Numbered/enumerated steps (high confidence)
// e.g., "step 1: sort 5 numbers step 2: reverse the array"
// "1) sort 5 numbers 2) reverse the array"
// "(1) sort 5 numbers (2) reverse the array"
// "1. sort 5 numbers 2. reverse the array"
if (has_sequential_pattern(buf)) {
// Try bullet point first (dash/asterisk/plus lists)
{
char *bp = remaining;
char bullet_steps[MAX_STEPS][MAX_PROMPT];
int bs_count = 0;
while (*bp && bs_count < MAX_STEPS) {
while (*bp == ' ') bp++;
if (*bp == '-' || *bp == '*' || *bp == '+') {
bp++; while (*bp == ' ') bp++;
char *start = bp;
while (*bp && *bp != '\n' &&
!(*bp == ' ' && (bp[1] == '-' || bp[1] == '*' || bp[1] == '+'))) bp++;
char saved = *bp;
*bp = '\0';
trim(start);
if (strlen(start) > 0) {
SNPRINTF_CHECK(bullet_steps[bs_count], MAX_PROMPT, "%s", start);
bs_count++;
}
*bp = saved;
if (*bp == '\n') bp++;
} else {
while (*bp && *bp != '\n') bp++;
if (*bp == '\n') bp++;
}
}
if (bs_count >= 2) {
for (int bsi = 0; bsi < bs_count; bsi++)
SNPRINTF_CHECK(steps[num_steps++], MAX_PROMPT, "%.8191s", bullet_steps[bsi]);
return num_steps;
}
}
char *p = remaining;
char current[MAX_PROMPT] = "";
int collecting = 0;
while (*p && num_steps < MAX_STEPS - 1) {
int step_found = 0;
if ((p[0] >= '1' && p[0] <= '9') && (p[1] == ')' || (p[1] == '.' && p[2] != '.' && !(p[2] >= '0' && p[2] <= '9')))) {
if (collecting && strlen(current) > 0) {
for (char *cp = current + 1; *cp; cp++) {
if (((cp[0] >= '1' && cp[0] <= '9') && (cp[1] == ')' || (cp[1] == '.' && cp[2] != '.' && !(cp[2] >= '0' && cp[2] <= '9'))))
|| (cp[0] == '(' && cp[1] >= '1' && cp[1] <= '9' && cp[2] == ')')
|| strncmp(cp, "step ", 5) == 0 || strncmp(cp, "schritt ", 8) == 0) { *cp = '\0'; break; }
}
trim(current);
SNPRINTF_CHECK(steps[num_steps], MAX_PROMPT, "%s", current);
num_steps++;
}
p += 2; while (*p && isspace((unsigned char)*p)) p++;
SNPRINTF_CHECK(current, sizeof(current), "%s", p);
collecting = 1;
step_found = 1;
} else if ((p[0] == '(') && (p[1] >= '1' && p[1] <= '9') && p[2] == ')') {
if (collecting && strlen(current) > 0) {
for (char *cp = current + 1; *cp; cp++) {
if (((cp[0] >= '1' && cp[0] <= '9') && (cp[1] == ')' || (cp[1] == '.' && cp[2] != '.' && !(cp[2] >= '0' && cp[2] <= '9'))))
|| (cp[0] == '(' && cp[1] >= '1' && cp[1] <= '9' && cp[2] == ')')
|| strncmp(cp, "step ", 5) == 0 || strncmp(cp, "schritt ", 8) == 0) { *cp = '\0'; break; }
}
trim(current);
SNPRINTF_CHECK(steps[num_steps], MAX_PROMPT, "%s", current);
num_steps++;
}
p += 3; while (*p && isspace((unsigned char)*p)) p++;
SNPRINTF_CHECK(current, sizeof(current), "%s", p);
collecting = 1;
step_found = 1;
} else if ((strncmp(p, "step ", 5) == 0 && p[5] >= '1' && p[5] <= '9') || (strncmp(p, "schritt ", 8) == 0 && p[8] >= '1' && p[8] <= '9')) {
int is_step = (strncmp(p, "step ", 5) == 0);
int prefix_len = is_step ? 5 : 8;
if (collecting && strlen(current) > 0) {
for (char *cp = current + 1; *cp; cp++) {
if (((cp[0] >= '1' && cp[0] <= '9') && (cp[1] == ')' || (cp[1] == '.' && cp[2] != '.' && !(cp[2] >= '0' && cp[2] <= '9'))))
|| (cp[0] == '(' && cp[1] >= '1' && cp[1] <= '9' && cp[2] == ')')
|| strncmp(cp, "step ", 5) == 0 || strncmp(cp, "schritt ", 8) == 0) { *cp = '\0'; break; }
}
trim(current);
SNPRINTF_CHECK(steps[num_steps], MAX_PROMPT, "%s", current);
num_steps++;
}
p += prefix_len; while (*p && isspace((unsigned char)*p)) p++;
if (*p == ':') p++;
while (*p && isspace((unsigned char)*p)) p++;
SNPRINTF_CHECK(current, sizeof(current), "%s", p);
collecting = 1;
step_found = 1;
}
if (!step_found) { p++; }
else {
while (*p && !((p[0] >= '1' && p[0] <= '9' && (p[1] == ')' || (p[1] == '.' && p[2] != '.' && !(p[2] >= '0' && p[2] <= '9'))))
|| (p[0] == '(' && p[1] >= '1' && p[1] <= '9' && p[2] == ')')
|| strncmp(p, "step ", 5) == 0 || strncmp(p, "schritt ", 8) == 0)) p++;
}
}
if (collecting && strlen(current) > 0) {
trim(current);
SNPRINTF_CHECK(steps[num_steps], MAX_PROMPT, "%s", current);
num_steps++;
}
if (num_steps >= 2) {
for (int si = 0; si < num_steps; si++) {
char *s = steps[si];
while (*s && ((*s >= '0' && *s <= '9') || *s == ')' || *s == '(' || *s == '.')) s++;
while (*s && isspace((unsigned char)*s)) s++;
if (s > steps[si]) memmove(steps[si], s, strlen(s) + 1);
}
return num_steps;
}
num_steps = 0;
remaining = buf;
}
// Phase 3: Conjunction-based splitting with extended pattern set
struct { const char *pat; int len; } patterns[] = {
// Long multi-word conjunctions (highest priority)
{" und dann ", 10}, {" und danach ", 12}, {" und anschließend ", 18},
{" anschließend ", 14},
{" als nächstes ", 14}, {" zum Schluss ", 13},
{" and then ", 10}, {" and finally ", 12},
{" first ", 7}, {" then ", 6}, {" finally ", 9},
{" next ", 6}, {" afterwards ", 12}, {" after that ", 12},
// German sequential
{" danach ", 8}, {" daraufhin ", 11}, {" zuerst ", 8},
{" als erstes ", 12}, {" als zweites ", 13}, {" als drittes ", 13},
{" nun ", 5},
// English sequential
{" firstly ", 9}, {" secondly ", 10}, {" thirdly ", 9},
{" subsequently ", 14}, {" thereafter ", 12},
// Punctuation (medium confidence)
{" . ", 3}, {". ", 2}, {"; ", 2}, {";", 1}, {": ", 2},
// Short conjunctions (lowest priority)
{" und ", 5}, {" and ", 5}, {", ", 2},
};
int npats = sizeof(patterns) / sizeof(patterns[0]);
while (num_steps < MAX_STEPS - 1) {
int best_pos = -1, best_len = 0;
for (int i = 0; i < npats; i++) {
char *pos = strstr(remaining, patterns[i].pat);
if (!pos) continue;
int idx = pos - remaining;
if (best_pos >= 0 && idx > best_pos) continue;
if (best_pos >= 0 && idx == best_pos) continue;
best_pos = idx; best_len = patterns[i].len;
}
if (best_pos < 0) break;
char step[MAX_PROMPT];
SNPRINTF_CHECK(step, MAX_PROMPT, "%.*s", best_pos, remaining);
trim(step);
if (strlen(step) > 0 && has_actionable_keyword(step)) {
// Avoid splitting on short conjunctions (" und ", " and ", ", ") when
// both sides are short (< 4 words each), as this is likely a list
// (e.g. "minimum und maximum") rather than sequential action steps,
// OR when both sides contain arithmetic operation keywords (e.g.
// "addiere 3 und multipliziere mit 10" → one compound action).
int is_short_conj = (best_len <= 5);
if (is_short_conj) {
char *rest = remaining + best_pos + best_len;
// Comma before "and"/"und"/"or"/"oder" is punctuation, not step separator
if (best_len == 2) {
char *rp = rest;
while (*rp == ' ') rp++;
if (strncmp(rp, "and ", 4) == 0 || strncmp(rp, "und ", 4) == 0
|| strncmp(rp, "or ", 3) == 0 || strncmp(rp, "oder ", 5) == 0)
{
char combined[MAX_PROMPT * 2];
SNPRINTF_CHECK(combined, sizeof(combined), "%s %s", step, rp);
trim(combined);
SNPRINTF_CHECK(remaining, MAX_PROMPT, "%.*s", MAX_PROMPT - 1, combined);
break;
}
}
int left_has_op = has_word(step, "add") || has_word(step, "sub") || has_word(step, "mul") || has_word(step, "div")
|| has_word(step, "addiere") || has_word(step, "subtrahier") || has_word(step, "multiplizier")
|| has_word(step, "dividier") || has_word(step, "plus") || has_word(step, "minus")
|| has_word(step, "mal") || has_word(step, "durch");
int right_has_op = has_word(rest, "add") || has_word(rest, "sub") || has_word(rest, "mul") || has_word(rest, "div")
|| has_word(rest, "addiere") || has_word(rest, "subtrahier") || has_word(rest, "multiplizier")
|| has_word(rest, "dividier") || has_word(rest, "plus") || has_word(rest, "minus")
|| has_word(rest, "mal") || has_word(rest, "durch");
int sw = 0, rw = 0;
char tmp[MAX_PROMPT];
char *saveptr;
SNPRINTF_CHECK(tmp, sizeof(tmp), "%s", step);
char *t = strtok_r(tmp, " ", &saveptr);
while (t) { trim(t); if (strlen(t) > 0) sw++; t = strtok_r(NULL, " ", &saveptr); }
SNPRINTF_CHECK(tmp, sizeof(tmp), "%s", rest);
t = strtok_r(tmp, " ", &saveptr);
while (t) { trim(t); if (strlen(t) > 0) rw++; t = strtok_r(NULL, " ", &saveptr); }
if ((sw < 4 && rw < 4) || (left_has_op && right_has_op)) {
// False split – merge and continue as single step
char combined[MAX_PROMPT * 2];
SNPRINTF_CHECK(combined, sizeof(combined), "%s %s", step, rest);
trim(combined);
SNPRINTF_CHECK(remaining, MAX_PROMPT, "%.*s", MAX_PROMPT - 1, combined);
break;
}
}
SNPRINTF_CHECK(steps[num_steps], MAX_PROMPT, "%s", step); num_steps++;
remaining += best_pos + best_len;
} else if (strlen(step) > 0) {
char *rest = remaining + best_pos + best_len;
char combined[MAX_PROMPT * 2];
SNPRINTF_CHECK(combined, sizeof(combined), "%s %s", step, rest);
trim(combined);
SNPRINTF_CHECK(remaining, MAX_PROMPT, "%.*s", MAX_PROMPT - 1, combined);
break;
} else {
remaining += best_pos + best_len;
}
}
trim(remaining);
if (strlen(remaining) > 0) { SNPRINTF_CHECK(steps[num_steps], MAX_PROMPT, "%s", remaining); num_steps++; }
if (verbose_flag) {
fprintf(stderr, "Split steps: num_steps=%d steps:", num_steps);
for (int d = 0; d < num_steps; d++) fprintf(stderr, " [%d]='%s'", d, steps[d]);
fprintf(stderr, "\n");
}
// Phase 4: Merge non-viable steps backward into their predecessor
if (num_steps > 1) {
int merged = 1;