embeddedpy 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of embeddedpy might be problematic. Click here for more details.

@@ -0,0 +1,1395 @@
1
+ """Runtime snippet library: split by function, with dependency declarations, for codegen to inline on demand.
2
+
3
+ Each generated .c includes only the runtime functions it actually uses (and their transitive dependencies) plus the public types;
4
+ the remaining snippets are excluded from the output, keeping it minimal and independently compilable.
5
+ """
6
+
7
+ import re
8
+ from typing import Dict, Iterable, List, Tuple
9
+
10
+ DEFAULT_ARENA_CAP = 8 * 1024
11
+
12
+ # The config layer lets the user write a 0..1 fraction; the compiler multiplies it by the total budget and rounds to two integers (byte counts),
13
+ # so the output contains only these two integer macros—no ratio, no conversion, no floating point.
14
+ # Whatever the user tunes only affects these two numbers; no intermediate value is left in the output.
15
+ DEFAULT_MANUAL_RATIO = 0.5
16
+ DEFAULT_MANUAL_CAP = int(DEFAULT_ARENA_CAP * DEFAULT_MANUAL_RATIO + 0.5)
17
+ DEFAULT_AUTO_CAP = DEFAULT_ARENA_CAP - DEFAULT_MANUAL_CAP
18
+
19
+ # Each public type in the output carries its own include guard: the types used by the .h and its companion .c need not be
20
+ # the same, and a single global guard would leave the later-included side unable to fill in the extra types it uses.
21
+ RUNTIME_TYPE_GUARD_PREFIX = "EMBEDDEDPY_RUNTIME_TYPE"
22
+
23
+ # Each snippet: code is the function body (without the static prefix, added uniformly by codegen),
24
+ # deps is the names of other runtime functions directly called by this function.
25
+ SNIPPETS: Dict[str, Dict] = {
26
+ # ---------------------------------------------------------------- arena
27
+ # Intermediate values of strings/bytestrings are sliced from two static pools; the runtime does not request memory from libc.
28
+ # malloc appears in the output if and only if the source code writes mem.alloc (explicit heap).
29
+ # The two pools each have an independent upper bound; the total size is always the arena_cap from config:
30
+ # auto pool -- temporary objects inside function bodies; at function exit the watermark rewinds to the frame's lower bound;
31
+ # manual pool -- values produced by module-level statements; zeroed only when the source writes mem.arena_reset().
32
+ # The two pools never borrow from each other, so how much is written at module level does not change how many rounds a function body can run; capacity success/failure is reproducible.
33
+ #
34
+ # Capacity comes entirely from config: arena_cap is the total budget, arena_manual_ratio is the 0..1 proportion.
35
+ # The compiler directly computes the two as integer capacity macros, rounded; the output has no floating point, no
36
+ # ratio macro, no multiplication, and no hardcoded capacity literals. C side can override each independently with -DPY_ARENA_MANUAL_CAP
37
+ # or -DPY_ARENA_AUTO_CAP; #ifndef guarantees override without modifying the output.
38
+ #
39
+ # py_arena_manual is emitted by the compiler based on whether "this statement is module-level in the user source":
40
+ # module-level statements toggle 1 / 0 around them; a user function entry forces 0 and restores it at exit.
41
+ # main is merely the C host carrying module-level statements, not a scope layer: when library / header
42
+ # has no main it is always 0, and all allocations are in the auto pool.
43
+ # union guarantees 8-byte alignment, avoiding misalignment when accessed as double / pointer; alignment loss is computed from the actual
44
+ # runtime address, without assuming the pool sits at a fixed address.
45
+ #
46
+ # Snippet code contains no C comments: snippets enter the output verbatim, and the output must contain zero comments.
47
+ "__arena__": {
48
+ "code": """#ifndef PY_ARENA_MANUAL_CAP
49
+ #define PY_ARENA_MANUAL_CAP {arena_manual_cap}
50
+ #endif
51
+ #ifndef PY_ARENA_AUTO_CAP
52
+ #define PY_ARENA_AUTO_CAP {arena_auto_cap}
53
+ #endif
54
+
55
+ static union {
56
+ long long align_i64;
57
+ double align_f64;
58
+ void *align_ptr;
59
+ unsigned char bytes[PY_ARENA_AUTO_CAP];
60
+ } py_arena_auto_pool;
61
+
62
+ static union {
63
+ long long align_i64;
64
+ double align_f64;
65
+ void *align_ptr;
66
+ unsigned char bytes[PY_ARENA_MANUAL_CAP];
67
+ } py_arena_manual_pool;
68
+
69
+ static size_t py_arena_lo = 0;
70
+ static size_t py_arena_hi = 0;
71
+ static int py_arena_manual = 0;
72
+ static int64_t py_arena_line = 0;
73
+
74
+ void py_arena_fatal(int64_t line, const char *pool, size_t used, size_t need, size_t cap) {
75
+ char msg[160];
76
+ snprintf(msg, sizeof(msg),
77
+ "arena exhausted (%s pool: used %llu + need %llu > cap %llu)",
78
+ pool, (unsigned long long)used, (unsigned long long)need,
79
+ (unsigned long long)cap);
80
+ py_fatal(line, msg);
81
+ }
82
+
83
+ void *py_arena_alloc_aligned(size_t size, size_t align, int64_t line) {
84
+ if (line <= 0) {
85
+ line = py_arena_line;
86
+ }
87
+ if (align < 8u) {
88
+ align = 8u;
89
+ }
90
+ size = (size + align - 1u) & ~(align - 1u);
91
+ unsigned char *base;
92
+ size_t *used;
93
+ size_t cap;
94
+ const char *pool;
95
+ if (py_arena_manual) {
96
+ base = py_arena_manual_pool.bytes + py_arena_hi;
97
+ used = &py_arena_hi;
98
+ cap = (size_t)PY_ARENA_MANUAL_CAP;
99
+ pool = "manual";
100
+ } else {
101
+ base = py_arena_auto_pool.bytes + py_arena_lo;
102
+ used = &py_arena_lo;
103
+ cap = (size_t)PY_ARENA_AUTO_CAP;
104
+ pool = "auto";
105
+ }
106
+ size_t pad = (size_t)((align - ((uintptr_t)base % (uintptr_t)align)) % align);
107
+ if (pad > cap - *used || size > cap - *used - pad) {
108
+ py_arena_fatal(line, pool, *used, size, cap);
109
+ }
110
+ void *p = (void *)(base + pad);
111
+ *used += pad + size;
112
+ return p;
113
+ }
114
+
115
+ void *py_arena_alloc(size_t size, int64_t line) {
116
+ return py_arena_alloc_aligned(size, 8u, line);
117
+ }""",
118
+ "deps": ["py_fatal"],
119
+ },
120
+ # The manual pool's reclamation function is a separate snippet: inlined only when the source writes mem.arena_reset().
121
+ # If not written, this function does not even appear in the output; otherwise an uncalled static function would trigger -Wunused.
122
+ # Depending on py_arena_alloc ensures __arena__'s variable definitions are placed before this snippet.
123
+ "__arena_reset__": {
124
+ "code": """void py_arena_reset(void) {
125
+ py_arena_hi = 0;
126
+ }""",
127
+ "deps": ["py_arena_alloc"],
128
+ },
129
+ # ---------------------------------------------------------------- Heap memory
130
+ # C-side implementation of mem.alloc / mem.free / mem.realloc.
131
+ #
132
+ # Why not use raw malloc/realloc: the CPython track's mem.alloc allocates via bytearray
133
+ # (zeroed), and mem.realloc uses "new bytearray + copy back old contents" (new region zeroed,
134
+ # old contents kept by min). C's malloc/realloc do not zero, so the two tracks would read different
135
+ # memory contents--same source, two results, the most intolerable divergence in this project.
136
+ #
137
+ # So we build a layer here: calloc allocation + a side table recording each block's byte count. On realloc,
138
+ # we compute the retained amount from the old byte count in the table, calloc the new region to zero, byte-for-byte
139
+ # consistent with the CPython track. The side table also makes mem.free release only pointers truly allocated by mem.alloc,
140
+ # corresponding to CPython track's RuntimeError.
141
+ "__heap__": {
142
+ "code": """typedef struct py_heap_rec {
143
+ void *ptr;
144
+ size_t bytes;
145
+ struct py_heap_rec *next;
146
+ } py_heap_rec;
147
+
148
+ static py_heap_rec *py_heap_recs = NULL;
149
+
150
+ static void heap_rec_add(void *p, size_t bytes) {
151
+ py_heap_rec *r = (py_heap_rec *)malloc(sizeof(py_heap_rec));
152
+ if (r == NULL) {
153
+ py_fatal(0, "out of memory");
154
+ }
155
+ r->ptr = p;
156
+ r->bytes = bytes;
157
+ r->next = py_heap_recs;
158
+ py_heap_recs = r;
159
+ }
160
+
161
+ static void heap_rec_drop(void *p) {
162
+ py_heap_rec *r = py_heap_recs;
163
+ py_heap_rec *prev = NULL;
164
+ while (r != NULL) {
165
+ if (r->ptr == p) {
166
+ if (prev == NULL) {
167
+ py_heap_recs = r->next;
168
+ } else {
169
+ prev->next = r->next;
170
+ }
171
+ free(r);
172
+ return;
173
+ }
174
+ prev = r;
175
+ r = r->next;
176
+ }
177
+ }
178
+
179
+ static size_t heap_rec_bytes(void *p) {
180
+ py_heap_rec *r = py_heap_recs;
181
+ while (r != NULL) {
182
+ if (r->ptr == p) {
183
+ return r->bytes;
184
+ }
185
+ r = r->next;
186
+ }
187
+ return 0;
188
+ }
189
+
190
+ void *py_mem_alloc(size_t bytes) {
191
+ void *p = calloc((size_t)1, bytes);
192
+ if (p == NULL) {
193
+ py_fatal(0, "out of memory");
194
+ }
195
+ heap_rec_add(p, bytes);
196
+ return p;
197
+ }
198
+
199
+ void py_mem_free(void *p) {
200
+ if (p == NULL) {
201
+ py_fatal(0, "mem.free on a null pointer");
202
+ }
203
+ if (heap_rec_bytes(p) == 0) {
204
+ py_fatal(0, "mem.free on an invalid or already-freed pointer");
205
+ }
206
+ heap_rec_drop(p);
207
+ free(p);
208
+ }
209
+
210
+ void *py_mem_realloc(void *p, size_t new_bytes) {
211
+ if (p == NULL) {
212
+ return py_mem_alloc(new_bytes);
213
+ }
214
+ size_t old_bytes = heap_rec_bytes(p);
215
+ if (old_bytes == 0) {
216
+ py_fatal(0, "mem.realloc on a pointer not returned by mem.alloc");
217
+ }
218
+ size_t keep = old_bytes < new_bytes ? old_bytes : new_bytes;
219
+ void *np = calloc((size_t)1, new_bytes);
220
+ if (np == NULL) {
221
+ py_fatal(0, "out of memory");
222
+ }
223
+ if (keep > 0) {
224
+ memcpy(np, p, keep);
225
+ }
226
+ heap_rec_drop(p);
227
+ free(p);
228
+ heap_rec_add(np, new_bytes);
229
+ return np;
230
+ }""",
231
+ "deps": ["py_fatal"],
232
+ },
233
+ # ---------------------------------------------------------------- String
234
+ "count_codepoints": {
235
+ "code": """int64_t count_codepoints(const char *s, int64_t blen) {
236
+ int64_t n = 0;
237
+ for (int64_t i = 0; i < blen; i++) {
238
+ if ((s[i] & 0xC0) != 0x80) {
239
+ n++;
240
+ }
241
+ }
242
+ return n;
243
+ }""",
244
+ "deps": [],
245
+ },
246
+ "py_str_nil": {
247
+ "code": """py_str py_str_nil(void) {
248
+ py_str s = {NULL, 0, 0};
249
+ return s;
250
+ }""",
251
+ "deps": [],
252
+ },
253
+ "py_str_at": {
254
+ # Text subscript: indexed by UTF-8 **code point** (consistent with Python's str[i]),
255
+ # returns the **first byte** of that character (C's char cannot hold multi-byte characters).
256
+ # Out of range terminates per C's convention (py_fatal), while the CPython track raises IndexError.
257
+ "code": """char py_str_at(py_str s, int64_t index) {
258
+ if (index < 0 || index >= s.len) {
259
+ py_fatal(0, "string index out of range");
260
+ }
261
+ int64_t n = 0;
262
+ for (int64_t i = 0; i < s.blen; i++) {
263
+ if ((s.data[i] & 0xC0) != 0x80) {
264
+ if (n == index) {
265
+ return s.data[i];
266
+ }
267
+ n++;
268
+ }
269
+ }
270
+ py_fatal(0, "string index out of range");
271
+ return 0;
272
+ }""",
273
+ "deps": ["py_fatal"],
274
+ },
275
+ "py_str_new": {
276
+ "code": """py_str py_str_new(const char *bytes, int64_t blen) {
277
+ py_str s;
278
+ if (blen <= 0) {
279
+ return py_str_nil();
280
+ }
281
+ char *buf = (char *)py_arena_alloc((size_t)blen, 0);
282
+ memcpy(buf, bytes, (size_t)blen);
283
+ s.data = buf;
284
+ s.blen = blen;
285
+ s.len = count_codepoints(buf, blen);
286
+ return s;
287
+ }""",
288
+ "deps": ["py_arena_alloc", "count_codepoints", "py_str_nil"],
289
+ },
290
+ "py_str_concat": {
291
+ "code": """py_str py_str_concat(py_str a, py_str b, int64_t line) {
292
+ if (a.blen <= 0) {
293
+ return b;
294
+ }
295
+ if (b.blen <= 0) {
296
+ return a;
297
+ }
298
+ int64_t total = a.blen + b.blen;
299
+ char *buf = (char *)py_arena_alloc((size_t)total, line);
300
+ memcpy(buf, a.data, (size_t)a.blen);
301
+ memcpy(buf + a.blen, b.data, (size_t)b.blen);
302
+ py_str s;
303
+ s.data = buf;
304
+ s.blen = total;
305
+ s.len = a.len + b.len;
306
+ return s;
307
+ }""",
308
+ "deps": ["py_arena_alloc"],
309
+ },
310
+ "py_str_len": {
311
+ "code": "int64_t py_str_len(py_str s) { return s.len; }",
312
+ "deps": [],
313
+ },
314
+ "py_str_eq": {
315
+ "code": """bool py_str_eq(py_str a, py_str b) {
316
+ if (a.blen != b.blen) {
317
+ return false;
318
+ }
319
+ if (a.blen == 0) {
320
+ return true;
321
+ }
322
+ return memcmp(a.data, b.data, (size_t)a.blen) == 0;
323
+ }""",
324
+ "deps": [],
325
+ },
326
+ "py_str_cmp": {
327
+ "code": """int py_str_cmp(py_str a, py_str b) {
328
+ int64_t n = a.blen < b.blen ? a.blen : b.blen;
329
+ if (n > 0) {
330
+ int c = memcmp(a.data, b.data, (size_t)n);
331
+ if (c != 0) {
332
+ return c;
333
+ }
334
+ }
335
+ if (a.blen == b.blen) {
336
+ return 0;
337
+ }
338
+ return a.blen < b.blen ? -1 : 1;
339
+ }""",
340
+ "deps": [],
341
+ },
342
+ "py_min_str": {
343
+ "code": "py_str py_min_str(py_str a, py_str b) { return py_str_cmp(a, b) <= 0 ? a : b; }",
344
+ "deps": ["py_str_cmp"],
345
+ },
346
+ "py_max_str": {
347
+ "code": "py_str py_max_str(py_str a, py_str b) { return py_str_cmp(a, b) >= 0 ? a : b; }",
348
+ "deps": ["py_str_cmp"],
349
+ },
350
+ "str_from_buffer": {
351
+ "code": """py_str str_from_buffer(char *buf, int64_t blen) {
352
+ py_str s;
353
+ s.data = buf;
354
+ s.blen = blen;
355
+ s.len = count_codepoints(buf, blen);
356
+ return s;
357
+ }""",
358
+ "deps": ["count_codepoints"],
359
+ },
360
+ "py_input": {
361
+ # CPython's input() raises EOFError on EOF and UnicodeDecodeError on invalid UTF-8;
362
+ # the C track uses plain-byte fgets and must terminate at the same two points,
363
+ # otherwise it would silently return a half bytestring, and the dual-track output would diverge from this line.
364
+ # Validation follows UTF-8 structure + shortest encoding + code point range, without introducing wide-char library dependencies.
365
+ "code": """py_str py_input(void) {
366
+ size_t cap = 256;
367
+ size_t len = 0;
368
+ char *buf = (char *)py_arena_alloc(cap, 0);
369
+ for (;;) {
370
+ if (fgets(buf + len, (int)(cap - len), stdin) == NULL) {
371
+ if (len == 0) {
372
+ py_fatal(0, "input: unexpected end of file or read error");
373
+ }
374
+ break;
375
+ }
376
+ len += strlen(buf + len);
377
+ if (len > 0 && buf[len - 1] == '\\n') {
378
+ len--;
379
+ break;
380
+ }
381
+ if (len + 1 >= cap) {
382
+ size_t ncap = cap * 2;
383
+ char *nbuf = (char *)py_arena_alloc(ncap, 0);
384
+ memcpy(nbuf, buf, len);
385
+ buf = nbuf;
386
+ cap = ncap;
387
+ }
388
+ }
389
+ while (len > 0 && buf[len - 1] == '\\r') {
390
+ len--;
391
+ }
392
+ int ok = 1;
393
+ for (size_t i = 0; i < len && ok; ) {
394
+ unsigned char ch = (unsigned char)buf[i];
395
+ size_t need = 0;
396
+ unsigned long cp = 0;
397
+ unsigned long min_cp = 0;
398
+ if (ch < 0x80u) { i++; continue; }
399
+ if ((ch & 0xE0u) == 0xC0u) { need = 1; cp = ch & 0x1Fu; min_cp = 0x80u; }
400
+ else if ((ch & 0xF0u) == 0xE0u) { need = 2; cp = ch & 0x0Fu; min_cp = 0x800u; }
401
+ else if ((ch & 0xF8u) == 0xF0u) { need = 3; cp = ch & 0x07u; min_cp = 0x10000u; }
402
+ else { ok = 0; break; }
403
+ if (i + need >= len) { ok = 0; break; }
404
+ for (size_t k = 1; k <= need; k++) {
405
+ unsigned char cc = (unsigned char)buf[i + k];
406
+ if ((cc & 0xC0u) != 0x80u) { ok = 0; break; }
407
+ cp = (cp << 6) | (unsigned long)(cc & 0x3Fu);
408
+ }
409
+ if (!ok) { break; }
410
+ if (cp < min_cp || cp > 0x10FFFFu || (cp >= 0xD800u && cp <= 0xDFFFu)) {
411
+ ok = 0;
412
+ break;
413
+ }
414
+ i += need + 1;
415
+ }
416
+ if (!ok) {
417
+ py_fatal(0, "input is not valid UTF-8");
418
+ }
419
+ return py_str_new(buf, (int64_t)len);
420
+ }""",
421
+ "deps": ["py_arena_alloc", "py_str_new", "py_str_nil", "py_fatal"],
422
+ },
423
+ "py_input_prompt": {
424
+ "code": """py_str py_input_prompt(py_str prompt) {
425
+ if (prompt.data != NULL && prompt.blen > 0) {
426
+ fwrite(prompt.data, 1, (size_t)prompt.blen, stdout);
427
+ fflush(stdout);
428
+ }
429
+ return py_input();
430
+ }""",
431
+ "deps": ["py_input"],
432
+ },
433
+ "py_str_cstr": {
434
+ "code": """char *py_str_cstr(py_str s) {
435
+ char *buf = (char *)py_arena_alloc((size_t)s.blen + 1, 0);
436
+ if (s.data != NULL && s.blen > 0) {
437
+ memcpy(buf, s.data, (size_t)s.blen);
438
+ }
439
+ buf[s.blen] = '\\0';
440
+ return buf;
441
+ }""",
442
+ "deps": ["py_arena_alloc"],
443
+ },
444
+ "py_open": {
445
+ "code": """py_file py_open(py_str path, py_str mode) {
446
+ py_file f;
447
+ f.fp = NULL;
448
+ if (path.data == NULL || path.blen <= 0 || mode.data == NULL || mode.blen <= 0) {
449
+ return f;
450
+ }
451
+ char *p = py_str_cstr(path);
452
+ char *m = py_str_cstr(mode);
453
+ char mbuf[16];
454
+ size_t n = 0;
455
+ int has_b = 0;
456
+ while (m[n] != '\\0' && n < sizeof(mbuf) - 2) {
457
+ mbuf[n] = m[n];
458
+ if (m[n] == 'b') {
459
+ has_b = 1;
460
+ }
461
+ n++;
462
+ }
463
+ if (!has_b) {
464
+ mbuf[n++] = 'b';
465
+ }
466
+ mbuf[n] = '\\0';
467
+ f.fp = (void *)fopen(p, mbuf);
468
+ return f;
469
+ }""",
470
+ "deps": ["py_str_cstr"],
471
+ },
472
+ "py_file_read": {
473
+ "code": """py_bytes py_file_read(py_file f, int64_t cap) {
474
+ py_bytes b;
475
+ b.data = NULL;
476
+ b.len = 0;
477
+ FILE *fp = (FILE *)f.fp;
478
+ if (fp == NULL || cap <= 0) {
479
+ return b;
480
+ }
481
+ uint8_t *buf = (uint8_t *)py_arena_alloc((size_t)cap, 0);
482
+ size_t n = fread(buf, 1, (size_t)cap, fp);
483
+ b.data = buf;
484
+ b.len = (int64_t)n;
485
+ return b;
486
+ }""",
487
+ "deps": ["py_arena_alloc"],
488
+ },
489
+ "py_file_write": {
490
+ "code": """int64_t py_file_write(py_file f, py_bytes data) {
491
+ FILE *fp = (FILE *)f.fp;
492
+ if (fp == NULL || data.data == NULL || data.len <= 0) {
493
+ return 0;
494
+ }
495
+ return (int64_t)fwrite(data.data, 1, (size_t)data.len, fp);
496
+ }""",
497
+ "deps": [],
498
+ },
499
+ "py_file_close": {
500
+ "code": """void py_file_close(py_file f) {
501
+ FILE *fp = (FILE *)f.fp;
502
+ if (fp != NULL) {
503
+ fclose(fp);
504
+ }
505
+ }""",
506
+ "deps": [],
507
+ },
508
+ "py_str_from_i64": {
509
+ "code": """py_str py_str_from_i64(int64_t v) {
510
+ char *buf = (char *)py_arena_alloc(32, 0);
511
+ int n = snprintf(buf, 32, "%" PRId64, v);
512
+ return str_from_buffer(buf, n);
513
+ }""",
514
+ "deps": ["py_arena_alloc", "str_from_buffer"],
515
+ },
516
+ "py_str_from_u64": {
517
+ "code": """py_str py_str_from_u64(uint64_t v) {
518
+ char *buf = (char *)py_arena_alloc(32, 0);
519
+ int n = snprintf(buf, 32, "%" PRIu64, v);
520
+ return str_from_buffer(buf, n);
521
+ }""",
522
+ "deps": ["py_arena_alloc", "str_from_buffer"],
523
+ },
524
+ "py_str_from_i64_base": {
525
+ "code": """py_str py_str_from_i64_base(int64_t v, int base) {
526
+ static const char hexdigits[] = "0123456789abcdef";
527
+ char *tmp = (char *)py_arena_alloc(80, 0);
528
+ int pos = 79;
529
+ tmp[pos] = '\\0';
530
+ uint64_t u = (v < 0) ? (0u - (uint64_t)v) : (uint64_t)v;
531
+ if (u == 0) {
532
+ tmp[--pos] = '0';
533
+ }
534
+ while (u > 0) {
535
+ int d = (int)(u % (uint64_t)base);
536
+ tmp[--pos] = hexdigits[d];
537
+ u /= (uint64_t)base;
538
+ }
539
+ if (v < 0) {
540
+ tmp[--pos] = '-';
541
+ }
542
+ const char *pre = (base == 16) ? "0x" : (base == 8 ? "0o" : "0b");
543
+ for (int i = (int)strlen(pre) - 1; i >= 0; i--) {
544
+ tmp[--pos] = pre[i];
545
+ }
546
+ return str_from_buffer(tmp + pos, (int64_t)(79 - pos));
547
+ }""",
548
+ "deps": ["py_arena_alloc", "str_from_buffer"],
549
+ },
550
+ "format_double": {
551
+ "code": """void format_double(double v, char *buf, size_t cap) {
552
+ if (isnan(v)) {
553
+ snprintf(buf, cap, "nan");
554
+ return;
555
+ }
556
+ if (isinf(v)) {
557
+ snprintf(buf, cap, v > 0 ? "inf" : "-inf");
558
+ return;
559
+ }
560
+ if (v == 0.0) {
561
+ snprintf(buf, cap, signbit(v) ? "-0.0" : "0.0");
562
+ return;
563
+ }
564
+ char tmp[64];
565
+ int prec = 17;
566
+ for (int p = 1; p <= 17; p++) {
567
+ snprintf(tmp, sizeof(tmp), "%.*e", p - 1, v);
568
+ if (strtod(tmp, NULL) == v) {
569
+ prec = p;
570
+ break;
571
+ }
572
+ }
573
+ snprintf(tmp, sizeof(tmp), "%.*e", prec - 1, v);
574
+ const char *s = tmp;
575
+ int neg = 0;
576
+ if (*s == '-') {
577
+ neg = 1;
578
+ s++;
579
+ } else if (*s == '+') {
580
+ s++;
581
+ }
582
+ char digits[32];
583
+ int ndig = 0;
584
+ while (*s != '\\0' && *s != 'e' && *s != 'E') {
585
+ if (*s >= '0' && *s <= '9' && ndig < 31) {
586
+ digits[ndig++] = *s;
587
+ }
588
+ s++;
589
+ }
590
+ digits[ndig] = '\\0';
591
+ while (ndig > 1 && digits[ndig - 1] == '0') {
592
+ digits[--ndig] = '\\0';
593
+ }
594
+ int exp10 = 0;
595
+ if (*s == 'e' || *s == 'E') {
596
+ exp10 = (int)strtol(s + 1, NULL, 10);
597
+ }
598
+ int decpt = exp10 + 1;
599
+ char out[64];
600
+ int w = 0;
601
+ if (neg) {
602
+ out[w++] = '-';
603
+ }
604
+ if (decpt <= -4 || decpt > 16) {
605
+ out[w++] = digits[0];
606
+ if (ndig > 1) {
607
+ out[w++] = '.';
608
+ for (int i = 1; i < ndig; i++) {
609
+ out[w++] = digits[i];
610
+ }
611
+ }
612
+ out[w++] = 'e';
613
+ int e = decpt - 1;
614
+ if (e < 0) {
615
+ out[w++] = '-';
616
+ e = -e;
617
+ } else {
618
+ out[w++] = '+';
619
+ }
620
+ char ebuf[8];
621
+ int elen = 0;
622
+ if (e == 0) {
623
+ ebuf[elen++] = '0';
624
+ }
625
+ while (e > 0) {
626
+ ebuf[elen++] = (char)('0' + e % 10);
627
+ e /= 10;
628
+ }
629
+ while (elen < 2) {
630
+ ebuf[elen++] = '0';
631
+ }
632
+ for (int i = elen - 1; i >= 0; i--) {
633
+ out[w++] = ebuf[i];
634
+ }
635
+ } else if (decpt <= 0) {
636
+ out[w++] = '0';
637
+ out[w++] = '.';
638
+ for (int i = 0; i < -decpt; i++) {
639
+ out[w++] = '0';
640
+ }
641
+ for (int i = 0; i < ndig; i++) {
642
+ out[w++] = digits[i];
643
+ }
644
+ } else if (decpt >= ndig) {
645
+ for (int i = 0; i < ndig; i++) {
646
+ out[w++] = digits[i];
647
+ }
648
+ for (int i = ndig; i < decpt; i++) {
649
+ out[w++] = '0';
650
+ }
651
+ out[w++] = '.';
652
+ out[w++] = '0';
653
+ } else {
654
+ for (int i = 0; i < decpt; i++) {
655
+ out[w++] = digits[i];
656
+ }
657
+ out[w++] = '.';
658
+ for (int i = decpt; i < ndig; i++) {
659
+ out[w++] = digits[i];
660
+ }
661
+ }
662
+ out[w] = '\\0';
663
+ snprintf(buf, cap, "%s", out);
664
+ }""",
665
+ "deps": [],
666
+ },
667
+ "py_str_from_f64": {
668
+ "code": """py_str py_str_from_f64(double v) {
669
+ char *buf = (char *)py_arena_alloc(64, 0);
670
+ format_double(v, buf, 64);
671
+ return str_from_buffer(buf, (int64_t)strlen(buf));
672
+ }""",
673
+ "deps": ["py_arena_alloc", "format_double", "str_from_buffer"],
674
+ },
675
+ "py_str_from_bool": {
676
+ "code": """py_str py_str_from_bool(bool v) {
677
+ return v ? py_str_new("True", 4) : py_str_new("False", 5);
678
+ }""",
679
+ "deps": ["py_str_new"],
680
+ },
681
+ # Single-byte char type to text: char / uchar / schar land on a 1-byte text.
682
+ "py_str_from_byte": {
683
+ "code": """py_str py_str_from_byte(uint8_t b) {
684
+ char tmp[1];
685
+ tmp[0] = (char)b;
686
+ return py_str_new(tmp, 1);
687
+ }""",
688
+ "deps": ["py_str_new"],
689
+ },
690
+ # Single-byte char type to bytestring: a one-byte bytestring.
691
+ "py_bytes_from_byte": {
692
+ "code": """py_bytes py_bytes_from_byte(uint8_t b) {
693
+ uint8_t *buf = (uint8_t *)py_arena_alloc(1, 0);
694
+ buf[0] = b;
695
+ py_bytes v;
696
+ v.data = buf;
697
+ v.len = 1;
698
+ return v;
699
+ }""",
700
+ "deps": ["py_arena_alloc"],
701
+ },
702
+ # Build bytestring from "pointer + byte length": copy into arena, detached from the source buffer.
703
+ "py_bytes_from_ptr": {
704
+ "code": """py_bytes py_bytes_from_ptr(const unsigned char *src, int64_t n) {
705
+ py_bytes v;
706
+ if (n <= 0) {
707
+ v.data = (const uint8_t *)"";
708
+ v.len = 0;
709
+ return v;
710
+ }
711
+ uint8_t *buf = (uint8_t *)py_arena_alloc((size_t)n, 0);
712
+ memcpy(buf, src, (size_t)n);
713
+ v.data = buf;
714
+ v.len = n;
715
+ return v;
716
+ }""",
717
+ "deps": ["py_arena_alloc"],
718
+ },
719
+ # Exit point for text/byte buffers: copy a private set of bytes and return a writable pointer to the copy.
720
+ # The copy is detached from the original value, so poke cannot modify the inside of py_str / py_bytes; even an empty buffer keeps 1 writable byte.
721
+ "py_str_data": {
722
+ "code": """char *py_str_data(py_str s) {
723
+ size_t n = (size_t)(s.blen > 0 ? s.blen : 1);
724
+ char *buf = (char *)py_arena_alloc(n, 0);
725
+ if (s.blen > 0) {
726
+ memcpy(buf, s.data, (size_t)s.blen);
727
+ }
728
+ return buf;
729
+ }""",
730
+ "deps": ["py_arena_alloc"],
731
+ },
732
+ "py_bytes_data": {
733
+ "code": """uint8_t *py_bytes_data(py_bytes b) {
734
+ size_t n = (size_t)(b.len > 0 ? b.len : 1);
735
+ uint8_t *buf = (uint8_t *)py_arena_alloc(n, 0);
736
+ if (b.len > 0) {
737
+ memcpy(buf, b.data, (size_t)b.len);
738
+ }
739
+ return buf;
740
+ }""",
741
+ "deps": ["py_arena_alloc"],
742
+ },
743
+ # ------------------------------------------------- Formatting with format spec
744
+ # f-string's {expr:spec}: spec is translated by the compiler into C's printf spec (cfmt).
745
+ # Use C99's snprintf(NULL, 0, ...) to first ask for the actual length, slice from the pool by length, then write once;
746
+ # buffer length is allocated by actual output, so a short result occupies only the bytes it actually needs.
747
+ "py_str_format_i64": {
748
+ "code": """py_str py_str_format_i64(const char *cfmt, int64_t v) {
749
+ int need = snprintf((char *)0, 0, cfmt, (long long)v);
750
+ if (need < 0) {
751
+ need = 0;
752
+ }
753
+ char *buf = (char *)py_arena_alloc((size_t)need + 1, 0);
754
+ int n = snprintf(buf, (size_t)need + 1, cfmt, (long long)v);
755
+ if (n < 0) {
756
+ n = 0;
757
+ }
758
+ if (n > need) {
759
+ n = need;
760
+ }
761
+ return str_from_buffer(buf, n);
762
+ }""",
763
+ "deps": ["py_arena_alloc", "str_from_buffer"],
764
+ },
765
+ "py_str_format_u64": {
766
+ "code": """py_str py_str_format_u64(const char *cfmt, uint64_t v) {
767
+ int need = snprintf((char *)0, 0, cfmt, (unsigned long long)v);
768
+ if (need < 0) {
769
+ need = 0;
770
+ }
771
+ char *buf = (char *)py_arena_alloc((size_t)need + 1, 0);
772
+ int n = snprintf(buf, (size_t)need + 1, cfmt, (unsigned long long)v);
773
+ if (n < 0) {
774
+ n = 0;
775
+ }
776
+ if (n > need) {
777
+ n = need;
778
+ }
779
+ return str_from_buffer(buf, n);
780
+ }""",
781
+ "deps": ["py_arena_alloc", "str_from_buffer"],
782
+ },
783
+ "py_str_format_f64": {
784
+ "code": """py_str py_str_format_f64(const char *cfmt, double v) {
785
+ int need = snprintf((char *)0, 0, cfmt, v);
786
+ if (need < 0) {
787
+ need = 0;
788
+ }
789
+ char *buf = (char *)py_arena_alloc((size_t)need + 1, 0);
790
+ int n = snprintf(buf, (size_t)need + 1, cfmt, v);
791
+ if (n < 0) {
792
+ n = 0;
793
+ }
794
+ if (n > need) {
795
+ n = need;
796
+ }
797
+ return str_from_buffer(buf, n);
798
+ }""",
799
+ "deps": ["py_arena_alloc", "str_from_buffer"],
800
+ },
801
+ "py_str_format_str": {
802
+ "code": """py_str py_str_format_str(const char *cfmt, py_str v) {
803
+ const char *p = cfmt;
804
+ int left = 0;
805
+ int width = 0;
806
+ int prec = -1;
807
+ if (*p == '%') p++;
808
+ if (*p == '-') { left = 1; p++; }
809
+ while (*p >= '0' && *p <= '9') { width = width * 10 + (*p - '0'); p++; }
810
+ if (*p == '.') {
811
+ p++;
812
+ prec = 0;
813
+ while (*p >= '0' && *p <= '9') { prec = prec * 10 + (*p - '0'); p++; }
814
+ }
815
+ const char *data = (v.data != NULL) ? v.data : "";
816
+ int64_t blen = (v.blen > 0) ? v.blen : 0;
817
+ int64_t cut = blen;
818
+ int64_t cp = 0;
819
+ int64_t i = 0;
820
+ while (i < blen) {
821
+ unsigned char c = (unsigned char)data[i];
822
+ int step = (c < 0x80) ? 1 : ((c < 0xE0) ? 2 : ((c < 0xF0) ? 3 : 4));
823
+ if (i + step > blen) { step = 1; }
824
+ if (prec >= 0 && cp >= prec) { cut = i; break; }
825
+ i += step;
826
+ cp++;
827
+ }
828
+ int64_t pad = (width > cp) ? (width - cp) : 0;
829
+ int64_t total = cut + pad;
830
+ char *out = (char *)py_arena_alloc((size_t)total + 1, 0);
831
+ int64_t w = 0;
832
+ if (!left) {
833
+ while (w < pad) { out[w++] = ' '; }
834
+ }
835
+ memcpy(out + w, data, (size_t)cut);
836
+ w += cut;
837
+ if (left) {
838
+ while (w < total) { out[w++] = ' '; }
839
+ }
840
+ out[w] = '\\0';
841
+ return str_from_buffer(out, w);
842
+ }""",
843
+ "deps": ["py_arena_alloc", "str_from_buffer"],
844
+ },
845
+ # ---------------------------------------------------------------- ByteString
846
+ "py_bytes_repr": {
847
+ "code": r"""int64_t py_bytes_repr(const uint8_t *data, int64_t len, char *out, int64_t cap) {
848
+ int64_t w = 0;
849
+ int has_single = 0;
850
+ int has_double = 0;
851
+ for (int64_t i = 0; i < len; i++) {
852
+ if (data[i] == (uint8_t)'\'') {
853
+ has_single = 1;
854
+ } else if (data[i] == (uint8_t)'"') {
855
+ has_double = 1;
856
+ }
857
+ }
858
+ char quote = (has_single && !has_double) ? '"' : '\'';
859
+ if (w < cap) { out[w++] = 'b'; }
860
+ if (w < cap) { out[w++] = quote; }
861
+ for (int64_t i = 0; i < len; i++) {
862
+ uint8_t c = data[i];
863
+ char tmp[8];
864
+ int n = 0;
865
+ if (c == (uint8_t)'\\') {
866
+ tmp[n++] = '\\';
867
+ tmp[n++] = '\\';
868
+ } else if (c == (uint8_t)'\t') {
869
+ tmp[n++] = '\\';
870
+ tmp[n++] = 't';
871
+ } else if (c == (uint8_t)'\n') {
872
+ tmp[n++] = '\\';
873
+ tmp[n++] = 'n';
874
+ } else if (c == (uint8_t)'\r') {
875
+ tmp[n++] = '\\';
876
+ tmp[n++] = 'r';
877
+ } else if (c == (uint8_t)quote) {
878
+ tmp[n++] = '\\';
879
+ tmp[n++] = quote;
880
+ } else if (c >= 0x20 && c <= 0x7e) {
881
+ tmp[n++] = (char)c;
882
+ } else {
883
+ static const char hex[] = "0123456789abcdef";
884
+ tmp[n++] = '\\';
885
+ tmp[n++] = 'x';
886
+ tmp[n++] = hex[(c >> 4) & 0x0f];
887
+ tmp[n++] = hex[c & 0x0f];
888
+ }
889
+ for (int k = 0; k < n; k++) {
890
+ if (w < cap) { out[w++] = tmp[k]; }
891
+ }
892
+ }
893
+ if (w < cap) { out[w++] = quote; }
894
+ if (w < cap) { out[w++] = '\0'; }
895
+ return w;
896
+ }""",
897
+ "deps": [],
898
+ },
899
+ # ---------------------------------------------------------------- Error
900
+ "py_fatal": {
901
+ # Error reporting is the output's only implicit dependency on the host environment: bare metal usually lacks stderr / exit.
902
+ # At build time, override the whole block with -DPY_FATAL_HOOK(msg,line)=..., no need to modify the output.
903
+ "code": """#ifndef PY_FATAL_HOOK
904
+ #define PY_FATAL_HOOK(msg, line) do { fflush(stdout); fprintf(stderr, "RuntimeError: %s (line %" PRId64 ")\\n", (msg), (line)); exit(4); } while (0)
905
+ #endif
906
+
907
+ void py_fatal(int64_t line, const char *msg) {
908
+ PY_FATAL_HOOK(msg, line);
909
+ }""",
910
+ "deps": [],
911
+ },
912
+ # ---------------------------------------------------------------- Numeric
913
+ "py_div_f64": {
914
+ "code": """double py_div_f64(double a, double b, int64_t line) {
915
+ if (b == 0.0) {
916
+ py_fatal(line, "division by zero");
917
+ }
918
+ return a / b;
919
+ }""",
920
+ "deps": ["py_fatal"],
921
+ },
922
+ "py_fdiv_f64": {
923
+ "code": """double py_fdiv_f64(double a, double b, int64_t line) {
924
+ if (b == 0.0) {
925
+ py_fatal(line, "division by zero");
926
+ }
927
+ return floor(a / b);
928
+ }""",
929
+ "deps": ["py_fatal"],
930
+ },
931
+ "py_mod_f64": {
932
+ "code": """double py_mod_f64(double a, double b, int64_t line) {
933
+ if (b == 0.0) {
934
+ py_fatal(line, "division by zero");
935
+ }
936
+ double r = fmod(a, b);
937
+ if (r != 0.0 && ((r < 0.0) != (b < 0.0))) {
938
+ r += b;
939
+ }
940
+ return r;
941
+ }""",
942
+ "deps": ["py_fatal"],
943
+ },
944
+ "py_pow_f64": {
945
+ "code": """double py_pow_f64(double a, double b, int64_t line) {
946
+ if (a == 0.0 && b < 0.0) {
947
+ py_fatal(line, "negative exponent of zero");
948
+ }
949
+ return pow(a, b);
950
+ }""",
951
+ "deps": ["py_fatal"],
952
+ },
953
+ "py_fdiv_i64": {
954
+ "code": """int64_t py_fdiv_i64(int64_t a, int64_t b, int64_t line) {
955
+ if (b == 0) {
956
+ py_fatal(line, "division by zero");
957
+ }
958
+ int64_t q = a / b;
959
+ int64_t r = a % b;
960
+ if (r != 0 && ((r < 0) != (b < 0))) {
961
+ q -= 1;
962
+ }
963
+ return q;
964
+ }""",
965
+ "deps": ["py_fatal"],
966
+ },
967
+ "py_fdiv_u64": {
968
+ "code": """uint64_t py_fdiv_u64(uint64_t a, uint64_t b, int64_t line) {
969
+ if (b == 0) {
970
+ py_fatal(line, "division by zero");
971
+ }
972
+ return a / b;
973
+ }""",
974
+ "deps": ["py_fatal"],
975
+ },
976
+ "py_mod_i64": {
977
+ "code": """int64_t py_mod_i64(int64_t a, int64_t b, int64_t line) {
978
+ if (b == 0) {
979
+ py_fatal(line, "division by zero");
980
+ }
981
+ int64_t r = a % b;
982
+ if (r != 0 && ((r < 0) != (b < 0))) {
983
+ r += b;
984
+ }
985
+ return r;
986
+ }""",
987
+ "deps": ["py_fatal"],
988
+ },
989
+ "py_mod_u64": {
990
+ "code": """uint64_t py_mod_u64(uint64_t a, uint64_t b, int64_t line) {
991
+ if (b == 0) {
992
+ py_fatal(line, "division by zero");
993
+ }
994
+ return a % b;
995
+ }""",
996
+ "deps": ["py_fatal"],
997
+ },
998
+ "py_pow_i64": {
999
+ "code": """int64_t py_pow_i64(int64_t base, int64_t exp, int64_t line) {
1000
+ if (exp < 0) {
1001
+ py_fatal(line, "integer power does not support a negative exponent");
1002
+ }
1003
+ uint64_t result = 1;
1004
+ uint64_t b = (uint64_t)base;
1005
+ uint64_t e = (uint64_t)exp;
1006
+ while (e > 0) {
1007
+ if (e & 1u) {
1008
+ result *= b;
1009
+ }
1010
+ b *= b;
1011
+ e >>= 1;
1012
+ }
1013
+ return (int64_t)result;
1014
+ }""",
1015
+ "deps": ["py_fatal"],
1016
+ },
1017
+ "py_range_step": {
1018
+ "code": """int64_t py_range_step(int64_t step, int64_t line) {
1019
+ if (step == 0) {
1020
+ py_fatal(line, "range step cannot be zero");
1021
+ }
1022
+ return step;
1023
+ }""",
1024
+ "deps": ["py_fatal"],
1025
+ },
1026
+ # ---------------------------------------------------------------- Utilities
1027
+ "py_iabs": {
1028
+ "code": "int64_t py_iabs(int64_t v) { return v < 0 ? -v : v; }",
1029
+ "deps": [],
1030
+ },
1031
+ "py_fabsd": {
1032
+ "code": "double py_fabsd(double v) { return fabs(v); }",
1033
+ "deps": [],
1034
+ },
1035
+ "py_min_i64": {
1036
+ "code": "int64_t py_min_i64(int64_t a, int64_t b) { return a < b ? a : b; }",
1037
+ "deps": [],
1038
+ },
1039
+ "py_max_i64": {
1040
+ "code": "int64_t py_max_i64(int64_t a, int64_t b) { return a > b ? a : b; }",
1041
+ "deps": [],
1042
+ },
1043
+ "py_min_u64": {
1044
+ "code": "uint64_t py_min_u64(uint64_t a, uint64_t b) { return a < b ? a : b; }",
1045
+ "deps": [],
1046
+ },
1047
+ "py_max_u64": {
1048
+ "code": "uint64_t py_max_u64(uint64_t a, uint64_t b) { return a > b ? a : b; }",
1049
+ "deps": [],
1050
+ },
1051
+ "py_min_f64": {
1052
+ "code": "double py_min_f64(double a, double b) { return a < b ? a : b; }",
1053
+ "deps": [],
1054
+ },
1055
+ "py_max_f64": {
1056
+ "code": "double py_max_f64(double a, double b) { return a > b ? a : b; }",
1057
+ "deps": [],
1058
+ },
1059
+ }
1060
+
1061
+ def render_snippet(
1062
+ name: str,
1063
+ arena_manual_cap: int = DEFAULT_MANUAL_CAP,
1064
+ arena_auto_cap: int = DEFAULT_AUTO_CAP,
1065
+ ) -> str:
1066
+ """Return the snippet's C text; the arena snippet's two integer capacities both come from the compile config.
1067
+
1068
+ The ratio conversion is done in config.py; here we only do string substitution, so the output contains
1069
+ no ratio, multiplication/division, or floating point, nor any hardcoded capacity literals.
1070
+ """
1071
+ code = SNIPPETS[name]["code"]
1072
+ if name == "__arena__":
1073
+ code = code.replace("{arena_manual_cap}", str(arena_manual_cap))
1074
+ code = code.replace("{arena_auto_cap}", str(arena_auto_cap))
1075
+ return code
1076
+
1077
+
1078
+ # These names do not start with py_, but are still runtime internal functions and must be recognized during scanning
1079
+ INTERNAL_NAMES = {
1080
+ "count_codepoints",
1081
+ "str_from_buffer",
1082
+ "format_double",
1083
+ "py_str_cstr",
1084
+ "heap_rec_add",
1085
+ "heap_rec_drop",
1086
+ "heap_rec_bytes",
1087
+ "py_str_format_i64",
1088
+ "py_str_format_u64",
1089
+ "py_str_format_f64",
1090
+ "py_str_format_str",
1091
+ }
1092
+
1093
+ # Some runtime functions are defined inside "composite snippets" like __arena__, not as independent entries.
1094
+ # When collecting dependencies, map them to the owning snippet.
1095
+ _CONTAINERS = {
1096
+ "py_arena_alloc": "__arena__",
1097
+ "py_arena_alloc_aligned": "__arena__",
1098
+ "py_arena_fatal": "__arena__",
1099
+ "py_arena_reset": "__arena_reset__",
1100
+ "py_mem_alloc": "__heap__",
1101
+ "py_mem_free": "__heap__",
1102
+ "py_mem_realloc": "__heap__",
1103
+ }
1104
+
1105
+
1106
+ # system header -> the symbols it provides that the runtime code will use. This is the single source of truth for "which headers the output needs":
1107
+ # codegen scans the final generated C body (excluding #include lines) to compare,
1108
+ # telling the user which is missing from their config. Used only for hints, never to auto-add #include.
1109
+ import re as _re
1110
+
1111
+ HEADER_SYMBOLS: Dict[str, Tuple[str, ...]] = {
1112
+ "stdio.h": (
1113
+ "printf", "snprintf", "fprintf", "sprintf", "fopen", "fclose",
1114
+ "fread", "fwrite", "fgets", "FILE", "stderr", "stdin", "stdout",
1115
+ ),
1116
+ "stdint.h": (
1117
+ "int8_t", "int16_t", "int32_t", "int64_t",
1118
+ "uint8_t", "uint16_t", "uint32_t", "uint64_t",
1119
+ "uintptr_t",
1120
+ ),
1121
+ "inttypes.h": ("PRId64", "PRIu64", "PRIx64", "PRId32", "PRIu32"),
1122
+ "math.h": ("pow", "fabs", "floor", "ceil", "isnan", "fmod", "sqrt"),
1123
+ "stdbool.h": ("bool", "true", "false"),
1124
+ "stddef.h": ("size_t", "NULL", "ptrdiff_t"),
1125
+ "stdlib.h": ("malloc", "calloc", "realloc", "free", "exit", "abort"),
1126
+ "string.h": (
1127
+ "memcpy", "memmove", "memset", "memcmp",
1128
+ "strlen", "strcmp", "strncpy", "strcpy",
1129
+ ),
1130
+ }
1131
+
1132
+ # Display names for composite snippets (the X in "runtime snippet X" in hints).
1133
+ SNIPPET_DISPLAY = {
1134
+ "__arena__": "py_arena_alloc",
1135
+ "__arena_reset__": "py_arena_reset",
1136
+ "__heap__": "py_mem_alloc",
1137
+ }
1138
+
1139
+ # C string literals: strip before scanning symbols to avoid false positives from text like "true" / "false".
1140
+ _C_STRING_RE = _re.compile(r'"(?:[^"\\]|\\.)*"')
1141
+ _SYMBOL_RES = {
1142
+ header: tuple((sym, _re.compile(r"\b" + _re.escape(sym) + r"\b")) for sym in syms)
1143
+ for header, syms in HEADER_SYMBOLS.items()
1144
+ }
1145
+
1146
+
1147
+ def headers_in_code(code: str) -> Dict[str, List[str]]:
1148
+ """Return the system symbols appearing in a piece of C code, grouped by header (order within a group preserved from the table)."""
1149
+ found: Dict[str, List[str]] = {}
1150
+ for line in code.splitlines():
1151
+ clean = _C_STRING_RE.sub('""', line)
1152
+ for header, pairs in _SYMBOL_RES.items():
1153
+ hits = found.setdefault(header, [])
1154
+ for sym, rx in pairs:
1155
+ if sym not in hits and rx.search(clean):
1156
+ hits.append(sym)
1157
+ return {h: v for h, v in found.items() if v}
1158
+
1159
+
1160
+ def required_headers(lines: List[str]) -> Dict[str, List[str]]:
1161
+ """Scan the generated output body (skipping #include lines) and return which system headers and their symbols are actually needed."""
1162
+ body = "\n".join(l for l in lines if not l.lstrip().startswith("#include"))
1163
+ return headers_in_code(body)
1164
+
1165
+
1166
+ # Which system symbols each snippet uses: computed once at module load, for hints to pick representative snippets.
1167
+ SNIPPET_HEADERS: Dict[str, Dict[str, List[str]]] = {
1168
+ name: headers_in_code(entry["code"]) for name, entry in SNIPPETS.items()
1169
+ }
1170
+
1171
+
1172
+ def snippet_display(name: str) -> str:
1173
+ """The snippet name used in hint text."""
1174
+ return SNIPPET_DISPLAY.get(name, name)
1175
+
1176
+
1177
+ def collect(used: set) -> List[str]:
1178
+ """Given the directly used function names, compute the transitive dependency closure and return the names in dependency order.
1179
+
1180
+ Dependencies come first, ensuring C compilation defines before use.
1181
+ """
1182
+ order: List[str] = []
1183
+ seen: set = set()
1184
+
1185
+ def visit(name: str) -> None:
1186
+ name = _CONTAINERS.get(name, name)
1187
+ if name in seen:
1188
+ return
1189
+ if name not in SNIPPETS:
1190
+ return
1191
+ seen.add(name)
1192
+ for dep in SNIPPETS[name]["deps"]:
1193
+ visit(dep)
1194
+ order.append(name)
1195
+
1196
+ for name in sorted(used):
1197
+ visit(name)
1198
+ return order
1199
+
1200
+
1201
+ # ---------------------------------------------------------------------------
1202
+ # Runtime public API
1203
+ # ---------------------------------------------------------------------------
1204
+ # The name list of runtime functions, grouped by purpose; together with INTERNAL_NAMES it forms
1205
+ # RESERVED_IDENTIFIERS, used to block user identifiers that collide with runtime names.
1206
+ PUBLIC_API = (
1207
+ ("Memory", ("py_arena_alloc", "py_arena_alloc_aligned", "py_arena_reset", "py_mem_alloc", "py_mem_free", "py_mem_realloc")),
1208
+ (
1209
+ "String",
1210
+ (
1211
+ "py_str_nil",
1212
+ "py_str_new",
1213
+ "py_str_concat",
1214
+ "py_str_len",
1215
+ "py_str_eq",
1216
+ "py_str_cmp",
1217
+ "py_min_str",
1218
+ "py_max_str",
1219
+ "py_str_from_i64",
1220
+ "py_str_from_u64",
1221
+ "py_str_from_i64_base",
1222
+ "py_str_from_f64",
1223
+ "py_str_from_bool",
1224
+ "py_str_from_byte",
1225
+ "py_str_data",
1226
+ ),
1227
+ ),
1228
+ ("Input", ("py_input", "py_input_prompt")),
1229
+ ("File", ("py_open", "py_file_read", "py_file_write", "py_file_close")),
1230
+ ("ByteString", (
1231
+ "py_bytes_repr", "py_bytes_from_byte", "py_bytes_from_ptr", "py_bytes_data",
1232
+ )),
1233
+ ("Error", ("py_fatal",)),
1234
+ (
1235
+ "NumericSemantics",
1236
+ (
1237
+ "py_div_f64",
1238
+ "py_fdiv_f64",
1239
+ "py_mod_f64",
1240
+ "py_pow_f64",
1241
+ "py_fdiv_i64",
1242
+ "py_fdiv_u64",
1243
+ "py_mod_i64",
1244
+ "py_mod_u64",
1245
+ "py_pow_i64",
1246
+ "py_range_step",
1247
+ ),
1248
+ ),
1249
+ (
1250
+ "Utilities",
1251
+ (
1252
+ "py_iabs",
1253
+ "py_fabsd",
1254
+ "py_min_i64",
1255
+ "py_max_i64",
1256
+ "py_min_u64",
1257
+ "py_max_u64",
1258
+ "py_min_f64",
1259
+ "py_max_f64",
1260
+ ),
1261
+ ),
1262
+ )
1263
+
1264
+
1265
+ # ---------------------------------------------------------------------------
1266
+ # Runtime public types (C-side structs)
1267
+ # ---------------------------------------------------------------------------
1268
+ # Field order is the memory layout; generated C inlines in this order.
1269
+ RUNTIME_TYPES = (
1270
+ {
1271
+ "name": "py_str",
1272
+ "fields": (
1273
+ ("const char *", "data"),
1274
+ ("int64_t", "len"),
1275
+ ("int64_t", "blen"),
1276
+ ),
1277
+ },
1278
+ {
1279
+ "name": "py_bytes",
1280
+ "fields": (
1281
+ ("const uint8_t *", "data"),
1282
+ ("int64_t", "len"),
1283
+ ),
1284
+ },
1285
+ {
1286
+ "name": "py_file",
1287
+ "fields": (("void *", "fp"),),
1288
+ },
1289
+ )
1290
+
1291
+ # When multiple outputs include each other, they share this set of guard macro names, so the same typedef is not redefined.
1292
+ RUNTIME_TYPE_GUARDS = {
1293
+ spec["name"]: f"{RUNTIME_TYPE_GUARD_PREFIX}_{spec['name'].upper()}"
1294
+ for spec in RUNTIME_TYPES
1295
+ }
1296
+
1297
+ # ---------------------------------------------------------------------------
1298
+ # Macro name reserved words
1299
+ # ---------------------------------------------------------------------------
1300
+ # C macros are the preprocessor's plain-text substitution and will hit every same-named identifier in the output. Runtime snippets are inlined
1301
+ # by the compiler and invisible to the user, so a macro name cannot duplicate any of their identifiers (e.g. ``b`` would replace
1302
+ # ``py_block *b`` with ``py_block *1``). Reserved words are derived directly from the snippet's own C text;
1303
+ # the snippet set is the sole source, no separate list to maintain.
1304
+ _IDENT_RE = re.compile(r"[A-Za-z_][A-Za-z0-9_]*")
1305
+
1306
+
1307
+ def _c_identifiers(text: str) -> set:
1308
+ """Scan out the identifiers in C text that **will actually be substituted by the preprocessor**.
1309
+
1310
+ The contents of character constants and string literals do not count: the ``b`` in ``out[w++] = 'b'`` won't be
1311
+ replaced by ``#define b 1``, and counting such words as reserved words only causes false positives. Snippets themselves contain no comments,
1312
+ but we still skip comments here to stay robust when snippet text is adjusted.
1313
+ """
1314
+ out: set = set()
1315
+ i = 0
1316
+ n = len(text)
1317
+ while i < n:
1318
+ c = text[i]
1319
+ if c == '"' or c == "'":
1320
+ quote = c
1321
+ i += 1
1322
+ while i < n:
1323
+ if text[i] == "\\":
1324
+ i += 2
1325
+ continue
1326
+ if text[i] == quote:
1327
+ i += 1
1328
+ break
1329
+ i += 1
1330
+ continue
1331
+ if c == "/" and i + 1 < n and text[i + 1] == "/":
1332
+ while i < n and text[i] != "\n":
1333
+ i += 1
1334
+ continue
1335
+ if c == "/" and i + 1 < n and text[i + 1] == "*":
1336
+ i += 2
1337
+ while i + 1 < n and not (text[i] == "*" and text[i + 1] == "/"):
1338
+ i += 1
1339
+ i += 2
1340
+ continue
1341
+ if c.isalpha() or c == "_":
1342
+ j = i
1343
+ while j < n and (text[j].isalnum() or text[j] == "_"):
1344
+ j += 1
1345
+ out.add(text[i:j])
1346
+ i = j
1347
+ continue
1348
+ i += 1
1349
+ return out
1350
+
1351
+
1352
+ def _runtime_identifiers() -> frozenset:
1353
+ names = set()
1354
+ for snippet in SNIPPETS.values():
1355
+ names.update(_c_identifiers(snippet["code"]))
1356
+ for _, group in PUBLIC_API:
1357
+ names.update(group)
1358
+ names.update(INTERNAL_NAMES)
1359
+ for item in RUNTIME_TYPES:
1360
+ names.add(item["name"])
1361
+ for _, field in item["fields"]:
1362
+ names.add(field)
1363
+ names.discard("arena_cap") # capacity placeholder inside snippets, not a runtime identifier
1364
+ names.update(RUNTIME_TYPE_GUARDS.values())
1365
+ return frozenset(names)
1366
+
1367
+
1368
+ RESERVED_IDENTIFIERS = _runtime_identifiers()
1369
+
1370
+
1371
+ def render_types(names: Iterable[str]) -> str:
1372
+ """Render only the runtime public types the output actually uses.
1373
+
1374
+ Each type carries its own include guard, so when multiple outputs include each other, the later-included side can still
1375
+ fill in the extra types it uses. No comments are allowed in the generated output.
1376
+ """
1377
+ wanted = set(names)
1378
+ blocks = []
1379
+ for spec in RUNTIME_TYPES:
1380
+ if spec["name"] not in wanted:
1381
+ continue
1382
+ fields = []
1383
+ for ctype, name in spec["fields"]:
1384
+ sep = "" if ctype.endswith("*") else " "
1385
+ fields.append(f" {ctype}{sep}{name};")
1386
+ guard = RUNTIME_TYPE_GUARDS[spec["name"]]
1387
+ lines = [
1388
+ f"#ifndef {guard}",
1389
+ f"#define {guard}",
1390
+ f"typedef struct {spec['name']} {{",
1391
+ ]
1392
+ lines.extend(fields)
1393
+ lines.extend([f"}} {spec['name']};", "#endif"])
1394
+ blocks.append("\n".join(lines))
1395
+ return "\n\n".join(blocks)