mirror of
https://github.com/micropython/micropython.git
synced 2025-08-01 02:11:11 +02:00
Allow qstr's with non-ident chars, construct good identifier for them.
Also, add qstr's for string appearing in unix REPL loop, gross effect being less allocations for each command run.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import argparse
|
||||
import re
|
||||
from htmlentitydefs import codepoint2name
|
||||
|
||||
# this must match the equivalent function in qstr.c
|
||||
def compute_hash(qstr):
|
||||
@@ -10,7 +11,7 @@ def compute_hash(qstr):
|
||||
|
||||
def do_work(infiles):
|
||||
# read the qstrs in from the input files
|
||||
qstrs = []
|
||||
qstrs = {}
|
||||
for infile in infiles:
|
||||
with open(infile, 'rt') as f:
|
||||
line_number = 0
|
||||
@@ -23,28 +24,29 @@ def do_work(infiles):
|
||||
continue
|
||||
|
||||
# verify line is of the correct form
|
||||
match = re.match(r'Q\(([0-9A-Za-z_]+)\)$', line)
|
||||
match = re.match(r'Q\((.+)\)$', line)
|
||||
if not match:
|
||||
print('({}:{}) bad qstr format, got {}'.format(infile, line_number, line))
|
||||
return False
|
||||
|
||||
# get the qstr value
|
||||
qstr = match.group(1)
|
||||
ident = re.sub(r'[^A-Za-z0-9_]', lambda s: "_" + codepoint2name[ord(s.group(0))] + "_", qstr)
|
||||
|
||||
# don't add duplicates
|
||||
if qstr in qstrs:
|
||||
if ident in qstrs:
|
||||
continue
|
||||
|
||||
# add the qstr to the list
|
||||
qstrs.append(qstr)
|
||||
qstrs[ident] = qstr
|
||||
|
||||
# process the qstrs, printing out the generated C header file
|
||||
print('// This file was automatically generated by makeqstrdata.py')
|
||||
print('')
|
||||
for qstr in qstrs:
|
||||
for ident, qstr in qstrs.items():
|
||||
qhash = compute_hash(qstr)
|
||||
qlen = len(qstr)
|
||||
print('Q({}, (const byte*)"\\x{:02x}\\x{:02x}\\x{:02x}\\x{:02x}" "{}")'.format(qstr, qhash & 0xff, (qhash >> 8) & 0xff, qlen & 0xff, (qlen >> 8) & 0xff, qstr))
|
||||
print('Q({}, (const byte*)"\\x{:02x}\\x{:02x}\\x{:02x}\\x{:02x}" "{}")'.format(ident, qhash & 0xff, (qhash >> 8) & 0xff, qlen & 0xff, (qlen >> 8) & 0xff, qstr))
|
||||
|
||||
return True
|
||||
|
||||
|
Reference in New Issue
Block a user