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

* Bumped version (2.1.1.svn57).

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
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] = $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 '[' 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->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 } 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->evalInt();
400 if (size <= 0) {
401 PARSE_ERR(@5, (String("Array variable '") + name + "' must be declared with positive array size.").c_str());
402 } else if (sizeExpr->unitType() || sizeExpr->hasUnitFactorNow()) {
403 PARSE_ERR(@5, "Units are not allowed as array size.");
404 } else {
405 if (sizeExpr->isFinal())
406 PARSE_WRN(@5, "Final operator '!' is meaningless here.");
407 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 } else {
416 if (qPatch)
417 context->patchVars[name].exprBlock = ASSIGNED_EXPR_BLOCK(@7);
418 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 }
438 bool argsOK = true;
439 if (declType == EMPTY_EXPR) {
440 argsOK = false;
441 } 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 }
482 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 }
489 }
490 }
491 }
492 }
493 | assignment {
494 $$ = $1;
495 }
496 | WHILE '(' expr ')' opt_statements END WHILE {
497 if ($3->exprType() == INT_EXPR) {
498 IntExprRef expr = $3;
499 if (expr->asNumber()->unitType() ||
500 expr->asNumber()->hasUnitFactorEver())
501 PARSE_WRN(@3, "Condition for 'while' loops contains a unit.");
502 else if (expr->isFinal() && expr->isConstExpr())
503 PARSE_WRN(@3, "Final operator '!' is meaningless here.");
504 $$ = new While(expr, $5);
505 } else {
506 PARSE_ERR(@3, "Condition for 'while' loops must be integer expression.");
507 $$ = new While(new IntLiteral({ .value = 0 }), $5);
508 }
509 }
510 | SYNCHRONIZED opt_statements END SYNCHRONIZED {
511 $$ = new SyncBlock($2);
512 }
513 | IF '(' expr ')' opt_statements ELSE opt_statements END IF {
514 if ($3->exprType() == INT_EXPR) {
515 IntExprRef expr = $3;
516 if (expr->asNumber()->unitType() ||
517 expr->asNumber()->hasUnitFactorEver())
518 PARSE_WRN(@3, "Condition for 'if' contains a unit.");
519 else if (expr->isFinal() && expr->isConstExpr())
520 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 $$ = new If(new IntLiteral({ .value = 0 }), $5, $7);
525 }
526 }
527 | IF '(' expr ')' opt_statements END IF {
528 if ($3->exprType() == INT_EXPR) {
529 IntExprRef expr = $3;
530 if (expr->asNumber()->unitType() ||
531 expr->asNumber()->hasUnitFactorEver())
532 PARSE_WRN(@3, "Condition for 'if' contains a unit.");
533 else if (expr->isFinal() && expr->isConstExpr())
534 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 $$ = new If(new IntLiteral({ .value = 0 }), $5);
539 }
540 }
541 | SELECT expr caseclauses END SELECT {
542 if ($2->exprType() == INT_EXPR) {
543 IntExprRef expr = $2;
544 if (expr->unitType() || expr->hasUnitFactorEver()) {
545 PARSE_ERR(@2, "Units are not allowed here.");
546 $$ = new SelectCase(new IntLiteral({ .value = 0 }), $3);
547 } else {
548 if (expr->isFinal() && expr->isConstExpr())
549 PARSE_WRN(@2, "Final operator '!' is meaningless here.");
550 $$ = new SelectCase(expr, $3);
551 }
552 } else {
553 PARSE_ERR(@2, "Statement 'select' can only by applied to integer expressions.");
554 $$ = new SelectCase(new IntLiteral({ .value = 0 }), $3);
555 }
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 CASE INTEGER opt_statements {
570 $$ = CaseBranch();
571 $$.from = new IntLiteral({ .value = $2 });
572 $$.statements = $3;
573 }
574 | CASE INTEGER TO INTEGER opt_statements {
575 $$ = CaseBranch();
576 $$.from = new IntLiteral({ .value = $2 });
577 $$.to = new IntLiteral({ .value = $4 });
578 $$.statements = $5;
579 }
580
581 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 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 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 PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());
607 $$ = new FunctionCall(name, args, NULL);
608 } else if (context->functionProvider->isFunctionDisabled(fn,context)) {
609 PARSE_DROP(@$);
610 $$ = new NoFunctionCall;
611 } else if (args->argsCount() < fn->minRequiredArgs()) {
612 PARSE_ERR(@3, (String("Built-in function '") + name + "' requires at least " + ToString(fn->minRequiredArgs()) + " arguments.").c_str());
613 $$ = new FunctionCall(name, args, NULL);
614 } else if (args->argsCount() > fn->maxAllowedArgs()) {
615 PARSE_ERR(@3, (String("Built-in function '") + name + "' accepts max. " + ToString(fn->maxAllowedArgs()) + " arguments.").c_str());
616 $$ = new FunctionCall(name, args, NULL);
617 } else {
618 bool argsOK = true;
619 for (vmint i = 0; i < args->argsCount(); ++i) {
620 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 argsOK = false;
623 break;
624 } 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 } 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 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 } 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 else
639 PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' does not expect a unit prefix.").c_str());
640 argsOK = false;
641 break;
642 } else if (!fn->acceptsArgFinal(i) && isNumber(args->arg(i)->exprType()) && args->arg(i)->asNumber()->isFinal()) {
643 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 }
647 }
648 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 $$ = 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 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 PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());
670 $$ = new FunctionCall(name, args, NULL);
671 } else if (context->functionProvider->isFunctionDisabled(fn,context)) {
672 PARSE_DROP(@$);
673 $$ = new NoFunctionCall;
674 } else if (fn->minRequiredArgs() > 0) {
675 PARSE_ERR(@3, (String("Built-in function '") + name + "' requires at least " + ToString(fn->minRequiredArgs()) + " arguments.").c_str());
676 $$ = 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 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 PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());
691 $$ = new FunctionCall(name, args, NULL);
692 } else if (context->functionProvider->isFunctionDisabled(fn,context)) {
693 PARSE_DROP(@$);
694 $$ = new NoFunctionCall;
695 } else if (fn->minRequiredArgs() > 0) {
696 PARSE_ERR(@1, (String("Built-in function '") + name + "' requires at least " + ToString(fn->minRequiredArgs()) + " arguments.").c_str());
697 $$ = 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 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 | PATCH {
742 $$ = QUALIFIER_PATCH;
743 }
744
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 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 PARSE_ERR(@1, (String("Variable assignment: No variable declared with name '") + name + "'.").c_str());
768 else if (var->isConstExpr())
769 PARSE_ERR(@2, (String("Variable assignment: Cannot modify const variable '") + name + "'.").c_str());
770 else if (!var->isAssignable())
771 PARSE_ERR(@2, (String("Variable assignment: Variable '") + name + "' is not assignable.").c_str());
772 else if (var->exprType() != $3->exprType())
773 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 else if (isNumber(var->exprType())) {
775 NumberVariableRef numberVar = var;
776 NumberExprRef expr = $3;
777 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 }
782
783 if (var)
784 $$ = new Assignment(var, $3);
785 else
786 $$ = new NoOperation;
787 }
788 | VARIABLE '[' expr ']' ASSIGNMENT expr {
789 const char* name = $1;
790 VariableRef var = context->variableByName(name);
791 if (!var)
792 PARSE_ERR(@1, (String("No variable declared with name '") + name + "'.").c_str());
793 else if (!isArray(var->exprType()))
794 PARSE_ERR(@2, (String("Variable '") + name + "' is not an array variable.").c_str());
795 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 else if ($3->exprType() != INT_EXPR)
800 PARSE_ERR(@3, (String("Array variable '") + name + "' accessed with non integer expression.").c_str());
801 else if ($3->asInt()->unitType())
802 PARSE_ERR(@3, "Unit types are not allowed as array index.");
803 else if ($6->exprType() != scalarTypeOfArray(var->exprType()))
804 PARSE_ERR(@5, (String("Variable '") + name + "' was declared as " + typeStr(var->exprType()) + ", assigned expression is " + typeStr($6->exprType()) + " though.").c_str());
805 else if ($6->asNumber()->unitType())
806 PARSE_ERR(@6, "Unit types are not allowed for array variables.");
807 else if ($6->asNumber()->isFinal())
808 PARSE_ERR(@6, "Final operator '!' not allowed for array variables.");
809 else if ($3->isConstExpr() && $3->asInt()->evalInt() >= ((ArrayExprRef)var)->arraySize())
810 PARSE_WRN(@3, (String("Index ") + ToString($3->asInt()->evalInt()) +
811 " exceeds size of array variable '" + name +
812 "' which was declared with size " +
813 ToString(((ArrayExprRef)var)->arraySize()) + ".").c_str());
814 else if ($3->asInt()->isFinal())
815 PARSE_WRN(@3, "Final operator '!' is meaningless here.");
816
817 if (!var) {
818 $$ = new NoOperation;
819 } else if (var->exprType() == INT_ARR_EXPR) {
820 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 }
827
828 unary_expr:
829 INTEGER {
830 $$ = new IntLiteral({ .value = $1 });
831 }
832 | REAL {
833 $$ = new RealLiteral({ .value = $1 });
834 }
835 | INTEGER_UNIT {
836 IntLiteralRef literal = new IntLiteral({
837 .value = $1.iValue,
838 .unitFactor = VMUnit::unitFactor($1.prefix),
839 .unitType = $1.unit
840 });
841 $$ = literal;
842 }
843 | REAL_UNIT {
844 RealLiteralRef literal = new RealLiteral({
845 .value = $1.fValue,
846 .unitFactor = VMUnit::unitFactor($1.prefix),
847 .unitType = $1.unit
848 });
849 $$ = literal;
850 }
851 | 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 PARSE_ERR(@1, (String("No variable declared with name '") + $1 + "'.").c_str());
861 $$ = new IntLiteral({ .value = 0 });
862 }
863 }
864 | VARIABLE '[' expr ']' {
865 const char* name = $1;
866 VariableRef var = context->variableByName(name);
867 if (!var) {
868 PARSE_ERR(@1, (String("No variable declared with name '") + name + "'.").c_str());
869 $$ = new IntLiteral({ .value = 0 });
870 } else if (!isArray(var->exprType())) {
871 PARSE_ERR(@2, (String("Variable '") + name + "' is not an array variable.").c_str());
872 $$ = new IntLiteral({ .value = 0 });
873 } else if ($3->exprType() != INT_EXPR) {
874 PARSE_ERR(@3, (String("Array variable '") + name + "' accessed with non integer expression.").c_str());
875 $$ = new IntLiteral({ .value = 0 });
876 } else if ($3->asInt()->unitType() || $3->asInt()->hasUnitFactorEver()) {
877 PARSE_ERR(@3, "Units are not allowed as array index.");
878 $$ = new IntLiteral({ .value = 0 });
879 } else {
880 if ($3->isConstExpr() && $3->asInt()->evalInt() >= ((ArrayExprRef)var)->arraySize())
881 PARSE_WRN(@3, (String("Index ") + ToString($3->asInt()->evalInt()) +
882 " exceeds size of array variable '" + name +
883 "' which was declared with size " +
884 ToString(((ArrayExprRef)var)->arraySize()) + ".").c_str());
885 else if ($3->asInt()->isFinal())
886 PARSE_WRN(@3, "Final operator '!' is meaningless here.");
887 if (var->exprType() == REAL_ARR_EXPR) {
888 $$ = new RealArrayElement(var, $3);
889 } else {
890 $$ = new IntArrayElement(var, $3);
891 }
892 }
893 }
894 | '(' expr ')' {
895 $$ = $2;
896 }
897 | functioncall {
898 $$ = $1;
899 }
900 | '+' unary_expr {
901 $$ = $2;
902 }
903 | '-' unary_expr {
904 $$ = new Neg($2);
905 }
906 | 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 $$ = new IntLiteral({ .value = 0 });
910 } else if ($2->asInt()->unitType() || $2->asInt()->hasUnitFactorEver()) {
911 PARSE_ERR(@2, "Units are not allowed for operands of bitwise operations.");
912 $$ = new IntLiteral({ .value = 0 });
913 } else {
914 $$ = new BitwiseNot($2);
915 }
916 }
917 | NOT unary_expr {
918 if ($2->exprType() != INT_EXPR) {
919 PARSE_ERR(@2, (String("Right operand of operator 'not' must be an integer expression, is ") + typeStr($2->exprType()) + " though.").c_str());
920 $$ = new IntLiteral({ .value = 0 });
921 } else if ($2->asInt()->unitType() || $2->asInt()->hasUnitFactorEver()) {
922 PARSE_ERR(@2, "Units are not allowed for operands of logical operations.");
923 $$ = new IntLiteral({ .value = 0 });
924 } else {
925 $$ = new Not($2);
926 }
927 }
928 | '!' unary_expr {
929 if (!isNumber($2->exprType())) {
930 PARSE_ERR(@2, (String("Right operand of \"final\" operator '!' must be a scalar number expression, is ") + typeStr($2->exprType()) + " though.").c_str());
931 $$ = new IntLiteral({ .value = 0 });
932 } else {
933 $$ = new Final($2);
934 }
935 }
936
937 expr:
938 concat_expr
939
940 concat_expr:
941 logical_or_expr
942 | concat_expr '&' logical_or_expr {
943 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 logical_or_expr:
955 logical_and_expr
956 | logical_or_expr OR logical_and_expr {
957 ExpressionRef lhs = $1;
958 ExpressionRef rhs = $3;
959 if (lhs->exprType() != INT_EXPR) {
960 PARSE_ERR(@1, (String("Left operand of operator 'or' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
961 $$ = new IntLiteral({ .value = 0 });
962 } else if (rhs->exprType() != INT_EXPR) {
963 PARSE_ERR(@3, (String("Right operand of operator 'or' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
964 $$ = new IntLiteral({ .value = 0 });
965 } else if (lhs->asInt()->unitType() || lhs->asInt()->hasUnitFactorEver()) {
966 PARSE_ERR(@1, "Units are not allowed for operands of logical operations.");
967 $$ = new IntLiteral({ .value = 0 });
968 } else if (rhs->asInt()->unitType() || rhs->asInt()->hasUnitFactorEver()) {
969 PARSE_ERR(@3, "Units are not allowed for operands of logical operations.");
970 $$ = new IntLiteral({ .value = 0 });
971 } else {
972 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 $$ = new Or(lhs, rhs);
977 }
978 }
979
980 logical_and_expr:
981 bitwise_or_expr {
982 $$ = $1;
983 }
984 | logical_and_expr AND bitwise_or_expr {
985 ExpressionRef lhs = $1;
986 ExpressionRef rhs = $3;
987 if (lhs->exprType() != INT_EXPR) {
988 PARSE_ERR(@1, (String("Left operand of operator 'and' 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 'and' 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 '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 $$ = new And(lhs, rhs);
1005 }
1006 }
1007
1008 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 $$ = new IntLiteral({ .value = 0 });
1016 } 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 $$ = new IntLiteral({ .value = 0 });
1019 } else if (lhs->asInt()->unitType() || lhs->asInt()->hasUnitFactorEver()) {
1020 PARSE_ERR(@1, "Units are not allowed for operands of bitwise operations.");
1021 $$ = new IntLiteral({ .value = 0 });
1022 } else if (rhs->asInt()->unitType() || rhs->asInt()->hasUnitFactorEver()) {
1023 PARSE_ERR(@3, "Units are not allowed for operands of bitwise operations.");
1024 $$ = new IntLiteral({ .value = 0 });
1025 } else {
1026 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 $$ = 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 $$ = new IntLiteral({ .value = 0 });
1044 } 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 $$ = 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 '.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 $$ = new BitwiseAnd(lhs, rhs);
1059 }
1060 }
1061
1062 rel_expr:
1063 add_expr
1064 | rel_expr '<' add_expr {
1065 ExpressionRef lhs = $1;
1066 ExpressionRef rhs = $3;
1067 if (!isNumber(lhs->exprType())) {
1068 PARSE_ERR(@1, (String("Left operand of operator '<' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1069 $$ = new IntLiteral({ .value = 0 });
1070 } else if (!isNumber(rhs->exprType())) {
1071 PARSE_ERR(@3, (String("Right operand of operator '<' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1072 $$ = new IntLiteral({ .value = 0 });
1073 } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1074 PARSE_ERR(@2, (String("Operands of relative operations must have same unit, left operand is ") +
1075 unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1076 unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1077 $$ = new IntLiteral({ .value = 0 });
1078 } else {
1079 if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1080 PARSE_WRN(@3, "Right operand of '<' comparison is not 'final', left operand is 'final' though.");
1081 else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1082 PARSE_WRN(@1, "Left operand of '<' comparison is not 'final', right operand is 'final' though.");
1083 $$ = new Relation(lhs, Relation::LESS_THAN, rhs);
1084 }
1085 }
1086 | rel_expr '>' add_expr {
1087 ExpressionRef lhs = $1;
1088 ExpressionRef rhs = $3;
1089 if (!isNumber(lhs->exprType())) {
1090 PARSE_ERR(@1, (String("Left operand of operator '>' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1091 $$ = new IntLiteral({ .value = 0 });
1092 } else if (!isNumber(rhs->exprType())) {
1093 PARSE_ERR(@3, (String("Right operand of operator '>' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1094 $$ = new IntLiteral({ .value = 0 });
1095 } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1096 PARSE_ERR(@2, (String("Operands of relative operations must have same unit, left operand is ") +
1097 unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1098 unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1099 $$ = new IntLiteral({ .value = 0 });
1100 } else {
1101 if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1102 PARSE_WRN(@3, "Right operand of '>' comparison is not 'final', left operand is 'final' though.");
1103 else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1104 PARSE_WRN(@1, "Left operand of '>' comparison is not 'final', right operand is 'final' though.");
1105 $$ = new Relation(lhs, Relation::GREATER_THAN, rhs);
1106 }
1107 }
1108 | rel_expr LE add_expr {
1109 ExpressionRef lhs = $1;
1110 ExpressionRef rhs = $3;
1111 if (!isNumber(lhs->exprType())) {
1112 PARSE_ERR(@1, (String("Left operand of operator '<=' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1113 $$ = new IntLiteral({ .value = 0 });
1114 } else if (!isNumber(rhs->exprType())) {
1115 PARSE_ERR(@3, (String("Right operand of operator '<=' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1116 $$ = new IntLiteral({ .value = 0 });
1117 } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1118 PARSE_ERR(@2, (String("Operands of relative operations must have same unit, left operand is ") +
1119 unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1120 unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1121 $$ = new IntLiteral({ .value = 0 });
1122 } else {
1123 if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1124 PARSE_WRN(@3, "Right operand of '<=' comparison is not 'final', left operand is 'final' though.");
1125 else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1126 PARSE_WRN(@1, "Left operand of '<=' comparison is not 'final', right operand is 'final' though.");
1127 $$ = 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 if (!isNumber(lhs->exprType())) {
1134 PARSE_ERR(@1, (String("Left operand of operator '>=' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1135 $$ = new IntLiteral({ .value = 0 });
1136 } else if (!isNumber(rhs->exprType())) {
1137 PARSE_ERR(@3, (String("Right operand of operator '>=' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1138 $$ = new IntLiteral({ .value = 0 });
1139 } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1140 PARSE_ERR(@2, (String("Operands of relative operations must have same unit, left operand is ") +
1141 unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1142 unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1143 $$ = new IntLiteral({ .value = 0 });
1144 } else {
1145 if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1146 PARSE_WRN(@3, "Right operand of '>=' comparison is not 'final', left operand is 'final' though.");
1147 else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1148 PARSE_WRN(@1, "Left operand of '>=' comparison is not 'final', right operand is 'final' though.");
1149 $$ = new Relation(lhs, Relation::GREATER_OR_EQUAL, rhs);
1150 }
1151 }
1152 | rel_expr '=' add_expr {
1153 ExpressionRef lhs = $1;
1154 ExpressionRef rhs = $3;
1155 if (!isNumber(lhs->exprType())) {
1156 PARSE_ERR(@1, (String("Left operand of operator '=' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1157 $$ = new IntLiteral({ .value = 0 });
1158 } else if (!isNumber(rhs->exprType())) {
1159 PARSE_ERR(@3, (String("Right operand of operator '=' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1160 $$ = new IntLiteral({ .value = 0 });
1161 } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1162 PARSE_ERR(@2, (String("Operands of relative operations must have same unit, left operand is ") +
1163 unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1164 unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1165 $$ = new IntLiteral({ .value = 0 });
1166 } else {
1167 if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1168 PARSE_WRN(@3, "Right operand of '=' comparison is not 'final', left operand is 'final' though.");
1169 else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1170 PARSE_WRN(@1, "Left operand of '=' comparison is not 'final', right operand is 'final' though.");
1171 $$ = new Relation(lhs, Relation::EQUAL, rhs);
1172 }
1173 }
1174 | rel_expr '#' add_expr {
1175 ExpressionRef lhs = $1;
1176 ExpressionRef rhs = $3;
1177 if (!isNumber(lhs->exprType())) {
1178 PARSE_ERR(@1, (String("Left operand of operator '#' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1179 $$ = new IntLiteral({ .value = 0 });
1180 } else if (!isNumber(rhs->exprType())) {
1181 PARSE_ERR(@3, (String("Right operand of operator '#' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1182 $$ = new IntLiteral({ .value = 0 });
1183 } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1184 PARSE_ERR(@2, (String("Operands of relative operations must have same unit, left operand is ") +
1185 unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1186 unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1187 $$ = new IntLiteral({ .value = 0 });
1188 } else {
1189 if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1190 PARSE_WRN(@3, "Right operand of '#' comparison is not 'final', left operand is 'final' though.");
1191 else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1192 PARSE_WRN(@1, "Left operand of '#' comparison is not 'final', right operand is 'final' though.");
1193 $$ = new Relation(lhs, Relation::NOT_EQUAL, rhs);
1194 }
1195 }
1196
1197 add_expr:
1198 mul_expr
1199 | add_expr '+' mul_expr {
1200 ExpressionRef lhs = $1;
1201 ExpressionRef rhs = $3;
1202 if (!isNumber(lhs->exprType())) {
1203 PARSE_ERR(@1, (String("Left operand of operator '+' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1204 $$ = new IntLiteral({ .value = 0 });
1205 } else if (!isNumber(rhs->exprType())) {
1206 PARSE_ERR(@1, (String("Right operand of operator '+' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1207 $$ = new IntLiteral({ .value = 0 });
1208 } 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 $$ = new IntLiteral({ .value = 0 });
1212 } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1213 PARSE_ERR(@2, (String("Operands of '+' operations must have same unit, left operand is ") +
1214 unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1215 unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1216 $$ = new IntLiteral({ .value = 0 });
1217 } else {
1218 if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1219 PARSE_WRN(@3, "Right operand of '+' operation is not 'final', result will be 'final' though since left operand is 'final'.");
1220 else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1221 PARSE_WRN(@1, "Left operand of '+' operation is not 'final', result will be 'final' though since right operand is 'final'.");
1222 $$ = new Add(lhs,rhs);
1223 }
1224 }
1225 | add_expr '-' mul_expr {
1226 ExpressionRef lhs = $1;
1227 ExpressionRef rhs = $3;
1228 if (!isNumber(lhs->exprType())) {
1229 PARSE_ERR(@1, (String("Left operand of operator '-' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1230 $$ = new IntLiteral({ .value = 0 });
1231 } else if (!isNumber(rhs->exprType())) {
1232 PARSE_ERR(@1, (String("Right operand of operator '-' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1233 $$ = new IntLiteral({ .value = 0 });
1234 } 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 $$ = new IntLiteral({ .value = 0 });
1238 } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1239 PARSE_ERR(@2, (String("Operands of '-' operations must have same unit, left operand is ") +
1240 unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1241 unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1242 $$ = new IntLiteral({ .value = 0 });
1243 } else {
1244 if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1245 PARSE_WRN(@3, "Right operand of '-' operation is not 'final', result will be 'final' though since left operand is 'final'.");
1246 else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1247 PARSE_WRN(@1, "Left operand of '-' operation is not 'final', result will be 'final' though since right operand is 'final'.");
1248 $$ = 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 if (!isNumber(lhs->exprType())) {
1258 PARSE_ERR(@1, (String("Left operand of operator '*' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1259 $$ = new IntLiteral({ .value = 0 });
1260 } else if (!isNumber(rhs->exprType())) {
1261 PARSE_ERR(@1, (String("Right operand of operator '*' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1262 $$ = new IntLiteral({ .value = 0 });
1263 } else if (lhs->asNumber()->unitType() && rhs->asNumber()->unitType()) {
1264 PARSE_ERR(@2, (String("Only one operand of operator '*' may have a unit type, left operand is ") +
1265 unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1266 unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1267 $$ = new IntLiteral({ .value = 0 });
1268 } 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 $$ = new IntLiteral({ .value = 0 });
1272 } else {
1273 if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1274 PARSE_WRN(@3, "Right operand of '*' operation is not 'final', result will be 'final' though since left operand is 'final'.");
1275 else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1276 PARSE_WRN(@1, "Left operand of '*' operation is not 'final', result will be 'final' though since right operand is 'final'.");
1277 $$ = new Mul(lhs,rhs);
1278 }
1279 }
1280 | mul_expr '/' unary_expr {
1281 ExpressionRef lhs = $1;
1282 ExpressionRef rhs = $3;
1283 if (!isNumber(lhs->exprType())) {
1284 PARSE_ERR(@1, (String("Left operand of operator '/' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1285 $$ = new IntLiteral({ .value = 0 });
1286 } else if (!isNumber(rhs->exprType())) {
1287 PARSE_ERR(@1, (String("Right operand of operator '/' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1288 $$ = new IntLiteral({ .value = 0 });
1289 } else if (lhs->asNumber()->unitType() && rhs->asNumber()->unitType() &&
1290 lhs->asNumber()->unitType() != rhs->asNumber()->unitType())
1291 {
1292 PARSE_ERR(@2, (String("Operands of operator '/' with two different unit types, 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->asNumber()->unitType() && rhs->asNumber()->unitType()) {
1297 PARSE_ERR(@3, ("Dividing left operand without any unit type by right operand with unit type (" +
1298 unitTypeStr(rhs->asNumber()->unitType()) + ") is not possible.").c_str());
1299 $$ = new IntLiteral({ .value = 0 });
1300 } 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 $$ = new IntLiteral({ .value = 0 });
1304 } else {
1305 if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1306 PARSE_WRN(@3, "Right operand of '/' operation is not 'final', result will be 'final' though since left operand is 'final'.");
1307 else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1308 PARSE_WRN(@1, "Left operand of '/' operation is not 'final', result will be 'final' though since right operand is 'final'.");
1309 $$ = 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 PARSE_ERR(@1, (String("Left operand of modulo operator must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1317 $$ = new IntLiteral({ .value = 0 });
1318 } else if (rhs->exprType() != INT_EXPR) {
1319 PARSE_ERR(@3, (String("Right operand of modulo operator must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1320 $$ = new IntLiteral({ .value = 0 });
1321 } else {
1322 if (lhs->asInt()->unitType() || lhs->asInt()->hasUnitFactorEver())
1323 PARSE_ERR(@1, "Operands of modulo operator must not use any unit.");
1324 if (rhs->asInt()->unitType() || rhs->asInt()->hasUnitFactorEver())
1325 PARSE_ERR(@3, "Operands of modulo operator must not use any unit.");
1326 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 $$ = 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 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 }
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 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 }
1349
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 /*
1380 do_not_strip_quotes: ;
1381 */
1382 }
1383
1384 if (! yyres)
1385 return (int) yystrlen (yystr);
1386
1387 return int( yystpcpy (yyres, yystr) - yyres );
1388 }

  ViewVC Help
Powered by ViewVC