/[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 2951 - (hide annotations) (download)
Fri Jul 15 20:07:47 2016 UTC (7 years, 9 months ago) by schoenebeck
File size: 32143 byte(s)
* NKSP language: Added support for user defined script functions.
* Bumped version (2.0.0.svn18).

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

  ViewVC Help
Powered by ViewVC