/[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 3557 - (hide annotations) (download)
Sun Aug 18 00:06:04 2019 UTC (4 years, 7 months ago) by schoenebeck
File size: 38993 byte(s)
* NKSP: Introducing 64 bit support for NKSP integer scripts
  variables (declare $foo).
* Require C++11 compiler support.
* Autoconf: Added m4/ax_cxx_compile_stdcxx.m4 macro which is used
  for checking in configure for C++11 support (as mandatory
  requirement) and automatically adds compiler argument if required
  (e.g. -std=C++11).
* Bumped version (2.1.1.svn3).

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     %token <sValue> STRING "string literal"
43     %token <sValue> IDENTIFIER "function name"
44     %token <sValue> VARIABLE "variable name"
45     %token ON "keyword 'on'"
46     %token END "keyword 'end'"
47     %token INIT "keyword 'init'"
48     %token NOTE "keyword 'note'"
49     %token RELEASE "keyword 'release'"
50     %token CONTROLLER "keyword 'controller'"
51     %token DECLARE "keyword 'declare'"
52     %token ASSIGNMENT "operator ':='"
53     %token CONST_ "keyword 'const'"
54     %token POLYPHONIC "keyword 'polyphonic'"
55     %token WHILE "keyword 'while'"
56 schoenebeck 3260 %token SYNCHRONIZED "keyword 'synchronized'"
57 schoenebeck 3008 %token IF "keyword 'if'"
58     %token ELSE "keyword 'else'"
59     %token SELECT "keyword 'select'"
60     %token CASE "keyword 'case'"
61     %token TO "keyword 'to'"
62     %token OR "operator 'or'"
63     %token AND "operator 'and'"
64     %token NOT "operator 'not'"
65     %token BITWISE_OR "bitwise operator '.or.'"
66     %token BITWISE_AND "bitwise operator '.and.'"
67     %token BITWISE_NOT "bitwise operator '.not.'"
68     %token FUNCTION "keyword 'function'"
69     %token CALL "keyword 'call'"
70     %token MOD "operator 'mod'"
71     %token LE "operator '<='"
72     %token GE "operator '>='"
73     %token END_OF_FILE 0 "end of file"
74 schoenebeck 3259 %token UNKNOWN_CHAR "unknown character"
75 schoenebeck 2581
76 schoenebeck 2951 %type <nEventHandlers> script sections
77     %type <nEventHandler> section eventhandler
78     %type <nStatements> statements opt_statements userfunctioncall
79 schoenebeck 2581 %type <nStatement> statement assignment
80     %type <nFunctionCall> functioncall
81     %type <nArgs> args
82 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
83 schoenebeck 2581 %type <nCaseBranch> caseclause
84     %type <nCaseBranches> caseclauses
85    
86     %start script
87    
88     %%
89    
90     script:
91 schoenebeck 2951 sections {
92 schoenebeck 2581 $$ = context->handlers = $1;
93     }
94    
95 schoenebeck 2951 sections:
96     section {
97 schoenebeck 2581 $$ = new EventHandlers();
98 schoenebeck 2951 if ($1) $$->add($1);
99 schoenebeck 2581 }
100 schoenebeck 2951 | sections section {
101 schoenebeck 2581 $$ = $1;
102 schoenebeck 2951 if ($2) $$->add($2);
103 schoenebeck 2581 }
104    
105 schoenebeck 2951 section:
106     function_declaration {
107     $$ = EventHandlerRef();
108     }
109     | eventhandler {
110     $$ = $1;
111     }
112    
113 schoenebeck 2581 eventhandler:
114 schoenebeck 2947 ON NOTE opt_statements END ON {
115 schoenebeck 2581 if (context->onNote)
116 schoenebeck 2888 PARSE_ERR(@2, "Redeclaration of 'note' event handler.");
117 schoenebeck 2581 context->onNote = new OnNote($3);
118     $$ = context->onNote;
119     }
120 schoenebeck 2947 | ON INIT opt_statements END ON {
121 schoenebeck 2581 if (context->onInit)
122 schoenebeck 2888 PARSE_ERR(@2, "Redeclaration of 'init' event handler.");
123 schoenebeck 2581 context->onInit = new OnInit($3);
124     $$ = context->onInit;
125     }
126 schoenebeck 2947 | ON RELEASE opt_statements END ON {
127 schoenebeck 2581 if (context->onRelease)
128 schoenebeck 2888 PARSE_ERR(@2, "Redeclaration of 'release' event handler.");
129 schoenebeck 2581 context->onRelease = new OnRelease($3);
130     $$ = context->onRelease;
131     }
132 schoenebeck 2947 | ON CONTROLLER opt_statements END ON {
133 schoenebeck 2581 if (context->onController)
134 schoenebeck 2888 PARSE_ERR(@2, "Redeclaration of 'controller' event handler.");
135 schoenebeck 2581 context->onController = new OnController($3);
136     $$ = context->onController;
137     }
138    
139 schoenebeck 2951 function_declaration:
140     FUNCTION IDENTIFIER opt_statements END FUNCTION {
141     const char* name = $2;
142     if (context->functionProvider->functionByName(name)) {
143     PARSE_ERR(@2, (String("There is already a built-in function with name '") + name + "'.").c_str());
144     } else if (context->userFunctionByName(name)) {
145     PARSE_ERR(@2, (String("There is already a user defined function with name '") + name + "'.").c_str());
146     } else {
147     context->userFnTable[name] = $3;
148     }
149     }
150    
151 schoenebeck 2947 opt_statements:
152 schoenebeck 2911 /* epsilon (empty argument) */ {
153     $$ = new Statements();
154     }
155     | statements {
156     $$ = $1;
157     }
158    
159 schoenebeck 2581 statements:
160     statement {
161     $$ = new Statements();
162     if ($1) {
163     if (!isNoOperation($1)) $$->add($1); // filter out NoOperation statements
164     } else
165 schoenebeck 2888 PARSE_WRN(@1, "Not a statement.");
166 schoenebeck 2581 }
167     | statements statement {
168     $$ = $1;
169     if ($2) {
170     if (!isNoOperation($2)) $$->add($2); // filter out NoOperation statements
171     } else
172 schoenebeck 2888 PARSE_WRN(@2, "Not a statement.");
173 schoenebeck 2581 }
174    
175     statement:
176     functioncall {
177     $$ = $1;
178     }
179 schoenebeck 2951 | userfunctioncall {
180     $$ = $1;
181     }
182 schoenebeck 2581 | DECLARE VARIABLE {
183     const char* name = $2;
184     //printf("declared var '%s'\n", name);
185     if (context->variableByName(name))
186 schoenebeck 2888 PARSE_ERR(@2, (String("Redeclaration of variable '") + name + "'.").c_str());
187 schoenebeck 2581 if (name[0] == '@') {
188     context->vartable[name] = new StringVariable(context);
189     $$ = new NoOperation;
190     } else {
191     context->vartable[name] = new IntVariable(context);
192     $$ = new NoOperation;
193     }
194     }
195     | DECLARE POLYPHONIC VARIABLE {
196     const char* name = $3;
197     //printf("declared polyphonic var '%s'\n", name);
198     if (context->variableByName(name))
199 schoenebeck 2888 PARSE_ERR(@3, (String("Redeclaration of variable '") + name + "'.").c_str());
200 schoenebeck 2581 if (name[0] != '$') {
201 schoenebeck 2888 PARSE_ERR(@3, "Polyphonic variables may only be declared as integers.");
202 schoenebeck 2581 $$ = new FunctionCall("nothing", new Args, NULL); // whatever
203     } else {
204     context->vartable[name] = new PolyphonicIntVariable(context);
205     $$ = new NoOperation;
206     }
207     }
208     | DECLARE VARIABLE ASSIGNMENT expr {
209     const char* name = $2;
210     //printf("declared assign var '%s'\n", name);
211     if (context->variableByName(name))
212 schoenebeck 2888 PARSE_ERR(@2, (String("Redeclaration of variable '") + name + "'.").c_str());
213 schoenebeck 2581 if ($4->exprType() == STRING_EXPR) {
214     if (name[0] == '$')
215 schoenebeck 2888 PARSE_WRN(@2, (String("Variable '") + name + "' declared as integer, string expression assigned though.").c_str());
216 schoenebeck 2581 StringExprRef expr = $4;
217     if (expr->isConstExpr()) {
218     const String s = expr->evalStr();
219     StringVariableRef var = new StringVariable(context);
220     context->vartable[name] = var;
221     $$ = new Assignment(var, new StringLiteral(s));
222     } else {
223     StringVariableRef var = new StringVariable(context);
224     context->vartable[name] = var;
225     $$ = new Assignment(var, expr);
226     }
227     } else {
228     if (name[0] == '@')
229 schoenebeck 2888 PARSE_WRN(@2, (String("Variable '") + name + "' declared as string, integer expression assigned though.").c_str());
230 schoenebeck 2581 IntExprRef expr = $4;
231     if (expr->isConstExpr()) {
232 schoenebeck 3557 const vmint i = expr->evalInt();
233 schoenebeck 2581 IntVariableRef var = new IntVariable(context);
234     context->vartable[name] = var;
235     $$ = new Assignment(var, new IntLiteral(i));
236     } else {
237     IntVariableRef var = new IntVariable(context);
238     context->vartable[name] = var;
239     $$ = new Assignment(var, expr);
240     }
241     }
242     }
243     | DECLARE VARIABLE '[' expr ']' {
244     //printf("declare array without args\n");
245     const char* name = $2;
246     if (!$4->isConstExpr()) {
247 schoenebeck 2888 PARSE_ERR(@4, (String("Array variable '") + name + "' must be declared with constant array size.").c_str());
248 schoenebeck 2581 $$ = new FunctionCall("nothing", new Args, NULL); // whatever
249     } else if ($4->exprType() != INT_EXPR) {
250 schoenebeck 2888 PARSE_ERR(@4, (String("Size of array variable '") + name + "' declared with non integer expression.").c_str());
251 schoenebeck 2581 $$ = new FunctionCall("nothing", new Args, NULL); // whatever
252     } else if (context->variableByName(name)) {
253 schoenebeck 2888 PARSE_ERR(@2, (String("Redeclaration of variable '") + name + "'.").c_str());
254 schoenebeck 2581 $$ = new FunctionCall("nothing", new Args, NULL); // whatever
255     } else {
256     IntExprRef expr = $4;
257 schoenebeck 3557 vmint size = expr->evalInt();
258 schoenebeck 2581 if (size <= 0) {
259 schoenebeck 2888 PARSE_ERR(@4, (String("Array variable '") + name + "' declared with array size " + ToString(size) + ".").c_str());
260 schoenebeck 2581 $$ = new FunctionCall("nothing", new Args, NULL); // whatever
261     } else {
262     context->vartable[name] = new IntArrayVariable(context, size);
263     $$ = new NoOperation;
264     }
265     }
266     }
267     | DECLARE VARIABLE '[' expr ']' ASSIGNMENT '(' args ')' {
268     const char* name = $2;
269     if (!$4->isConstExpr()) {
270 schoenebeck 2888 PARSE_ERR(@4, (String("Array variable '") + name + "' must be declared with constant array size.").c_str());
271 schoenebeck 2581 $$ = new FunctionCall("nothing", new Args, NULL); // whatever
272     } else if ($4->exprType() != INT_EXPR) {
273 schoenebeck 2888 PARSE_ERR(@4, (String("Size of array variable '") + name + "' declared with non integer expression.").c_str());
274 schoenebeck 2581 $$ = new FunctionCall("nothing", new Args, NULL); // whatever
275     } else if (context->variableByName(name)) {
276 schoenebeck 2888 PARSE_ERR(@2, (String("Redeclaration of variable '") + name + "'.").c_str());
277 schoenebeck 2581 $$ = new FunctionCall("nothing", new Args, NULL); // whatever
278     } else {
279     IntExprRef sizeExpr = $4;
280     ArgsRef args = $8;
281 schoenebeck 3557 vmint size = sizeExpr->evalInt();
282 schoenebeck 2581 if (size <= 0) {
283 schoenebeck 2888 PARSE_ERR(@4, (String("Array variable '") + name + "' must be declared with positive array size.").c_str());
284 schoenebeck 2581 $$ = new FunctionCall("nothing", new Args, NULL); // whatever
285     } else if (args->argsCount() > size) {
286 schoenebeck 3257 PARSE_ERR(@8, (String("Array variable '") + name +
287 schoenebeck 2581 "' was declared with size " + ToString(size) +
288     " but " + ToString(args->argsCount()) +
289     " values were assigned." ).c_str());
290     $$ = new FunctionCall("nothing", new Args, NULL); // whatever
291     } else {
292     bool argsOK = true;
293 schoenebeck 3557 for (vmint i = 0; i < args->argsCount(); ++i) {
294 schoenebeck 2581 if (args->arg(i)->exprType() != INT_EXPR) {
295     PARSE_ERR(
296 schoenebeck 2888 @8,
297 schoenebeck 2581 (String("Array variable '") + name +
298     "' declared with invalid assignment values. Assigned element " +
299     ToString(i+1) + " is not an integer expression.").c_str()
300     );
301     argsOK = false;
302     break;
303     }
304     }
305 schoenebeck 3013 if (argsOK) {
306     context->vartable[name] = new IntArrayVariable(context, size, args);
307     $$ = new NoOperation;
308     } else
309 schoenebeck 2581 $$ = new FunctionCall("nothing", new Args, NULL); // whatever
310     }
311     }
312     }
313 schoenebeck 3257 | DECLARE CONST_ VARIABLE '[' expr ']' ASSIGNMENT '(' args ')' {
314     const char* name = $3;
315     if (!$5->isConstExpr()) {
316     PARSE_ERR(@5, (String("Array variable '") + name + "' must be declared with constant array size.").c_str());
317     $$ = new FunctionCall("nothing", new Args, NULL); // whatever
318     } else if ($5->exprType() != INT_EXPR) {
319     PARSE_ERR(@5, (String("Size of array variable '") + name + "' declared with non integer expression.").c_str());
320     $$ = new FunctionCall("nothing", new Args, NULL); // whatever
321     } else if (context->variableByName(name)) {
322     PARSE_ERR(@3, (String("Redeclaration of variable '") + name + "'.").c_str());
323     $$ = new FunctionCall("nothing", new Args, NULL); // whatever
324     } else {
325     IntExprRef sizeExpr = $5;
326     ArgsRef args = $9;
327 schoenebeck 3557 vmint size = sizeExpr->evalInt();
328 schoenebeck 3257 if (size <= 0) {
329     PARSE_ERR(@5, (String("Array variable '") + name + "' must be declared with positive array size.").c_str());
330     $$ = new FunctionCall("nothing", new Args, NULL); // whatever
331     } else if (args->argsCount() > size) {
332     PARSE_ERR(@9, (String("Array variable '") + name +
333     "' was declared with size " + ToString(size) +
334     " but " + ToString(args->argsCount()) +
335     " values were assigned." ).c_str());
336     $$ = new FunctionCall("nothing", new Args, NULL); // whatever
337     } else {
338     bool argsOK = true;
339 schoenebeck 3557 for (vmint i = 0; i < args->argsCount(); ++i) {
340 schoenebeck 3257 if (args->arg(i)->exprType() != INT_EXPR) {
341     PARSE_ERR(
342     @9,
343     (String("Array variable '") + name +
344     "' declared with invalid assignment values. Assigned element " +
345     ToString(i+1) + " is not an integer expression.").c_str()
346     );
347     argsOK = false;
348     break;
349     }
350     if (!args->arg(i)->isConstExpr()) {
351     PARSE_ERR(
352     @9,
353     (String("const array variable '") + name +
354     "' must be defined with const values. Assigned element " +
355     ToString(i+1) + " is not a const expression though.").c_str()
356     );
357     argsOK = false;
358     break;
359     }
360     }
361     if (argsOK) {
362     context->vartable[name] = new IntArrayVariable(context, size, args, true);
363     $$ = new NoOperation;
364     } else
365     $$ = new FunctionCall("nothing", new Args, NULL); // whatever
366     }
367     }
368     }
369 schoenebeck 2585 | DECLARE CONST_ VARIABLE ASSIGNMENT expr {
370 schoenebeck 2581 const char* name = $3;
371     if ($5->exprType() == STRING_EXPR) {
372     if (name[0] == '$')
373 schoenebeck 2888 PARSE_WRN(@5, "Variable declared as integer, string expression assigned though.");
374 schoenebeck 2581 String s;
375     StringExprRef expr = $5;
376     if (expr->isConstExpr())
377     s = expr->evalStr();
378     else
379 schoenebeck 2888 PARSE_ERR(@5, (String("Assignment to const string variable '") + name + "' requires const expression.").c_str());
380 schoenebeck 2581 ConstStringVariableRef var = new ConstStringVariable(context, s);
381     context->vartable[name] = var;
382     //$$ = new Assignment(var, new StringLiteral(s));
383     $$ = new NoOperation();
384     } else {
385     if (name[0] == '@')
386 schoenebeck 2888 PARSE_WRN(@5, "Variable declared as string, integer expression assigned though.");
387 schoenebeck 3557 vmint i = 0;
388 schoenebeck 2581 IntExprRef expr = $5;
389     if (expr->isConstExpr())
390     i = expr->evalInt();
391     else
392 schoenebeck 2888 PARSE_ERR(@5, (String("Assignment to const integer variable '") + name + "' requires const expression.").c_str());
393 schoenebeck 2581 ConstIntVariableRef var = new ConstIntVariable(i);
394     context->vartable[name] = var;
395     //$$ = new Assignment(var, new IntLiteral(i));
396     $$ = new NoOperation();
397     }
398     }
399     | assignment {
400     $$ = $1;
401     }
402 schoenebeck 2947 | WHILE '(' expr ')' opt_statements END WHILE {
403 schoenebeck 2581 if ($3->exprType() == INT_EXPR) {
404     $$ = new While($3, $5);
405     } else {
406 schoenebeck 2888 PARSE_ERR(@3, "Condition for 'while' loops must be integer expression.");
407 schoenebeck 2581 $$ = new While(new IntLiteral(0), $5);
408     }
409     }
410 schoenebeck 3260 | SYNCHRONIZED opt_statements END SYNCHRONIZED {
411     $$ = new SyncBlock($2);
412     }
413 schoenebeck 2947 | IF '(' expr ')' opt_statements ELSE opt_statements END IF {
414 schoenebeck 2581 $$ = new If($3, $5, $7);
415     }
416 schoenebeck 2947 | IF '(' expr ')' opt_statements END IF {
417 schoenebeck 2581 $$ = new If($3, $5);
418     }
419     | SELECT expr caseclauses END SELECT {
420     if ($2->exprType() == INT_EXPR) {
421     $$ = new SelectCase($2, $3);
422     } else {
423 schoenebeck 2888 PARSE_ERR(@2, "Statement 'select' can only by applied to integer expressions.");
424 schoenebeck 2581 $$ = new SelectCase(new IntLiteral(0), $3);
425     }
426     }
427    
428     caseclauses:
429     caseclause {
430     $$ = CaseBranches();
431     $$.push_back($1);
432     }
433     | caseclauses caseclause {
434     $$ = $1;
435     $$.push_back($2);
436     }
437    
438     caseclause:
439 schoenebeck 2947 CASE INTEGER opt_statements {
440 schoenebeck 2581 $$ = CaseBranch();
441     $$.from = new IntLiteral($2);
442     $$.statements = $3;
443     }
444 schoenebeck 2947 | CASE INTEGER TO INTEGER opt_statements {
445 schoenebeck 2581 $$ = CaseBranch();
446     $$.from = new IntLiteral($2);
447     $$.to = new IntLiteral($4);
448     $$.statements = $5;
449     }
450    
451 schoenebeck 2951 userfunctioncall:
452     CALL IDENTIFIER {
453     const char* name = $2;
454     StatementsRef fn = context->userFunctionByName(name);
455     if (context->functionProvider->functionByName(name)) {
456     PARSE_ERR(@1, (String("Keyword 'call' must only be used for user defined functions, not for any built-in function like '") + name + "'.").c_str());
457     $$ = StatementsRef();
458     } else if (!fn) {
459     PARSE_ERR(@2, (String("No user defined function with name '") + name + "'.").c_str());
460     $$ = StatementsRef();
461     } else {
462     $$ = fn;
463     }
464     }
465    
466 schoenebeck 2581 functioncall:
467     IDENTIFIER '(' args ')' {
468     const char* name = $1;
469     //printf("function call of '%s' with args\n", name);
470     ArgsRef args = $3;
471     VMFunction* fn = context->functionProvider->functionByName(name);
472 schoenebeck 2951 if (context->userFunctionByName(name)) {
473     PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
474     $$ = new FunctionCall(name, args, NULL);
475     } else if (!fn) {
476 schoenebeck 2888 PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());
477 schoenebeck 2581 $$ = new FunctionCall(name, args, NULL);
478 schoenebeck 3311 } else if (context->functionProvider->isFunctionDisabled(fn,context)) {
479     PARSE_DROP(@$);
480     $$ = new NoFunctionCall;
481 schoenebeck 2581 } else if (args->argsCount() < fn->minRequiredArgs()) {
482 schoenebeck 2888 PARSE_ERR(@3, (String("Built-in function '") + name + "' requires at least " + ToString(fn->minRequiredArgs()) + " arguments.").c_str());
483 schoenebeck 2581 $$ = new FunctionCall(name, args, NULL);
484     } else if (args->argsCount() > fn->maxAllowedArgs()) {
485 schoenebeck 2888 PARSE_ERR(@3, (String("Built-in function '") + name + "' accepts max. " + ToString(fn->maxAllowedArgs()) + " arguments.").c_str());
486 schoenebeck 2581 $$ = new FunctionCall(name, args, NULL);
487     } else {
488     bool argsOK = true;
489 schoenebeck 3557 for (vmint i = 0; i < args->argsCount(); ++i) {
490 schoenebeck 2581 if (args->arg(i)->exprType() != fn->argType(i) && !fn->acceptsArgType(i, args->arg(i)->exprType())) {
491 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());
492 schoenebeck 2581 argsOK = false;
493     break;
494 schoenebeck 2945 } else if (fn->modifiesArg(i) && !args->arg(i)->isModifyable()) {
495     PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' expects an assignable variable.").c_str());
496     argsOK = false;
497     break;
498 schoenebeck 2581 }
499     }
500     $$ = new FunctionCall(name, args, argsOK ? fn : NULL);
501     }
502     }
503     | IDENTIFIER '(' ')' {
504     const char* name = $1;
505     //printf("function call of '%s' (with empty args)\n", name);
506     ArgsRef args = new Args;
507     VMFunction* fn = context->functionProvider->functionByName(name);
508 schoenebeck 2951 if (context->userFunctionByName(name)) {
509     PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
510     $$ = new FunctionCall(name, args, NULL);
511     } else if (!fn) {
512 schoenebeck 2888 PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());
513 schoenebeck 2581 $$ = new FunctionCall(name, args, NULL);
514 schoenebeck 3311 } else if (context->functionProvider->isFunctionDisabled(fn,context)) {
515     PARSE_DROP(@$);
516     $$ = new NoFunctionCall;
517 schoenebeck 2581 } else if (fn->minRequiredArgs() > 0) {
518 schoenebeck 2888 PARSE_ERR(@3, (String("Built-in function '") + name + "' requires at least " + ToString(fn->minRequiredArgs()) + " arguments.").c_str());
519 schoenebeck 2581 $$ = new FunctionCall(name, args, NULL);
520     } else {
521     $$ = new FunctionCall(name, args, fn);
522     }
523     }
524     | IDENTIFIER {
525     const char* name = $1;
526     //printf("function call of '%s' (without args)\n", name);
527     ArgsRef args = new Args;
528     VMFunction* fn = context->functionProvider->functionByName(name);
529 schoenebeck 2951 if (context->userFunctionByName(name)) {
530     PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
531     $$ = new FunctionCall(name, args, NULL);
532     } else if (!fn) {
533 schoenebeck 2888 PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());
534 schoenebeck 2581 $$ = new FunctionCall(name, args, NULL);
535 schoenebeck 3311 } else if (context->functionProvider->isFunctionDisabled(fn,context)) {
536     PARSE_DROP(@$);
537     $$ = new NoFunctionCall;
538 schoenebeck 2581 } else if (fn->minRequiredArgs() > 0) {
539 schoenebeck 2888 PARSE_ERR(@1, (String("Built-in function '") + name + "' requires at least " + ToString(fn->minRequiredArgs()) + " arguments.").c_str());
540 schoenebeck 2581 $$ = new FunctionCall(name, args, NULL);
541     } else {
542     $$ = new FunctionCall(name, args, fn);
543     }
544     }
545    
546     args:
547     arg {
548     $$ = new Args();
549     $$->add($1);
550     }
551     | args ',' arg {
552     $$ = $1;
553     $$->add($3);
554     }
555    
556     arg:
557     expr
558    
559     assignment:
560     VARIABLE ASSIGNMENT expr {
561     //printf("variable lookup with name '%s' as assignment expr\n", $1);
562     const char* name = $1;
563     VariableRef var = context->variableByName(name);
564     if (!var)
565 schoenebeck 2888 PARSE_ERR(@1, (String("Variable assignment: No variable declared with name '") + name + "'.").c_str());
566 schoenebeck 2581 else if (var->isConstExpr())
567 schoenebeck 2888 PARSE_ERR(@2, (String("Variable assignment: Cannot modify const variable '") + name + "'.").c_str());
568 schoenebeck 2942 else if (!var->isAssignable())
569     PARSE_ERR(@2, (String("Variable assignment: Variable '") + name + "' is not assignable.").c_str());
570 schoenebeck 2581 else if (var->exprType() != $3->exprType())
571 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());
572 schoenebeck 2581 $$ = new Assignment(var, $3);
573     }
574     | VARIABLE '[' expr ']' ASSIGNMENT expr {
575     const char* name = $1;
576     VariableRef var = context->variableByName(name);
577     if (!var)
578 schoenebeck 2888 PARSE_ERR(@1, (String("No variable declared with name '") + name + "'.").c_str());
579 schoenebeck 2581 else if (var->exprType() != INT_ARR_EXPR)
580 schoenebeck 2888 PARSE_ERR(@2, (String("Variable '") + name + "' is not an array variable.").c_str());
581 schoenebeck 3253 else if (var->isConstExpr())
582     PARSE_ERR(@5, (String("Variable assignment: Cannot modify const array variable '") + name + "'.").c_str());
583     else if (!var->isAssignable())
584     PARSE_ERR(@5, (String("Variable assignment: Array variable '") + name + "' is not assignable.").c_str());
585 schoenebeck 2581 else if ($3->exprType() != INT_EXPR)
586 schoenebeck 2888 PARSE_ERR(@3, (String("Array variable '") + name + "' accessed with non integer expression.").c_str());
587 schoenebeck 2581 else if ($6->exprType() != INT_EXPR)
588 schoenebeck 2888 PARSE_ERR(@5, (String("Value assigned to array variable '") + name + "' must be an integer expression.").c_str());
589 schoenebeck 3257 else if ($3->isConstExpr() && $3->asInt()->evalInt() >= ((IntArrayVariableRef)var)->arraySize())
590     PARSE_WRN(@3, (String("Index ") + ToString($3->asInt()->evalInt()) +
591     " exceeds size of array variable '" + name +
592     "' which was declared with size " +
593     ToString(((IntArrayVariableRef)var)->arraySize()) + ".").c_str());
594 schoenebeck 2581 IntArrayElementRef element = new IntArrayElement(var, $3);
595     $$ = new Assignment(element, $6);
596     }
597    
598     unary_expr:
599     INTEGER {
600     $$ = new IntLiteral($1);
601     }
602     | STRING {
603     $$ = new StringLiteral($1);
604     }
605     | VARIABLE {
606     //printf("variable lookup with name '%s' as unary expr\n", $1);
607     VariableRef var = context->variableByName($1);
608     if (var)
609     $$ = var;
610     else {
611 schoenebeck 2888 PARSE_ERR(@1, (String("No variable declared with name '") + $1 + "'.").c_str());
612 schoenebeck 2581 $$ = new IntLiteral(0);
613     }
614     }
615     | VARIABLE '[' expr ']' {
616     const char* name = $1;
617     VariableRef var = context->variableByName(name);
618     if (!var) {
619 schoenebeck 2888 PARSE_ERR(@1, (String("No variable declared with name '") + name + "'.").c_str());
620 schoenebeck 2581 $$ = new IntLiteral(0);
621     } else if (var->exprType() != INT_ARR_EXPR) {
622 schoenebeck 2888 PARSE_ERR(@2, (String("Variable '") + name + "' is not an array variable.").c_str());
623 schoenebeck 2581 $$ = new IntLiteral(0);
624     } else if ($3->exprType() != INT_EXPR) {
625 schoenebeck 2888 PARSE_ERR(@3, (String("Array variable '") + name + "' accessed with non integer expression.").c_str());
626 schoenebeck 2581 $$ = new IntLiteral(0);
627     } else {
628 schoenebeck 3257 if ($3->isConstExpr() && $3->asInt()->evalInt() >= ((IntArrayVariableRef)var)->arraySize())
629     PARSE_WRN(@3, (String("Index ") + ToString($3->asInt()->evalInt()) +
630     " exceeds size of array variable '" + name +
631     "' which was declared with size " +
632     ToString(((IntArrayVariableRef)var)->arraySize()) + ".").c_str());
633 schoenebeck 2581 $$ = new IntArrayElement(var, $3);
634     }
635     }
636     | '(' expr ')' {
637     $$ = $2;
638     }
639     | functioncall {
640     $$ = $1;
641     }
642     | '-' unary_expr {
643     $$ = new Neg($2);
644     }
645 schoenebeck 2935 | BITWISE_NOT unary_expr {
646     if ($2->exprType() != INT_EXPR) {
647     PARSE_ERR(@2, (String("Right operand of bitwise operator '.not.' must be an integer expression, is ") + typeStr($2->exprType()) + " though.").c_str());
648     $$ = new IntLiteral(0);
649     } else {
650     $$ = new BitwiseNot($2);
651     }
652     }
653 schoenebeck 2581 | NOT unary_expr {
654     if ($2->exprType() != INT_EXPR) {
655 schoenebeck 2888 PARSE_ERR(@2, (String("Right operand of operator 'not' must be an integer expression, is ") + typeStr($2->exprType()) + " though.").c_str());
656 schoenebeck 2581 $$ = new IntLiteral(0);
657     } else {
658     $$ = new Not($2);
659     }
660     }
661    
662     expr:
663     concat_expr
664    
665     concat_expr:
666 schoenebeck 2935 logical_or_expr
667     | concat_expr '&' logical_or_expr {
668 schoenebeck 2581 ExpressionRef lhs = $1;
669     ExpressionRef rhs = $3;
670     if (lhs->isConstExpr() && rhs->isConstExpr()) {
671     $$ = new StringLiteral(
672     lhs->evalCastToStr() + rhs->evalCastToStr()
673     );
674     } else {
675     $$ = new ConcatString(lhs, rhs);
676     }
677     }
678    
679 schoenebeck 2935 logical_or_expr:
680     logical_and_expr
681     | logical_or_expr OR logical_and_expr {
682 schoenebeck 2581 ExpressionRef lhs = $1;
683     ExpressionRef rhs = $3;
684     if (lhs->exprType() != INT_EXPR) {
685 schoenebeck 2888 PARSE_ERR(@1, (String("Left operand of operator 'or' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
686 schoenebeck 2581 $$ = new IntLiteral(0);
687     } else if (rhs->exprType() != INT_EXPR) {
688 schoenebeck 2888 PARSE_ERR(@3, (String("Right operand of operator 'or' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
689 schoenebeck 2581 $$ = new IntLiteral(0);
690     } else {
691     $$ = new Or(lhs, rhs);
692     }
693     }
694    
695 schoenebeck 2935 logical_and_expr:
696     bitwise_or_expr {
697 schoenebeck 2581 $$ = $1;
698     }
699 schoenebeck 2935 | logical_and_expr AND bitwise_or_expr {
700 schoenebeck 2581 ExpressionRef lhs = $1;
701     ExpressionRef rhs = $3;
702     if (lhs->exprType() != INT_EXPR) {
703 schoenebeck 2888 PARSE_ERR(@1, (String("Left operand of operator 'and' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
704 schoenebeck 2581 $$ = new IntLiteral(0);
705     } else if (rhs->exprType() != INT_EXPR) {
706 schoenebeck 2888 PARSE_ERR(@3, (String("Right operand of operator 'and' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
707 schoenebeck 2581 $$ = new IntLiteral(0);
708     } else {
709     $$ = new And(lhs, rhs);
710     }
711     }
712    
713 schoenebeck 2935 bitwise_or_expr:
714     bitwise_and_expr
715     | bitwise_or_expr BITWISE_OR bitwise_and_expr {
716     ExpressionRef lhs = $1;
717     ExpressionRef rhs = $3;
718     if (lhs->exprType() != INT_EXPR) {
719     PARSE_ERR(@1, (String("Left operand of bitwise operator '.or.' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
720     $$ = new IntLiteral(0);
721     } else if (rhs->exprType() != INT_EXPR) {
722     PARSE_ERR(@3, (String("Right operand of bitwise operator '.or.' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
723     $$ = new IntLiteral(0);
724     } else {
725     $$ = new BitwiseOr(lhs, rhs);
726     }
727     }
728    
729     bitwise_and_expr:
730     rel_expr {
731     $$ = $1;
732     }
733     | bitwise_and_expr BITWISE_AND rel_expr {
734     ExpressionRef lhs = $1;
735     ExpressionRef rhs = $3;
736     if (lhs->exprType() != INT_EXPR) {
737     PARSE_ERR(@1, (String("Left operand of bitwise operator '.and.' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
738     $$ = new IntLiteral(0);
739     } else if (rhs->exprType() != INT_EXPR) {
740     PARSE_ERR(@3, (String("Right operand of bitwise operator '.and.' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
741     $$ = new IntLiteral(0);
742     } else {
743     $$ = new BitwiseAnd(lhs, rhs);
744     }
745     }
746    
747 schoenebeck 2581 rel_expr:
748     add_expr
749     | rel_expr '<' add_expr {
750     ExpressionRef lhs = $1;
751     ExpressionRef rhs = $3;
752     if (lhs->exprType() != INT_EXPR) {
753 schoenebeck 2888 PARSE_ERR(@1, (String("Left operand of operator '<' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
754 schoenebeck 2581 $$ = new IntLiteral(0);
755     } else if (rhs->exprType() != INT_EXPR) {
756 schoenebeck 2888 PARSE_ERR(@3, (String("Right operand of operator '<' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
757 schoenebeck 2581 $$ = new IntLiteral(0);
758     } else {
759     $$ = new Relation(lhs, Relation::LESS_THAN, rhs);
760     }
761     }
762     | rel_expr '>' add_expr {
763     ExpressionRef lhs = $1;
764     ExpressionRef rhs = $3;
765     if (lhs->exprType() != INT_EXPR) {
766 schoenebeck 2888 PARSE_ERR(@1, (String("Left operand of operator '>' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
767 schoenebeck 2581 $$ = new IntLiteral(0);
768     } else if (rhs->exprType() != INT_EXPR) {
769 schoenebeck 2888 PARSE_ERR(@3, (String("Right operand of operator '>' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
770 schoenebeck 2581 $$ = new IntLiteral(0);
771     } else {
772     $$ = new Relation(lhs, Relation::GREATER_THAN, rhs);
773     }
774     }
775     | rel_expr LE add_expr {
776     ExpressionRef lhs = $1;
777     ExpressionRef rhs = $3;
778     if (lhs->exprType() != INT_EXPR) {
779 schoenebeck 2888 PARSE_ERR(@1, (String("Left operand of operator '<=' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
780 schoenebeck 2581 $$ = new IntLiteral(0);
781     } else if (rhs->exprType() != INT_EXPR) {
782 schoenebeck 2888 PARSE_ERR(@3, (String("Right operand of operator '<=' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
783 schoenebeck 2581 $$ = new IntLiteral(0);
784     } else {
785     $$ = new Relation(lhs, Relation::LESS_OR_EQUAL, rhs);
786     }
787     }
788     | rel_expr GE add_expr {
789     ExpressionRef lhs = $1;
790     ExpressionRef rhs = $3;
791     if (lhs->exprType() != INT_EXPR) {
792 schoenebeck 2888 PARSE_ERR(@1, (String("Left operand of operator '>=' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
793 schoenebeck 2581 $$ = new IntLiteral(0);
794     } else if (rhs->exprType() != INT_EXPR) {
795 schoenebeck 2888 PARSE_ERR(@3, (String("Right operand of operator '>=' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
796 schoenebeck 2581 $$ = new IntLiteral(0);
797     } else {
798     $$ = new Relation(lhs, Relation::GREATER_OR_EQUAL, rhs);
799     }
800     }
801     | rel_expr '=' add_expr {
802     $$ = new Relation($1, Relation::EQUAL, $3);
803     }
804     | rel_expr '#' add_expr {
805     $$ = new Relation($1, Relation::NOT_EQUAL, $3);
806     }
807    
808     add_expr:
809     mul_expr
810     | add_expr '+' mul_expr {
811     ExpressionRef lhs = $1;
812     ExpressionRef rhs = $3;
813     if (lhs->exprType() != INT_EXPR) {
814 schoenebeck 2888 PARSE_ERR(@1, (String("Left operand of operator '+' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
815 schoenebeck 2581 $$ = new IntLiteral(0);
816     } else if (rhs->exprType() != INT_EXPR) {
817 schoenebeck 2888 PARSE_ERR(@3, (String("Right operand of operator '+' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
818 schoenebeck 2581 $$ = new IntLiteral(0);
819     } else {
820     $$ = new Add(lhs,rhs);
821     }
822     }
823     | add_expr '-' mul_expr {
824     ExpressionRef lhs = $1;
825     ExpressionRef rhs = $3;
826     if (lhs->exprType() != INT_EXPR) {
827 schoenebeck 2888 PARSE_ERR(@1, (String("Left operand of operator '-' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
828 schoenebeck 2581 $$ = new IntLiteral(0);
829     } else if (rhs->exprType() != INT_EXPR) {
830 schoenebeck 2888 PARSE_ERR(@3, (String("Right operand of operator '-' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
831 schoenebeck 2581 $$ = new IntLiteral(0);
832     } else {
833     $$ = new Sub(lhs,rhs);
834     }
835     }
836    
837     mul_expr:
838     unary_expr
839     | mul_expr '*' unary_expr {
840     ExpressionRef lhs = $1;
841     ExpressionRef rhs = $3;
842     if (lhs->exprType() != INT_EXPR) {
843 schoenebeck 2888 PARSE_ERR(@1, (String("Left operand of operator '*' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
844 schoenebeck 2581 $$ = new IntLiteral(0);
845     } else if (rhs->exprType() != INT_EXPR) {
846 schoenebeck 2888 PARSE_ERR(@3, (String("Right operand of operator '*' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
847 schoenebeck 2581 $$ = new IntLiteral(0);
848     } else {
849     $$ = new Mul(lhs,rhs);
850     }
851     }
852     | mul_expr '/' unary_expr {
853     ExpressionRef lhs = $1;
854     ExpressionRef rhs = $3;
855     if (lhs->exprType() != INT_EXPR) {
856 schoenebeck 2888 PARSE_ERR(@1, (String("Left operand of operator '/' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
857 schoenebeck 2581 $$ = new IntLiteral(0);
858     } else if (rhs->exprType() != INT_EXPR) {
859 schoenebeck 2888 PARSE_ERR(@3, (String("Right operand of operator '/' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
860 schoenebeck 2581 $$ = new IntLiteral(0);
861     } else {
862     $$ = new Div(lhs,rhs);
863     }
864     }
865     | mul_expr MOD unary_expr {
866     ExpressionRef lhs = $1;
867     ExpressionRef rhs = $3;
868     if (lhs->exprType() != INT_EXPR) {
869 schoenebeck 2888 PARSE_ERR(@1, (String("Left operand of modulo operator must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
870 schoenebeck 2581 $$ = new IntLiteral(0);
871     } else if (rhs->exprType() != INT_EXPR) {
872 schoenebeck 2888 PARSE_ERR(@3, (String("Right operand of modulo operator must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
873 schoenebeck 2581 $$ = new IntLiteral(0);
874     } else {
875     $$ = new Mod(lhs,rhs);
876     }
877     }
878    
879     %%
880    
881     void InstrScript_error(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* err) {
882     //fprintf(stderr, "%d: %s\n", locp->first_line, err);
883 schoenebeck 2889 context->addErr(locp->first_line, locp->last_line, locp->first_column+1, locp->last_column+1, err);
884 schoenebeck 2581 }
885    
886     void InstrScript_warning(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* txt) {
887     //fprintf(stderr, "WRN %d: %s\n", locp->first_line, txt);
888 schoenebeck 2889 context->addWrn(locp->first_line, locp->last_line, locp->first_column+1, locp->last_column+1, txt);
889 schoenebeck 2581 }
890 schoenebeck 3008
891     /// Custom implementation of yytnamerr() to ensure quotation is always stripped from token names before printing them to error messages.
892     int InstrScript_tnamerr(char* yyres, const char* yystr) {
893     if (*yystr == '"') {
894     int yyn = 0;
895     char const *yyp = yystr;
896     for (;;)
897     switch (*++yyp)
898     {
899     /*
900     case '\'':
901     case ',':
902     goto do_not_strip_quotes;
903    
904     case '\\':
905     if (*++yyp != '\\')
906     goto do_not_strip_quotes;
907     */
908     /* Fall through. */
909     default:
910     if (yyres)
911     yyres[yyn] = *yyp;
912     yyn++;
913     break;
914    
915     case '"':
916     if (yyres)
917     yyres[yyn] = '\0';
918     return yyn;
919     }
920 schoenebeck 3034 /*
921 schoenebeck 3008 do_not_strip_quotes: ;
922 schoenebeck 3034 */
923 schoenebeck 3008 }
924    
925     if (! yyres)
926 schoenebeck 3054 return (int) yystrlen (yystr);
927 schoenebeck 3008
928 schoenebeck 3054 return int( yystpcpy (yyres, yystr) - yyres );
929 schoenebeck 3008 }

  ViewVC Help
Powered by ViewVC