summaryrefslogtreecommitdiffstatshomepage
path: root/py/compile.c
diff options
context:
space:
mode:
authorDamien <damien.p.george@gmail.com>2013-12-10 17:41:49 +0000
committerDamien <damien.p.george@gmail.com>2013-12-10 17:41:49 +0000
commit6332174ab45c3ac3433e4311a344bcfbb0bde652 (patch)
tree20e102f22d0ebcedff09f2786e37cf7a06113ea5 /py/compile.c
parentdb4c361f1cb805dca8e4c2d6c6a9ac61ae0c9c36 (diff)
downloadmicropython-6332174ab45c3ac3433e4311a344bcfbb0bde652.tar.gz
micropython-6332174ab45c3ac3433e4311a344bcfbb0bde652.zip
py: compiler supports string juxtaposition=concatenation.
Diffstat (limited to 'py/compile.c')
-rw-r--r--py/compile.c32
1 files changed, 25 insertions, 7 deletions
diff --git a/py/compile.c b/py/compile.c
index 5e86b408c8..5a4d2dae49 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -2050,18 +2050,36 @@ void compile_power_dbl_star(compiler_t *comp, py_parse_node_struct_t *pns) {
void compile_atom_string(compiler_t *comp, py_parse_node_struct_t *pns) {
// a list of strings
- EMIT(load_const_verbatim_start);
- EMIT(load_const_verbatim_str, "'");
+
+ // check type of list (string or bytes) and count total number of bytes
int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
+ int n_bytes = 0;
+ int string_kind = PY_PARSE_NODE_NULL;
for (int i = 0; i < n; i++) {
- // TODO allow concatenation of either strings or bytes, but not mixed
assert(PY_PARSE_NODE_IS_LEAF(pns->nodes[i]));
- assert(PY_PARSE_NODE_LEAF_KIND(pns->nodes[i]) == PY_PARSE_NODE_STRING);
+ int pn_kind = PY_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
+ assert(pn_kind == PY_PARSE_NODE_STRING || pn_kind == PY_PARSE_NODE_BYTES);
+ if (i == 0) {
+ string_kind = pn_kind;
+ } else if (pn_kind != string_kind) {
+ printf("SyntaxError: cannot mix bytes and nonbytes literals\n");
+ return;
+ }
const char *str = qstr_str(PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
- EMIT(load_const_verbatim_strn, str, strlen(str));
+ n_bytes += strlen(str);
}
- EMIT(load_const_verbatim_str, "'");
- EMIT(load_const_verbatim_end);
+
+ // allocate memory for concatenated string/bytes
+ char *cat_str = m_new(char, n_bytes + 1);
+ cat_str[0] = '\0';
+
+ // concatenate string/bytes
+ for (int i = 0; i < n; i++) {
+ const char *str = qstr_str(PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
+ strcat(cat_str, str);
+ }
+
+ EMIT(load_const_str, qstr_from_str_take(cat_str), string_kind == PY_PARSE_NODE_BYTES);
}
// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node