1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
/*
csound_sco.y:
Copyright (C) 2013
John ffitch
This file is part of Csound.
The Csound Library is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
Csound is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Csound; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
%{
#ifndef NULL
#define NULL 0L
#endif
#include "csoundCore.h"
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include "score_param.h"
extern void csound_scoerror(SCORE_PARM *, void *,
CSOUND *, ScoreTree *, const char*);
extern int csound_scolex(ScoreTree**, CSOUND *, void *);
#define LINE csound_scoget_lineno(scanner)
#define LOCN csound_scoget_locn(scanner)
extern int csound_scoget_locn(void *);
extern int csound_scoget_lineno(void *);
static ScoreTree* makesco(CSOUND *csound, int op, ListItem* car,
ScoreTree* cdr, int, int);
static ScoreTree* makesco1(CSOUND *csound, ScoreTree* car, ScoreTree* cdr);
static ListItem* makelist(CSOUND *csound, double car, ListItem* cdr);
%}
%pure_parser
%parse-param {SCORE_PARM *parm}
%parse-param {void *scanner}
%lex-param { CSOUND * csound }
%lex-param {yyscan_t *scanner}
%union {
int oper;
double val;
ScoreTree *t;
ListItem *l;
}
%type <oper> opcode
%type <val> arg exp term fac constant
%type <t> scoline scolines statement
%type <l> arglist
%token NEWLINE
%token T_ERROR
%token STRING_TOKEN
%token INTEGER_TOKEN
%token NUMBER_TOKEN
%start scofile
%left S_AND S_OR
%left '|'
%left '&'
%left S_LT S_GT S_LEQ S_GEQ S_EQ S_NEQ
%left S_BITSHIFT_LEFT S_BITSHIFT_RIGHT
%left '+' '-'
%left '*' '/' '%'
%left '^'
%left '#'
%right '~'
%right S_UNOT
%right S_UMINUS
%token T_HIGHEST
%pure-parser
%error-verbose
%token T_NP
%token T_PP
%token T_CNP
%token T_CPP
%pure_parser
/* %error-verbose */
%parse-param { CSOUND * csound }
%parse-param { ScoreTree * scoTree }
%%
scofile : scolines { printf("result %p\n", $1); *scoTree = *$1;}
;
scoline : statement
{
$$ = $1;
}
| '{' scolines '}' NEWLINE
{
$$ = $2;
}
;
scolines : scoline scolines
{
$$ = makesco1(csound, $1, $2);
}
| { $$ = NULL; }
;
statement : opcode arglist NEWLINE
{
printf("op=%c\n", $1);
$$ = makesco(csound, $1, NULL, NULL, LINE,LOCN);
}
;
opcode : 'i' { $$ = 'i'; }
| 'f' { $$ = 'f'; }
| 'a' { $$ = 'a'; }
| 'e' { $$ = 'e'; }
| 's' { $$ = 's'; }
| 't' { $$ = 't'; }
;
arglist : arg arglist { $$ = makelist(csound,
$1, $2);}
| { $$ = NULL; }
;
arg : NUMBER_TOKEN { $$ = parm->fval;}
| INTEGER_TOKEN { $$ = (MYFLT)parm->ival;}
| STRING_TOKEN {}
| '[' exp ']' { $$ = $2; }
| T_NP { $$ = nan("1"); }
| T_PP { $$ = nan("2"); }
| T_CNP { $$ = nan("3"); }
| T_CPP { $$ = nan("4"); }
;
exp : exp '+' exp { $$ = $1 + $3; }
| exp '+' error
| exp '-' exp { $$ = $1 - $3; }
| exp '-' error
| '-' exp %prec S_UMINUS { $$ = - $2; }
| '-' error { }
| '+' exp %prec S_UMINUS { $$ = $2; }
| '+' error { }
| term { $$ = $1; }
;
term : exp '*' exp { $$ = $1 * $3; }
| exp '*' error
| exp '/' exp { $$ = $1 / $3; }
| exp '/' error
| exp '^' exp { $$ = pow($1, $3); }
| exp '^' error
| exp '%' exp { $$ = (double)((int)$1 % (int)$3); }
| exp '%' error
| fac { $$ = $1; }
;
fac : constant { $$ = $1; }
| exp '|' exp { $$ = (int)$1 | (int)$3; }
| exp '|' error
| exp '&' exp { $$ = (int)$1 & (int)$3; }
| exp '&' error
| exp '#' exp { $$ = (int)$1 ^ (int)$3; }
| exp '#' error
| exp S_BITSHIFT_LEFT exp
{ $$ = (int)$1 << (int)$3; }
| exp S_BITSHIFT_LEFT error
| exp S_BITSHIFT_RIGHT exp
{ $$ = (int)$1 >> (int)$3; }
| exp S_BITSHIFT_RIGHT error
| '~' exp %prec S_UMINUS
{ $$ = ~(int)$2; }
| '~' error { $$ = 0; }
| '(' exp ')' { $$ = $2; }
| '(' exp error { $$ = 0; }
| '(' error { $$ = 0; }
;
constant : NUMBER_TOKEN { $$ = parm->fval; }
| INTEGER_TOKEN { $$ = (MYFLT)parm->ival; }
;
%%
#ifdef SOME_FINE_DAY
void
yyerror(char *s, ...)
{
va_list ap;
va_start(ap, s);
if(yylloc.first_line)
fprintf(stderr, "%d.%d-%d.%d: error: ",
yylloc.first_line, yylloc.first_column,
yylloc.last_line, yylloc.last_column);
vfprintf(stderr, s, ap);
fprintf(stderr, "\n");
}
void
lyyerror(YYLTYPE t, char *s, ...)
{
va_list ap;
va_start(ap, s);
if(t.first_line)
fprintf(stderr, "%d.%d-%d.%d: error: ", t.first_line, t.first_column,
t.last_line, t.last_column);
vfprintf(stderr, s, ap);
fprintf(stderr, "\n");
}
#endif
extern void do_baktrace(CSOUND *csound, uint64_t files);
extern char *csound_scoget_text ( void *scanner );
extern char *csound_scoget_current_pointer(void *yyscanner);
void
csound_scoerror(SCORE_PARM *parm, void *yyscanner, CSOUND *cs, ScoreTree *t,
const char* str)
{
char ch;
char *p = csound_scoget_current_pointer(yyscanner)-1;
int line = csound_scoget_lineno(yyscanner);
uint64_t files = csound_scoget_locn(yyscanner);
if (*p=='\0') line--;
cs->Message(cs, Str("\nerror: %s (token \"%s\")"),
str, csound_scoget_text(yyscanner));
do_baktrace(cs, files);
cs->Message(cs, Str(" line %d:\n>>>"), line);
while ((ch=*--p) != '\n' && ch != '\0');
do {
ch = *++p;
if (ch == '\n') break;
cs->Message(cs, "%c", ch);
} while (ch != '\n' && ch != '\0');
cs->Message(cs, " <<<\n");
}
int csound_scowrap()
{
#ifdef DEBUG
printf("\n === END OF INPUT ===\n");
#endif
return (1);
}
static ScoreTree* makesco(CSOUND *csound, int op, ListItem* car, ScoreTree* cdr,
int line, int locn)
{
ScoreTree* a = (ScoreTree*)csound->Malloc(csound, sizeof(ScoreTree));
a->op = op;
a->args = car;
a->next = cdr;
a->line = line;
a->locn = locn;
return a;
}
static ScoreTree* makesco1(CSOUND *csound, ScoreTree* car, ScoreTree* cdr)
{
car->next = cdr;
return car;
}
static ListItem* makelist(CSOUND *csound, double car, ListItem* cdr)
{
ListItem* a = (ListItem*)csound->Malloc(csound, sizeof(ListItem));
a->val = car;
a->args = cdr;
return a;
}
#if 0
int yylex(void)
{
int c;
while ((c=getchar())==' ');
if (isdigit(c)) {
int n = c-'0';
while (isdigit(c=getchar())) {
n = 10*n+c-'0';
}
ungetc(c, stdin);
return NUMBER_TOKEN;
}
return c=='\n' ? NEWLINE : c;
}
extern int csound_scodebug;
int main(void)
{
csound_scodebug = 1;
yyparse();
return 0;
}
#endif
|