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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3816 - (show annotations) (download)
Fri Aug 28 17:28:48 2020 UTC (3 years, 7 months ago) by schoenebeck
File size: 71733 byte(s)
* NKSP parser: Fixed crash if unary '-' operator was used on a non-number
  data type.

* NKSP parser: Raise parser error if either unary '-' or '+' operator was
  used with a non-number data type.

* Tests: Added test cases for the fixed issues described above.

* Bumped version (2.1.1.svn63).

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

  ViewVC Help
Powered by ViewVC