/[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 2945 by schoenebeck, Thu Jul 14 00:22:26 2016 UTC revision 3592 by schoenebeck, Mon Sep 2 09:40:44 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 <fValue> REAL "real number literal"
43  %token <sValue> IDENTIFIER  %token <iUnitValue> INTEGER_UNIT "integer literal with unit"
44  %token <sValue> VARIABLE  %token <fUnitValue> REAL_UNIT "real number literal with unit"
45  %token ON END INIT NOTE DECLARE ASSIGNMENT WHILE IF OR RELEASE AND ELSE  %token <sValue> STRING "string literal"
46  %token BITWISE_OR BITWISE_AND BITWISE_NOT  %token <sValue> IDENTIFIER "function name"
47  %token CONTROLLER SELECT CASE TO NOT CONST_ POLYPHONIC MOD  %token <sValue> VARIABLE "variable name"
48  %token LE GE  %token ON "keyword 'on'"
49    %token END "keyword 'end'"
50  %type <nEventHandlers> script eventhandlers  %token INIT "keyword 'init'"
51  %type <nEventHandler> eventhandler  %token NOTE "keyword 'note'"
52  %type <nStatements> statements body  %token RELEASE "keyword 'release'"
53    %token CONTROLLER "keyword 'controller'"
54    %token DECLARE "keyword 'declare'"
55    %token ASSIGNMENT "operator ':='"
56    %token CONST_ "keyword 'const'"
57    %token POLYPHONIC "keyword 'polyphonic'"
58    %token WHILE "keyword 'while'"
59    %token SYNCHRONIZED "keyword 'synchronized'"
60    %token IF "keyword 'if'"
61    %token ELSE "keyword 'else'"
62    %token SELECT "keyword 'select'"
63    %token CASE "keyword 'case'"
64    %token TO "keyword 'to'"
65    %token OR "operator 'or'"
66    %token AND "operator 'and'"
67    %token NOT "operator 'not'"
68    %token BITWISE_OR "bitwise operator '.or.'"
69    %token BITWISE_AND "bitwise operator '.and.'"
70    %token BITWISE_NOT "bitwise operator '.not.'"
71    %token FUNCTION "keyword 'function'"
72    %token CALL "keyword 'call'"
73    %token MOD "operator 'mod'"
74    %token LE "operator '<='"
75    %token GE "operator '>='"
76    %token END_OF_FILE 0 "end of file"
77    %token UNKNOWN_CHAR "unknown character"
78    
79    %type <nEventHandlers> script sections
80    %type <nEventHandler> section eventhandler
81    %type <nStatements> statements opt_statements userfunctioncall
82  %type <nStatement> statement assignment  %type <nStatement> statement assignment
83  %type <nFunctionCall> functioncall  %type <nFunctionCall> functioncall
84  %type <nArgs> args  %type <nArgs> args
# Line 58  Line 91 
91  %%  %%
92    
93  script:  script:
94      eventhandlers  {      sections  {
95          $$ = context->handlers = $1;          $$ = context->handlers = $1;
96      }      }
97    
98  eventhandlers:  sections:
99      eventhandler  {      section  {
100          $$ = new EventHandlers();          $$ = new EventHandlers();
101          $$->add($1);          if ($1) $$->add($1);
102        }
103        | sections section  {
104            $$ = $1;
105            if ($2) $$->add($2);
106        }
107    
108    section:
109        function_declaration  {
110            $$ = EventHandlerRef();
111      }      }
112      | eventhandlers eventhandler  {      | eventhandler  {
113          $$ = $1;          $$ = $1;
         $$->add($2);  
114      }      }
115    
116  eventhandler:  eventhandler:
117      ON NOTE body END ON  {      ON NOTE opt_statements END ON  {
118          if (context->onNote)          if (context->onNote)
119              PARSE_ERR(@2, "Redeclaration of 'note' event handler.");              PARSE_ERR(@2, "Redeclaration of 'note' event handler.");
120          context->onNote = new OnNote($3);          context->onNote = new OnNote($3);
121          $$ = context->onNote;          $$ = context->onNote;
122      }      }
123      | ON INIT body END ON  {      | ON INIT opt_statements END ON  {
124          if (context->onInit)          if (context->onInit)
125              PARSE_ERR(@2, "Redeclaration of 'init' event handler.");              PARSE_ERR(@2, "Redeclaration of 'init' event handler.");
126          context->onInit = new OnInit($3);          context->onInit = new OnInit($3);
127          $$ = context->onInit;          $$ = context->onInit;
128      }      }
129      | ON RELEASE body END ON  {      | ON RELEASE opt_statements END ON  {
130          if (context->onRelease)          if (context->onRelease)
131              PARSE_ERR(@2, "Redeclaration of 'release' event handler.");              PARSE_ERR(@2, "Redeclaration of 'release' event handler.");
132          context->onRelease = new OnRelease($3);          context->onRelease = new OnRelease($3);
133          $$ = context->onRelease;          $$ = context->onRelease;
134      }      }
135      | ON CONTROLLER body END ON  {      | ON CONTROLLER opt_statements END ON  {
136          if (context->onController)          if (context->onController)
137              PARSE_ERR(@2, "Redeclaration of 'controller' event handler.");              PARSE_ERR(@2, "Redeclaration of 'controller' event handler.");
138          context->onController = new OnController($3);          context->onController = new OnController($3);
139          $$ = context->onController;          $$ = context->onController;
140      }      }
141    
142  body:  function_declaration:
143        FUNCTION IDENTIFIER opt_statements END FUNCTION  {
144            const char* name = $2;
145            if (context->functionProvider->functionByName(name)) {
146                PARSE_ERR(@2, (String("There is already a built-in function with name '") + name + "'.").c_str());
147            } else if (context->userFunctionByName(name)) {
148                PARSE_ERR(@2, (String("There is already a user defined function with name '") + name + "'.").c_str());
149            } else {
150                context->userFnTable[name] = $3;
151            }
152        }
153    
154    opt_statements:
155      /* epsilon (empty argument) */  {      /* epsilon (empty argument) */  {
156          $$ = new Statements();          $$ = new Statements();
157      }      }
# Line 126  statement: Line 179  statement:
179      functioncall  {      functioncall  {
180          $$ = $1;          $$ = $1;
181      }      }
182        | userfunctioncall  {
183            $$ = $1;
184        }
185      | DECLARE VARIABLE  {      | DECLARE VARIABLE  {
186          const char* name = $2;          const char* name = $2;
187          //printf("declared var '%s'\n", name);          //printf("declared var '%s'\n", name);
188          if (context->variableByName(name))          if (context->variableByName(name)) {
189              PARSE_ERR(@2, (String("Redeclaration of variable '") + name + "'.").c_str());              PARSE_ERR(@2, (String("Redeclaration of variable '") + name + "'.").c_str());
190          if (name[0] == '@') {          } else if (name[0] == '@') {
191              context->vartable[name] = new StringVariable(context);              context->vartable[name] = new StringVariable(context);
192              $$ = new NoOperation;          } else if (name[0] == '~') {
193                context->vartable[name] = new RealVariable({
194                    .ctx = context
195                });
196            } else if (name[0] == '$') {
197                context->vartable[name] = new IntVariable({
198                    .ctx = context
199                });
200            } else if (name[0] == '?') {
201                PARSE_ERR(@2, (String("Real number array variable '") + name + "' declaration requires array size.").c_str());
202            } else if (name[0] == '%') {
203                PARSE_ERR(@2, (String("Integer array variable '") + name + "' declaration requires array size.").c_str());
204          } else {          } else {
205              context->vartable[name] = new IntVariable(context);              PARSE_ERR(@2, (String("Variable '") + name + "' declared with unknown type.").c_str());
             $$ = new NoOperation;  
206          }          }
207            $$ = new NoOperation;
208      }      }
209      | DECLARE POLYPHONIC VARIABLE  {      | DECLARE POLYPHONIC VARIABLE  {
210          const char* name = $3;          const char* name = $3;
211          //printf("declared polyphonic var '%s'\n", name);          //printf("declared polyphonic var '%s'\n", name);
212          if (context->variableByName(name))          if (context->variableByName(name)) {
213              PARSE_ERR(@3, (String("Redeclaration of variable '") + name + "'.").c_str());              PARSE_ERR(@3, (String("Redeclaration of variable '") + name + "'.").c_str());
214          if (name[0] != '$') {          } else if (name[0] != '$' && name[0] != '~') {
215              PARSE_ERR(@3, "Polyphonic variables may only be declared as integers.");              PARSE_ERR(@3, "Polyphonic variables must only be declared either as integer or real number type.");
216              $$ = new FunctionCall("nothing", new Args, NULL); // whatever          } else if (name[0] == '~') {
217          } else {              context->vartable[name] = new PolyphonicRealVariable({
218              context->vartable[name] = new PolyphonicIntVariable(context);                  .ctx = context
219              $$ = new NoOperation;              });
220            } else {
221                context->vartable[name] = new PolyphonicIntVariable({
222                    .ctx = context
223                });
224          }          }
225            $$ = new NoOperation;
226      }      }
227      | DECLARE VARIABLE ASSIGNMENT expr  {      | DECLARE VARIABLE ASSIGNMENT expr  {
228          const char* name = $2;          const char* name = $2;
229          //printf("declared assign var '%s'\n", name);          //printf("declared assign var '%s'\n", name);
230          if (context->variableByName(name))          const ExprType_t declType = exprTypeOfVarName(name);
231            if (context->variableByName(name)) {
232              PARSE_ERR(@2, (String("Redeclaration of variable '") + name + "'.").c_str());              PARSE_ERR(@2, (String("Redeclaration of variable '") + name + "'.").c_str());
233          if ($4->exprType() == STRING_EXPR) {              $$ = new NoOperation;
234              if (name[0] == '$')          } else if ($4->exprType() == STRING_EXPR) {
235                  PARSE_WRN(@2, (String("Variable '") + name + "' declared as integer, string expression assigned though.").c_str());              if (name[0] != '@')
236                    PARSE_WRN(@2, (String("Variable '") + name + "' declared as " + typeStr(declType) + ", string expression assigned though.").c_str());
237              StringExprRef expr = $4;              StringExprRef expr = $4;
238              if (expr->isConstExpr()) {              if (expr->isConstExpr()) {
239                  const String s = expr->evalStr();                  const String s = expr->evalStr();
# Line 171  statement: Line 245  statement:
245                  context->vartable[name] = var;                  context->vartable[name] = var;
246                  $$ = new Assignment(var, expr);                  $$ = new Assignment(var, expr);
247              }              }
248          } else {          } else if ($4->exprType() == REAL_EXPR) {
249              if (name[0] == '@')              if (name[0] != '~')
250                  PARSE_WRN(@2, (String("Variable '") + name + "' declared as string, integer expression assigned though.").c_str());                  PARSE_WRN(@2, (String("Variable '") + name + "' declared as " + typeStr(declType) + ", real number expression assigned though.").c_str());
251                RealExprRef expr = $4;
252                RealVariableRef var = new RealVariable({
253                    .ctx = context,
254                    .unitType = expr->unitType(),
255                    .isFinal = expr->isFinal()
256                });
257                if (expr->isConstExpr()) {
258                    $$ = new Assignment(var, new RealLiteral({
259                        .value = expr->evalReal(),
260                        .unitFactor = expr->unitFactor(),
261                        .unitType = expr->unitType(),
262                        .isFinal = expr->isFinal()
263                    }));
264                } else {
265                    $$ = new Assignment(var, expr);
266                }
267                context->vartable[name] = var;
268            } else if ($4->exprType() == INT_EXPR) {
269                if (name[0] != '$')
270                    PARSE_WRN(@2, (String("Variable '") + name + "' declared as " + typeStr(declType) + ", integer expression assigned though.").c_str());
271              IntExprRef expr = $4;              IntExprRef expr = $4;
272                IntVariableRef var = new IntVariable({
273                    .ctx = context,
274                    .unitType = expr->unitType(),
275                    .isFinal = expr->isFinal()
276                });
277              if (expr->isConstExpr()) {              if (expr->isConstExpr()) {
278                  const int i = expr->evalInt();                  $$ = new Assignment(var, new IntLiteral({
279                  IntVariableRef var = new IntVariable(context);                      .value = expr->evalInt(),
280                  context->vartable[name] = var;                      .unitFactor = expr->unitFactor(),
281                  $$ = new Assignment(var, new IntLiteral(i));                      .unitType = expr->unitType(),
282                        .isFinal = expr->isFinal()
283                    }));
284              } else {              } else {
                 IntVariableRef var = new IntVariable(context);  
                 context->vartable[name] = var;  
285                  $$ = new Assignment(var, expr);                  $$ = new Assignment(var, expr);
286              }              }
287                context->vartable[name] = var;
288            } else if ($4->exprType() == EMPTY_EXPR) {
289                PARSE_ERR(@4, "Expression does not result in a value.");
290                $$ = new NoOperation;
291            } else if (isArray($4->exprType())) {
292                PARSE_ERR(@2, (String("Variable '") + name + "' declared as scalar type, array expression assigned though.").c_str());
293                $$ = new NoOperation;
294          }          }
295      }      }
296      | DECLARE VARIABLE '[' expr ']'  {      | DECLARE VARIABLE '[' expr ']'  {
# Line 192  statement: Line 298  statement:
298          const char* name = $2;          const char* name = $2;
299          if (!$4->isConstExpr()) {          if (!$4->isConstExpr()) {
300              PARSE_ERR(@4, (String("Array variable '") + name + "' must be declared with constant array size.").c_str());              PARSE_ERR(@4, (String("Array variable '") + name + "' must be declared with constant array size.").c_str());
             $$ = new FunctionCall("nothing", new Args, NULL); // whatever  
301          } else if ($4->exprType() != INT_EXPR) {          } else if ($4->exprType() != INT_EXPR) {
302              PARSE_ERR(@4, (String("Size of array variable '") + name + "' declared with non integer expression.").c_str());              PARSE_ERR(@4, (String("Size of array variable '") + name + "' declared with non integer expression.").c_str());
             $$ = new FunctionCall("nothing", new Args, NULL); // whatever  
303          } else if (context->variableByName(name)) {          } else if (context->variableByName(name)) {
304              PARSE_ERR(@2, (String("Redeclaration of variable '") + name + "'.").c_str());              PARSE_ERR(@2, (String("Redeclaration of variable '") + name + "'.").c_str());
             $$ = new FunctionCall("nothing", new Args, NULL); // whatever  
305          } else {          } else {
306              IntExprRef expr = $4;              IntExprRef sizeExpr = $4;
307              int size = expr->evalInt();              if (sizeExpr->unitType() || sizeExpr->hasUnitFactorNow()) {
308              if (size <= 0) {                  PARSE_ERR(@4, "Units are not allowed as array size.");
                 PARSE_ERR(@4, (String("Array variable '") + name + "' declared with array size " + ToString(size) + ".").c_str());  
                 $$ = new FunctionCall("nothing", new Args, NULL); // whatever  
309              } else {              } else {
310                  context->vartable[name] = new IntArrayVariable(context, size);                  if (sizeExpr->isFinal())
311                  $$ = new NoOperation;                      PARSE_WRN(@4, "Final operator '!' is meaningless here.");
312                    vmint size = sizeExpr->evalInt();
313                    if (size <= 0) {
314                        PARSE_ERR(@4, (String("Array variable '") + name + "' declared with array size " + ToString(size) + ".").c_str());
315                    } else {
316                        if (name[0] == '?') {
317                            context->vartable[name] = new RealArrayVariable(context, size);
318                        } else if (name[0] == '%') {
319                            context->vartable[name] = new IntArrayVariable(context, size);
320                        } else {
321                            PARSE_ERR(@2, (String("Variable '") + name + "' declared as unknown array type: use either '%' or '?' instead of '" + String(name).substr(0,1) + "'.").c_str());
322                        }
323                    }
324              }              }
325          }          }
326            $$ = new NoOperation;
327      }      }
328      | DECLARE VARIABLE '[' expr ']' ASSIGNMENT '(' args ')'  {      | DECLARE VARIABLE '[' expr ']' ASSIGNMENT '(' args ')'  {
329          const char* name = $2;          const char* name = $2;
330          if (!$4->isConstExpr()) {          if (!$4->isConstExpr()) {
331              PARSE_ERR(@4, (String("Array variable '") + name + "' must be declared with constant array size.").c_str());              PARSE_ERR(@4, (String("Array variable '") + name + "' must be declared with constant array size.").c_str());
             $$ = new FunctionCall("nothing", new Args, NULL); // whatever  
332          } else if ($4->exprType() != INT_EXPR) {          } else if ($4->exprType() != INT_EXPR) {
333              PARSE_ERR(@4, (String("Size of array variable '") + name + "' declared with non integer expression.").c_str());              PARSE_ERR(@4, (String("Size of array variable '") + name + "' declared with non integer expression.").c_str());
             $$ = new FunctionCall("nothing", new Args, NULL); // whatever  
334          } else if (context->variableByName(name)) {          } else if (context->variableByName(name)) {
335              PARSE_ERR(@2, (String("Redeclaration of variable '") + name + "'.").c_str());              PARSE_ERR(@2, (String("Redeclaration of variable '") + name + "'.").c_str());
             $$ = new FunctionCall("nothing", new Args, NULL); // whatever  
336          } else {          } else {
337              IntExprRef sizeExpr = $4;              IntExprRef sizeExpr = $4;
338              ArgsRef args = $8;              ArgsRef args = $8;
339              int size = sizeExpr->evalInt();              vmint size = sizeExpr->evalInt();
340              if (size <= 0) {              if (size <= 0) {
341                  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());
                 $$ = new FunctionCall("nothing", new Args, NULL); // whatever  
342              } else if (args->argsCount() > size) {              } else if (args->argsCount() > size) {
343                  PARSE_ERR(@8, (String("Variable '") + name +                  PARSE_ERR(@8, (String("Array variable '") + name +
344                              "' was declared with size " + ToString(size) +
345                              " but " + ToString(args->argsCount()) +
346                              " values were assigned." ).c_str());
347                } else if (sizeExpr->unitType() || sizeExpr->hasUnitFactorNow()) {
348                    PARSE_ERR(@4, "Units are not allowed as array size.");
349                } else {
350                    if (sizeExpr->isFinal())
351                        PARSE_WRN(@4, "Final operator '!' is meaningless here.");
352                    ExprType_t declType = EMPTY_EXPR;
353                    if (name[0] == '%') {
354                        declType = INT_EXPR;
355                    } else if (name[0] == '?') {
356                        declType = REAL_EXPR;
357                    } else if (name[0] == '$') {
358                        PARSE_ERR(@2, (String("Variable '") + name + "' declaration ambiguous: Use '%' as name prefix for integer arrays instead of '$'.").c_str());
359                    } else if (name[0] == '~') {
360                        PARSE_ERR(@2, (String("Variable '") + name + "' declaration ambiguous: Use '?' as name prefix for real number arrays instead of '~'.").c_str());
361                    } else {
362                        PARSE_ERR(@2, (String("Variable '") + name + "' declared as unknown array type: use either '%' or '?' instead of '" + String(name).substr(0,1) + "'.").c_str());
363                    }
364                    bool argsOK = true;
365                    if (declType == EMPTY_EXPR) {
366                        argsOK = false;
367                    } else {
368                        for (vmint i = 0; i < args->argsCount(); ++i) {
369                            if (args->arg(i)->exprType() != declType) {
370                                PARSE_ERR(
371                                    @8,
372                                    (String("Array variable '") + name +
373                                    "' declared with invalid assignment values. Assigned element " +
374                                    ToString(i+1) + " is not an " + typeStr(declType) + " expression.").c_str()
375                                );
376                                argsOK = false;
377                                break;
378                            } else if (args->arg(i)->asNumber()->unitType()) {
379                                PARSE_ERR(
380                                    @8,
381                                    (String("Array variable '") + name +
382                                    "' declared with invalid assignment values. Assigned element " +
383                                    ToString(i+1) + " contains a unit type, only metric prefixes are allowed for arrays.").c_str()
384                                );
385                                argsOK = false;
386                                break;
387                            } else if (args->arg(i)->asNumber()->isFinal()) {
388                                PARSE_ERR(
389                                    @8,
390                                    (String("Array variable '") + name +
391                                    "' declared with invalid assignment values. Assigned element " +
392                                    ToString(i+1) + " declared as 'final' value.").c_str()
393                                );
394                                argsOK = false;
395                                break;
396                            }
397                        }
398                    }
399                    if (argsOK) {
400                        if (declType == REAL_EXPR)
401                            context->vartable[name] = new RealArrayVariable(context, size, args);
402                        else
403                            context->vartable[name] = new IntArrayVariable(context, size, args);
404                    }
405                }
406            }
407            $$ = new NoOperation;
408        }
409        | DECLARE CONST_ VARIABLE '[' expr ']' ASSIGNMENT '(' args ')'  {
410            const char* name = $3;
411            if (!$5->isConstExpr()) {
412                PARSE_ERR(@5, (String("Array variable '") + name + "' must be declared with constant array size.").c_str());
413            } else if ($5->exprType() != INT_EXPR) {
414                PARSE_ERR(@5, (String("Size of array variable '") + name + "' declared with non integer expression.").c_str());
415            } else if (context->variableByName(name)) {
416                PARSE_ERR(@3, (String("Redeclaration of variable '") + name + "'.").c_str());
417            } else {
418                IntExprRef sizeExpr = $5;
419                ArgsRef args = $9;
420                vmint size = sizeExpr->evalInt();
421                if (size <= 0) {
422                    PARSE_ERR(@5, (String("Array variable '") + name + "' must be declared with positive array size.").c_str());
423                } else if (args->argsCount() > size) {
424                    PARSE_ERR(@9, (String("Array variable '") + name +
425                            "' was declared with size " + ToString(size) +                            "' was declared with size " + ToString(size) +
426                            " but " + ToString(args->argsCount()) +                            " but " + ToString(args->argsCount()) +
427                            " values were assigned." ).c_str());                            " values were assigned." ).c_str());
428                  $$ = new FunctionCall("nothing", new Args, NULL); // whatever                        } else if (sizeExpr->unitType() || sizeExpr->hasUnitFactorNow()) {
429                    PARSE_ERR(@5, "Units are not allowed as array size.");
430              } else {              } else {
431                    if (sizeExpr->isFinal())
432                        PARSE_WRN(@5, "Final operator '!' is meaningless here.");
433                    ExprType_t declType = EMPTY_EXPR;
434                    if (name[0] == '%') {
435                        declType = INT_EXPR;
436                    } else if (name[0] == '?') {
437                        declType = REAL_EXPR;
438                    } else if (name[0] == '$') {
439                        PARSE_ERR(@3, (String("Variable '") + name + "' declaration ambiguous: Use '%' as name prefix for integer arrays instead of '$'.").c_str());
440                    } else if (name[0] == '~') {
441                        PARSE_ERR(@3, (String("Variable '") + name + "' declaration ambiguous: Use '?' as name prefix for real number arrays instead of '~'.").c_str());
442                    } else {
443                        PARSE_ERR(@3, (String("Variable '") + name + "' declared as unknown array type: use either '%' or '?' instead of '" + String(name).substr(0,1) + "'.").c_str());
444                    }
445                  bool argsOK = true;                  bool argsOK = true;
446                  for (int i = 0; i < args->argsCount(); ++i) {                  if (declType == EMPTY_EXPR) {
447                      if (args->arg(i)->exprType() != INT_EXPR) {                      argsOK = false;
448                          PARSE_ERR(                  } else {
449                              @8,                      for (vmint i = 0; i < args->argsCount(); ++i) {
450                              (String("Array variable '") + name +                          if (args->arg(i)->exprType() != declType) {
451                              "' declared with invalid assignment values. Assigned element " +                              PARSE_ERR(
452                              ToString(i+1) + " is not an integer expression.").c_str()                                  @9,
453                          );                                  (String("const array variable '") + name +
454                          argsOK = false;                                  "' declared with invalid assignment values. Assigned element " +
455                          break;                                  ToString(i+1) + " is not an " + typeStr(declType) + " expression.").c_str()
456                                );
457                                argsOK = false;
458                                break;
459                            }
460                            if (!args->arg(i)->isConstExpr()) {
461                                PARSE_ERR(
462                                    @9,
463                                    (String("const array variable '") + name +
464                                    "' must be defined with const values. Assigned element " +
465                                    ToString(i+1) + " is not a const expression though.").c_str()
466                                );
467                                argsOK = false;
468                                break;
469                            } else if (args->arg(i)->asNumber()->unitType()) {
470                                PARSE_ERR(
471                                    @9,
472                                    (String("const array variable '") + name +
473                                    "' declared with invalid assignment values. Assigned element " +
474                                    ToString(i+1) + " contains a unit type, only metric prefixes are allowed for arrays.").c_str()
475                                );
476                                argsOK = false;
477                                break;
478                            } else if (args->arg(i)->asNumber()->isFinal()) {
479                                PARSE_ERR(
480                                    @9,
481                                    (String("const array variable '") + name +
482                                    "' declared with invalid assignment values. Assigned element " +
483                                    ToString(i+1) + " declared as 'final' value.").c_str()
484                                );
485                                argsOK = false;
486                                break;
487                            }
488                      }                      }
489                  }                  }
490                  if (argsOK)                  if (argsOK) {
491                      $$ = context->vartable[name] = new IntArrayVariable(context, size, args);                      if (declType == REAL_EXPR)
492                  else                          context->vartable[name] = new RealArrayVariable(context, size, args, true);
493                      $$ = new FunctionCall("nothing", new Args, NULL); // whatever                      else
494                            context->vartable[name] = new IntArrayVariable(context, size, args, true);
495                    }
496              }              }
497          }          }
498            $$ = new NoOperation;
499      }      }
500      | DECLARE CONST_ VARIABLE ASSIGNMENT expr  {      | DECLARE CONST_ VARIABLE ASSIGNMENT expr  {
501          const char* name = $3;          const char* name = $3;
502            const ExprType_t declType = exprTypeOfVarName(name);
503          if ($5->exprType() == STRING_EXPR) {          if ($5->exprType() == STRING_EXPR) {
504              if (name[0] == '$')              if (name[0] != '@')
505                  PARSE_WRN(@5, "Variable declared as integer, string expression assigned though.");                  PARSE_WRN(@5, (String("Variable '") + name + "' declared as " + typeStr(declType) + ", string expression assigned though.").c_str());
506              String s;              String s;
507              StringExprRef expr = $5;              StringExprRef expr = $5;
508              if (expr->isConstExpr())              if (expr->isConstExpr())
# Line 270  statement: Line 512  statement:
512              ConstStringVariableRef var = new ConstStringVariable(context, s);              ConstStringVariableRef var = new ConstStringVariable(context, s);
513              context->vartable[name] = var;              context->vartable[name] = var;
514              //$$ = new Assignment(var, new StringLiteral(s));              //$$ = new Assignment(var, new StringLiteral(s));
515              $$ = new NoOperation();          } else if ($5->exprType() == REAL_EXPR) {
516          } else {              if (name[0] != '~')
517              if (name[0] == '@')                  PARSE_WRN(@5, (String("Variable '") + name + "' declared as " + typeStr(declType) + ", real number expression assigned though.").c_str());
518                  PARSE_WRN(@5, "Variable declared as string, integer expression assigned though.");              RealExprRef expr = $5;
519              int i = 0;              if (!expr->isConstExpr()) {
520                    PARSE_ERR(@5, (String("Assignment to const real number variable '") + name + "' requires const expression.").c_str());
521                }
522                ConstRealVariableRef var = new ConstRealVariable({
523                    .value = (expr->isConstExpr()) ? expr->evalReal() : vmfloat(0),
524                    .unitFactor = (expr->isConstExpr()) ? expr->unitFactor() : VM_NO_FACTOR,
525                    .unitType = expr->unitType(),
526                    .isFinal = expr->isFinal()
527                });
528                context->vartable[name] = var;
529                //$$ = new Assignment(var, new IntLiteral(i));
530            } else if ($5->exprType() == INT_EXPR) {
531                if (name[0] != '$')
532                    PARSE_WRN(@5, (String("Variable '") + name + "' declared as " + typeStr(declType) + ", integer expression assigned though.").c_str());
533              IntExprRef expr = $5;              IntExprRef expr = $5;
534              if (expr->isConstExpr())              if (!expr->isConstExpr()) {
                 i = expr->evalInt();  
             else  
535                  PARSE_ERR(@5, (String("Assignment to const integer variable '") + name + "' requires const expression.").c_str());                  PARSE_ERR(@5, (String("Assignment to const integer variable '") + name + "' requires const expression.").c_str());
536              ConstIntVariableRef var = new ConstIntVariable(i);              }
537                ConstIntVariableRef var = new ConstIntVariable({
538                    .value = (expr->isConstExpr()) ? expr->evalInt() : 0,
539                    .unitFactor = (expr->isConstExpr()) ? expr->unitFactor() : VM_NO_FACTOR,
540                    .unitType = expr->unitType(),
541                    .isFinal = expr->isFinal()
542                });
543              context->vartable[name] = var;              context->vartable[name] = var;
544              //$$ = new Assignment(var, new IntLiteral(i));              //$$ = new Assignment(var, new IntLiteral(i));
545              $$ = new NoOperation();          } else if ($5->exprType() == EMPTY_EXPR) {
546                PARSE_ERR(@5, "Expression does not result in a value.");
547            } else if (isArray($5->exprType())) {
548                PARSE_ERR(@5, (String("Variable '") + name + "' declared as scalar type, array expression assigned though.").c_str());
549          }          }
550            $$ = new NoOperation();
551      }      }
552      | assignment  {      | assignment  {
553          $$ = $1;          $$ = $1;
554      }      }
555      | WHILE '(' expr ')' statements END WHILE  {      | WHILE '(' expr ')' opt_statements END WHILE  {
556          if ($3->exprType() == INT_EXPR) {          if ($3->exprType() == INT_EXPR) {
557              $$ = new While($3, $5);              IntExprRef expr = $3;
558                if (expr->asNumber()->unitType() ||
559                    expr->asNumber()->hasUnitFactorEver())
560                    PARSE_WRN(@3, "Condition for 'while' loops contains a unit.");
561                else if (expr->isFinal() && expr->isConstExpr())
562                    PARSE_WRN(@3, "Final operator '!' is meaningless here.");
563                $$ = new While(expr, $5);
564          } else {          } else {
565              PARSE_ERR(@3, "Condition for 'while' loops must be integer expression.");              PARSE_ERR(@3, "Condition for 'while' loops must be integer expression.");
566              $$ = new While(new IntLiteral(0), $5);              $$ = new While(new IntLiteral({ .value = 0 }), $5);
567          }          }
568      }      }
569      | IF '(' expr ')' statements ELSE statements END IF  {      | SYNCHRONIZED opt_statements END SYNCHRONIZED  {
570          $$ = new If($3, $5, $7);          $$ = new SyncBlock($2);
571        }
572        | IF '(' expr ')' opt_statements ELSE opt_statements END IF  {
573            if ($3->exprType() == INT_EXPR) {
574                IntExprRef expr = $3;
575                if (expr->asNumber()->unitType() ||
576                    expr->asNumber()->hasUnitFactorEver())
577                    PARSE_WRN(@3, "Condition for 'if' contains a unit.");
578                else if (expr->isFinal() && expr->isConstExpr())
579                    PARSE_WRN(@3, "Final operator '!' is meaningless here.");
580                $$ = new If($3, $5, $7);
581            } else {
582                PARSE_ERR(@3, "Condition for 'if' must be integer expression.");
583                $$ = new If(new IntLiteral({ .value = 0 }), $5, $7);
584            }
585      }      }
586      | IF '(' expr ')' statements END IF  {      | IF '(' expr ')' opt_statements END IF  {
587          $$ = new If($3, $5);          if ($3->exprType() == INT_EXPR) {
588                IntExprRef expr = $3;
589                if (expr->asNumber()->unitType() ||
590                    expr->asNumber()->hasUnitFactorEver())
591                    PARSE_WRN(@3, "Condition for 'if' contains a unit.");
592                else if (expr->isFinal() && expr->isConstExpr())
593                    PARSE_WRN(@3, "Final operator '!' is meaningless here.");
594                $$ = new If($3, $5);
595            } else {
596                PARSE_ERR(@3, "Condition for 'if' must be integer expression.");
597                $$ = new If(new IntLiteral({ .value = 0 }), $5);
598            }
599      }      }
600      | SELECT expr caseclauses END SELECT  {      | SELECT expr caseclauses END SELECT  {
601          if ($2->exprType() == INT_EXPR) {          if ($2->exprType() == INT_EXPR) {
602              $$ = new SelectCase($2, $3);              IntExprRef expr = $2;
603                if (expr->unitType() || expr->hasUnitFactorEver()) {
604                    PARSE_ERR(@2, "Units are not allowed here.");
605                    $$ = new SelectCase(new IntLiteral({ .value = 0 }), $3);
606                } else {
607                    if (expr->isFinal() && expr->isConstExpr())
608                        PARSE_WRN(@2, "Final operator '!' is meaningless here.");
609                    $$ = new SelectCase(expr, $3);
610                }
611          } else {          } else {
612              PARSE_ERR(@2, "Statement 'select' can only by applied to integer expressions.");              PARSE_ERR(@2, "Statement 'select' can only by applied to integer expressions.");
613              $$ = new SelectCase(new IntLiteral(0), $3);              $$ = new SelectCase(new IntLiteral({ .value = 0 }), $3);
614          }          }
615      }      }
616    
# Line 323  caseclauses: Line 625  caseclauses:
625      }      }
626    
627  caseclause:  caseclause:
628      CASE INTEGER statements  {      CASE INTEGER opt_statements  {
629          $$ = CaseBranch();          $$ = CaseBranch();
630          $$.from = new IntLiteral($2);          $$.from = new IntLiteral({ .value = $2 });
631          $$.statements = $3;          $$.statements = $3;
632      }      }
633      | CASE INTEGER TO INTEGER statements  {      | CASE INTEGER TO INTEGER opt_statements  {
634          $$ = CaseBranch();          $$ = CaseBranch();
635          $$.from = new IntLiteral($2);          $$.from = new IntLiteral({ .value = $2 });
636          $$.to   = new IntLiteral($4);          $$.to   = new IntLiteral({ .value = $4 });
637          $$.statements = $5;          $$.statements = $5;
638      }      }
639    
640    userfunctioncall:
641        CALL IDENTIFIER  {
642            const char* name = $2;
643            StatementsRef fn = context->userFunctionByName(name);
644            if (context->functionProvider->functionByName(name)) {
645                PARSE_ERR(@1, (String("Keyword 'call' must only be used for user defined functions, not for any built-in function like '") + name + "'.").c_str());
646                $$ = StatementsRef();
647            } else if (!fn) {
648                PARSE_ERR(@2, (String("No user defined function with name '") + name + "'.").c_str());
649                $$ = StatementsRef();
650            } else {
651                $$ = fn;
652            }
653        }
654    
655  functioncall:  functioncall:
656      IDENTIFIER '(' args ')'  {      IDENTIFIER '(' args ')'  {
657          const char* name = $1;          const char* name = $1;
658          //printf("function call of '%s' with args\n", name);          //printf("function call of '%s' with args\n", name);
659          ArgsRef args = $3;          ArgsRef args = $3;
660          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
661          if (!fn) {          if (context->userFunctionByName(name)) {
662                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
663                $$ = new FunctionCall(name, args, NULL);
664            } else if (!fn) {
665              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());
666              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
667            } else if (context->functionProvider->isFunctionDisabled(fn,context)) {
668                PARSE_DROP(@$);
669                $$ = new NoFunctionCall;
670          } else if (args->argsCount() < fn->minRequiredArgs()) {          } else if (args->argsCount() < fn->minRequiredArgs()) {
671              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());
672              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
# Line 352  functioncall: Line 675  functioncall:
675              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
676          } else {          } else {
677              bool argsOK = true;              bool argsOK = true;
678              for (int i = 0; i < args->argsCount(); ++i) {              for (vmint i = 0; i < args->argsCount(); ++i) {
679                  if (args->arg(i)->exprType() != fn->argType(i) && !fn->acceptsArgType(i, args->arg(i)->exprType())) {                  if (!fn->acceptsArgType(i, args->arg(i)->exprType())) {
680                      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 " + acceptedArgTypesStr(fn, i) + " type, but type " + typeStr(args->arg(i)->exprType()) + " was given instead.").c_str());
681                      argsOK = false;                      argsOK = false;
682                      break;                      break;
683                  } else if (fn->modifiesArg(i) && !args->arg(i)->isModifyable()) {                  } else if (fn->modifiesArg(i) && !args->arg(i)->isModifyable()) {
684                      PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' expects an assignable variable.").c_str());                      PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' expects an assignable variable.").c_str());
685                      argsOK = false;                      argsOK = false;
686                      break;                      break;
687                    } else if (isNumber(args->arg(i)->exprType()) && !fn->acceptsArgUnitType(i, args->arg(i)->asNumber()->unitType())) {
688                        if (args->arg(i)->asNumber()->unitType())
689                            PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' does not expect unit " + unitTypeStr(args->arg(i)->asNumber()->unitType()) +  ".").c_str());
690                        else
691                            PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' expects a unit.").c_str());
692                        argsOK = false;
693                        break;
694                    } else if (isNumber(args->arg(i)->exprType()) && args->arg(i)->asNumber()->hasUnitFactorEver() && !fn->acceptsArgUnitPrefix(i, args->arg(i)->asNumber()->unitType())) {
695                        if (args->arg(i)->asNumber()->unitType())
696                            PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' does not expect a unit prefix for unit" + unitTypeStr(args->arg(i)->asNumber()->unitType()) + ".").c_str());
697                        else
698                            PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' does not expect a unit prefix.").c_str());
699                        argsOK = false;
700                        break;
701                    } else if (!fn->acceptsArgFinal(i) && isNumber(args->arg(i)->exprType()) && args->arg(i)->asNumber()->isFinal()) {
702                        PARSE_ERR(@3, (String("Argument ") + ToString(i+1) + " of built-in function '" + name + "' does not expect a \"final\" value.").c_str());
703                        argsOK = false;
704                        break;
705                  }                  }
706              }              }
707                if (argsOK) {
708                    // perform built-in function's own, custom arguments checks (if any)
709                    fn->checkArgs(&*args, [&](String err) {
710                        PARSE_ERR(@3, (String("Built-in function '") + name + "()': " + err).c_str());
711                        argsOK = false;
712                    }, [&](String wrn) {
713                        PARSE_WRN(@3, (String("Built-in function '") + name + "()': " + wrn).c_str());
714                    });
715                }
716              $$ = new FunctionCall(name, args, argsOK ? fn : NULL);              $$ = new FunctionCall(name, args, argsOK ? fn : NULL);
717          }          }
718      }      }
# Line 371  functioncall: Line 721  functioncall:
721          //printf("function call of '%s' (with empty args)\n", name);          //printf("function call of '%s' (with empty args)\n", name);
722          ArgsRef args = new Args;          ArgsRef args = new Args;
723          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
724          if (!fn) {          if (context->userFunctionByName(name)) {
725                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
726                $$ = new FunctionCall(name, args, NULL);
727            } else if (!fn) {
728              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());
729              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
730            } else if (context->functionProvider->isFunctionDisabled(fn,context)) {
731                PARSE_DROP(@$);
732                $$ = new NoFunctionCall;
733          } else if (fn->minRequiredArgs() > 0) {          } else if (fn->minRequiredArgs() > 0) {
734              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());
735              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
# Line 386  functioncall: Line 742  functioncall:
742          //printf("function call of '%s' (without args)\n", name);          //printf("function call of '%s' (without args)\n", name);
743          ArgsRef args = new Args;          ArgsRef args = new Args;
744          VMFunction* fn = context->functionProvider->functionByName(name);          VMFunction* fn = context->functionProvider->functionByName(name);
745          if (!fn) {          if (context->userFunctionByName(name)) {
746                PARSE_ERR(@1, (String("Missing 'call' keyword before user defined function name '") + name + "'.").c_str());
747                $$ = new FunctionCall(name, args, NULL);
748            } else if (!fn) {
749              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());
750              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
751            } else if (context->functionProvider->isFunctionDisabled(fn,context)) {
752                PARSE_DROP(@$);
753                $$ = new NoFunctionCall;
754          } else if (fn->minRequiredArgs() > 0) {          } else if (fn->minRequiredArgs() > 0) {
755              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());
756              $$ = new FunctionCall(name, args, NULL);              $$ = new FunctionCall(name, args, NULL);
# Line 423  assignment: Line 785  assignment:
785              PARSE_ERR(@2, (String("Variable assignment: Variable '") + name + "' is not assignable.").c_str());              PARSE_ERR(@2, (String("Variable assignment: Variable '") + name + "' is not assignable.").c_str());
786          else if (var->exprType() != $3->exprType())          else if (var->exprType() != $3->exprType())
787              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());
788            else if (isNumber(var->exprType())) {
789                NumberVariableRef numberVar = var;
790                NumberExprRef expr = $3;
791                if (numberVar->unitType() != expr->unitType())
792                    PARSE_ERR(@3, (String("Variable assignment: Variable '") + name + "' has unit type " + unitTypeStr(numberVar->unitType()) + ", assignment has unit type " + unitTypeStr(expr->unitType()) + " though.").c_str());
793                else if (numberVar->isFinal() != expr->isFinal())
794                    PARSE_ERR(@3, (String("Variable assignment: Variable '") + name + "' was declared as " + String(numberVar->isFinal() ? "final" : "not final") + ", assignment is " + String(expr->isFinal() ? "final" : "not final") + " though.").c_str());
795            }
796          $$ = new Assignment(var, $3);          $$ = new Assignment(var, $3);
797      }      }
798      | VARIABLE '[' expr ']' ASSIGNMENT expr  {      | VARIABLE '[' expr ']' ASSIGNMENT expr  {
# Line 430  assignment: Line 800  assignment:
800          VariableRef var = context->variableByName(name);          VariableRef var = context->variableByName(name);
801          if (!var)          if (!var)
802              PARSE_ERR(@1, (String("No variable declared with name '") + name + "'.").c_str());              PARSE_ERR(@1, (String("No variable declared with name '") + name + "'.").c_str());
803          else if (var->exprType() != INT_ARR_EXPR)          else if (!isArray(var->exprType()))
804              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());
805            else if (var->isConstExpr())
806                PARSE_ERR(@5, (String("Variable assignment: Cannot modify const array variable '") + name + "'.").c_str());
807            else if (!var->isAssignable())
808                PARSE_ERR(@5, (String("Variable assignment: Array variable '") + name + "' is not assignable.").c_str());
809          else if ($3->exprType() != INT_EXPR)          else if ($3->exprType() != INT_EXPR)
810              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());
811          else if ($6->exprType() != INT_EXPR)          else if ($3->asInt()->unitType())
812              PARSE_ERR(@5, (String("Value assigned to array variable '") + name + "' must be an integer expression.").c_str());              PARSE_ERR(@3, "Unit types are not allowed as array index.");
813          IntArrayElementRef element = new IntArrayElement(var, $3);          else if ($6->exprType() != scalarTypeOfArray(var->exprType()))
814          $$ = new Assignment(element, $6);              PARSE_ERR(@5, (String("Variable '") + name + "' was declared as " + typeStr(var->exprType()) + ", assigned expression is " + typeStr($6->exprType()) + " though.").c_str());
815            else if ($6->asNumber()->unitType())
816                PARSE_ERR(@6, "Unit types are not allowed for array variables.");
817            else if ($6->asNumber()->isFinal())
818                PARSE_ERR(@6, "Final operator '!' not allowed for array variables.");
819            else if ($3->isConstExpr() && $3->asInt()->evalInt() >= ((ArrayExprRef)var)->arraySize())
820                PARSE_WRN(@3, (String("Index ") + ToString($3->asInt()->evalInt()) +
821                              " exceeds size of array variable '" + name +
822                              "' which was declared with size " +
823                              ToString(((ArrayExprRef)var)->arraySize()) + ".").c_str());
824            else if ($3->asInt()->isFinal())
825                PARSE_WRN(@3, "Final operator '!' is meaningless here.");
826            if (var->exprType() == INT_ARR_EXPR) {
827                IntArrayElementRef element = new IntArrayElement(var, $3);
828                $$ = new Assignment(element, $6);
829            } else if (var->exprType() == REAL_ARR_EXPR) {
830                RealArrayElementRef element = new RealArrayElement(var, $3);
831                $$ = new Assignment(element, $6);
832            } else {
833                $$ = new NoOperation; // actually not possible to ever get here
834            }
835      }      }
836    
837  unary_expr:  unary_expr:
838      INTEGER  {      INTEGER  {
839          $$ = new IntLiteral($1);          $$ = new IntLiteral({ .value = $1 });
840        }
841        | REAL  {
842            $$ = new RealLiteral({ .value = $1 });
843        }
844        | INTEGER_UNIT  {
845            IntLiteralRef literal = new IntLiteral({
846                .value = $1.iValue,
847                .unitFactor = VMUnit::unitFactor($1.prefix),
848                .unitType = $1.unit
849            });
850            $$ = literal;
851        }
852        | REAL_UNIT  {
853            RealLiteralRef literal = new RealLiteral({
854                .value = $1.fValue,
855                .unitFactor = VMUnit::unitFactor($1.prefix),
856                .unitType = $1.unit
857            });
858            $$ = literal;
859      }      }
860      | STRING    {      | STRING    {
861          $$ = new StringLiteral($1);          $$ = new StringLiteral($1);
# Line 454  unary_expr: Line 867  unary_expr:
867              $$ = var;              $$ = var;
868          else {          else {
869              PARSE_ERR(@1, (String("No variable declared with name '") + $1 + "'.").c_str());              PARSE_ERR(@1, (String("No variable declared with name '") + $1 + "'.").c_str());
870              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
871          }          }
872      }      }
873      | VARIABLE '[' expr ']'  {      | VARIABLE '[' expr ']'  {
# Line 462  unary_expr: Line 875  unary_expr:
875          VariableRef var = context->variableByName(name);          VariableRef var = context->variableByName(name);
876          if (!var) {          if (!var) {
877              PARSE_ERR(@1, (String("No variable declared with name '") + name + "'.").c_str());              PARSE_ERR(@1, (String("No variable declared with name '") + name + "'.").c_str());
878              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
879          } else if (var->exprType() != INT_ARR_EXPR) {          } else if (!isArray(var->exprType())) {
880              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());
881              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
882          } else if ($3->exprType() != INT_EXPR) {          } else if ($3->exprType() != INT_EXPR) {
883              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());
884              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
885          } else {          } else if ($3->asInt()->unitType() || $3->asInt()->hasUnitFactorEver()) {
886              $$ = new IntArrayElement(var, $3);              PARSE_ERR(@3, "Units are not allowed as array index.");
887                $$ = new IntLiteral({ .value = 0 });
888            } else {
889                if ($3->isConstExpr() && $3->asInt()->evalInt() >= ((ArrayExprRef)var)->arraySize())
890                    PARSE_WRN(@3, (String("Index ") + ToString($3->asInt()->evalInt()) +
891                                   " exceeds size of array variable '" + name +
892                                   "' which was declared with size " +
893                                   ToString(((ArrayExprRef)var)->arraySize()) + ".").c_str());
894                else if ($3->asInt()->isFinal())
895                    PARSE_WRN(@3, "Final operator '!' is meaningless here.");
896                if (var->exprType() == REAL_ARR_EXPR) {
897                    $$ = new RealArrayElement(var, $3);
898                } else {
899                    $$ = new IntArrayElement(var, $3);
900                }
901          }          }
902      }      }
903      | '(' expr ')'  {      | '(' expr ')'  {
# Line 479  unary_expr: Line 906  unary_expr:
906      | functioncall  {      | functioncall  {
907          $$ = $1;          $$ = $1;
908      }      }
909        | '+' unary_expr  {
910            $$ = $2;
911        }
912      | '-' unary_expr  {      | '-' unary_expr  {
913          $$ = new Neg($2);          $$ = new Neg($2);
914      }      }
915      | BITWISE_NOT unary_expr  {      | BITWISE_NOT unary_expr  {
916          if ($2->exprType() != INT_EXPR) {          if ($2->exprType() != INT_EXPR) {
917              PARSE_ERR(@2, (String("Right operand of bitwise operator '.not.' must be an integer expression, is ") + typeStr($2->exprType()) + " though.").c_str());              PARSE_ERR(@2, (String("Right operand of bitwise operator '.not.' must be an integer expression, is ") + typeStr($2->exprType()) + " though.").c_str());
918              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
919            } else if ($2->asInt()->unitType() || $2->asInt()->hasUnitFactorEver()) {
920                PARSE_ERR(@2, "Units are not allowed for operands of bitwise operations.");
921                $$ = new IntLiteral({ .value = 0 });
922          } else {          } else {
923              $$ = new BitwiseNot($2);              $$ = new BitwiseNot($2);
924          }          }
# Line 493  unary_expr: Line 926  unary_expr:
926      | NOT unary_expr  {      | NOT unary_expr  {
927          if ($2->exprType() != INT_EXPR) {          if ($2->exprType() != INT_EXPR) {
928              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());
929              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
930            } else if ($2->asInt()->unitType() || $2->asInt()->hasUnitFactorEver()) {
931                PARSE_ERR(@2, "Units are not allowed for operands of logical operations.");
932                $$ = new IntLiteral({ .value = 0 });
933          } else {          } else {
934              $$ = new Not($2);              $$ = new Not($2);
935          }          }
936      }      }
937        | '!' unary_expr  {
938            if (!isNumber($2->exprType())) {
939                PARSE_ERR(@2, (String("Right operand of \"final\" operator '!' must be a scalar number expression, is ") + typeStr($2->exprType()) + " though.").c_str());
940                $$ = new IntLiteral({ .value = 0 });
941            } else {
942                $$ = new Final($2);
943            }
944        }
945    
946  expr:  expr:
947      concat_expr      concat_expr
# Line 523  logical_or_expr: Line 967  logical_or_expr:
967          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
968          if (lhs->exprType() != INT_EXPR) {          if (lhs->exprType() != INT_EXPR) {
969              PARSE_ERR(@1, (String("Left operand of operator 'or' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());              PARSE_ERR(@1, (String("Left operand of operator 'or' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
970              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
971          } else if (rhs->exprType() != INT_EXPR) {          } else if (rhs->exprType() != INT_EXPR) {
972              PARSE_ERR(@3, (String("Right operand of operator 'or' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());              PARSE_ERR(@3, (String("Right operand of operator 'or' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
973              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
974          } else {          } else if (lhs->asInt()->unitType() || lhs->asInt()->hasUnitFactorEver()) {
975                PARSE_ERR(@1, "Units are not allowed for operands of logical operations.");
976                $$ = new IntLiteral({ .value = 0 });
977            } else if (rhs->asInt()->unitType() || rhs->asInt()->hasUnitFactorEver()) {
978                PARSE_ERR(@3, "Units are not allowed for operands of logical operations.");
979                $$ = new IntLiteral({ .value = 0 });
980            } else {
981                if (lhs->asInt()->isFinal() && !rhs->asInt()->isFinal())
982                    PARSE_WRN(@3, "Right operand of 'or' operation is not 'final', result will be 'final' though since left operand is 'final'.");
983                else if (!lhs->asInt()->isFinal() && rhs->asInt()->isFinal())
984                    PARSE_WRN(@1, "Left operand of 'or' operation is not 'final', result will be 'final' though since right operand is 'final'.");
985              $$ = new Or(lhs, rhs);              $$ = new Or(lhs, rhs);
986          }          }
987      }      }
# Line 541  logical_and_expr: Line 995  logical_and_expr:
995          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
996          if (lhs->exprType() != INT_EXPR) {          if (lhs->exprType() != INT_EXPR) {
997              PARSE_ERR(@1, (String("Left operand of operator 'and' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());              PARSE_ERR(@1, (String("Left operand of operator 'and' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
998              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
999          } else if (rhs->exprType() != INT_EXPR) {          } else if (rhs->exprType() != INT_EXPR) {
1000              PARSE_ERR(@3, (String("Right operand of operator 'and' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());              PARSE_ERR(@3, (String("Right operand of operator 'and' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1001              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1002          } else {          } else if (lhs->asInt()->unitType() || lhs->asInt()->hasUnitFactorEver()) {
1003                PARSE_ERR(@1, "Units are not allowed for operands of logical operations.");
1004                $$ = new IntLiteral({ .value = 0 });
1005            } else if (rhs->asInt()->unitType() || rhs->asInt()->hasUnitFactorEver()) {
1006                PARSE_ERR(@3, "Units are not allowed for operands of logical operations.");
1007                $$ = new IntLiteral({ .value = 0 });
1008            } else {
1009                if (lhs->asInt()->isFinal() && !rhs->asInt()->isFinal())
1010                    PARSE_WRN(@3, "Right operand of 'and' operation is not 'final', result will be 'final' though since left operand is 'final'.");
1011                else if (!lhs->asInt()->isFinal() && rhs->asInt()->isFinal())
1012                    PARSE_WRN(@1, "Left operand of 'and' operation is not 'final', result will be 'final' though since right operand is 'final'.");
1013              $$ = new And(lhs, rhs);              $$ = new And(lhs, rhs);
1014          }          }
1015      }      }
# Line 557  bitwise_or_expr: Line 1021  bitwise_or_expr:
1021          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
1022          if (lhs->exprType() != INT_EXPR) {          if (lhs->exprType() != INT_EXPR) {
1023              PARSE_ERR(@1, (String("Left operand of bitwise operator '.or.' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());              PARSE_ERR(@1, (String("Left operand of bitwise operator '.or.' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1024              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1025          } else if (rhs->exprType() != INT_EXPR) {          } else if (rhs->exprType() != INT_EXPR) {
1026              PARSE_ERR(@3, (String("Right operand of bitwise operator '.or.' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());              PARSE_ERR(@3, (String("Right operand of bitwise operator '.or.' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1027              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1028          } else {          } else if (lhs->asInt()->unitType() || lhs->asInt()->hasUnitFactorEver()) {
1029                PARSE_ERR(@1, "Units are not allowed for operands of bitwise operations.");
1030                $$ = new IntLiteral({ .value = 0 });
1031            } else if (rhs->asInt()->unitType() || rhs->asInt()->hasUnitFactorEver()) {
1032                PARSE_ERR(@3, "Units are not allowed for operands of bitwise operations.");
1033                $$ = new IntLiteral({ .value = 0 });
1034            } else {
1035                if (lhs->asInt()->isFinal() && !rhs->asInt()->isFinal())
1036                    PARSE_WRN(@3, "Right operand of '.or.' operation is not 'final', result will be 'final' though since left operand is 'final'.");
1037                else if (!lhs->asInt()->isFinal() && rhs->asInt()->isFinal())
1038                    PARSE_WRN(@1, "Left operand of '.or.' operation is not 'final', result will be 'final' though since right operand is 'final'.");
1039              $$ = new BitwiseOr(lhs, rhs);              $$ = new BitwiseOr(lhs, rhs);
1040          }          }
1041      }      }
# Line 575  bitwise_and_expr: Line 1049  bitwise_and_expr:
1049          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
1050          if (lhs->exprType() != INT_EXPR) {          if (lhs->exprType() != INT_EXPR) {
1051              PARSE_ERR(@1, (String("Left operand of bitwise operator '.and.' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());              PARSE_ERR(@1, (String("Left operand of bitwise operator '.and.' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1052              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1053          } else if (rhs->exprType() != INT_EXPR) {          } else if (rhs->exprType() != INT_EXPR) {
1054              PARSE_ERR(@3, (String("Right operand of bitwise operator '.and.' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());              PARSE_ERR(@3, (String("Right operand of bitwise operator '.and.' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1055              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1056          } else {          } else if (lhs->asInt()->unitType() || lhs->asInt()->hasUnitFactorEver()) {
1057                PARSE_ERR(@1, "Units are not allowed for operands of bitwise operations.");
1058                $$ = new IntLiteral({ .value = 0 });
1059            } else if (rhs->asInt()->unitType() || rhs->asInt()->hasUnitFactorEver()) {
1060                PARSE_ERR(@3, "Units are not allowed for operands of bitwise operations.");
1061                $$ = new IntLiteral({ .value = 0 });
1062            } else {
1063                if (lhs->asInt()->isFinal() && !rhs->asInt()->isFinal())
1064                    PARSE_WRN(@3, "Right operand of '.and.' operation is not 'final', result will be 'final' though since left operand is 'final'.");
1065                else if (!lhs->asInt()->isFinal() && rhs->asInt()->isFinal())
1066                    PARSE_WRN(@1, "Left operand of '.and.' operation is not 'final', result will be 'final' though since right operand is 'final'.");
1067              $$ = new BitwiseAnd(lhs, rhs);              $$ = new BitwiseAnd(lhs, rhs);
1068          }          }
1069      }      }
# Line 589  rel_expr: Line 1073  rel_expr:
1073      | rel_expr '<' add_expr  {      | rel_expr '<' add_expr  {
1074          ExpressionRef lhs = $1;          ExpressionRef lhs = $1;
1075          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
1076          if (lhs->exprType() != INT_EXPR) {          if (!isNumber(lhs->exprType())) {
1077              PARSE_ERR(@1, (String("Left operand of operator '<' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());              PARSE_ERR(@1, (String("Left operand of operator '<' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1078              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1079          } else if (rhs->exprType() != INT_EXPR) {          } else if (!isNumber(rhs->exprType())) {
1080              PARSE_ERR(@3, (String("Right operand of operator '<' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());              PARSE_ERR(@3, (String("Right operand of operator '<' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1081              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1082          } else {          } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1083                PARSE_ERR(@2, (String("Operands of relative operations must have same unit, left operand is ") +
1084                    unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1085                    unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1086                $$ = new IntLiteral({ .value = 0 });
1087            } else {
1088                if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1089                    PARSE_WRN(@3, "Right operand of '<' comparison is not 'final', left operand is 'final' though.");
1090                else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1091                    PARSE_WRN(@1, "Left operand of '<' comparison is not 'final', right operand is 'final' though.");
1092              $$ = new Relation(lhs, Relation::LESS_THAN, rhs);              $$ = new Relation(lhs, Relation::LESS_THAN, rhs);
1093          }          }
1094      }      }
1095      | rel_expr '>' add_expr  {      | rel_expr '>' add_expr  {
1096          ExpressionRef lhs = $1;          ExpressionRef lhs = $1;
1097          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
1098          if (lhs->exprType() != INT_EXPR) {          if (!isNumber(lhs->exprType())) {
1099              PARSE_ERR(@1, (String("Left operand of operator '>' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());              PARSE_ERR(@1, (String("Left operand of operator '>' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1100              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1101          } else if (rhs->exprType() != INT_EXPR) {          } else if (!isNumber(rhs->exprType())) {
1102              PARSE_ERR(@3, (String("Right operand of operator '>' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());              PARSE_ERR(@3, (String("Right operand of operator '>' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1103              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1104          } else {          } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1105                PARSE_ERR(@2, (String("Operands of relative operations must have same unit, left operand is ") +
1106                    unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1107                    unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1108                $$ = new IntLiteral({ .value = 0 });
1109            } else {
1110                if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1111                    PARSE_WRN(@3, "Right operand of '>' comparison is not 'final', left operand is 'final' though.");
1112                else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1113                    PARSE_WRN(@1, "Left operand of '>' comparison is not 'final', right operand is 'final' though.");
1114              $$ = new Relation(lhs, Relation::GREATER_THAN, rhs);              $$ = new Relation(lhs, Relation::GREATER_THAN, rhs);
1115          }          }
1116      }      }
1117      | rel_expr LE add_expr  {      | rel_expr LE add_expr  {
1118          ExpressionRef lhs = $1;          ExpressionRef lhs = $1;
1119          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
1120          if (lhs->exprType() != INT_EXPR) {          if (!isNumber(lhs->exprType())) {
1121              PARSE_ERR(@1, (String("Left operand of operator '<=' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());              PARSE_ERR(@1, (String("Left operand of operator '<=' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1122              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1123          } else if (rhs->exprType() != INT_EXPR) {          } else if (!isNumber(rhs->exprType())) {
1124              PARSE_ERR(@3, (String("Right operand of operator '<=' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());              PARSE_ERR(@3, (String("Right operand of operator '<=' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1125              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1126          } else {          } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1127                PARSE_ERR(@2, (String("Operands of relative operations must have same unit, left operand is ") +
1128                    unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1129                    unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1130                $$ = new IntLiteral({ .value = 0 });
1131            } else {
1132                if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1133                    PARSE_WRN(@3, "Right operand of '<=' comparison is not 'final', left operand is 'final' though.");
1134                else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1135                    PARSE_WRN(@1, "Left operand of '<=' comparison is not 'final', right operand is 'final' though.");
1136              $$ = new Relation(lhs, Relation::LESS_OR_EQUAL, rhs);              $$ = new Relation(lhs, Relation::LESS_OR_EQUAL, rhs);
1137          }          }
1138      }      }
1139      | rel_expr GE add_expr  {      | rel_expr GE add_expr  {
1140          ExpressionRef lhs = $1;          ExpressionRef lhs = $1;
1141          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
1142          if (lhs->exprType() != INT_EXPR) {          if (!isNumber(lhs->exprType())) {
1143              PARSE_ERR(@1, (String("Left operand of operator '>=' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());              PARSE_ERR(@1, (String("Left operand of operator '>=' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1144              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1145          } else if (rhs->exprType() != INT_EXPR) {          } else if (!isNumber(rhs->exprType())) {
1146              PARSE_ERR(@3, (String("Right operand of operator '>=' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());              PARSE_ERR(@3, (String("Right operand of operator '>=' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1147              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1148          } else {          } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1149                PARSE_ERR(@2, (String("Operands of relative operations must have same unit, left operand is ") +
1150                    unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1151                    unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1152                $$ = new IntLiteral({ .value = 0 });
1153            } else {
1154                if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1155                    PARSE_WRN(@3, "Right operand of '>=' comparison is not 'final', left operand is 'final' though.");
1156                else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1157                    PARSE_WRN(@1, "Left operand of '>=' comparison is not 'final', right operand is 'final' though.");
1158              $$ = new Relation(lhs, Relation::GREATER_OR_EQUAL, rhs);              $$ = new Relation(lhs, Relation::GREATER_OR_EQUAL, rhs);
1159          }          }
1160      }      }
1161      | rel_expr '=' add_expr  {      | rel_expr '=' add_expr  {
1162          $$ = new Relation($1, Relation::EQUAL, $3);          ExpressionRef lhs = $1;
1163            ExpressionRef rhs = $3;
1164            if (!isNumber(lhs->exprType())) {
1165                PARSE_ERR(@1, (String("Left operand of operator '=' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1166                $$ = new IntLiteral({ .value = 0 });
1167            } else if (!isNumber(rhs->exprType())) {
1168                PARSE_ERR(@3, (String("Right operand of operator '=' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1169                $$ = new IntLiteral({ .value = 0 });
1170            } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1171                PARSE_ERR(@2, (String("Operands of relative operations must have same unit, left operand is ") +
1172                    unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1173                    unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1174                $$ = new IntLiteral({ .value = 0 });
1175            } else {
1176                if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1177                    PARSE_WRN(@3, "Right operand of '=' comparison is not 'final', left operand is 'final' though.");
1178                else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1179                    PARSE_WRN(@1, "Left operand of '=' comparison is not 'final', right operand is 'final' though.");
1180                $$ = new Relation(lhs, Relation::EQUAL, rhs);
1181            }
1182      }      }
1183      | rel_expr '#' add_expr  {      | rel_expr '#' add_expr  {
1184          $$ = new Relation($1, Relation::NOT_EQUAL, $3);          ExpressionRef lhs = $1;
1185            ExpressionRef rhs = $3;
1186            if (!isNumber(lhs->exprType())) {
1187                PARSE_ERR(@1, (String("Left operand of operator '#' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1188                $$ = new IntLiteral({ .value = 0 });
1189            } else if (!isNumber(rhs->exprType())) {
1190                PARSE_ERR(@3, (String("Right operand of operator '#' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1191                $$ = new IntLiteral({ .value = 0 });
1192            } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1193                PARSE_ERR(@2, (String("Operands of relative operations must have same unit, left operand is ") +
1194                    unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1195                    unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1196                $$ = new IntLiteral({ .value = 0 });
1197            } else {
1198                if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1199                    PARSE_WRN(@3, "Right operand of '#' comparison is not 'final', left operand is 'final' though.");
1200                else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1201                    PARSE_WRN(@1, "Left operand of '#' comparison is not 'final', right operand is 'final' though.");
1202                $$ = new Relation(lhs, Relation::NOT_EQUAL, rhs);
1203            }
1204      }      }
1205    
1206  add_expr:  add_expr:
# Line 650  add_expr: Line 1208  add_expr:
1208      | add_expr '+' mul_expr  {      | add_expr '+' mul_expr  {
1209          ExpressionRef lhs = $1;          ExpressionRef lhs = $1;
1210          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
1211          if (lhs->exprType() != INT_EXPR) {          if (!isNumber(lhs->exprType())) {
1212              PARSE_ERR(@1, (String("Left operand of operator '+' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());              PARSE_ERR(@1, (String("Left operand of operator '+' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1213              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1214          } else if (rhs->exprType() != INT_EXPR) {          } else if (!isNumber(rhs->exprType())) {
1215              PARSE_ERR(@3, (String("Right operand of operator '+' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());              PARSE_ERR(@1, (String("Right operand of operator '+' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1216              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1217          } else {          } else if (lhs->exprType() != rhs->exprType()) {
1218                PARSE_ERR(@2, (String("Operands of operator '+' must have same type; left operand is ") +
1219                          typeStr(lhs->exprType()) + " and right operand is " + typeStr(rhs->exprType()) + " though.").c_str());
1220                $$ = new IntLiteral({ .value = 0 });
1221            } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1222                PARSE_ERR(@2, (String("Operands of '+' operations must have same unit, left operand is ") +
1223                    unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1224                    unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1225                $$ = new IntLiteral({ .value = 0 });
1226            } else {
1227                if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1228                    PARSE_WRN(@3, "Right operand of '+' operation is not 'final', result will be 'final' though since left operand is 'final'.");
1229                else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1230                    PARSE_WRN(@1, "Left operand of '+' operation is not 'final', result will be 'final' though since right operand is 'final'.");
1231              $$ = new Add(lhs,rhs);              $$ = new Add(lhs,rhs);
1232          }          }
1233      }      }
1234      | add_expr '-' mul_expr  {      | add_expr '-' mul_expr  {
1235          ExpressionRef lhs = $1;          ExpressionRef lhs = $1;
1236          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
1237          if (lhs->exprType() != INT_EXPR) {          if (!isNumber(lhs->exprType())) {
1238              PARSE_ERR(@1, (String("Left operand of operator '-' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());              PARSE_ERR(@1, (String("Left operand of operator '-' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1239              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1240          } else if (rhs->exprType() != INT_EXPR) {          } else if (!isNumber(rhs->exprType())) {
1241              PARSE_ERR(@3, (String("Right operand of operator '-' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());              PARSE_ERR(@1, (String("Right operand of operator '-' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1242              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1243          } else {          } else if (lhs->exprType() != rhs->exprType()) {
1244                PARSE_ERR(@2, (String("Operands of operator '-' must have same type; left operand is ") +
1245                          typeStr(lhs->exprType()) + " and right operand is " + typeStr(rhs->exprType()) + " though.").c_str());
1246                $$ = new IntLiteral({ .value = 0 });
1247            } else if (lhs->asNumber()->unitType() != rhs->asNumber()->unitType()) {
1248                PARSE_ERR(@2, (String("Operands of '-' operations must have same unit, left operand is ") +
1249                    unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1250                    unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1251                $$ = new IntLiteral({ .value = 0 });
1252            } else {
1253                if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1254                    PARSE_WRN(@3, "Right operand of '-' operation is not 'final', result will be 'final' though since left operand is 'final'.");
1255                else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1256                    PARSE_WRN(@1, "Left operand of '-' operation is not 'final', result will be 'final' though since right operand is 'final'.");
1257              $$ = new Sub(lhs,rhs);              $$ = new Sub(lhs,rhs);
1258          }          }
1259      }      }
# Line 679  mul_expr: Line 1263  mul_expr:
1263      | mul_expr '*' unary_expr  {      | mul_expr '*' unary_expr  {
1264          ExpressionRef lhs = $1;          ExpressionRef lhs = $1;
1265          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
1266          if (lhs->exprType() != INT_EXPR) {          if (!isNumber(lhs->exprType())) {
1267              PARSE_ERR(@1, (String("Left operand of operator '*' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());              PARSE_ERR(@1, (String("Left operand of operator '*' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1268              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1269          } else if (rhs->exprType() != INT_EXPR) {          } else if (!isNumber(rhs->exprType())) {
1270              PARSE_ERR(@3, (String("Right operand of operator '*' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());              PARSE_ERR(@1, (String("Right operand of operator '*' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1271              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1272          } else {          } else if (lhs->asNumber()->unitType() && rhs->asNumber()->unitType()) {
1273                PARSE_ERR(@2, (String("Only one operand of operator '*' may have a unit type, left operand is ") +
1274                    unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1275                    unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1276                $$ = new IntLiteral({ .value = 0 });
1277            } else if (lhs->exprType() != rhs->exprType()) {
1278                PARSE_ERR(@2, (String("Operands of operator '*' must have same type; left operand is ") +
1279                          typeStr(lhs->exprType()) + " and right operand is " + typeStr(rhs->exprType()) + " though.").c_str());
1280                $$ = new IntLiteral({ .value = 0 });
1281            } else {
1282                if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1283                    PARSE_WRN(@3, "Right operand of '*' operation is not 'final', result will be 'final' though since left operand is 'final'.");
1284                else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1285                    PARSE_WRN(@1, "Left operand of '*' operation is not 'final', result will be 'final' though since right operand is 'final'.");
1286              $$ = new Mul(lhs,rhs);              $$ = new Mul(lhs,rhs);
1287          }          }
1288      }      }
1289      | mul_expr '/' unary_expr  {      | mul_expr '/' unary_expr  {
1290          ExpressionRef lhs = $1;          ExpressionRef lhs = $1;
1291          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
1292          if (lhs->exprType() != INT_EXPR) {          if (!isNumber(lhs->exprType())) {
1293              PARSE_ERR(@1, (String("Left operand of operator '/' must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());              PARSE_ERR(@1, (String("Left operand of operator '/' must be a scalar number expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1294              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1295          } else if (rhs->exprType() != INT_EXPR) {          } else if (!isNumber(rhs->exprType())) {
1296              PARSE_ERR(@3, (String("Right operand of operator '/' must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());              PARSE_ERR(@1, (String("Right operand of operator '/' must be a scalar number expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1297              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1298          } else {          } else if (lhs->asNumber()->unitType() && rhs->asNumber()->unitType() &&
1299                       lhs->asNumber()->unitType() != rhs->asNumber()->unitType())
1300            {
1301                PARSE_ERR(@2, (String("Operands of operator '/' with two different unit types, left operand is ") +
1302                    unitTypeStr(lhs->asNumber()->unitType()) + " and right operand is " +
1303                    unitTypeStr(rhs->asNumber()->unitType()) + " though.").c_str());
1304                $$ = new IntLiteral({ .value = 0 });
1305            } else if (!lhs->asNumber()->unitType() && rhs->asNumber()->unitType()) {
1306                PARSE_ERR(@3, ("Dividing left operand without any unit type by right operand with unit type (" +
1307                    unitTypeStr(rhs->asNumber()->unitType()) + ") is not possible.").c_str());
1308                $$ = new IntLiteral({ .value = 0 });
1309            } else if (lhs->exprType() != rhs->exprType()) {
1310                PARSE_ERR(@2, (String("Operands of operator '/' must have same type; left operand is ") +
1311                          typeStr(lhs->exprType()) + " and right operand is " + typeStr(rhs->exprType()) + " though.").c_str());
1312                $$ = new IntLiteral({ .value = 0 });
1313            } else {
1314                if (lhs->asNumber()->isFinal() && !rhs->asNumber()->isFinal())
1315                    PARSE_WRN(@3, "Right operand of '/' operation is not 'final', result will be 'final' though since left operand is 'final'.");
1316                else if (!lhs->asNumber()->isFinal() && rhs->asNumber()->isFinal())
1317                    PARSE_WRN(@1, "Left operand of '/' operation is not 'final', result will be 'final' though since right operand is 'final'.");
1318              $$ = new Div(lhs,rhs);              $$ = new Div(lhs,rhs);
1319          }          }
1320      }      }
# Line 707  mul_expr: Line 1323  mul_expr:
1323          ExpressionRef rhs = $3;          ExpressionRef rhs = $3;
1324          if (lhs->exprType() != INT_EXPR) {          if (lhs->exprType() != INT_EXPR) {
1325              PARSE_ERR(@1, (String("Left operand of modulo operator must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());              PARSE_ERR(@1, (String("Left operand of modulo operator must be an integer expression, is ") + typeStr(lhs->exprType()) + " though.").c_str());
1326              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1327          } else if (rhs->exprType() != INT_EXPR) {          } else if (rhs->exprType() != INT_EXPR) {
1328              PARSE_ERR(@3, (String("Right operand of modulo operator must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());              PARSE_ERR(@3, (String("Right operand of modulo operator must be an integer expression, is ") + typeStr(rhs->exprType()) + " though.").c_str());
1329              $$ = new IntLiteral(0);              $$ = new IntLiteral({ .value = 0 });
1330          } else {          } else {
1331                if (lhs->asInt()->unitType() || lhs->asInt()->hasUnitFactorEver())
1332                    PARSE_ERR(@1, "Operands of modulo operator must not use any unit.");
1333                if (rhs->asInt()->unitType() || rhs->asInt()->hasUnitFactorEver())
1334                    PARSE_ERR(@3, "Operands of modulo operator must not use any unit.");
1335                if (lhs->asInt()->isFinal() && !rhs->asInt()->isFinal())
1336                    PARSE_WRN(@3, "Right operand of 'mod' operation is not 'final', result will be 'final' though since left operand is 'final'.");
1337                else if (!lhs->asInt()->isFinal() && rhs->asInt()->isFinal())
1338                    PARSE_WRN(@1, "Left operand of 'mod' operation is not 'final', result will be 'final' though since right operand is 'final'.");
1339              $$ = new Mod(lhs,rhs);              $$ = new Mod(lhs,rhs);
1340          }          }
1341      }      }
# Line 727  void InstrScript_warning(YYLTYPE* locp, Line 1351  void InstrScript_warning(YYLTYPE* locp,
1351      //fprintf(stderr, "WRN %d: %s\n", locp->first_line, txt);      //fprintf(stderr, "WRN %d: %s\n", locp->first_line, txt);
1352      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);
1353  }  }
1354    
1355    /// Custom implementation of yytnamerr() to ensure quotation is always stripped from token names before printing them to error messages.
1356    int InstrScript_tnamerr(char* yyres, const char* yystr) {
1357      if (*yystr == '"') {
1358          int yyn = 0;
1359          char const *yyp = yystr;
1360          for (;;)
1361            switch (*++yyp)
1362              {
1363    /*
1364              case '\'':
1365              case ',':
1366                goto do_not_strip_quotes;
1367    
1368              case '\\':
1369                if (*++yyp != '\\')
1370                  goto do_not_strip_quotes;
1371    */
1372                /* Fall through.  */
1373              default:
1374                if (yyres)
1375                  yyres[yyn] = *yyp;
1376                yyn++;
1377                break;
1378    
1379              case '"':
1380                if (yyres)
1381                  yyres[yyn] = '\0';
1382                return yyn;
1383              }
1384    /*
1385        do_not_strip_quotes: ;
1386    */
1387        }
1388    
1389      if (! yyres)
1390        return (int) yystrlen (yystr);
1391    
1392      return int( yystpcpy (yyres, yystr) - yyres );
1393    }

Legend:
Removed from v.2945  
changed lines
  Added in v.3592

  ViewVC Help
Powered by ViewVC