Files
visormesh/palm-client/tools/sect2.py
T
rich 55b77c84a7 palm-client: third code section (vmcode3); checker resolves COFF symbol sections
visormesh.def adds vmcode3 and sections.h VM_SECT3, ahead of new screens
(segment 2 is at 27 KB). check_sections.py checks every extra section, and
now maps COFF symbols' (sec N) to section names: before, it only resolved
section symbols such as .text, not named ones. sect2.py takes --sect3.
2026-11-01 21:49:29 -05:00

25 lines
1.2 KiB
Python

#!/usr/bin/env python3
"""Puts a C file's static functions in an extra code segment: inserts a
'static <signature> VM_SECT2;' (or --sect3: VM_SECT3) forward declaration for
each one after the last #include (see src/sections.h). Run once per file
moved; idempotent."""
import re
import sys
macro = 'VM_SECT3' if '--sect3' in sys.argv else 'VM_SECT2'
for path in [a for a in sys.argv[1:] if not a.startswith('--')]:
src = open(path).read()
marker = '/* In an extra code segment (' + macro + '): see sections.h for the rules. */'
if 'code segment: see sections.h' in src or 'code segment (VM_SECT' in src:
print(f'{path}: already done')
continue
if marker in src:
print(f'{path}: already done')
continue
sigs = re.findall(r'^(static\s+(?!const\s+UInt|UInt8\s+g|Char\s+\*g)[^=;{]*?\w+\s*\([^;{]*?\))\s*\n\{', src, re.M)
decls = '\n'.join(re.sub(r'\s+', ' ', s) + ' ' + macro + ';' for s in sigs)
last_inc = list(re.finditer(r'^#include .*\n', src, re.M))[-1]
src = src[:last_inc.end()] + f'\n{marker}\n{decls}\n' + src[last_inc.end():]
open(path, 'w').write(src)
print(f'{path}: {len(sigs)} static functions')