Skip to content

Commit ab5d082

Browse files
committed
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.
1 parent d552db4 commit ab5d082

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

py/makeqstrdata.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import argparse
22
import re
3+
from htmlentitydefs import codepoint2name
34

45
# this must match the equivalent function in qstr.c
56
def compute_hash(qstr):
@@ -10,7 +11,7 @@ def compute_hash(qstr):
1011

1112
def do_work(infiles):
1213
# read the qstrs in from the input files
13-
qstrs = []
14+
qstrs = {}
1415
for infile in infiles:
1516
with open(infile, 'rt') as f:
1617
line_number = 0
@@ -23,28 +24,29 @@ def do_work(infiles):
2324
continue
2425

2526
# verify line is of the correct form
26-
match = re.match(r'Q\(([0-9A-Za-z_]+)\)$', line)
27+
match = re.match(r'Q\((.+)\)$', line)
2728
if not match:
2829
print('({}:{}) bad qstr format, got {}'.format(infile, line_number, line))
2930
return False
3031

3132
# get the qstr value
3233
qstr = match.group(1)
34+
ident = re.sub(r'[^A-Za-z0-9_]', lambda s: "_" + codepoint2name[ord(s.group(0))] + "_", qstr)
3335

3436
# don't add duplicates
35-
if qstr in qstrs:
37+
if ident in qstrs:
3638
continue
3739

3840
# add the qstr to the list
39-
qstrs.append(qstr)
41+
qstrs[ident] = qstr
4042

4143
# process the qstrs, printing out the generated C header file
4244
print('// This file was automatically generated by makeqstrdata.py')
4345
print('')
44-
for qstr in qstrs:
46+
for ident, qstr in qstrs.items():
4547
qhash = compute_hash(qstr)
4648
qlen = len(qstr)
47-
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))
49+
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))
4850

4951
return True
5052

py/qstrdefs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,11 @@ Q(sort)
8080
Q(join)
8181
Q(strip)
8282
Q(format)
83+
84+
Q(<module>)
85+
Q(<lambda>)
86+
Q(<listcomp>)
87+
Q(<dictcomp>)
88+
Q(<setcomp>)
89+
Q(<genexpr>)
90+
Q(<stdin>)

py/scope.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,27 @@ scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint
1818
scope->source_file = source_file;
1919
switch (kind) {
2020
case SCOPE_MODULE:
21-
scope->simple_name = QSTR_FROM_STR_STATIC("<module>");
21+
scope->simple_name = MP_QSTR__lt_module_gt_;
2222
break;
2323
case SCOPE_FUNCTION:
2424
case SCOPE_CLASS:
2525
assert(MP_PARSE_NODE_IS_STRUCT(pn));
2626
scope->simple_name = MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn)->nodes[0]);
2727
break;
2828
case SCOPE_LAMBDA:
29-
scope->simple_name = QSTR_FROM_STR_STATIC("<lambda>");
29+
scope->simple_name = MP_QSTR__lt_lambda_gt_;
3030
break;
3131
case SCOPE_LIST_COMP:
32-
scope->simple_name = QSTR_FROM_STR_STATIC("<listcomp>");
32+
scope->simple_name = MP_QSTR__lt_listcomp_gt_;
3333
break;
3434
case SCOPE_DICT_COMP:
35-
scope->simple_name = QSTR_FROM_STR_STATIC("<dictcomp>");
35+
scope->simple_name = MP_QSTR__lt_dictcomp_gt_;
3636
break;
3737
case SCOPE_SET_COMP:
38-
scope->simple_name = QSTR_FROM_STR_STATIC("<setcomp>");
38+
scope->simple_name = MP_QSTR__lt_setcomp_gt_;
3939
break;
4040
case SCOPE_GEN_EXPR:
41-
scope->simple_name = QSTR_FROM_STR_STATIC("<genexpr>");
41+
scope->simple_name = MP_QSTR__lt_genexpr_gt_;
4242
break;
4343
default:
4444
assert(0);

0 commit comments

Comments
 (0)