summaryrefslogtreecommitdiff
path: root/parse/gram.y
diff options
context:
space:
mode:
authorandrewc <andrewchamberss@gmail.com>2016-10-28 16:48:15 +1300
committerAndrew Chambers <andrewchamberss@gmail.com>2016-10-28 19:43:46 +1300
commit96aa84cdbe397910f532f8c802c8de9825e49c59 (patch)
tree6dae3bae3b0fb66435a7ce8e7cee47815e1227d7 /parse/gram.y
parentd0ffb958c7247028223c7e53253249d52a47a324 (diff)
downloadmc-96aa84cdbe397910f532f8c802c8de9825e49c59.tar.gz
merge consecutive string literals
Diffstat (limited to 'parse/gram.y')
-rw-r--r--parse/gram.y16
1 files changed, 14 insertions, 2 deletions
diff --git a/parse/gram.y b/parse/gram.y
index ba61966..6a61935 100644
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -141,7 +141,7 @@ static void setupinit(Node *n);
%type <trait> traitdef
%type <node> exprln retexpr goto continue break expr atomicexpr
-%type <node> littok literal asnexpr lorexpr landexpr borexpr
+%type <node> littok literal asnexpr lorexpr landexpr borexpr strlit
%type <node> bandexpr cmpexpr unionexpr addexpr mulexpr shiftexpr prefixexpr postfixexpr
%type <node> funclit seqlit tuplit name block stmt label use
%type <node> fnparam declbody declcore typedeclcore structent arrayelt structelt tuphead
@@ -780,7 +780,7 @@ literal : funclit {$$ = mkexpr($1->loc, Olit, $1, NULL);}
tuplit : Toparen tupbody Tcparen
{$$ = mkexprl($1->loc, Otup, $2.nl, $2.nn);}
-littok : Tstrlit {$$ = mkstr($1->loc, $1->strval);}
+littok : strlit {$$ = $1;}
| Tchrlit {$$ = mkchar($1->loc, $1->chrval);}
| Tfloatlit {$$ = mkfloat($1->loc, $1->fltval);}
| Tboollit {$$ = mkbool($1->loc, !strcmp($1->id, "true"));}
@@ -792,6 +792,18 @@ littok : Tstrlit {$$ = mkstr($1->loc, $1->strval);}
}
;
+strlit : Tstrlit { $$ = mkstr($1->loc, $1->strval); }
+ | strlit Tstrlit {
+ Str merged;
+
+ merged.len = $1->lit.strval.len + $2->strval.len;
+ merged.buf = malloc(merged.len);
+ memcpy(merged.buf, $1->lit.strval.buf, $1->lit.strval.len);
+ memcpy(merged.buf + $1->lit.strval.len, $2->strval.buf, $2->strval.len);
+ $$ = mkstr($1->loc, merged);
+ }
+ ;
+
funclit : Tobrace params Tendln blkbody Tcbrace {
size_t i;
Node *fn, *lit;