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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3564 - (hide annotations) (download)
Sat Aug 24 09:18:57 2019 UTC (4 years, 7 months ago) by schoenebeck
File size: 53619 byte(s)
NKSP: Bug fixes regarding new measurement units feature:

* Fix: Engine internal Fade of script synthesis parameters volume, pitch
  and pan were not rendered at all.

* Fix: Backward compatibility of built-in function arguments without a
  metric unit prefix was broken (resulted in incorrect value
  transformation).

* Fix: built-in script function change_play_pos() resolved wrong arguments.

* Bumped version (2.1.1.svn5).

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

  ViewVC Help
Powered by ViewVC