Allow string concatenation in the parser as well, for now only for immediate strings and __FUNC__, maybe later -fpermissive or somethign could allow it on const-delcared string variables

This commit is contained in:
Wolfgang Bumiller 2013-04-24 16:07:31 +02:00
parent 8ffdfbfd97
commit 66305c676a
6 changed files with 38 additions and 3 deletions

1
ast.c
View file

@ -316,6 +316,7 @@ ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
self->isfield = false;
self->cvq = CV_NONE;
self->hasvalue = false;
self->isimm = false;
self->uses = 0;
memset(&self->constval, 0, sizeof(self->constval));

1
ast.h
View file

@ -177,6 +177,7 @@ struct ast_value_s
int cvq; /* const/var qualifier */
bool isfield; /* this declares a field */
bool isimm; /* an immediate, not just const */
bool hasvalue;
union {
double vfloat;

View file

@ -217,6 +217,7 @@ static ast_value* parser_const_float(parser_t *parser, double d)
out = ast_value_new(ctx, "#IMMEDIATE", TYPE_FLOAT);
out->cvq = CV_CONST;
out->hasvalue = true;
out->isimm = true;
out->constval.vfloat = d;
vec_push(parser->imm_float, out);
return out;
@ -279,6 +280,7 @@ static ast_value* parser_const_string(parser_t *parser, const char *str, bool do
out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_STRING);
out->cvq = CV_CONST;
out->hasvalue = true;
out->isimm = true;
out->constval.vstring = parser_strdup(str);
vec_push(parser->imm_string, out);
util_htseth(parser->ht_imm_string, str, hash, out);
@ -296,6 +298,7 @@ static ast_value* parser_const_vector(parser_t *parser, vector v)
out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_VECTOR);
out->cvq = CV_CONST;
out->hasvalue = true;
out->isimm = true;
out->constval.vvec = v;
vec_push(parser->imm_vector, out);
return out;
@ -2182,8 +2185,27 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma
wantop = true;
}
else {
parseerror(parser, "expected operator or end of statement");
goto onerr;
/* in this case we might want to allow constant string concatenation */
bool concatenated = false;
if (parser->tok == TOKEN_STRINGCONST && vec_size(sy.out)) {
ast_expression *lexpr = vec_last(sy.out).out;
if (ast_istype(lexpr, ast_value)) {
ast_value *last = (ast_value*)lexpr;
if (last->isimm == true && last->cvq == CV_CONST &&
last->hasvalue && last->expression.vtype == TYPE_STRING)
{
char *newstr = NULL;
util_asprintf(&newstr, "%s%s", last->constval.vstring, parser_tokval(parser));
vec_last(sy.out).out = (ast_expression*)parser_const_string(parser, newstr, false);
mem_d(newstr);
concatenated = true;
}
}
}
if (!concatenated) {
parseerror(parser, "expected operator or end of statement");
goto onerr;
}
}
if (!parser_next(parser)) {

View file

@ -1,3 +1,9 @@
void main() {
#ifdef SIMPLE
print(__FUNC__, "\n");
#elifdef CONCATENATED
print(__FUNC__ "\n");
#else
# error this is wrong
#endif
}

View file

@ -1,5 +1,5 @@
I: predef_func.qc
D: simple __FUNC__ case
T: -execute
C: -std=fte -fftepp-predefs
C: -std=fteqcc -DSIMPLE
M: main

View file

@ -0,0 +1,5 @@
I: predef_func.qc
D: concatenated __FUNC__ case
T: -execute
C: -std=fteqcc -DCONCATENATED
M: main