/[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 3786 - (hide annotations) (download)
Sun Jun 7 15:39:31 2020 UTC (3 years, 11 months ago) by schoenebeck
File size: 70305 byte(s)
* NKSP: Fixed crash when undefined variable was referenced.

* Bumped version (2.1.1.svn57).

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

  ViewVC Help
Powered by ViewVC