/[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 2911 by schoenebeck, Mon May 16 20:27:33 2016 UTC revision 3557 by schoenebeck, Sun Aug 18 00:06:04 2019 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 PARSE_DROP(loc)     context->addPreprocessorComment(loc.first_line, loc.last_line, loc.first_column+1, loc.last_column+1);
27        #define yytnamerr(res,str)  InstrScript_tnamerr(res, str)
28  %}  %}
29    
30  // generate reentrant safe parser  // generate reentrant safe parser
# Line 29  Line 32 
32  %parse-param { LinuxSampler::ParserContext* context }  %parse-param { LinuxSampler::ParserContext* context }
33  %lex-param { void* scanner }  %lex-param { void* scanner }
34  // 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
35  %name-prefix "InstrScript_"  // (NOTE: "=" is deprecated here with Bison 3.x, however removing it would cause an error with Bison 2.x)
36    %name-prefix="InstrScript_"
37  %locations  %locations
38  %defines  %defines
39  %error-verbose  %error-verbose
40    
41  %token <iValue> INTEGER  %token <iValue> INTEGER "integer literal"
42  %token <sValue> STRING  %token <sValue> STRING "string literal"
43  %token <sValue> IDENTIFIER  %token <sValue> IDENTIFIER "function name"
44  %token <sValue> VARIABLE  %token <sValue> VARIABLE "variable name"
45  %token ON END INIT NOTE DECLARE ASSIGNMENT WHILE IF OR RELEASE AND ELSE  %token ON "keyword 'on'"
46  %token CONTROLLER SELECT CASE TO NOT CONST_ POLYPHONIC MOD  %token END "keyword 'end'"
47  %token LE GE  %token INIT "keyword 'init'"
48    %token NOTE "keyword 'note'"
49  %type <nEventHandlers> script eventhandlers  %token RELEASE "keyword 'release'"
50  %type <nEventHandler> eventhandler  %token CONTROLLER "keyword 'controller'"
51  %type <nStatements> statements body  %token DECLARE "keyword 'declare'"
52    %token ASSIGNMENT "operator ':='"
53    %token CONST_ "keyword 'const'"
54    %token POLYPHONIC "keyword 'polyphonic'"
55    %token WHILE "keyword 'while'"
56    %token SYNCHRONIZED "keyword 'synchronized'"
57    %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    %token UNKNOWN_CHAR "unknown character"
75    
76    %type <nEventHandlers> script sections
77    %type <nEventHandler> section eventhandler
78    %type <nStatements> statements opt_statements userfunctioncall
79  %type <nStatement> statement assignment  %type <nStatement> statement assignment
80  %type <nFunctionCall> functioncall  %type <nFunctionCall> functioncall
81  %type <nArgs> args  %type <nArgs> args
82  %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
83  %type <nCaseBranch> caseclause  %type <nCaseBranch> caseclause
84  %type <nCaseBranches> caseclauses  %type <nCaseBranches> caseclauses
85    
# Line 57  Line 88 
88  %%  %%
89    
90  script:  script:
91      eventhandlers  {      sections  {
92          $$ = context->handlers = $1;          $$ = context->handlers = $1;
93      }      }
94    
95  eventhandlers:  sections:
96      eventhandler  {      section  {
97          $$ = new EventHandlers();          $$ = new EventHandlers();
98          $$->add($1);          if ($1) $$->add($1);
99        }
100        | sections section  {
101            $$ = $1;
102            if ($2) $$->add($2);
103      }      }
104      | eventhandlers eventhandler  {  
105    section:
106        function_declaration  {
107            $$ = EventHandlerRef();
108        }
109        | eventhandler  {
110          $$ = $1;          $$ = $1;
         $$->add($2);  
111      }      }
112    
113  eventhandler:  eventhandler:
114      ON NOTE body END ON  {      ON NOTE opt_statements END ON  {
115          if (context->onNote)          if (context->onNote)
116              PARSE_ERR(@2, "Redeclaration of 'note' event handler.");              PARSE_ERR(@2, "Redeclaration of 'note' event handler.");
117          context->onNote = new OnNote($3);          context->onNote = new OnNote($3);
118          $$ = context->onNote;          $$ = context->onNote;
119      }      }
120      | ON INIT body END ON  {      | ON INIT opt_statements END ON  {
121          if (context->onInit)          if (context->onInit)
122              PARSE_ERR(@2, "Redeclaration of 'init' event handler.");              PARSE_ERR(@2, "Redeclaration of 'init' event handler.");
123          context->onInit = new OnInit($3);          context->onInit = new OnInit($3);
124          $$ = context->onInit;          $$ = context->onInit;
125      }      }
126      | ON RELEASE body END ON  {      | ON RELEASE opt_statements END ON  {
127          if (context->onRelease)          if (context->onRelease)
128              PARSE_ERR(@2, "Redeclaration of 'release' event handler.");              PARSE_ERR(@2, "Redeclaration of 'release' event handler.");
129          context->onRelease = new OnRelease($3);          context->onRelease = new OnRelease($3);
130          $$ = context->onRelease;          $$ = context->onRelease;
131      }      }
132      | ON CONTROLLER body END ON  {      | ON CONTROLLER opt_statements END ON  {
133          if (context->onController)          if (context->onController)
134              PARSE_ERR(@2, "Redeclaration of 'controller' event handler.");              PARSE_ERR(@2, "Redeclaration of 'controller' event handler.");
135          context->onController = new OnController($3);          context->onController = new OnController($3);
136          $$ = context->onController;          $$ = context->onController;
137      }      }
138    
139  body:  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    opt_statements:
152      /* epsilon (empty argument) */  {      /* epsilon (empty argument) */  {
153          $$ = new Statements();          $$ = new Statements();
154      }      }
# Line 125  statement: Line 176  statement:
176      functioncall  {      functioncall  {
177          $$ = $1;          $$ = $1;
178      }      }
179        | userfunctioncall  {
180            $$ = $1;
181        }
182      | DECLARE VARIABLE  {      | DECLARE VARIABLE  {
183          const char* name = $2;          const char* name = $2;
184          //printf("declared var '%s'\n", name);          //printf("declared var '%s'\n", name);
# Line 175  statement: Line 229  statement:
229                  PARSE_WRN(@2, (String("Variable '") + name + "' declared as string, integer expression assigned though.").c_str());                  PARSE_WRN(@2, (String("Variable '") + name + "' declared as string, integer expression assigned though.").c_str());
230              IntExprRef expr = $4;              IntExprRef expr = $4;
231              if (expr->isConstExpr()) {              if (expr->isConstExpr()) {
232                  const int i = expr->evalInt();                  const vmint i = expr->evalInt();
233                  IntVariableRef var = new IntVariable(context);                  IntVariableRef var = new IntVariable(context);
234                  context->vartable[name] = var;                  context->vartable[name] = var;
235                  $$ = new Assignment(var, new IntLiteral(i));                  $$ = new Assignment(var, new IntLiteral(i));
# Line 200  statement: Line 254  statement:
254              $$ = new FunctionCall("nothing", new Args, NULL); // whatever              $$ = new FunctionCall("nothing", new Args, NULL); // whatever
255          } else {          } else {
256              IntExprRef expr = $4;              IntExprRef expr = $4;
257              int size = expr->evalInt();              vmint size = expr->evalInt();
258              if (size <= 0) {              if (size <= 0) {
259                  PARSE_ERR(@4, (String("Array variable '") + name + "' declared with array size " + ToString(size) + ".").c_str());                  PARSE_ERR(@4, (String("Array variable '") + name + "' declared with array size " + ToString(size) + ".").c_str());
260                  $$ = new FunctionCall("nothing", new Args, NULL); // whatever                  $$ = new FunctionCall("nothing", new Args, NULL); // whatever
# Line 224  statement: Line 278  statement:
278          } else {          } else {
279              IntExprRef sizeExpr = $4;              IntExprRef sizeExpr = $4;
280              ArgsRef args = $8;              ArgsRef args = $8;
281              int size = sizeExpr->evalInt();              vmint size = sizeExpr->evalInt();
282              if (size <= 0) {              if (size <= 0) {
283                  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());
284                  $$ = new FunctionCall("nothing", new Args, NULL); // whatever                  $$ = new FunctionCall("nothing", new Args, NULL); // whatever
285              } else if (args->argsCount() > size) {              } else if (args->argsCount() > size) {
286                  PARSE_ERR(@8, (String("Variable '") + name +                  PARSE_ERR(@8, (String("Array variable '") + name +
287                            "' was declared with size " + ToString(size) +                            "' was declared with size " + ToString(size) +
288                            " but " + ToString(args->argsCount()) +                            " but " + ToString(args->argsCount()) +
289                            " values were assigned." ).c_str());                            " values were assigned." ).c_str());
290                  $$ = new FunctionCall("nothing", new Args, NULL); // whatever                            $$ = new FunctionCall("nothing", new Args, NULL); // whatever          
291              } else {              } else {
292                  bool argsOK = true;                  bool argsOK = true;
293                  for (int i = 0; i < args->argsCount(); ++i) {                  for (vmint i = 0; i < args->argsCount(); ++i) {
294                      if (args->arg(i)->exprType() != INT_EXPR) {                      if (args->arg(i)->exprType() != INT_EXPR) {
295                          PARSE_ERR(                          PARSE_ERR(
296                              @8,                              @8,
# Line 248  statement: Line 302  statement:
302                          break;                          break;
303                      }                      }
304                  }                  }
305                  if (argsOK)                  if (argsOK) {
306                      $$ = context->vartable[name] = new IntArrayVariable(context, size, args);                      context->vartable[name] = new IntArrayVariable(context, size, args);
307                  else                      $$ = new NoOperation;
308                    } else
309                        $$ = new FunctionCall("nothing", new Args, NULL); // whatever
310                }
311            }
312        }
313        | 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                vmint size = sizeExpr->evalInt();
328                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                    for (vmint i = 0; i < args->argsCount(); ++i) {
340                        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                      $$ = new FunctionCall("nothing", new Args, NULL); // whatever
366              }              }
367          }          }
# Line 273  statement: Line 384  statement:
384          } else {          } else {
385              if (name[0] == '@')              if (name[0] == '@')
386                  PARSE_WRN(@5, "Variable declared as string, integer expression assigned though.");                  PARSE_WRN(@5, "Variable declared as string, integer expression assigned though.");
387              int i = 0;              vmint i = 0;
388              IntExprRef expr = $5;              IntExprRef expr = $5;
389              if (expr->isConstExpr())              if (expr->isConstExpr())
390                  i = expr->evalInt();                  i = expr->evalInt();
# Line 288  statement: Line 399  statement:
399      | assignment  {      | assignment  {
400          $$ = $1;          $$ = $1;
401      }      }
402      | WHILE '(' expr ')' statements END WHILE  {      | WHILE '(' expr ')' opt_statements END WHILE  {
403          if ($3->exprType() == INT_EXPR) {          if ($3->exprType() == INT_EXPR) {
404              $$ = new While($3, $5);              $$ = new While($3, $5);
405          } else {          } else {
# Line 296  statement: Line 407  statement:
407              $$ = new While(new IntLiteral(0), $5);              $$ = new While(new IntLiteral(0), $5);
408          }          }
409      }      }
410      | IF '(' expr ')' statements ELSE statements END IF  {      | SYNCHRONIZED opt_statements END SYNCHRONIZED  {
411            $$ = new SyncBlock($2);
412        }
413        | IF '(' expr ')' opt_statements ELSE opt_statements END IF  {
414          $$ = new If($3, $5, $7);          $$ = new If($3, $5, $7);
415      }      }
416      | IF '(' expr ')' statements END IF  {      | IF '(' expr ')' opt_statements END IF  {
417          $$ = new If($3, $5);          $$ = new If($3, $5);
418      }      }
419      | SELECT expr caseclauses END SELECT  {      | SELECT expr caseclauses END SELECT  {
# Line 322  caseclauses: Line 436  caseclauses:
436      }      }
437    
438  caseclause:  caseclause:
439      CASE INTEGER statements  {      CASE INTEGER opt_statements  {
440          $$ = CaseBranch();          $$ = CaseBranch();
441          $$.from = new IntLiteral($2);          $$.from = new IntLiteral($2);
442          $$.statements = $3;          $$.statements = $3;
443      }      }
444      | CASE INTEGER TO INTEGER statements  {      | CASE INTEGER TO INTEGER opt_statements  {
445          $$ = CaseBranch();          $$ = CaseBranch();
446          $$.from = new IntLiteral($2);          $$.from = new IntLiteral($2);
447          $$.to   = new IntLiteral($4);          $$.to   = new IntLiteral($4);
448          $$.statements = $5;          $$.statements = $5;
449      }      }
450    
451    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  functioncall:  functioncall:
467      IDENTIFIER '(' args ')'  {      IDENTIFIER '(' args ')'  {
468          const char* name = $1;          const char* name = $1;
469          //printf("function call of '%s' with args\n", name);          //printf("function call of '%s' with args\n", name);
470          ArgsRef args = $3;          ArgsRef args = $3;
471          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
472          if (!fn) {          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              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());
477              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
478            } else if (context->functionProvider->isFunctionDisabled(fn,context)) {
479                PARSE_DROP(@$);
480                $$ = new NoFunctionCall;
481          } else if (args->argsCount() < fn->minRequiredArgs()) {          } else if (args->argsCount() < fn->minRequiredArgs()) {
482              PARSE_ERR(@3, (String("Built-in function '") + name + "' requires at least " + ToString(fn->minRequiredArgs()) + " arguments.").c_str());              PARSE_ERR(@3, (String("Built-in function '") + name + "' requires at least " + ToString(fn->minRequiredArgs()) + " arguments.").c_str());
483              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
# Line 351  functioncall: Line 486  functioncall:
486              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
487          } else {          } else {
488              bool argsOK = true;              bool argsOK = true;
489              for (int i = 0; i < args->argsCount(); ++i) {              for (vmint i = 0; i < args->argsCount(); ++i) {
490                  if (args->arg(i)->exprType() != fn->argType(i) && !fn->acceptsArgType(i, args->arg(i)->exprType())) {                  if (args->arg(i)->exprType() != fn->argType(i) && !fn->acceptsArgType(i, args->arg(i)->exprType())) {
491                      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());
492                      argsOK = false;                      argsOK = false;
493                      break;                      break;
494                    } 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                  }                  }
499              }              }
500              $$ = new FunctionCall(name, args, argsOK ? fn : NULL);              $$ = new FunctionCall(name, args, argsOK ? fn : NULL);
# Line 366  functioncall: Line 505  functioncall:
505          //printf("function call of '%s' (with empty args)\n", name);          //printf("function call of '%s' (with empty args)\n", name);
506          ArgsRef args = new Args;          ArgsRef args = new Args;
507          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
508          if (!fn) {          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              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());
513              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
514            } else if (context->functionProvider->isFunctionDisabled(fn,context)) {
515                PARSE_DROP(@$);
516                $$ = new NoFunctionCall;
517          } else if (fn->minRequiredArgs() > 0) {          } else if (fn->minRequiredArgs() > 0) {
518              PARSE_ERR(@3, (String("Built-in function '") + name + "' requires at least " + ToString(fn->minRequiredArgs()) + " arguments.").c_str());              PARSE_ERR(@3, (String("Built-in function '") + name + "' requires at least " + ToString(fn->minRequiredArgs()) + " arguments.").c_str());
519              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
# Line 381  functioncall: Line 526  functioncall:
526          //printf("function call of '%s' (without args)\n", name);          //printf("function call of '%s' (without args)\n", name);
527          ArgsRef args = new Args;          ArgsRef args = new Args;
528          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
529          if (!fn) {          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              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());
534              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
535            } else if (context->functionProvider->isFunctionDisabled(fn,context)) {
536                PARSE_DROP(@$);
537                $$ = new NoFunctionCall;
538          } else if (fn->minRequiredArgs() > 0) {          } else if (fn->minRequiredArgs() > 0) {
539              PARSE_ERR(@1, (String("Built-in function '") + name + "' requires at least " + ToString(fn->minRequiredArgs()) + " arguments.").c_str());              PARSE_ERR(@1, (String("Built-in function '") + name + "' requires at least " + ToString(fn->minRequiredArgs()) + " arguments.").c_str());
540              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
# Line 414  assignment: Line 565  assignment:
565              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());
566          else if (var->isConstExpr())          else if (var->isConstExpr())
567              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());
568            else if (!var->isAssignable())
569                PARSE_ERR(@2, (String("Variable assignment: Variable '") + name + "' is not assignable.").c_str());
570          else if (var->exprType() != $3->exprType())          else if (var->exprType() != $3->exprType())
571              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());
572          $$ = new Assignment(var, $3);          $$ = new Assignment(var, $3);
# Line 425  assignment: Line 578  assignment:
578              PARSE_ERR(@1, (String("No variable declared with name '") + name + "'.").c_str());              PARSE_ERR(@1, (String("No variable declared with name '") + name + "'.").c_str());
579          else if (var->exprType() != INT_ARR_EXPR)          else if (var->exprType() != INT_ARR_EXPR)
580              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());
581            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          else if ($3->exprType() != INT_EXPR)          else if ($3->exprType() != INT_EXPR)
586              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());
587          else if ($6->exprType() != INT_EXPR)          else if ($6->exprType() != INT_EXPR)
588              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());
589            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          IntArrayElementRef element = new IntArrayElement(var, $3);          IntArrayElementRef element = new IntArrayElement(var, $3);
595          $$ = new Assignment(element, $6);          $$ = new Assignment(element, $6);
596      }      }
# Line 463  unary_expr: Line 625  unary_expr:
625              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());
626              $$ = new IntLiteral(0);              $$ = new IntLiteral(0);
627          } else {          } else {
628                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              $$ = new IntArrayElement(var, $3);              $$ = new IntArrayElement(var, $3);
634          }          }
635      }      }
# Line 475  unary_expr: Line 642  unary_expr:
642      | '-' unary_expr  {      | '-' unary_expr  {
643          $$ = new Neg($2);          $$ = new Neg($2);
644      }      }
645        | 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      | NOT unary_expr  {      | NOT unary_expr  {
654          if ($2->exprType() != INT_EXPR) {          if ($2->exprType() != INT_EXPR) {
655              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 488  expr: Line 663  expr:
663      concat_expr      concat_expr
664    
665  concat_expr:  concat_expr:
666      or_expr      logical_or_expr
667      | concat_expr '&' or_expr  {      | concat_expr '&' logical_or_expr  {
668          ExpressionRef lhs = $1;          ExpressionRef lhs = $1;
669          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
670          if (lhs->isConstExpr() && rhs->isConstExpr()) {          if (lhs->isConstExpr() && rhs->isConstExpr()) {
# Line 501  concat_expr: Line 676  concat_expr:
676          }          }
677      }      }
678    
679  or_expr:  logical_or_expr:
680      and_expr      logical_and_expr
681      | or_expr OR and_expr  {      | logical_or_expr OR logical_and_expr  {
682          ExpressionRef lhs = $1;          ExpressionRef lhs = $1;
683          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
684          if (lhs->exprType() != INT_EXPR) {          if (lhs->exprType() != INT_EXPR) {
# Line 517  or_expr: Line 692  or_expr:
692          }          }
693      }      }
694    
695  and_expr:  logical_and_expr:
696      rel_expr  {      bitwise_or_expr  {
697          $$ = $1;          $$ = $1;
698      }      }
699      | and_expr AND rel_expr  {      | logical_and_expr AND bitwise_or_expr  {
700          ExpressionRef lhs = $1;          ExpressionRef lhs = $1;
701          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
702          if (lhs->exprType() != INT_EXPR) {          if (lhs->exprType() != INT_EXPR) {
# Line 535  and_expr: Line 710  and_expr:
710          }          }
711      }      }
712    
713    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  rel_expr:  rel_expr:
748        add_expr        add_expr
749      | rel_expr '<' add_expr  {      | rel_expr '<' add_expr  {
# Line 678  void InstrScript_warning(YYLTYPE* locp, Line 887  void InstrScript_warning(YYLTYPE* locp,
887      //fprintf(stderr, "WRN %d: %s\n", locp->first_line, txt);      //fprintf(stderr, "WRN %d: %s\n", locp->first_line, txt);
888      context->addWrn(locp->first_line, locp->last_line, locp->first_column+1, locp->last_column+1, txt);      context->addWrn(locp->first_line, locp->last_line, locp->first_column+1, locp->last_column+1, txt);
889  }  }
890    
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    /*
921        do_not_strip_quotes: ;
922    */
923        }
924    
925      if (! yyres)
926        return (int) yystrlen (yystr);
927    
928      return int( yystpcpy (yyres, yystr) - yyres );
929    }

Legend:
Removed from v.2911  
changed lines
  Added in v.3557

  ViewVC Help
Powered by ViewVC