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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2888 by schoenebeck, Sun Apr 24 18:16:10 2016 UTC revision 3259 by schoenebeck, Wed May 31 14:41:04 2017 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (c) 2014-2016 Christian Schoenebeck and Andreas Persson   * Copyright (c) 2014-2017 Christian Schoenebeck and Andreas Persson
3   *   *
4   * http://www.linuxsampler.org   * http://www.linuxsampler.org
5   *   *
# Line 18  Line 18 
18    
19      void InstrScript_error(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* err);      void InstrScript_error(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* err);
20      void InstrScript_warning(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* txt);      void InstrScript_warning(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* txt);
21        int InstrScript_tnamerr(char* yyres, const char* yystr);
22      int InstrScript_lex(YYSTYPE* lvalp, YYLTYPE* llocp, void* scanner);      int InstrScript_lex(YYSTYPE* lvalp, YYLTYPE* llocp, void* scanner);
23      #define scanner context->scanner      #define scanner context->scanner
24      #define PARSE_ERR(loc,txt)  yyerror(&loc, context, txt)      #define PARSE_ERR(loc,txt)  yyerror(&loc, context, txt)
25      #define PARSE_WRN(loc,txt)  InstrScript_warning(&loc, context, txt)      #define PARSE_WRN(loc,txt)  InstrScript_warning(&loc, context, txt)
26        #define yytnamerr(res,str)  InstrScript_tnamerr(res, str)
27  %}  %}
28    
29  // generate reentrant safe parser  // generate reentrant safe parser
# Line 29  Line 31 
31  %parse-param { LinuxSampler::ParserContext* context }  %parse-param { LinuxSampler::ParserContext* context }
32  %lex-param { void* scanner }  %lex-param { void* scanner }
33  // avoid symbol collision with other (i.e. future) auto generated (f)lex scanners  // avoid symbol collision with other (i.e. future) auto generated (f)lex scanners
34  %name-prefix "InstrScript_"  // (NOTE: "=" is deprecated here with Bison 3.x, however removing it would cause an error with Bison 2.x)
35    %name-prefix="InstrScript_"
36  %locations  %locations
37  %defines  %defines
38  %error-verbose  %error-verbose
39    
40  %token <iValue> INTEGER  %token <iValue> INTEGER "integer literal"
41  %token <sValue> STRING  %token <sValue> STRING "string literal"
42  %token <sValue> IDENTIFIER  %token <sValue> IDENTIFIER "function name"
43  %token <sValue> VARIABLE  %token <sValue> VARIABLE "variable name"
44  %token ON END INIT NOTE DECLARE ASSIGNMENT WHILE IF OR RELEASE AND ELSE  %token ON "keyword 'on'"
45  %token CONTROLLER SELECT CASE TO NOT CONST_ POLYPHONIC MOD  %token END "keyword 'end'"
46  %token LE GE  %token INIT "keyword 'init'"
47    %token NOTE "keyword 'note'"
48  %type <nEventHandlers> script eventhandlers  %token RELEASE "keyword 'release'"
49  %type <nEventHandler> eventhandler  %token CONTROLLER "keyword 'controller'"
50  %type <nStatements> statements  %token DECLARE "keyword 'declare'"
51    %token ASSIGNMENT "operator ':='"
52    %token CONST_ "keyword 'const'"
53    %token POLYPHONIC "keyword 'polyphonic'"
54    %token WHILE "keyword 'while'"
55    %token IF "keyword 'if'"
56    %token ELSE "keyword 'else'"
57    %token SELECT "keyword 'select'"
58    %token CASE "keyword 'case'"
59    %token TO "keyword 'to'"
60    %token OR "operator 'or'"
61    %token AND "operator 'and'"
62    %token NOT "operator 'not'"
63    %token BITWISE_OR "bitwise operator '.or.'"
64    %token BITWISE_AND "bitwise operator '.and.'"
65    %token BITWISE_NOT "bitwise operator '.not.'"
66    %token FUNCTION "keyword 'function'"
67    %token CALL "keyword 'call'"
68    %token MOD "operator 'mod'"
69    %token LE "operator '<='"
70    %token GE "operator '>='"
71    %token END_OF_FILE 0 "end of file"
72    %token UNKNOWN_CHAR "unknown character"
73    
74    %type <nEventHandlers> script sections
75    %type <nEventHandler> section eventhandler
76    %type <nStatements> statements opt_statements userfunctioncall
77  %type <nStatement> statement assignment  %type <nStatement> statement assignment
78  %type <nFunctionCall> functioncall  %type <nFunctionCall> functioncall
79  %type <nArgs> args  %type <nArgs> args
80  %type <nExpression> arg expr or_expr and_expr rel_expr add_expr mul_expr unary_expr concat_expr  %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
81  %type <nCaseBranch> caseclause  %type <nCaseBranch> caseclause
82  %type <nCaseBranches> caseclauses  %type <nCaseBranches> caseclauses
83    
# Line 57  Line 86 
86  %%  %%
87    
88  script:  script:
89      eventhandlers  {      sections  {
90          $$ = context->handlers = $1;          $$ = context->handlers = $1;
91      }      }
92    
93  eventhandlers:  sections:
94      eventhandler  {      section  {
95          $$ = new EventHandlers();          $$ = new EventHandlers();
96          $$->add($1);          if ($1) $$->add($1);
97      }      }
98      | eventhandlers eventhandler  {      | sections section  {
99            $$ = $1;
100            if ($2) $$->add($2);
101        }
102    
103    section:
104        function_declaration  {
105            $$ = EventHandlerRef();
106        }
107        | eventhandler  {
108          $$ = $1;          $$ = $1;
         $$->add($2);  
109      }      }
110    
111  eventhandler:  eventhandler:
112      ON NOTE statements END ON  {      ON NOTE opt_statements END ON  {
113          if (context->onNote)          if (context->onNote)
114              PARSE_ERR(@2, "Redeclaration of 'note' event handler.");              PARSE_ERR(@2, "Redeclaration of 'note' event handler.");
115          context->onNote = new OnNote($3);          context->onNote = new OnNote($3);
116          $$ = context->onNote;          $$ = context->onNote;
117      }      }
118      | ON INIT statements END ON  {      | ON INIT opt_statements END ON  {
119          if (context->onInit)          if (context->onInit)
120              PARSE_ERR(@2, "Redeclaration of 'init' event handler.");              PARSE_ERR(@2, "Redeclaration of 'init' event handler.");
121          context->onInit = new OnInit($3);          context->onInit = new OnInit($3);
122          $$ = context->onInit;          $$ = context->onInit;
123      }      }
124      | ON RELEASE statements END ON  {      | ON RELEASE opt_statements END ON  {
125          if (context->onRelease)          if (context->onRelease)
126              PARSE_ERR(@2, "Redeclaration of 'release' event handler.");              PARSE_ERR(@2, "Redeclaration of 'release' event handler.");
127          context->onRelease = new OnRelease($3);          context->onRelease = new OnRelease($3);
128          $$ = context->onRelease;          $$ = context->onRelease;
129      }      }
130      | ON CONTROLLER statements END ON  {      | ON CONTROLLER opt_statements END ON  {
131          if (context->onController)          if (context->onController)
132              PARSE_ERR(@2, "Redeclaration of 'controller' event handler.");              PARSE_ERR(@2, "Redeclaration of 'controller' event handler.");
133          context->onController = new OnController($3);          context->onController = new OnController($3);
134          $$ = context->onController;          $$ = context->onController;
135      }      }
136    
137    function_declaration:
138        FUNCTION IDENTIFIER opt_statements END FUNCTION  {
139            const char* name = $2;
140            if (context->functionProvider->functionByName(name)) {
141                PARSE_ERR(@2, (String("There is already a built-in function with name '") + name + "'.").c_str());
142            } else if (context->userFunctionByName(name)) {
143                PARSE_ERR(@2, (String("There is already a user defined function with name '") + name + "'.").c_str());
144            } else {
145                context->userFnTable[name] = $3;
146            }
147        }
148    
149    opt_statements:
150        /* epsilon (empty argument) */  {
151            $$ = new Statements();
152        }
153        | statements  {
154            $$ = $1;
155        }
156    
157  statements:  statements:
158      statement  {      statement  {
159          $$ = new Statements();          $$ = new Statements();
# Line 117  statement: Line 174  statement:
174      functioncall  {      functioncall  {
175          $$ = $1;          $$ = $1;
176      }      }
177        | userfunctioncall  {
178            $$ = $1;
179        }
180      | DECLARE VARIABLE  {      | DECLARE VARIABLE  {
181          const char* name = $2;          const char* name = $2;
182          //printf("declared var '%s'\n", name);          //printf("declared var '%s'\n", name);
# Line 221  statement: Line 281  statement:
281                  PARSE_ERR(@4, (String("Array variable '") + name + "' must be declared with positive array size.").c_str());                  PARSE_ERR(@4, (String("Array variable '") + name + "' must be declared with positive array size.").c_str());
282                  $$ = new FunctionCall("nothing", new Args, NULL); // whatever                  $$ = new FunctionCall("nothing", new Args, NULL); // whatever
283              } else if (args->argsCount() > size) {              } else if (args->argsCount() > size) {
284                  PARSE_ERR(@8, (String("Variable '") + name +                  PARSE_ERR(@8, (String("Array variable '") + name +
285                            "' was declared with size " + ToString(size) +                            "' was declared with size " + ToString(size) +
286                            " but " + ToString(args->argsCount()) +                            " but " + ToString(args->argsCount()) +
287                            " values were assigned." ).c_str());                            " values were assigned." ).c_str());
# Line 240  statement: Line 300  statement:
300                          break;                          break;
301                      }                      }
302                  }                  }
303                  if (argsOK)                  if (argsOK) {
304                      $$ = context->vartable[name] = new IntArrayVariable(context, size, args);                      context->vartable[name] = new IntArrayVariable(context, size, args);
305                  else                      $$ = new NoOperation;
306                    } else
307                        $$ = new FunctionCall("nothing", new Args, NULL); // whatever
308                }
309            }
310        }
311        | DECLARE CONST_ VARIABLE '[' expr ']' ASSIGNMENT '(' args ')'  {
312            const char* name = $3;
313            if (!$5->isConstExpr()) {
314                PARSE_ERR(@5, (String("Array variable '") + name + "' must be declared with constant array size.").c_str());
315                $$ = new FunctionCall("nothing", new Args, NULL); // whatever
316            } else if ($5->exprType() != INT_EXPR) {
317                PARSE_ERR(@5, (String("Size of array variable '") + name + "' declared with non integer expression.").c_str());
318                $$ = new FunctionCall("nothing", new Args, NULL); // whatever
319            } else if (context->variableByName(name)) {
320                PARSE_ERR(@3, (String("Redeclaration of variable '") + name + "'.").c_str());
321                $$ = new FunctionCall("nothing", new Args, NULL); // whatever
322            } else {
323                IntExprRef sizeExpr = $5;
324                ArgsRef args = $9;
325                int size = sizeExpr->evalInt();
326                if (size <= 0) {
327                    PARSE_ERR(@5, (String("Array variable '") + name + "' must be declared with positive array size.").c_str());
328                    $$ = new FunctionCall("nothing", new Args, NULL); // whatever
329                } else if (args->argsCount() > size) {
330                    PARSE_ERR(@9, (String("Array variable '") + name +
331                              "' was declared with size " + ToString(size) +
332                              " but " + ToString(args->argsCount()) +
333                              " values were assigned." ).c_str());
334                    $$ = new FunctionCall("nothing", new Args, NULL); // whatever          
335                } else {
336                    bool argsOK = true;
337                    for (int i = 0; i < args->argsCount(); ++i) {
338                        if (args->arg(i)->exprType() != INT_EXPR) {
339                            PARSE_ERR(
340                                @9,
341                                (String("Array variable '") + name +
342                                "' declared with invalid assignment values. Assigned element " +
343                                ToString(i+1) + " is not an integer expression.").c_str()
344                            );
345                            argsOK = false;
346                            break;
347                        }
348                        if (!args->arg(i)->isConstExpr()) {
349                            PARSE_ERR(
350                                @9,
351                                (String("const array variable '") + name +
352                                "' must be defined with const values. Assigned element " +
353                                ToString(i+1) + " is not a const expression though.").c_str()
354                            );
355                            argsOK = false;
356                            break;
357                        }
358                    }
359                    if (argsOK) {
360                        context->vartable[name] = new IntArrayVariable(context, size, args, true);
361                        $$ = new NoOperation;
362                    } else
363                      $$ = new FunctionCall("nothing", new Args, NULL); // whatever                      $$ = new FunctionCall("nothing", new Args, NULL); // whatever
364              }              }
365          }          }
# Line 280  statement: Line 397  statement:
397      | assignment  {      | assignment  {
398          $$ = $1;          $$ = $1;
399      }      }
400      | WHILE '(' expr ')' statements END WHILE  {      | WHILE '(' expr ')' opt_statements END WHILE  {
401          if ($3->exprType() == INT_EXPR) {          if ($3->exprType() == INT_EXPR) {
402              $$ = new While($3, $5);              $$ = new While($3, $5);
403          } else {          } else {
# Line 288  statement: Line 405  statement:
405              $$ = new While(new IntLiteral(0), $5);              $$ = new While(new IntLiteral(0), $5);
406          }          }
407      }      }
408      | IF '(' expr ')' statements ELSE statements END IF  {      | IF '(' expr ')' opt_statements ELSE opt_statements END IF  {
409          $$ = new If($3, $5, $7);          $$ = new If($3, $5, $7);
410      }      }
411      | IF '(' expr ')' statements END IF  {      | IF '(' expr ')' opt_statements END IF  {
412          $$ = new If($3, $5);          $$ = new If($3, $5);
413      }      }
414      | SELECT expr caseclauses END SELECT  {      | SELECT expr caseclauses END SELECT  {
# Line 314  caseclauses: Line 431  caseclauses:
431      }      }
432    
433  caseclause:  caseclause:
434      CASE INTEGER statements  {      CASE INTEGER opt_statements  {
435          $$ = CaseBranch();          $$ = CaseBranch();
436          $$.from = new IntLiteral($2);          $$.from = new IntLiteral($2);
437          $$.statements = $3;          $$.statements = $3;
438      }      }
439      | CASE INTEGER TO INTEGER statements  {      | CASE INTEGER TO INTEGER opt_statements  {
440          $$ = CaseBranch();          $$ = CaseBranch();
441          $$.from = new IntLiteral($2);          $$.from = new IntLiteral($2);
442          $$.to   = new IntLiteral($4);          $$.to   = new IntLiteral($4);
443          $$.statements = $5;          $$.statements = $5;
444      }      }
445    
446    userfunctioncall:
447        CALL IDENTIFIER  {
448            const char* name = $2;
449            StatementsRef fn = context->userFunctionByName(name);
450            if (context->functionProvider->functionByName(name)) {
451                PARSE_ERR(@1, (String("Keyword 'call' must only be used for user defined functions, not for any built-in function like '") + name + "'.").c_str());
452                $$ = StatementsRef();
453            } else if (!fn) {
454                PARSE_ERR(@2, (String("No user defined function with name '") + name + "'.").c_str());
455                $$ = StatementsRef();
456            } else {
457                $$ = fn;
458            }
459        }
460    
461  functioncall:  functioncall:
462      IDENTIFIER '(' args ')'  {      IDENTIFIER '(' args ')'  {
463          const char* name = $1;          const char* name = $1;
464          //printf("function call of '%s' with args\n", name);          //printf("function call of '%s' with args\n", name);
465          ArgsRef args = $3;          ArgsRef args = $3;
466          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
467          if (!fn) {          if (context->userFunctionByName(name)) {
468                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
469                $$ = new FunctionCall(name, args, NULL);
470            } else if (!fn) {
471              PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());              PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());
472              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
473          } else if (args->argsCount() < fn->minRequiredArgs()) {          } else if (args->argsCount() < fn->minRequiredArgs()) {
# Line 348  functioncall: Line 483  functioncall:
483                      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());                      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());
484                      argsOK = false;                      argsOK = false;
485                      break;                      break;
486                    } else if (fn->modifiesArg(i) && !args->arg(i)->isModifyable()) {
487                        PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' expects an assignable variable.").c_str());
488                        argsOK = false;
489                        break;
490                  }                  }
491              }              }
492              $$ = new FunctionCall(name, args, argsOK ? fn : NULL);              $$ = new FunctionCall(name, args, argsOK ? fn : NULL);
# Line 358  functioncall: Line 497  functioncall:
497          //printf("function call of '%s' (with empty args)\n", name);          //printf("function call of '%s' (with empty args)\n", name);
498          ArgsRef args = new Args;          ArgsRef args = new Args;
499          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
500          if (!fn) {          if (context->userFunctionByName(name)) {
501                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
502                $$ = new FunctionCall(name, args, NULL);
503            } else if (!fn) {
504              PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());              PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());
505              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
506          } else if (fn->minRequiredArgs() > 0) {          } else if (fn->minRequiredArgs() > 0) {
# Line 373  functioncall: Line 515  functioncall:
515          //printf("function call of '%s' (without args)\n", name);          //printf("function call of '%s' (without args)\n", name);
516          ArgsRef args = new Args;          ArgsRef args = new Args;
517          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
518          if (!fn) {          if (context->userFunctionByName(name)) {
519                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
520                $$ = new FunctionCall(name, args, NULL);
521            } else if (!fn) {
522              PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());              PARSE_ERR(@1, (String("No built-in function with name '") + name + "'.").c_str());
523              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
524          } else if (fn->minRequiredArgs() > 0) {          } else if (fn->minRequiredArgs() > 0) {
# Line 406  assignment: Line 551  assignment:
551              PARSE_ERR(@1, (String("Variable assignment: No variable declared with name '") + name + "'.").c_str());              PARSE_ERR(@1, (String("Variable assignment: No variable declared with name '") + name + "'.").c_str());
552          else if (var->isConstExpr())          else if (var->isConstExpr())
553              PARSE_ERR(@2, (String("Variable assignment: Cannot modify const variable '") + name + "'.").c_str());              PARSE_ERR(@2, (String("Variable assignment: Cannot modify const variable '") + name + "'.").c_str());
554            else if (!var->isAssignable())
555                PARSE_ERR(@2, (String("Variable assignment: Variable '") + name + "' is not assignable.").c_str());
556          else if (var->exprType() != $3->exprType())          else if (var->exprType() != $3->exprType())
557              PARSE_ERR(@3, (String("Variable assignment: Variable '") + name + "' is of type " + typeStr(var->exprType()) + ", assignment is of type " + typeStr($3->exprType()) + " though.").c_str());              PARSE_ERR(@3, (String("Variable assignment: Variable '") + name + "' is of type " + typeStr(var->exprType()) + ", assignment is of type " + typeStr($3->exprType()) + " though.").c_str());
558          $$ = new Assignment(var, $3);          $$ = new Assignment(var, $3);
# Line 417  assignment: Line 564  assignment:
564              PARSE_ERR(@1, (String("No variable declared with name '") + name + "'.").c_str());              PARSE_ERR(@1, (String("No variable declared with name '") + name + "'.").c_str());
565          else if (var->exprType() != INT_ARR_EXPR)          else if (var->exprType() != INT_ARR_EXPR)
566              PARSE_ERR(@2, (String("Variable '") + name + "' is not an array variable.").c_str());              PARSE_ERR(@2, (String("Variable '") + name + "' is not an array variable.").c_str());
567            else if (var->isConstExpr())
568                PARSE_ERR(@5, (String("Variable assignment: Cannot modify const array variable '") + name + "'.").c_str());
569            else if (!var->isAssignable())
570                PARSE_ERR(@5, (String("Variable assignment: Array variable '") + name + "' is not assignable.").c_str());
571          else if ($3->exprType() != INT_EXPR)          else if ($3->exprType() != INT_EXPR)
572              PARSE_ERR(@3, (String("Array variable '") + name + "' accessed with non integer expression.").c_str());              PARSE_ERR(@3, (String("Array variable '") + name + "' accessed with non integer expression.").c_str());
573          else if ($6->exprType() != INT_EXPR)          else if ($6->exprType() != INT_EXPR)
574              PARSE_ERR(@5, (String("Value assigned to array variable '") + name + "' must be an integer expression.").c_str());              PARSE_ERR(@5, (String("Value assigned to array variable '") + name + "' must be an integer expression.").c_str());
575            else if ($3->isConstExpr() && $3->asInt()->evalInt() >= ((IntArrayVariableRef)var)->arraySize())
576                PARSE_WRN(@3, (String("Index ") + ToString($3->asInt()->evalInt()) +
577                              " exceeds size of array variable '" + name +
578                              "' which was declared with size " +
579                              ToString(((IntArrayVariableRef)var)->arraySize()) + ".").c_str());
580          IntArrayElementRef element = new IntArrayElement(var, $3);          IntArrayElementRef element = new IntArrayElement(var, $3);
581          $$ = new Assignment(element, $6);          $$ = new Assignment(element, $6);
582      }      }
# Line 455  unary_expr: Line 611  unary_expr:
611              PARSE_ERR(@3, (String("Array variable '") + name + "' accessed with non integer expression.").c_str());              PARSE_ERR(@3, (String("Array variable '") + name + "' accessed with non integer expression.").c_str());
612              $$ = new IntLiteral(0);              $$ = new IntLiteral(0);
613          } else {          } else {
614                if ($3->isConstExpr() && $3->asInt()->evalInt() >= ((IntArrayVariableRef)var)->arraySize())
615                    PARSE_WRN(@3, (String("Index ") + ToString($3->asInt()->evalInt()) +
616                                   " exceeds size of array variable '" + name +
617                                   "' which was declared with size " +
618                                   ToString(((IntArrayVariableRef)var)->arraySize()) + ".").c_str());
619              $$ = new IntArrayElement(var, $3);              $$ = new IntArrayElement(var, $3);
620          }          }
621      }      }
# Line 467  unary_expr: Line 628  unary_expr:
628      | '-' unary_expr  {      | '-' unary_expr  {
629          $$ = new Neg($2);          $$ = new Neg($2);
630      }      }
631        | BITWISE_NOT unary_expr  {
632            if ($2->exprType() != INT_EXPR) {
633                PARSE_ERR(@2, (String("Right operand of bitwise operator '.not.' must be an integer expression, is ") + typeStr($2->exprType()) + " though.").c_str());
634                $$ = new IntLiteral(0);
635            } else {
636                $$ = new BitwiseNot($2);
637            }
638        }
639      | NOT unary_expr  {      | NOT unary_expr  {
640          if ($2->exprType() != INT_EXPR) {          if ($2->exprType() != INT_EXPR) {
641              PARSE_ERR(@2, (String("Right operand of operator 'not' must be an integer expression, is ") + typeStr($2->exprType()) + " though.").c_str());              PARSE_ERR(@2, (String("Right operand of operator 'not' must be an integer expression, is ") + typeStr($2->exprType()) + " though.").c_str());
# Line 480  expr: Line 649  expr:
649      concat_expr      concat_expr
650    
651  concat_expr:  concat_expr:
652      or_expr      logical_or_expr
653      | concat_expr '&' or_expr  {      | concat_expr '&' logical_or_expr  {
654          ExpressionRef lhs = $1;          ExpressionRef lhs = $1;
655          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
656          if (lhs->isConstExpr() && rhs->isConstExpr()) {          if (lhs->isConstExpr() && rhs->isConstExpr()) {
# Line 493  concat_expr: Line 662  concat_expr:
662          }          }
663      }      }
664    
665  or_expr:  logical_or_expr:
666      and_expr      logical_and_expr
667      | or_expr OR and_expr  {      | logical_or_expr OR logical_and_expr  {
668          ExpressionRef lhs = $1;          ExpressionRef lhs = $1;
669          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
670          if (lhs->exprType() != INT_EXPR) {          if (lhs->exprType() != INT_EXPR) {
# Line 509  or_expr: Line 678  or_expr:
678          }          }
679      }      }
680    
681  and_expr:  logical_and_expr:
682      rel_expr  {      bitwise_or_expr  {
683          $$ = $1;          $$ = $1;
684      }      }
685      | and_expr AND rel_expr  {      | logical_and_expr AND bitwise_or_expr  {
686          ExpressionRef lhs = $1;          ExpressionRef lhs = $1;
687          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
688          if (lhs->exprType() != INT_EXPR) {          if (lhs->exprType() != INT_EXPR) {
# Line 527  and_expr: Line 696  and_expr:
696          }          }
697      }      }
698    
699    bitwise_or_expr:
700        bitwise_and_expr
701        | bitwise_or_expr BITWISE_OR bitwise_and_expr  {
702            ExpressionRef lhs = $1;
703            ExpressionRef rhs = $3;
704            if (lhs->exprType() != INT_EXPR) {
705                PARSE_ERR(@1, (String("Left operand of bitwise operator '.or.' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
706                $$ = new IntLiteral(0);
707            } else if (rhs->exprType() != INT_EXPR) {
708                PARSE_ERR(@3, (String("Right operand of bitwise operator '.or.' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
709                $$ = new IntLiteral(0);
710            } else {
711                $$ = new BitwiseOr(lhs, rhs);
712            }
713        }
714    
715    bitwise_and_expr:
716        rel_expr  {
717            $$ = $1;
718        }
719        | bitwise_and_expr BITWISE_AND rel_expr  {
720            ExpressionRef lhs = $1;
721            ExpressionRef rhs = $3;
722            if (lhs->exprType() != INT_EXPR) {
723                PARSE_ERR(@1, (String("Left operand of bitwise operator '.and.' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
724                $$ = new IntLiteral(0);
725            } else if (rhs->exprType() != INT_EXPR) {
726                PARSE_ERR(@3, (String("Right operand of bitwise operator '.and.' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
727                $$ = new IntLiteral(0);
728            } else {
729                $$ = new BitwiseAnd(lhs, rhs);
730            }
731        }
732    
733  rel_expr:  rel_expr:
734        add_expr        add_expr
735      | rel_expr '<' add_expr  {      | rel_expr '<' add_expr  {
# Line 663  mul_expr: Line 866  mul_expr:
866    
867  void InstrScript_error(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* err) {  void InstrScript_error(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* err) {
868      //fprintf(stderr, "%d: %s\n", locp->first_line, err);      //fprintf(stderr, "%d: %s\n", locp->first_line, err);
869      context->addErr(locp->first_line, locp->first_column+1, err);      context->addErr(locp->first_line, locp->last_line, locp->first_column+1, locp->last_column+1, err);
870  }  }
871    
872  void InstrScript_warning(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* txt) {  void InstrScript_warning(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* txt) {
873      //fprintf(stderr, "WRN %d: %s\n", locp->first_line, txt);      //fprintf(stderr, "WRN %d: %s\n", locp->first_line, txt);
874      context->addWrn(locp->first_line, locp->first_column+1, txt);      context->addWrn(locp->first_line, locp->last_line, locp->first_column+1, locp->last_column+1, txt);
875    }
876    
877    /// Custom implementation of yytnamerr() to ensure quotation is always stripped from token names before printing them to error messages.
878    int InstrScript_tnamerr(char* yyres, const char* yystr) {
879      if (*yystr == '"') {
880          int yyn = 0;
881          char const *yyp = yystr;
882          for (;;)
883            switch (*++yyp)
884              {
885    /*
886              case '\'':
887              case ',':
888                goto do_not_strip_quotes;
889    
890              case '\\':
891                if (*++yyp != '\\')
892                  goto do_not_strip_quotes;
893    */
894                /* Fall through.  */
895              default:
896                if (yyres)
897                  yyres[yyn] = *yyp;
898                yyn++;
899                break;
900    
901              case '"':
902                if (yyres)
903                  yyres[yyn] = '\0';
904                return yyn;
905              }
906    /*
907        do_not_strip_quotes: ;
908    */
909        }
910    
911      if (! yyres)
912        return (int) yystrlen (yystr);
913    
914      return int( yystpcpy (yyres, yystr) - yyres );
915  }  }

Legend:
Removed from v.2888  
changed lines
  Added in v.3259

  ViewVC Help
Powered by ViewVC