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