|
| 1 | +from requests import get |
| 2 | + |
| 3 | +print('downloading...') |
| 4 | +t = get('https://raw.githubusercontent.com/dragoncoder047/ulisp-esp32/master/ulisp-esp32.ino').text |
| 5 | + |
| 6 | +''' |
| 7 | +#define ULISP_FUNCTION(id,name,doc) const char string_##id[] PROGMEM = id; const char doc_##id[] PROGMEM = doc; |
| 8 | +#define TBL_ENTRY(id,fun,mm) { string_##id, fun, mm, doc_##id } |
| 9 | +''' |
| 10 | + |
| 11 | +import re |
| 12 | +stringerRE = re.compile(r'STRINGER\((\S+?),\s+"(\S*?|)?"?\)') |
| 13 | +docserRE = re.compile(r'doc.+("\(?(\S+)[\s\S]+?);', re.MULTILINE) |
| 14 | +lineinRE = re.compile(r'\{\s+string_(\S+),\s+(\S+),\s+0x\d+,\s+NULL\s+\}') |
| 15 | +header = r'object \*%s \(object \*args, object \*env)\s+\{' |
| 16 | + |
| 17 | +print('finding strings') |
| 18 | +strings = {} |
| 19 | +for s in stringerRE.finditer(t): |
| 20 | + strings[s.group(1), s.group(0)] = s.group(2) |
| 21 | +print(strings) |
| 22 | + |
| 23 | +print('finding docs') |
| 24 | +docs = {} |
| 25 | +for s in docserRE.finditer(t): |
| 26 | + if len(s.group(1)) < 1000: |
| 27 | + docs[s.group(2).removesuffix('\\n"')] = [s.group(1), s.group(0)] |
| 28 | +print(docs) |
| 29 | + |
| 30 | +print('finding tbl') |
| 31 | +tbl = {} |
| 32 | +for s in lineinRE.finditer(t): |
| 33 | + tbl[s.group(1)] = [s.group(2), s.group(0)] |
| 34 | +print(tbl) |
| 35 | + |
| 36 | +print('replacing') |
| 37 | +for [[name, match], ls] in strings.items(): |
| 38 | + print(name) |
| 39 | + t, n = re.subn(header % name, f'ULISP_FUNCTION({name}, "{ls}", {docs.get(ls, [55])[0]})\n' + re.escape(header.replace('\\','')) % name, t, 1) |
| 40 | + if n: |
| 41 | + t = t.replace(match, '', 1) # clear STRINGER |
| 42 | + t = t.replace(docs.get(ls, [0, '\xFF'])[1], '', 1) # clear DOCS |
| 43 | + |
0 commit comments