/[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 3790 - (show annotations) (download)
Sun Jun 14 14:29:41 2020 UTC (3 years, 10 months ago) by schoenebeck
File size: 70815 byte(s)
NKSP: Relaxed array variable declaration.

* NKSP: Just throw a warning, not an error if an array variable of
  size zero was declared.

* NKSP: Allow omitting explicit array size on array variable declaration
  if combined with immediate value assignment
  (e.g. declare %foo[] := ( 1, 2, 3 ) ).

* Bumped version (2.1.1.svn59).

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

  ViewVC Help
Powered by ViewVC