/[svn]/linuxsampler/trunk/src/scriptvm/parser.y
ViewVC logotype

Annotation of /linuxsampler/trunk/src/scriptvm/parser.y

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3595 - (hide annotations) (download)
Tue Sep 3 11:06:33 2019 UTC (4 years, 7 months ago) by schoenebeck
File size: 71515 byte(s)
* Fixed compiler errors with GCC 8.x.
* Bumped version (2.1.1.svn16).

1 schoenebeck 2581 /*
2 schoenebeck 3253 * Copyright (c) 2014-2017 Christian Schoenebeck and Andreas Persson
3 schoenebeck 2581 *
4     * http://www.linuxsampler.org
5     *
6     * This file is part of LinuxSampler and released under the same terms.
7     * See README file for details.
8     */
9 schoenebeck 2888
10     /* Parser for NKSP real-time instrument script language. */
11 schoenebeck 2581
12     %{
13     #define YYERROR_VERBOSE 1
14     #include "parser_shared.h"
15     #include <string>
16     #include <map>
17     using namespace LinuxSampler;
18    
19     void InstrScript_error(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* err);
20     void InstrScript_warning(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* txt);
21 schoenebeck 3008 int InstrScript_tnamerr(char* yyres, const char* yystr);
22 schoenebeck 2581 int InstrScript_lex(YYSTYPE* lvalp, YYLTYPE* llocp, void* scanner);
23     #define scanner context->scanner
24 schoenebeck 2888 #define PARSE_ERR(loc,txt) yyerror(&loc, context, txt)
25     #define PARSE_WRN(loc,txt) InstrScript_warning(&loc, context, txt)
26 schoenebeck 3311 #define PARSE_DROP(loc) context->addPreprocessorComment(loc.first_line, loc.last_line, loc.first_column+1, loc.last_column+1);
27 schoenebeck 3008 #define yytnamerr(res,str) InstrScript_tnamerr(res, str)
28 schoenebeck 2581 %}
29    
30     // generate reentrant safe parser
31     %pure-parser
32     %parse-param { LinuxSampler::ParserContext* context }
33     %lex-param { void* scanner }
34     // avoid symbol collision with other (i.e. future) auto generated (f)lex scanners
35 schoenebeck 3052 // (NOTE: "=" is deprecated here with Bison 3.x, however removing it would cause an error with Bison 2.x)
36     %name-prefix="InstrScript_"
37 schoenebeck 2581 %locations
38     %defines
39     %error-verbose
40    
41 schoenebeck 3008 %token <iValue> INTEGER "integer literal"
42 schoenebeck 3573 %token <fValue> REAL "real number literal"
43 schoenebeck 3561 %token <iUnitValue> INTEGER_UNIT "integer literal with unit"
44 schoenebeck 3573 %token <fUnitValue> REAL_UNIT "real number literal with unit"
45 schoenebeck 3008 %token <sValue> STRING "string literal"
46     %token <sValue> IDENTIFIER "function name"
47     %token <sValue> VARIABLE "variable name"
48     %token ON "keyword 'on'"
49     %token END "keyword 'end'"
50     %token INIT "keyword 'init'"
51     %token NOTE "keyword 'note'"
52     %token RELEASE "keyword 'release'"
53     %token CONTROLLER "keyword 'controller'"
54     %token DECLARE "keyword 'declare'"
55     %token ASSIGNMENT "operator ':='"
56     %token CONST_ "keyword 'const'"
57     %token POLYPHONIC "keyword 'polyphonic'"
58     %token WHILE "keyword 'while'"
59 schoenebeck 3260 %token SYNCHRONIZED "keyword 'synchronized'"
60 schoenebeck 3008 %token IF "keyword 'if'"
61     %token ELSE "keyword 'else'"
62     %token SELECT "keyword 'select'"
63     %token CASE "keyword 'case'"
64     %token TO "keyword 'to'"
65     %token OR "operator 'or'"
66     %token AND "operator 'and'"
67     %token NOT "operator 'not'"
68     %token BITWISE_OR "bitwise operator '.or.'"
69     %token BITWISE_AND "bitwise operator '.and.'"
70     %token BITWISE_NOT "bitwise operator '.not.'"
71     %token FUNCTION "keyword 'function'"
72     %token CALL "keyword 'call'"
73     %token MOD "operator 'mod'"
74     %token LE "operator '<='"
75     %token GE "operator '>='"
76     %token END_OF_FILE 0 "end of file"
77 schoenebeck 3259 %token UNKNOWN_CHAR "unknown character"
78 schoenebeck 2581
79 schoenebeck 2951 %type <nEventHandlers> script sections
80     %type <nEventHandler> section eventhandler
81     %type <nStatements> statements opt_statements userfunctioncall
82 schoenebeck 2581 %type <nStatement> statement assignment
83     %type <nFunctionCall> functioncall
84     %type <nArgs> args
85 schoenebeck 2935 %type <nExpression> arg expr logical_or_expr logical_and_expr bitwise_or_expr bitwise_and_expr rel_expr add_expr mul_expr unary_expr concat_expr
86 schoenebeck 2581 %type <nCaseBranch> caseclause
87     %type <nCaseBranches> caseclauses
88    
89     %start script
90    
91     %%
92    
93     script:
94 schoenebeck 2951 sections {
95 schoenebeck 2581 $$ = context->handlers = $1;
96     }
97    
98 schoenebeck 2951 sections:
99     section {
100 schoenebeck 2581 $$ = new EventHandlers();
101 schoenebeck 2951 if ($1) $$->add($1);
102 schoenebeck 2581 }
103 schoenebeck 2951 | sections section {
104 schoenebeck 2581 $$ = $1;
105 schoenebeck 2951 if ($2) $$->add($2);
106 schoenebeck 2581 }
107    
108 schoenebeck 2951 section:
109     function_declaration {
110     $$ = EventHandlerRef();
111     }
112     | eventhandler {
113     $$ = $1;
114     }
115    
116 schoenebeck 2581 eventhandler:
117 schoenebeck 2947 ON NOTE opt_statements END ON {
118 schoenebeck 2581 if (context->onNote)
119 schoenebeck 2888 PARSE_ERR(@2, "Redeclaration of 'note' event handler.");
120 schoenebeck 2581 context->onNote = new OnNote($3);
121     $$ = context->onNote;
122     }
123 schoenebeck 2947 | ON INIT opt_statements END ON {
124 schoenebeck 2581 if (context->onInit)
125 schoenebeck 2888 PARSE_ERR(@2, "Redeclaration of 'init' event handler.");
126 schoenebeck 2581 context->onInit = new OnInit($3);
127     $$ = context->onInit;
128     }
129 schoenebeck 2947 | ON RELEASE opt_statements END ON {
130 schoenebeck 2581 if (context->onRelease)
131 schoenebeck 2888 PARSE_ERR(@2, "Redeclaration of 'release' event handler.");
132 schoenebeck 2581 context->onRelease = new OnRelease($3);
133     $$ = context->onRelease;
134     }
135 schoenebeck 2947 | ON CONTROLLER opt_statements END ON {
136 schoenebeck 2581 if (context->onController)
137 schoenebeck 2888 PARSE_ERR(@2, "Redeclaration of 'controller' event handler.");
138 schoenebeck 2581 context->onController = new OnController($3);
139     $$ = context->onController;
140     }
141    
142 schoenebeck 2951 function_declaration:
143     FUNCTION IDENTIFIER opt_statements END FUNCTION {
144     const char* name = $2;
145     if (context->functionProvider->functionByName(name)) {
146     PARSE_ERR(@2, (String("There is already a built-in function with name '") + name + "'.").c_str());
147     } else if (context->userFunctionByName(name)) {
148     PARSE_ERR(@2, (String("There is already a user defined function with name '") + name + "'.").c_str());
149     } else {
150     context->userFnTable[name] = $3;
151     }
152     }
153    
154 schoenebeck 2947 opt_statements:
155 schoenebeck 2911 /* epsilon (empty argument) */ {
156     $$ = new Statements();
157     }
158     | statements {
159     $$ = $1;
160     }
161    
162 schoenebeck 2581 statements:
163     statement {
164     $$ = new Statements();
165     if ($1) {
166     if (!isNoOperation($1)) $$->add($1); // filter out NoOperation statements
167     } else
168 schoenebeck 2888 PARSE_WRN(@1, "Not a statement.");
169 schoenebeck 2581 }
170     | statements statement {
171     $$ = $1;
172     if ($2) {
173     if (!isNoOperation($2)) $$->add($2); // filter out NoOperation statements
174     } else
175 schoenebeck 2888 PARSE_WRN(@2, "Not a statement.");
176 schoenebeck 2581 }
177    
178     statement:
179     functioncall {
180     $$ = $1;
181     }
182 schoenebeck 2951 | userfunctioncall {
183     $$ = $1;
184     }
185 schoenebeck 2581 | DECLARE VARIABLE {
186     const char* name = $2;
187     //printf("declared var '%s'\n", name);
188 schoenebeck 3573 if (context->variableByName(name)) {
189 schoenebeck 2888 PARSE_ERR(@2, (String("Redeclaration of variable '") + name + "'.").c_str());
190 schoenebeck 3573 } else if (name[0] == '@') {
191 schoenebeck 2581 context->vartable[name] = new StringVariable(context);
192 schoenebeck 3573 } else if (name[0] == '~') {
193 schoenebeck 3581 context->vartable[name] = new RealVariable({
194     .ctx = context
195     });
196 schoenebeck 3573 } else if (name[0] == '$') {
197 schoenebeck 3581 context->vartable[name] = new IntVariable({
198     .ctx = context
199     });
200 schoenebeck 3573 } else if (name[0] == '?') {
201     PARSE_ERR(@2, (String("Real number array variable '") + name + "' declaration requires array size.").c_str());
202     } else if (name[0] == '%') {
203     PARSE_ERR(@2, (String("Integer array variable '") + name + "' declaration requires array size.").c_str());
204 schoenebeck 2581 } else {
205 schoenebeck 3573 PARSE_ERR(@2, (String("Variable '") + name + "' declared with unknown type.").c_str());
206 schoenebeck 2581 }
207 schoenebeck 3573 $$ = new NoOperation;
208 schoenebeck 2581 }
209     | DECLARE POLYPHONIC VARIABLE {
210     const char* name = $3;
211     //printf("declared polyphonic var '%s'\n", name);
212 schoenebeck 3573 if (context->variableByName(name)) {
213 schoenebeck 2888 PARSE_ERR(@3, (String("Redeclaration of variable '") + name + "'.").c_str());
214 schoenebeck 3573 } else if (name[0] != '$' && name[0] != '~') {
215     PARSE_ERR(@3, "Polyphonic variables must only be declared either as integer or real number type.");
216     } else if (name[0] == '~') {
217 schoenebeck 3581 context->vartable[name] = new PolyphonicRealVariable({
218     .ctx = context
219     });
220 schoenebeck 2581 } else {
221 schoenebeck 3581 context->vartable[name] = new PolyphonicIntVariable({
222     .ctx = context
223     });
224 schoenebeck 2581 }
225 schoenebeck 3573 $$ = new NoOperation;
226 schoenebeck 2581 }
227     | DECLARE VARIABLE ASSIGNMENT expr {
228     const char* name = $2;
229     //printf("declared assign var '%s'\n", name);
230 schoenebeck 3573 const ExprType_t declType = exprTypeOfVarName(name);
231     if (context->variableByName(name)) {
232 schoenebeck 2888 PARSE_ERR(@2, (String("Redeclaration of variable '") + name + "'.").c_str());
233 schoenebeck 3573 $$ = new NoOperation;
234     } else if ($4->exprType() == STRING_EXPR) {
235     if (name[0] != '@')
236     PARSE_WRN(@2, (String("Variable '") + name + "' declared as " + typeStr(declType) + ", string expression assigned though.").c_str());
237 schoenebeck 2581 StringExprRef expr = $4;
238     if (expr->isConstExpr()) {
239     const String s = expr->evalStr();
240     StringVariableRef var = new StringVariable(context);
241     context->vartable[name] = var;
242     $$ = new Assignment(var, new StringLiteral(s));
243     } else {
244     StringVariableRef var = new StringVariable(context);
245     context->vartable[name] = var;
246     $$ = new Assignment(var, expr);
247     }
248 schoenebeck 3573 } else if ($4->exprType() == REAL_EXPR) {
249     if (name[0] != '~')
250     PARSE_WRN(@2, (String("Variable '") + name + "' declared as " + typeStr(declType) + ", real number expression assigned though.").c_str());
251     RealExprRef expr = $4;
252 schoenebeck 3581 RealVariableRef var = new RealVariable({
253     .ctx = context,
254     .unitType = expr->unitType(),
255     .isFinal = expr->isFinal()
256     });
257 schoenebeck 3573 if (expr->isConstExpr()) {
258 schoenebeck 3581 $$ = new Assignment(var, new RealLiteral({
259     .value = expr->evalReal(),
260     .unitFactor = expr->unitFactor(),
261     .unitType = expr->unitType(),
262     .isFinal = expr->isFinal()
263     }));
264 schoenebeck 3573 } else {
265     $$ = new Assignment(var, expr);
266     }
267     context->vartable[name] = var;
268     } else if ($4->exprType() == INT_EXPR) {
269     if (name[0] != '$')
270     PARSE_WRN(@2, (String("Variable '") + name + "' declared as " + typeStr(declType) + ", integer expression assigned though.").c_str());
271 schoenebeck 2581 IntExprRef expr = $4;
272 schoenebeck 3581 IntVariableRef var = new IntVariable({
273     .ctx = context,
274     .unitType = expr->unitType(),
275     .isFinal = expr->isFinal()
276     });
277 schoenebeck 2581 if (expr->isConstExpr()) {
278 schoenebeck 3581 $$ = new Assignment(var, new IntLiteral({
279     .value = expr->evalInt(),
280     .unitFactor = expr->unitFactor(),
281     .unitType = expr->unitType(),
282     .isFinal = expr->isFinal()
283     }));
284 schoenebeck 2581 } else {
285     $$ = new Assignment(var, expr);
286     }
287 schoenebeck 3561 context->vartable[name] = var;
288 schoenebeck 3573 } else if ($4->exprType() == EMPTY_EXPR) {
289     PARSE_ERR(@4, "Expression does not result in a value.");
290     $$ = new NoOperation;
291     } else if (isArray($4->exprType())) {
292     PARSE_ERR(@2, (String("Variable '") + name + "' declared as scalar type, array expression assigned though.").c_str());
293     $$ = new NoOperation;
294 schoenebeck 2581 }
295     }
296     | DECLARE VARIABLE '[' expr ']' {
297     //printf("declare array without args\n");
298     const char* name = $2;
299     if (!$4->isConstExpr()) {
300 schoenebeck 2888 PARSE_ERR(@4, (String("Array variable '") + name + "' must be declared with constant array size.").c_str());
301 schoenebeck 2581 } else if ($4->exprType() != INT_EXPR) {
302 schoenebeck 2888 PARSE_ERR(@4, (String("Size of array variable '") + name + "' declared with non integer expression.").c_str());
303 schoenebeck 2581 } else if (context->variableByName(name)) {
304 schoenebeck 2888 PARSE_ERR(@2, (String("Redeclaration of variable '") + name + "'.").c_str());
305 schoenebeck 2581 } else {
306 schoenebeck 3581 IntExprRef sizeExpr = $4;
307     if (sizeExpr->unitType() || sizeExpr->hasUnitFactorNow()) {
308 schoenebeck 3561 PARSE_ERR(@4, "Units are not allowed as array size.");
309 schoenebeck 2581 } else {
310 schoenebeck 3581 if (sizeExpr->isFinal())
311 schoenebeck 3561 PARSE_WRN(@4, "Final operator '!' is meaningless here.");
312 schoenebeck 3581 vmint size = sizeExpr->evalInt();
313 schoenebeck 3561 if (size <= 0) {
314     PARSE_ERR(@4, (String("Array variable '") + name + "' declared with array size " + ToString(size) + ".").c_str());
315     } else {
316 schoenebeck 3573 if (name[0] == '?') {
317     context->vartable[name] = new RealArrayVariable(context, size);
318     } else if (name[0] == '%') {
319     context->vartable[name] = new IntArrayVariable(context, size);
320     } else {
321     PARSE_ERR(@2, (String("Variable '") + name + "' declared as unknown array type: use either '%' or '?' instead of '" + String(name).substr(0,1) + "'.").c_str());
322     }
323 schoenebeck 3561 }
324 schoenebeck 2581 }
325     }
326 schoenebeck 3573 $$ = new NoOperation;
327 schoenebeck 2581 }
328     | DECLARE VARIABLE '[' expr ']' ASSIGNMENT '(' args ')' {
329     const char* name = $2;
330     if (!$4->isConstExpr()) {
331 schoenebeck 2888 PARSE_ERR(@4, (String("Array variable '") + name + "' must be declared with constant array size.").c_str());
332 schoenebeck 2581 } else if ($4->exprType() != INT_EXPR) {
333 schoenebeck 2888 PARSE_ERR(@4, (String("Size of array variable '") + name + "' declared with non integer expression.").c_str());
334 schoenebeck 2581 } else if (context->variableByName(name)) {
335 schoenebeck 2888 PARSE_ERR(@2, (String("Redeclaration of variable '") + name + "'.").c_str());
336 schoenebeck 2581 } else {
337     IntExprRef sizeExpr = $4;
338     ArgsRef args = $8;
339 schoenebeck 3557 vmint size = sizeExpr->evalInt();
340 schoenebeck 2581 if (size <= 0) {
341 schoenebeck 2888 PARSE_ERR(@4, (String("Array variable '") + name + "' must be declared with positive array size.").c_str());
342 schoenebeck 2581 } else if (args->argsCount() > size) {
343 schoenebeck 3257 PARSE_ERR(@8, (String("Array variable '") + name +
344 schoenebeck 2581 "' was declared with size " + ToString(size) +
345     " but " + ToString(args->argsCount()) +
346     " values were assigned." ).c_str());
347 schoenebeck 3581 } else if (sizeExpr->unitType() || sizeExpr->hasUnitFactorNow()) {
348 schoenebeck 3561 PARSE_ERR(@4, "Units are not allowed as array size.");
349 schoenebeck 2581 } else {
350 schoenebeck 3561 if (sizeExpr->isFinal())
351     PARSE_WRN(@4, "Final operator '!' is meaningless here.");
352 schoenebeck 3573 ExprType_t declType = EMPTY_EXPR;
353     if (name[0] == '%') {
354     declType = INT_EXPR;
355     } else if (name[0] == '?') {
356     declType = REAL_EXPR;
357     } else if (name[0] == '$') {
358     PARSE_ERR(@2, (String("Variable '") + name + "' declaration ambiguous: Use '%' as name prefix for integer arrays instead of '$'.").c_str());
359     } else if (name[0] == '~') {
360     PARSE_ERR(@2, (String("Variable '") + name + "' declaration ambiguous: Use '?' as name prefix for real number arrays instead of '~'.").c_str());
361     } else {
362     PARSE_ERR(@2, (String("Variable '") + name + "' declared as unknown array type: use either '%' or '?' instead of '" + String(name).substr(0,1) + "'.").c_str());
363     }
364 schoenebeck 2581 bool argsOK = true;
365 schoenebeck 3573 if (declType == EMPTY_EXPR) {
366     argsOK = false;
367     } else {
368     for (vmint i = 0; i < args->argsCount(); ++i) {
369     if (args->arg(i)->exprType() != declType) {
370     PARSE_ERR(
371     @8,
372     (String("Array variable '") + name +
373     "' declared with invalid assignment values. Assigned element " +
374     ToString(i+1) + " is not an " + typeStr(declType) + " expression.").c_str()
375     );
376     argsOK = false;
377     break;
378 schoenebeck 3582 } else if (args->arg(i)->asNumber()->unitType()) {
379 schoenebeck 3573 PARSE_ERR(
380     @8,
381     (String("Array variable '") + name +
382     "' declared with invalid assignment values. Assigned element " +
383 schoenebeck 3581 ToString(i+1) + " contains a unit type, only metric prefixes are allowed for arrays.").c_str()
384 schoenebeck 3573 );
385     argsOK = false;
386     break;
387 schoenebeck 3582 } else if (args->arg(i)->asNumber()->isFinal()) {
388 schoenebeck 3581 PARSE_ERR(
389     @8,
390     (String("Array variable '") + name +
391     "' declared with invalid assignment values. Assigned element " +
392     ToString(i+1) + " declared as 'final' value.").c_str()
393     );
394     argsOK = false;
395     break;
396 schoenebeck 3573 }
397 schoenebeck 2581 }
398     }
399 schoenebeck 3013 if (argsOK) {
400 schoenebeck 3573 if (declType == REAL_EXPR)
401     context->vartable[name] = new RealArrayVariable(context, size, args);
402     else
403     context->vartable[name] = new IntArrayVariable(context, size, args);
404     }
405 schoenebeck 2581 }
406     }
407 schoenebeck 3573 $$ = new NoOperation;
408 schoenebeck 2581 }
409 schoenebeck 3257 | DECLARE CONST_ VARIABLE '[' expr ']' ASSIGNMENT '(' args ')' {
410     const char* name = $3;
411     if (!$5->isConstExpr()) {
412     PARSE_ERR(@5, (String("Array variable '") + name + "' must be declared with constant array size.").c_str());
413     } else if ($5->exprType() != INT_EXPR) {
414     PARSE_ERR(@5, (String("Size of array variable '") + name + "' declared with non integer expression.").c_str());
415     } else if (context->variableByName(name)) {
416     PARSE_ERR(@3, (String("Redeclaration of variable '") + name + "'.").c_str());
417     } else {
418     IntExprRef sizeExpr = $5;
419     ArgsRef args = $9;
420 schoenebeck 3557 vmint size = sizeExpr->evalInt();
421 schoenebeck 3257 if (size <= 0) {
422     PARSE_ERR(@5, (String("Array variable '") + name + "' must be declared with positive array size.").c_str());
423     } else if (args->argsCount() > size) {
424     PARSE_ERR(@9, (String("Array variable '") + name +
425     "' was declared with size " + ToString(size) +
426     " but " + ToString(args->argsCount()) +
427     " values were assigned." ).c_str());
428 schoenebeck 3581 } else if (sizeExpr->unitType() || sizeExpr->hasUnitFactorNow()) {
429 schoenebeck 3561 PARSE_ERR(@5, "Units are not allowed as array size.");
430 schoenebeck 3257 } else {
431 schoenebeck 3561 if (sizeExpr->isFinal())
432     PARSE_WRN(@5, "Final operator '!' is meaningless here.");
433 schoenebeck 3573 ExprType_t declType = EMPTY_EXPR;
434     if (name[0] == '%') {
435     declType = INT_EXPR;
436     } else if (name[0] == '?') {
437     declType = REAL_EXPR;
438     } else if (name[0] == '$') {
439     PARSE_ERR(@3, (String("Variable '") + name + "' declaration ambiguous: Use '%' as name prefix for integer arrays instead of '$'.").c_str());
440     } else if (name[0] == '~') {
441     PARSE_ERR(@3, (String("Variable '") + name + "' declaration ambiguous: Use '?' as name prefix for real number arrays instead of '~'.").c_str());
442     } else {
443     PARSE_ERR(@3, (String("Variable '") + name + "' declared as unknown array type: use either '%' or '?' instead of '" + String(name).substr(0,1) + "'.").c_str());
444     }
445 schoenebeck 3257 bool argsOK = true;
446 schoenebeck 3573 if (declType == EMPTY_EXPR) {
447     argsOK = false;
448     } else {
449     for (vmint i = 0; i < args->argsCount(); ++i) {
450     if (args->arg(i)->exprType() != declType) {
451     PARSE_ERR(
452     @9,
453 schoenebeck 3581 (String("const array variable '") + name +
454 schoenebeck 3573 "' declared with invalid assignment values. Assigned element " +
455     ToString(i+1) + " is not an " + typeStr(declType) + " expression.").c_str()
456     );
457     argsOK = false;
458     break;
459     }
460     if (!args->arg(i)->isConstExpr()) {
461     PARSE_ERR(
462     @9,
463     (String("const array variable '") + name +
464     "' must be defined with const values. Assigned element " +
465     ToString(i+1) + " is not a const expression though.").c_str()
466     );
467     argsOK = false;
468     break;
469 schoenebeck 3582 } else if (args->arg(i)->asNumber()->unitType()) {
470 schoenebeck 3573 PARSE_ERR(
471     @9,
472     (String("const array variable '") + name +
473     "' declared with invalid assignment values. Assigned element " +
474 schoenebeck 3581 ToString(i+1) + " contains a unit type, only metric prefixes are allowed for arrays.").c_str()
475 schoenebeck 3573 );
476     argsOK = false;
477     break;
478 schoenebeck 3582 } else if (args->arg(i)->asNumber()->isFinal()) {
479 schoenebeck 3581 PARSE_ERR(
480     @9,
481     (String("const array variable '") + name +
482     "' declared with invalid assignment values. Assigned element " +
483     ToString(i+1) + " declared as 'final' value.").c_str()
484     );
485     argsOK = false;
486     break;
487 schoenebeck 3573 }
488 schoenebeck 3257 }
489     }
490     if (argsOK) {
491 schoenebeck 3573 if (declType == REAL_EXPR)
492     context->vartable[name] = new RealArrayVariable(context, size, args, true);
493     else
494     context->vartable[name] = new IntArrayVariable(context, size, args, true);
495     }
496 schoenebeck 3257 }
497     }
498 schoenebeck 3573 $$ = new NoOperation;
499 schoenebeck 3257 }
500 schoenebeck 2585 | DECLARE CONST_ VARIABLE ASSIGNMENT expr {
501 schoenebeck 2581 const char* name = $3;
502 schoenebeck 3573 const ExprType_t declType = exprTypeOfVarName(name);
503 schoenebeck 2581 if ($5->exprType() == STRING_EXPR) {
504 schoenebeck 3573 if (name[0] != '@')
505     PARSE_WRN(@5, (String("Variable '") + name + "' declared as " + typeStr(declType) + ", string expression assigned though.").c_str());
506 schoenebeck 2581 String s;
507     StringExprRef expr = $5;
508     if (expr->isConstExpr())
509     s = expr->evalStr();
510     else
511 schoenebeck 2888 PARSE_ERR(@5, (String("Assignment to const string variable '") + name + "' requires const expression.").c_str());
512 schoenebeck 2581 ConstStringVariableRef var = new ConstStringVariable(context, s);
513     context->vartable[name] = var;
514     //$$ = new Assignment(var, new StringLiteral(s));
515 schoenebeck 3573 } else if ($5->exprType() == REAL_EXPR) {
516     if (name[0] != '~')
517     PARSE_WRN(@5, (String("Variable '") + name + "' declared as " + typeStr(declType) + ", real number expression assigned though.").c_str());
518     RealExprRef expr = $5;
519 schoenebeck 3581 if (!expr->isConstExpr()) {
520 schoenebeck 3573 PARSE_ERR(@5, (String("Assignment to const real number variable '") + name + "' requires const expression.").c_str());
521 schoenebeck 3581 }
522 schoenebeck 3595 ConstRealVariableRef var = new ConstRealVariable(
523     #if defined(__GNUC__) && !defined(__clang__)
524     (const RealVarDef&) // GCC 8.x requires this cast here (looks like a GCC bug to me); cast would cause an error with clang though
525     #endif
526     {
527 schoenebeck 3581 .value = (expr->isConstExpr()) ? expr->evalReal() : vmfloat(0),
528     .unitFactor = (expr->isConstExpr()) ? expr->unitFactor() : VM_NO_FACTOR,
529     .unitType = expr->unitType(),
530     .isFinal = expr->isFinal()
531     });
532 schoenebeck 3573 context->vartable[name] = var;
533     //$$ = new Assignment(var, new IntLiteral(i));
534     } else if ($5->exprType() == INT_EXPR) {
535     if (name[0] != '$')
536     PARSE_WRN(@5, (String("Variable '") + name + "' declared as " + typeStr(declType) + ", integer expression assigned though.").c_str());
537 schoenebeck 2581 IntExprRef expr = $5;
538 schoenebeck 3581 if (!expr->isConstExpr()) {
539 schoenebeck 2888 PARSE_ERR(@5, (String("Assignment to const integer variable '") + name + "' requires const expression.").c_str());
540 schoenebeck 3581 }
541 schoenebeck 3595 ConstIntVariableRef var = new ConstIntVariable(
542     #if defined(__GNUC__) && !defined(__clang__)
543     (const IntVarDef&) // GCC 8.x requires this cast here (looks like a GCC bug to me); cast would cause an error with clang though
544     #endif
545     {
546 schoenebeck 3581 .value = (expr->isConstExpr()) ? expr->evalInt() : 0,
547     .unitFactor = (expr->isConstExpr()) ? expr->unitFactor() : VM_NO_FACTOR,
548     .unitType = expr->unitType(),
549     .isFinal = expr->isFinal()
550     });
551 schoenebeck 2581 context->vartable[name] = var;
552     //$$ = new Assignment(var, new IntLiteral(i));
553 schoenebeck 3573 } else if ($5->exprType() == EMPTY_EXPR) {
554     PARSE_ERR(@5, "Expression does not result in a value.");
555     } else if (isArray($5->exprType())) {
556     PARSE_ERR(@5, (String("Variable '") + name + "' declared as scalar type, array expression assigned though.").c_str());
557 schoenebeck 2581 }
558 schoenebeck 3573 $$ = new NoOperation();
559 schoenebeck 2581 }
560     | assignment {
561     $$ = $1;
562     }
563 schoenebeck 2947 | WHILE '(' expr ')' opt_statements END WHILE {
564 schoenebeck 2581 if ($3->exprType() == INT_EXPR) {
565 schoenebeck 3561 IntExprRef expr = $3;
566 schoenebeck 3582 if (expr->asNumber()->unitType() ||
567     expr->asNumber()->hasUnitFactorEver())
568 schoenebeck 3581 PARSE_WRN(@3, "Condition for 'while' loops contains a unit.");
569     else if (expr->isFinal() && expr->isConstExpr())
570 schoenebeck 3561 PARSE_WRN(@3, "Final operator '!' is meaningless here.");
571     $$ = new While(expr, $5);
572 schoenebeck 2581 } else {
573 schoenebeck 2888 PARSE_ERR(@3, "Condition for 'while' loops must be integer expression.");
574 schoenebeck 3581 $$ = new While(new IntLiteral({ .value = 0 }), $5);
575 schoenebeck 2581 }
576     }
577 schoenebeck 3260 | SYNCHRONIZED opt_statements END SYNCHRONIZED {
578     $$ = new SyncBlock($2);
579     }
580 schoenebeck 2947 | IF '(' expr ')' opt_statements ELSE opt_statements END IF {
581 schoenebeck 3561 if ($3->exprType() == INT_EXPR) {
582     IntExprRef expr = $3;
583 schoenebeck 3582 if (expr->asNumber()->unitType() ||
584     expr->asNumber()->hasUnitFactorEver())
585 schoenebeck 3581 PARSE_WRN(@3, "Condition for 'if' contains a unit.");
586     else if (expr->isFinal() && expr->isConstExpr())
587 schoenebeck 3561 PARSE_WRN(@3, "Final operator '!' is meaningless here.");
588     $$ = new If($3, $5, $7);
589     } else {
590     PARSE_ERR(@3, "Condition for 'if' must be integer expression.");
591 schoenebeck 3581 $$ = new If(new IntLiteral({ .value = 0 }), $5, $7);
592 schoenebeck 3561 }
593 schoenebeck 2581 }
594 schoenebeck 2947 | IF '(' expr ')' opt_statements END IF {
595 schoenebeck 3561 if ($3->exprType() == INT_EXPR) {
596     IntExprRef expr = $3;
597 schoenebeck 3582 if (expr->asNumber()->unitType() ||
598     expr->asNumber()->hasUnitFactorEver())
599 schoenebeck 3581 PARSE_WRN(@3, "Condition for 'if' contains a unit.");
600     else if (expr->isFinal() && expr->isConstExpr())
601 schoenebeck 3561 PARSE_WRN(@3, "Final operator '!' is meaningless here.");
602     $$ = new If($3, $5);
603     } else {
604     PARSE_ERR(@3, "Condition for 'if' must be integer expression.");
605 schoenebeck 3581 $$ = new If(new IntLiteral({ .value = 0 }), $5);
606 schoenebeck 3561 }
607 schoenebeck 2581 }
608     | SELECT expr caseclauses END SELECT {
609     if ($2->exprType() == INT_EXPR) {
610 schoenebeck 3561 IntExprRef expr = $2;
611 schoenebeck 3581 if (expr->unitType() || expr->hasUnitFactorEver()) {
612 schoenebeck 3561 PARSE_ERR(@2, "Units are not allowed here.");
613 schoenebeck 3581 $$ = new SelectCase(new IntLiteral({ .value = 0 }), $3);
614 schoenebeck 3561 } else {
615     if (expr->isFinal() && expr->isConstExpr())
616     PARSE_WRN(@2, "Final operator '!' is meaningless here.");
617     $$ = new SelectCase(expr, $3);
618     }
619 schoenebeck 2581 } else {
620 schoenebeck 2888 PARSE_ERR(@2, "Statement 'select' can only by applied to integer expressions.");
621 schoenebeck 3581 $$ = new SelectCase(new IntLiteral({ .value = 0 }), $3);
622 schoenebeck 2581 }
623     }
624    
625     caseclauses:
626     caseclause {
627     $$ = CaseBranches();
628     $$.push_back($1);
629     }
630     | caseclauses caseclause {
631     $$ = $1;
632     $$.push_back($2);
633     }
634    
635     caseclause:
636 schoenebeck 2947 CASE INTEGER opt_statements {
637 schoenebeck 2581 $$ = CaseBranch();
638 schoenebeck 3581 $$.from = new IntLiteral({ .value = $2 });
639 schoenebeck 2581 $$.statements = $3;
640     }
641 schoenebeck 2947 | CASE INTEGER TO INTEGER opt_statements {
642 schoenebeck 2581 $$ = CaseBranch();
643 schoenebeck 3581 $$.from = new IntLiteral({ .value = $2 });
644     $$.to = new IntLiteral({ .value = $4 });
645 schoenebeck 2581 $$.statements = $5;
646     }
647    
648 schoenebeck 2951 userfunctioncall:
649     CALL IDENTIFIER {
650     const char* name = $2;
651     StatementsRef fn = context->userFunctionByName(name);
652     if (context->functionProvider->functionByName(name)) {
653     PARSE_ERR(@1, (String("Keyword 'call' must only be used for user defined functions, not for any built-in function like '") + name + "'.").c_str());
654     $$ = StatementsRef();
655     } else if (!fn) {
656     PARSE_ERR(@2, (String("No user defined function with name '") + name + "'.").c_str());
657     $$ = StatementsRef();
658     } else {
659     $$ = fn;
660     }
661     }
662    
663 schoenebeck 2581 functioncall:
664     IDENTIFIER '(' args ')' {
665     const char* name = $1;
666     //printf("function call of '%s' with args\n", name);
667     ArgsRef args = $3;
668     VMFunction* fn = context->functionProvider->functionByName(name);
669 schoenebeck 2951 if (context->userFunctionByName(name)) {
670     PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
671     $$ = new FunctionCall(name, args, NULL);
672     } else if (!fn) {
673 schoenebeck 2888 PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());
674 schoenebeck 2581 $$ = new FunctionCall(name, args, NULL);
675 schoenebeck 3311 } else if (context->functionProvider->isFunctionDisabled(fn,context)) {
676     PARSE_DROP(@$);
677     $$ = new NoFunctionCall;
678 schoenebeck 2581 } else if (args->argsCount() < fn->minRequiredArgs()) {
679 schoenebeck 2888 PARSE_ERR(@3, (String("Built-in function '") + name + "' requires at least " + ToString(fn->minRequiredArgs()) + " arguments.").c_str());
680 schoenebeck 2581 $$ = new FunctionCall(name, args, NULL);
681     } else if (args->argsCount() > fn->maxAllowedArgs()) {
682 schoenebeck 2888 PARSE_ERR(@3, (String("Built-in function '") + name + "' accepts max. " + ToString(fn->maxAllowedArgs()) + " arguments.").c_str());
683 schoenebeck 2581 $$ = new FunctionCall(name, args, NULL);
684     } else {
685     bool argsOK = true;
686 schoenebeck 3557 for (vmint i = 0; i < args->argsCount(); ++i) {
687 schoenebeck 3585 if (!fn->acceptsArgType(i, args->arg(i)->exprType())) {
688     PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' expects " + acceptedArgTypesStr(fn, i) + " type, but type " + typeStr(args->arg(i)->exprType()) + " was given instead.").c_str());
689 schoenebeck 2581 argsOK = false;
690     break;
691 schoenebeck 2945 } else if (fn->modifiesArg(i) && !args->arg(i)->isModifyable()) {
692     PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' expects an assignable variable.").c_str());
693     argsOK = false;
694     break;
695 schoenebeck 3582 } else if (isNumber(args->arg(i)->exprType()) && !fn->acceptsArgUnitType(i, args->arg(i)->asNumber()->unitType())) {
696     if (args->arg(i)->asNumber()->unitType())
697     PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' does not expect unit " + unitTypeStr(args->arg(i)->asNumber()->unitType()) + ".").c_str());
698 schoenebeck 3561 else
699     PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' expects a unit.").c_str());
700     argsOK = false;
701     break;
702 schoenebeck 3582 } else if (isNumber(args->arg(i)->exprType()) && args->arg(i)->asNumber()->hasUnitFactorEver() && !fn->acceptsArgUnitPrefix(i, args->arg(i)->asNumber()->unitType())) {
703     if (args->arg(i)->asNumber()->unitType())
704     PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' does not expect a unit prefix for unit" + unitTypeStr(args->arg(i)->asNumber()->unitType()) + ".").c_str());
705 schoenebeck 3564 else
706     PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' does not expect a unit prefix.").c_str());
707 schoenebeck 3561 argsOK = false;
708     break;
709 schoenebeck 3582 } else if (!fn->acceptsArgFinal(i) && isNumber(args->arg(i)->exprType()) && args->arg(i)->asNumber()->isFinal()) {
710 schoenebeck 3561 PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' does not expect a \"final\" value.").c_str());
711     argsOK = false;
712     break;
713 schoenebeck 2581 }
714     }
715 schoenebeck 3581 if (argsOK) {
716     // perform built-in function's own, custom arguments checks (if any)
717     fn->checkArgs(&*args, [&](String err) {
718     PARSE_ERR(@3, (String("Built-in function '") + name + "()': " + err).c_str());
719     argsOK = false;
720     }, [&](String wrn) {
721     PARSE_WRN(@3, (String("Built-in function '") + name + "()': " + wrn).c_str());
722     });
723     }
724 schoenebeck 2581 $$ = new FunctionCall(name, args, argsOK ? fn : NULL);
725     }
726     }
727     | IDENTIFIER '(' ')' {
728     const char* name = $1;
729     //printf("function call of '%s' (with empty args)\n", name);
730     ArgsRef args = new Args;
731     VMFunction* fn = context->functionProvider->functionByName(name);
732 schoenebeck 2951 if (context->userFunctionByName(name)) {
733     PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
734     $$ = new FunctionCall(name, args, NULL);
735     } else if (!fn) {
736 schoenebeck 2888 PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());
737 schoenebeck 2581 $$ = new FunctionCall(name, args, NULL);
738 schoenebeck 3311 } else if (context->functionProvider->isFunctionDisabled(fn,context)) {
739     PARSE_DROP(@$);
740     $$ = new NoFunctionCall;
741 schoenebeck 2581 } else if (fn->minRequiredArgs() > 0) {
742 schoenebeck 2888 PARSE_ERR(@3, (String("Built-in function '") + name + "' requires at least " + ToString(fn->minRequiredArgs()) + " arguments.").c_str());
743 schoenebeck 2581 $$ = new FunctionCall(name, args, NULL);
744     } else {
745     $$ = new FunctionCall(name, args, fn);
746     }
747     }
748     | IDENTIFIER {
749     const char* name = $1;
750     //printf("function call of '%s' (without args)\n", name);
751     ArgsRef args = new Args;
752     VMFunction* fn = context->functionProvider->functionByName(name);
753 schoenebeck 2951 if (context->userFunctionByName(name)) {
754     PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
755     $$ = new FunctionCall(name, args, NULL);
756     } else if (!fn) {
757 schoenebeck 2888 PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());
758 schoenebeck 2581 $$ = new FunctionCall(name, args, NULL);
759 schoenebeck 3311 } else if (context->functionProvider->isFunctionDisabled(fn,context)) {
760     PARSE_DROP(@$);
761     $$ = new NoFunctionCall;
762 schoenebeck 2581 } else if (fn->minRequiredArgs() > 0) {
763 schoenebeck 2888 PARSE_ERR(@1, (String("Built-in function '") + name + "' requires at least " + ToString(fn->minRequiredArgs()) + " arguments.").c_str());
764 schoenebeck 2581 $$ = new FunctionCall(name, args, NULL);
765     } else {
766     $$ = new FunctionCall(name, args, fn);
767     }
768     }
769    
770     args:
771     arg {
772     $$ = new Args();
773     $$->add($1);
774     }
775     | args ',' arg {
776     $$ = $1;
777     $$->add($3);
778     }
779    
780     arg:
781     expr
782    
783     assignment:
784     VARIABLE ASSIGNMENT expr {
785     //printf("variable lookup with name '%s' as assignment expr\n", $1);
786     const char* name = $1;
787     VariableRef var = context->variableByName(name);
788     if (!var)
789 schoenebeck 2888 PARSE_ERR(@1, (String("Variable assignment: No variable declared with name '") + name + "'.").c_str());
790 schoenebeck 2581 else if (var->isConstExpr())
791 schoenebeck 2888 PARSE_ERR(@2, (String("Variable assignment: Cannot modify const variable '") + name + "'.").c_str());
792 schoenebeck 2942 else if (!var->isAssignable())
793     PARSE_ERR(@2, (String("Variable assignment: Variable '") + name + "' is not assignable.").c_str());
794 schoenebeck 2581 else if (var->exprType() != $3->exprType())
795 schoenebeck 2888 PARSE_ERR(@3, (String("Variable assignment: Variable '") + name + "' is of type " + typeStr(var->exprType()) + ", assignment is of type " + typeStr($3->exprType()) + " though.").c_str());
796 schoenebeck 3582 else if (isNumber(var->exprType())) {
797     NumberVariableRef numberVar = var;
798     NumberExprRef expr = $3;
799 schoenebeck 3573 if (numberVar->unitType() != expr->unitType())
800     PARSE_ERR(@3, (String("Variable assignment: Variable '") + name + "' has unit type " + unitTypeStr(numberVar->unitType()) + ", assignment has unit type " + unitTypeStr(expr->unitType()) + " though.").c_str());
801     else if (numberVar->isFinal() != expr->isFinal())
802     PARSE_ERR(@3, (String("Variable assignment: Variable '") + name + "' was declared as " + String(numberVar->isFinal() ? "final" : "not final") + ", assignment is " + String(expr->isFinal() ? "final" : "not final") + " though.").c_str());
803 schoenebeck 3561 }
804 schoenebeck 2581 $$ = new Assignment(var, $3);
805     }
806     | VARIABLE '[' expr ']' ASSIGNMENT expr {
807     const char* name = $1;
808     VariableRef var = context->variableByName(name);
809     if (!var)
810 schoenebeck 2888 PARSE_ERR(@1, (String("No variable declared with name '") + name + "'.").c_str());
811 schoenebeck 3573 else if (!isArray(var->exprType()))
812 schoenebeck 2888 PARSE_ERR(@2, (String("Variable '") + name + "' is not an array variable.").c_str());
813 schoenebeck 3253 else if (var->isConstExpr())
814     PARSE_ERR(@5, (String("Variable assignment: Cannot modify const array variable '") + name + "'.").c_str());
815     else if (!var->isAssignable())
816     PARSE_ERR(@5, (String("Variable assignment: Array variable '") + name + "' is not assignable.").c_str());
817 schoenebeck 2581 else if ($3->exprType() != INT_EXPR)
818 schoenebeck 2888 PARSE_ERR(@3, (String("Array variable '") + name + "' accessed with non integer expression.").c_str());
819 schoenebeck 3561 else if ($3->asInt()->unitType())
820     PARSE_ERR(@3, "Unit types are not allowed as array index.");
821 schoenebeck 3586 else if ($6->exprType() != scalarTypeOfArray(var->exprType()))
822 schoenebeck 3573 PARSE_ERR(@5, (String("Variable '") + name + "' was declared as " + typeStr(var->exprType()) + ", assigned expression is " + typeStr($6->exprType()) + " though.").c_str());
823 schoenebeck 3582 else if ($6->asNumber()->unitType())
824 schoenebeck 3561 PARSE_ERR(@6, "Unit types are not allowed for array variables.");
825 schoenebeck 3582 else if ($6->asNumber()->isFinal())
826 schoenebeck 3561 PARSE_ERR(@6, "Final operator '!' not allowed for array variables.");
827 schoenebeck 3573 else if ($3->isConstExpr() && $3->asInt()->evalInt() >= ((ArrayExprRef)var)->arraySize())
828 schoenebeck 3257 PARSE_WRN(@3, (String("Index ") + ToString($3->asInt()->evalInt()) +
829     " exceeds size of array variable '" + name +
830     "' which was declared with size " +
831 schoenebeck 3573 ToString(((ArrayExprRef)var)->arraySize()) + ".").c_str());
832 schoenebeck 3561 else if ($3->asInt()->isFinal())
833     PARSE_WRN(@3, "Final operator '!' is meaningless here.");
834 schoenebeck 3573 if (var->exprType() == INT_ARR_EXPR) {
835     IntArrayElementRef element = new IntArrayElement(var, $3);
836     $$ = new Assignment(element, $6);
837     } else if (var->exprType() == REAL_ARR_EXPR) {
838     RealArrayElementRef element = new RealArrayElement(var, $3);
839     $$ = new Assignment(element, $6);
840     } else {
841 schoenebeck 3581 $$ = new NoOperation; // actually not possible to ever get here
842 schoenebeck 3573 }
843 schoenebeck 2581 }
844    
845     unary_expr:
846     INTEGER {
847 schoenebeck 3581 $$ = new IntLiteral({ .value = $1 });
848 schoenebeck 2581 }
849 schoenebeck 3573 | REAL {
850 schoenebeck 3581 $$ = new RealLiteral({ .value = $1 });
851 schoenebeck 3573 }
852 schoenebeck 3561 | INTEGER_UNIT {
853 schoenebeck 3581 IntLiteralRef literal = new IntLiteral({
854     .value = $1.iValue,
855     .unitFactor = VMUnit::unitFactor($1.prefix),
856     .unitType = $1.unit
857     });
858 schoenebeck 3561 $$ = literal;
859     }
860 schoenebeck 3573 | REAL_UNIT {
861 schoenebeck 3581 RealLiteralRef literal = new RealLiteral({
862     .value = $1.fValue,
863     .unitFactor = VMUnit::unitFactor($1.prefix),
864     .unitType = $1.unit
865     });
866 schoenebeck 3573 $$ = literal;
867     }
868 schoenebeck 2581 | STRING {
869     $$ = new StringLiteral($1);
870     }
871     | VARIABLE {
872     //printf("variable lookup with name '%s' as unary expr\n", $1);
873     VariableRef var = context->variableByName($1);
874     if (var)
875     $$ = var;
876     else {
877 schoenebeck 2888 PARSE_ERR(@1, (String("No variable declared with name '") + $1 + "'.").c_str());
878 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
879 schoenebeck 2581 }
880     }
881     | VARIABLE '[' expr ']' {
882     const char* name = $1;
883     VariableRef var = context->variableByName(name);
884     if (!var) {
885 schoenebeck 2888 PARSE_ERR(@1, (String("No variable declared with name '") + name + "'.").c_str());
886 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
887 schoenebeck 3573 } else if (!isArray(var->exprType())) {
888 schoenebeck 2888 PARSE_ERR(@2, (String("Variable '") + name + "' is not an array variable.").c_str());
889 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
890 schoenebeck 2581 } else if ($3->exprType() != INT_EXPR) {
891 schoenebeck 2888 PARSE_ERR(@3, (String("Array variable '") + name + "' accessed with non integer expression.").c_str());
892 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
893     } else if ($3->asInt()->unitType() || $3->asInt()->hasUnitFactorEver()) {
894 schoenebeck 3561 PARSE_ERR(@3, "Units are not allowed as array index.");
895 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
896 schoenebeck 2581 } else {
897 schoenebeck 3573 if ($3->isConstExpr() && $3->asInt()->evalInt() >= ((ArrayExprRef)var)->arraySize())
898 schoenebeck 3257 PARSE_WRN(@3, (String("Index ") + ToString($3->asInt()->evalInt()) +
899     " exceeds size of array variable '" + name +
900     "' which was declared with size " +
901 schoenebeck 3573 ToString(((ArrayExprRef)var)->arraySize()) + ".").c_str());
902 schoenebeck 3561 else if ($3->asInt()->isFinal())
903     PARSE_WRN(@3, "Final operator '!' is meaningless here.");
904 schoenebeck 3573 if (var->exprType() == REAL_ARR_EXPR) {
905     $$ = new RealArrayElement(var, $3);
906     } else {
907     $$ = new IntArrayElement(var, $3);
908     }
909 schoenebeck 2581 }
910     }
911     | '(' expr ')' {
912     $$ = $2;
913     }
914     | functioncall {
915     $$ = $1;
916     }
917 schoenebeck 3592 | '+' unary_expr {
918     $$ = $2;
919     }
920 schoenebeck 2581 | '-' unary_expr {
921     $$ = new Neg($2);
922     }
923 schoenebeck 2935 | BITWISE_NOT unary_expr {
924     if ($2->exprType() != INT_EXPR) {
925     PARSE_ERR(@2, (String("Right operand of bitwise operator '.not.' must be an integer expression, is ") + typeStr($2->exprType()) + " though.").c_str());
926 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
927     } else if ($2->asInt()->unitType() || $2->asInt()->hasUnitFactorEver()) {
928 schoenebeck 3561 PARSE_ERR(@2, "Units are not allowed for operands of bitwise operations.");
929 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
930 schoenebeck 2935 } else {
931     $$ = new BitwiseNot($2);
932     }
933     }
934 schoenebeck 2581 | NOT unary_expr {
935     if ($2->exprType() != INT_EXPR) {
936 schoenebeck 2888 PARSE_ERR(@2, (String("Right operand of operator 'not' must be an integer expression, is ") + typeStr($2->exprType()) + " though.").c_str());
937 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
938     } else if ($2->asInt()->unitType() || $2->asInt()->hasUnitFactorEver()) {
939 schoenebeck 3561 PARSE_ERR(@2, "Units are not allowed for operands of logical operations.");
940 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
941 schoenebeck 2581 } else {
942     $$ = new Not($2);
943     }
944     }
945 schoenebeck 3561 | '!' unary_expr {
946 schoenebeck 3582 if (!isNumber($2->exprType())) {
947 schoenebeck 3573 PARSE_ERR(@2, (String("Right operand of \"final\" operator '!' must be a scalar number expression, is ") + typeStr($2->exprType()) + " though.").c_str());
948 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
949 schoenebeck 3561 } else {
950     $$ = new Final($2);
951     }
952     }
953 schoenebeck 2581
954     expr:
955     concat_expr
956    
957     concat_expr:
958 schoenebeck 2935 logical_or_expr
959     | concat_expr '&' logical_or_expr {
960 schoenebeck 2581 ExpressionRef lhs = $1;
961     ExpressionRef rhs = $3;
962     if (lhs->isConstExpr() && rhs->isConstExpr()) {
963     $$ = new StringLiteral(
964     lhs->evalCastToStr() + rhs->evalCastToStr()
965     );
966     } else {
967     $$ = new ConcatString(lhs, rhs);
968     }
969     }
970    
971 schoenebeck 2935 logical_or_expr:
972     logical_and_expr
973     | logical_or_expr OR logical_and_expr {
974 schoenebeck 2581 ExpressionRef lhs = $1;
975     ExpressionRef rhs = $3;
976     if (lhs->exprType() != INT_EXPR) {
977 schoenebeck 2888 PARSE_ERR(@1, (String("Left operand of operator 'or' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
978 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
979 schoenebeck 2581 } else if (rhs->exprType() != INT_EXPR) {
980 schoenebeck 2888 PARSE_ERR(@3, (String("Right operand of operator 'or' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
981 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
982     } else if (lhs->asInt()->unitType() || lhs->asInt()->hasUnitFactorEver()) {
983 schoenebeck 3561 PARSE_ERR(@1, "Units are not allowed for operands of logical operations.");
984 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
985     } else if (rhs->asInt()->unitType() || rhs->asInt()->hasUnitFactorEver()) {
986 schoenebeck 3561 PARSE_ERR(@3, "Units are not allowed for operands of logical operations.");
987 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
988 schoenebeck 2581 } else {
989 schoenebeck 3581 if (lhs->asInt()->isFinal() && !rhs->asInt()->isFinal())
990     PARSE_WRN(@3, "Right operand of 'or' operation is not 'final', result will be 'final' though since left operand is 'final'.");
991     else if (!lhs->asInt()->isFinal() && rhs->asInt()->isFinal())
992     PARSE_WRN(@1, "Left operand of 'or' operation is not 'final', result will be 'final' though since right operand is 'final'.");
993 schoenebeck 2581 $$ = new Or(lhs, rhs);
994     }
995     }
996    
997 schoenebeck 2935 logical_and_expr:
998     bitwise_or_expr {
999 schoenebeck 2581 $$ = $1;
1000     }
1001 schoenebeck 2935 | logical_and_expr AND bitwise_or_expr {
1002 schoenebeck 2581 ExpressionRef lhs = $1;
1003     ExpressionRef rhs = $3;
1004     if (lhs->exprType() != INT_EXPR) {
1005 schoenebeck 2888 PARSE_ERR(@1, (String("Left operand of operator 'and' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1006 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1007 schoenebeck 2581 } else if (rhs->exprType() != INT_EXPR) {
1008 schoenebeck 2888 PARSE_ERR(@3, (String("Right operand of operator 'and' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1009 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1010     } else if (lhs->asInt()->unitType() || lhs->asInt()->hasUnitFactorEver()) {
1011 schoenebeck 3561 PARSE_ERR(@1, "Units are not allowed for operands of logical operations.");
1012 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1013     } else if (rhs->asInt()->unitType() || rhs->asInt()->hasUnitFactorEver()) {
1014 schoenebeck 3561 PARSE_ERR(@3, "Units are not allowed for operands of logical operations.");
1015 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1016 schoenebeck 2581 } else {
1017 schoenebeck 3581 if (lhs->asInt()->isFinal() && !rhs->asInt()->isFinal())
1018     PARSE_WRN(@3, "Right operand of 'and' operation is not 'final', result will be 'final' though since left operand is 'final'.");
1019     else if (!lhs->asInt()->isFinal() && rhs->asInt()->isFinal())
1020     PARSE_WRN(@1, "Left operand of 'and' operation is not 'final', result will be 'final' though since right operand is 'final'.");
1021 schoenebeck 2581 $$ = new And(lhs, rhs);
1022     }
1023     }
1024    
1025 schoenebeck 2935 bitwise_or_expr:
1026     bitwise_and_expr
1027     | bitwise_or_expr BITWISE_OR bitwise_and_expr {
1028     ExpressionRef lhs = $1;
1029     ExpressionRef rhs = $3;
1030     if (lhs->exprType() != INT_EXPR) {
1031     PARSE_ERR(@1, (String("Left operand of bitwise operator '.or.' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1032 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1033 schoenebeck 2935 } else if (rhs->exprType() != INT_EXPR) {
1034     PARSE_ERR(@3, (String("Right operand of bitwise operator '.or.' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1035 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1036     } else if (lhs->asInt()->unitType() || lhs->asInt()->hasUnitFactorEver()) {
1037 schoenebeck 3561 PARSE_ERR(@1, "Units are not allowed for operands of bitwise operations.");
1038 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1039     } else if (rhs->asInt()->unitType() || rhs->asInt()->hasUnitFactorEver()) {
1040 schoenebeck 3561 PARSE_ERR(@3, "Units are not allowed for operands of bitwise operations.");
1041 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1042 schoenebeck 2935 } else {
1043 schoenebeck 3581 if (lhs->asInt()->isFinal() && !rhs->asInt()->isFinal())
1044     PARSE_WRN(@3, "Right operand of '.or.' operation is not 'final', result will be 'final' though since left operand is 'final'.");
1045     else if (!lhs->asInt()->isFinal() && rhs->asInt()->isFinal())
1046     PARSE_WRN(@1, "Left operand of '.or.' operation is not 'final', result will be 'final' though since right operand is 'final'.");
1047 schoenebeck 2935 $$ = new BitwiseOr(lhs, rhs);
1048     }
1049     }
1050    
1051     bitwise_and_expr:
1052     rel_expr {
1053     $$ = $1;
1054     }
1055     | bitwise_and_expr BITWISE_AND rel_expr {
1056     ExpressionRef lhs = $1;
1057     ExpressionRef rhs = $3;
1058     if (lhs->exprType() != INT_EXPR) {
1059     PARSE_ERR(@1, (String("Left operand of bitwise operator '.and.' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1060 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1061 schoenebeck 2935 } else if (rhs->exprType() != INT_EXPR) {
1062     PARSE_ERR(@3, (String("Right operand of bitwise operator '.and.' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1063 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1064     } else if (lhs->asInt()->unitType() || lhs->asInt()->hasUnitFactorEver()) {
1065 schoenebeck 3561 PARSE_ERR(@1, "Units are not allowed for operands of bitwise operations.");
1066 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1067     } else if (rhs->asInt()->unitType() || rhs->asInt()->hasUnitFactorEver()) {
1068 schoenebeck 3561 PARSE_ERR(@3, "Units are not allowed for operands of bitwise operations.");
1069 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1070 schoenebeck 2935 } else {
1071 schoenebeck 3581 if (lhs->asInt()->isFinal() && !rhs->asInt()->isFinal())
1072     PARSE_WRN(@3, "Right operand of '.and.' operation is not 'final', result will be 'final' though since left operand is 'final'.");
1073     else if (!lhs->asInt()->isFinal() && rhs->asInt()->isFinal())
1074     PARSE_WRN(@1, "Left operand of '.and.' operation is not 'final', result will be 'final' though since right operand is 'final'.");
1075 schoenebeck 2935 $$ = new BitwiseAnd(lhs, rhs);
1076     }
1077     }
1078    
1079 schoenebeck 2581 rel_expr:
1080     add_expr
1081     | rel_expr '<' add_expr {
1082     ExpressionRef lhs = $1;
1083     ExpressionRef rhs = $3;
1084 schoenebeck 3582 if (!isNumber(lhs->exprType())) {
1085 schoenebeck 3573 PARSE_ERR(@1, (String("Left operand of operator '<' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1086 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1087 schoenebeck 3582 } else if (!isNumber(rhs->exprType())) {
1088 schoenebeck 3573 PARSE_ERR(@3, (String("Right operand of operator '<' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1089 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1090 schoenebeck 3582 } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1091 schoenebeck 3581 PARSE_ERR(@2, (String("Operands of relative operations must have same unit, left operand is ") +
1092 schoenebeck 3582 unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1093     unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1094 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1095 schoenebeck 2581 } else {
1096 schoenebeck 3582 if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1097 schoenebeck 3581 PARSE_WRN(@3, "Right operand of '<' comparison is not 'final', left operand is 'final' though.");
1098 schoenebeck 3582 else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1099 schoenebeck 3581 PARSE_WRN(@1, "Left operand of '<' comparison is not 'final', right operand is 'final' though.");
1100 schoenebeck 2581 $$ = new Relation(lhs, Relation::LESS_THAN, rhs);
1101     }
1102     }
1103     | rel_expr '>' add_expr {
1104     ExpressionRef lhs = $1;
1105     ExpressionRef rhs = $3;
1106 schoenebeck 3582 if (!isNumber(lhs->exprType())) {
1107 schoenebeck 3573 PARSE_ERR(@1, (String("Left operand of operator '>' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1108 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1109 schoenebeck 3582 } else if (!isNumber(rhs->exprType())) {
1110 schoenebeck 3573 PARSE_ERR(@3, (String("Right operand of operator '>' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1111 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1112 schoenebeck 3582 } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1113 schoenebeck 3581 PARSE_ERR(@2, (String("Operands of relative operations must have same unit, left operand is ") +
1114 schoenebeck 3582 unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1115     unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1116 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1117 schoenebeck 2581 } else {
1118 schoenebeck 3582 if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1119 schoenebeck 3581 PARSE_WRN(@3, "Right operand of '>' comparison is not 'final', left operand is 'final' though.");
1120 schoenebeck 3582 else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1121 schoenebeck 3581 PARSE_WRN(@1, "Left operand of '>' comparison is not 'final', right operand is 'final' though.");
1122 schoenebeck 2581 $$ = new Relation(lhs, Relation::GREATER_THAN, rhs);
1123     }
1124     }
1125     | rel_expr LE add_expr {
1126     ExpressionRef lhs = $1;
1127     ExpressionRef rhs = $3;
1128 schoenebeck 3582 if (!isNumber(lhs->exprType())) {
1129 schoenebeck 3573 PARSE_ERR(@1, (String("Left operand of operator '<=' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1130 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1131 schoenebeck 3582 } else if (!isNumber(rhs->exprType())) {
1132 schoenebeck 3573 PARSE_ERR(@3, (String("Right operand of operator '<=' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1133 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1134 schoenebeck 3582 } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1135 schoenebeck 3581 PARSE_ERR(@2, (String("Operands of relative operations must have same unit, left operand is ") +
1136 schoenebeck 3582 unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1137     unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1138 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1139 schoenebeck 2581 } else {
1140 schoenebeck 3582 if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1141 schoenebeck 3581 PARSE_WRN(@3, "Right operand of '<=' comparison is not 'final', left operand is 'final' though.");
1142 schoenebeck 3582 else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1143 schoenebeck 3581 PARSE_WRN(@1, "Left operand of '<=' comparison is not 'final', right operand is 'final' though.");
1144 schoenebeck 2581 $$ = new Relation(lhs, Relation::LESS_OR_EQUAL, rhs);
1145     }
1146     }
1147     | rel_expr GE add_expr {
1148     ExpressionRef lhs = $1;
1149     ExpressionRef rhs = $3;
1150 schoenebeck 3582 if (!isNumber(lhs->exprType())) {
1151 schoenebeck 3573 PARSE_ERR(@1, (String("Left operand of operator '>=' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1152 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1153 schoenebeck 3582 } else if (!isNumber(rhs->exprType())) {
1154 schoenebeck 3573 PARSE_ERR(@3, (String("Right operand of operator '>=' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1155 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1156 schoenebeck 3582 } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1157 schoenebeck 3581 PARSE_ERR(@2, (String("Operands of relative operations must have same unit, left operand is ") +
1158 schoenebeck 3582 unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1159     unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1160 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1161 schoenebeck 2581 } else {
1162 schoenebeck 3582 if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1163 schoenebeck 3581 PARSE_WRN(@3, "Right operand of '>=' comparison is not 'final', left operand is 'final' though.");
1164 schoenebeck 3582 else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1165 schoenebeck 3581 PARSE_WRN(@1, "Left operand of '>=' comparison is not 'final', right operand is 'final' though.");
1166 schoenebeck 2581 $$ = new Relation(lhs, Relation::GREATER_OR_EQUAL, rhs);
1167     }
1168     }
1169     | rel_expr '=' add_expr {
1170 schoenebeck 3561 ExpressionRef lhs = $1;
1171     ExpressionRef rhs = $3;
1172 schoenebeck 3582 if (!isNumber(lhs->exprType())) {
1173 schoenebeck 3573 PARSE_ERR(@1, (String("Left operand of operator '=' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1174 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1175 schoenebeck 3582 } else if (!isNumber(rhs->exprType())) {
1176 schoenebeck 3573 PARSE_ERR(@3, (String("Right operand of operator '=' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1177 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1178 schoenebeck 3582 } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1179 schoenebeck 3581 PARSE_ERR(@2, (String("Operands of relative operations must have same unit, left operand is ") +
1180 schoenebeck 3582 unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1181     unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1182 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1183 schoenebeck 3561 } else {
1184 schoenebeck 3582 if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1185 schoenebeck 3581 PARSE_WRN(@3, "Right operand of '=' comparison is not 'final', left operand is 'final' though.");
1186 schoenebeck 3582 else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1187 schoenebeck 3581 PARSE_WRN(@1, "Left operand of '=' comparison is not 'final', right operand is 'final' though.");
1188 schoenebeck 3561 $$ = new Relation(lhs, Relation::EQUAL, rhs);
1189     }
1190 schoenebeck 2581 }
1191     | rel_expr '#' add_expr {
1192 schoenebeck 3561 ExpressionRef lhs = $1;
1193     ExpressionRef rhs = $3;
1194 schoenebeck 3582 if (!isNumber(lhs->exprType())) {
1195 schoenebeck 3573 PARSE_ERR(@1, (String("Left operand of operator '#' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1196 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1197 schoenebeck 3582 } else if (!isNumber(rhs->exprType())) {
1198 schoenebeck 3573 PARSE_ERR(@3, (String("Right operand of operator '#' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1199 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1200 schoenebeck 3582 } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1201 schoenebeck 3581 PARSE_ERR(@2, (String("Operands of relative operations must have same unit, left operand is ") +
1202 schoenebeck 3582 unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1203     unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1204 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1205 schoenebeck 3561 } else {
1206 schoenebeck 3582 if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1207 schoenebeck 3581 PARSE_WRN(@3, "Right operand of '#' comparison is not 'final', left operand is 'final' though.");
1208 schoenebeck 3582 else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1209 schoenebeck 3581 PARSE_WRN(@1, "Left operand of '#' comparison is not 'final', right operand is 'final' though.");
1210 schoenebeck 3561 $$ = new Relation(lhs, Relation::NOT_EQUAL, rhs);
1211     }
1212 schoenebeck 2581 }
1213    
1214     add_expr:
1215     mul_expr
1216     | add_expr '+' mul_expr {
1217     ExpressionRef lhs = $1;
1218     ExpressionRef rhs = $3;
1219 schoenebeck 3582 if (!isNumber(lhs->exprType())) {
1220 schoenebeck 3573 PARSE_ERR(@1, (String("Left operand of operator '+' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1221 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1222 schoenebeck 3582 } else if (!isNumber(rhs->exprType())) {
1223 schoenebeck 3573 PARSE_ERR(@1, (String("Right operand of operator '+' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1224 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1225 schoenebeck 3573 } else if (lhs->exprType() != rhs->exprType()) {
1226     PARSE_ERR(@2, (String("Operands of operator '+' must have same type; left operand is ") +
1227     typeStr(lhs->exprType()) + " and right operand is " + typeStr(rhs->exprType()) + " though.").c_str());
1228 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1229 schoenebeck 3582 } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1230 schoenebeck 3581 PARSE_ERR(@2, (String("Operands of '+' operations must have same unit, left operand is ") +
1231 schoenebeck 3582 unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1232     unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1233 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1234 schoenebeck 2581 } else {
1235 schoenebeck 3582 if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1236 schoenebeck 3581 PARSE_WRN(@3, "Right operand of '+' operation is not 'final', result will be 'final' though since left operand is 'final'.");
1237 schoenebeck 3582 else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1238 schoenebeck 3581 PARSE_WRN(@1, "Left operand of '+' operation is not 'final', result will be 'final' though since right operand is 'final'.");
1239 schoenebeck 2581 $$ = new Add(lhs,rhs);
1240     }
1241     }
1242     | add_expr '-' mul_expr {
1243     ExpressionRef lhs = $1;
1244     ExpressionRef rhs = $3;
1245 schoenebeck 3582 if (!isNumber(lhs->exprType())) {
1246 schoenebeck 3573 PARSE_ERR(@1, (String("Left operand of operator '-' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1247 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1248 schoenebeck 3582 } else if (!isNumber(rhs->exprType())) {
1249 schoenebeck 3573 PARSE_ERR(@1, (String("Right operand of operator '-' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1250 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1251 schoenebeck 3573 } else if (lhs->exprType() != rhs->exprType()) {
1252     PARSE_ERR(@2, (String("Operands of operator '-' must have same type; left operand is ") +
1253     typeStr(lhs->exprType()) + " and right operand is " + typeStr(rhs->exprType()) + " though.").c_str());
1254 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1255 schoenebeck 3582 } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1256 schoenebeck 3581 PARSE_ERR(@2, (String("Operands of '-' operations must have same unit, left operand is ") +
1257 schoenebeck 3582 unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1258     unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1259 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1260 schoenebeck 2581 } else {
1261 schoenebeck 3582 if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1262 schoenebeck 3581 PARSE_WRN(@3, "Right operand of '-' operation is not 'final', result will be 'final' though since left operand is 'final'.");
1263 schoenebeck 3582 else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1264 schoenebeck 3581 PARSE_WRN(@1, "Left operand of '-' operation is not 'final', result will be 'final' though since right operand is 'final'.");
1265 schoenebeck 2581 $$ = new Sub(lhs,rhs);
1266     }
1267     }
1268    
1269     mul_expr:
1270     unary_expr
1271     | mul_expr '*' unary_expr {
1272     ExpressionRef lhs = $1;
1273     ExpressionRef rhs = $3;
1274 schoenebeck 3582 if (!isNumber(lhs->exprType())) {
1275 schoenebeck 3573 PARSE_ERR(@1, (String("Left operand of operator '*' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1276 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1277 schoenebeck 3582 } else if (!isNumber(rhs->exprType())) {
1278 schoenebeck 3573 PARSE_ERR(@1, (String("Right operand of operator '*' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1279 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1280 schoenebeck 3582 } else if (lhs->asNumber()->unitType() && rhs->asNumber()->unitType()) {
1281 schoenebeck 3581 PARSE_ERR(@2, (String("Only one operand of operator '*' may have a unit type, left operand is ") +
1282 schoenebeck 3582 unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1283     unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1284 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1285 schoenebeck 3573 } else if (lhs->exprType() != rhs->exprType()) {
1286     PARSE_ERR(@2, (String("Operands of operator '*' must have same type; left operand is ") +
1287     typeStr(lhs->exprType()) + " and right operand is " + typeStr(rhs->exprType()) + " though.").c_str());
1288 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1289 schoenebeck 2581 } else {
1290 schoenebeck 3582 if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1291 schoenebeck 3581 PARSE_WRN(@3, "Right operand of '*' operation is not 'final', result will be 'final' though since left operand is 'final'.");
1292 schoenebeck 3582 else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1293 schoenebeck 3581 PARSE_WRN(@1, "Left operand of '*' operation is not 'final', result will be 'final' though since right operand is 'final'.");
1294 schoenebeck 2581 $$ = new Mul(lhs,rhs);
1295     }
1296     }
1297     | mul_expr '/' unary_expr {
1298     ExpressionRef lhs = $1;
1299     ExpressionRef rhs = $3;
1300 schoenebeck 3582 if (!isNumber(lhs->exprType())) {
1301 schoenebeck 3573 PARSE_ERR(@1, (String("Left operand of operator '/' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1302 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1303 schoenebeck 3582 } else if (!isNumber(rhs->exprType())) {
1304 schoenebeck 3573 PARSE_ERR(@1, (String("Right operand of operator '/' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1305 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1306 schoenebeck 3582 } else if (lhs->asNumber()->unitType() && rhs->asNumber()->unitType() &&
1307     lhs->asNumber()->unitType() != rhs->asNumber()->unitType())
1308 schoenebeck 3561 {
1309 schoenebeck 3581 PARSE_ERR(@2, (String("Operands of operator '/' with two different unit types, left operand is ") +
1310 schoenebeck 3582 unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1311     unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1312 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1313 schoenebeck 3582 } else if (!lhs->asNumber()->unitType() && rhs->asNumber()->unitType()) {
1314 schoenebeck 3581 PARSE_ERR(@3, ("Dividing left operand without any unit type by right operand with unit type (" +
1315 schoenebeck 3582 unitTypeStr(rhs->asNumber()->unitType()) + ") is not possible.").c_str());
1316 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1317 schoenebeck 3573 } else if (lhs->exprType() != rhs->exprType()) {
1318     PARSE_ERR(@2, (String("Operands of operator '/' must have same type; left operand is ") +
1319     typeStr(lhs->exprType()) + " and right operand is " + typeStr(rhs->exprType()) + " though.").c_str());
1320 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1321 schoenebeck 2581 } else {
1322 schoenebeck 3582 if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1323 schoenebeck 3581 PARSE_WRN(@3, "Right operand of '/' operation is not 'final', result will be 'final' though since left operand is 'final'.");
1324 schoenebeck 3582 else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1325 schoenebeck 3581 PARSE_WRN(@1, "Left operand of '/' operation is not 'final', result will be 'final' though since right operand is 'final'.");
1326 schoenebeck 2581 $$ = new Div(lhs,rhs);
1327     }
1328     }
1329     | mul_expr MOD unary_expr {
1330     ExpressionRef lhs = $1;
1331     ExpressionRef rhs = $3;
1332     if (lhs->exprType() != INT_EXPR) {
1333 schoenebeck 2888 PARSE_ERR(@1, (String("Left operand of modulo operator must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1334 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1335 schoenebeck 2581 } else if (rhs->exprType() != INT_EXPR) {
1336 schoenebeck 2888 PARSE_ERR(@3, (String("Right operand of modulo operator must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1337 schoenebeck 3581 $$ = new IntLiteral({ .value = 0 });
1338 schoenebeck 2581 } else {
1339 schoenebeck 3581 if (lhs->asInt()->unitType() || lhs->asInt()->hasUnitFactorEver())
1340 schoenebeck 3561 PARSE_ERR(@1, "Operands of modulo operator must not use any unit.");
1341 schoenebeck 3581 if (rhs->asInt()->unitType() || rhs->asInt()->hasUnitFactorEver())
1342 schoenebeck 3561 PARSE_ERR(@3, "Operands of modulo operator must not use any unit.");
1343 schoenebeck 3581 if (lhs->asInt()->isFinal() && !rhs->asInt()->isFinal())
1344     PARSE_WRN(@3, "Right operand of 'mod' operation is not 'final', result will be 'final' though since left operand is 'final'.");
1345     else if (!lhs->asInt()->isFinal() && rhs->asInt()->isFinal())
1346     PARSE_WRN(@1, "Left operand of 'mod' operation is not 'final', result will be 'final' though since right operand is 'final'.");
1347 schoenebeck 2581 $$ = new Mod(lhs,rhs);
1348     }
1349     }
1350    
1351     %%
1352    
1353     void InstrScript_error(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* err) {
1354     //fprintf(stderr, "%d: %s\n", locp->first_line, err);
1355 schoenebeck 2889 context->addErr(locp->first_line, locp->last_line, locp->first_column+1, locp->last_column+1, err);
1356 schoenebeck 2581 }
1357    
1358     void InstrScript_warning(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* txt) {
1359     //fprintf(stderr, "WRN %d: %s\n", locp->first_line, txt);
1360 schoenebeck 2889 context->addWrn(locp->first_line, locp->last_line, locp->first_column+1, locp->last_column+1, txt);
1361 schoenebeck 2581 }
1362 schoenebeck 3008
1363     /// Custom implementation of yytnamerr() to ensure quotation is always stripped from token names before printing them to error messages.
1364     int InstrScript_tnamerr(char* yyres, const char* yystr) {
1365     if (*yystr == '"') {
1366     int yyn = 0;
1367     char const *yyp = yystr;
1368     for (;;)
1369     switch (*++yyp)
1370     {
1371     /*
1372     case '\'':
1373     case ',':
1374     goto do_not_strip_quotes;
1375    
1376     case '\\':
1377     if (*++yyp != '\\')
1378     goto do_not_strip_quotes;
1379     */
1380     /* Fall through. */
1381     default:
1382     if (yyres)
1383     yyres[yyn] = *yyp;
1384     yyn++;
1385     break;
1386    
1387     case '"':
1388     if (yyres)
1389     yyres[yyn] = '\0';
1390     return yyn;
1391     }
1392 schoenebeck 3034 /*
1393 schoenebeck 3008 do_not_strip_quotes: ;
1394 schoenebeck 3034 */
1395 schoenebeck 3008 }
1396    
1397     if (! yyres)
1398 schoenebeck 3054 return (int) yystrlen (yystr);
1399 schoenebeck 3008
1400 schoenebeck 3054 return int( yystpcpy (yyres, yystr) - yyres );
1401 schoenebeck 3008 }

  ViewVC Help
Powered by ViewVC