aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/grap/grap.y
blob: 6d96b2f18e51d075edfc856ed2e3f82458a660a5 (plain)
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
%{
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "grap.h"

//#define	RAND_MAX 32767	/* if your rand() returns bigger, change this too */

extern int yylex(void);
extern int yyparse(void);

%}

%token	<i>	FRAME TICKS GRID LABEL COORD
%token	<i>	LINE ARROW CIRCLE DRAW NEW PLOT NEXT
%token	<p>	PIC
%token	<i>	COPY THRU UNTIL
%token	<i>	FOR FROM TO BY AT WITH
%token	<i>	IF
%token	<p>	GRAPH THEN ELSE DOSTR
%token	<i>	DOT DASH INVIS SOLID
%token	<i>	TEXT JUST SIZE
%token	<i>	LOG EXP SIN COS ATAN2 SQRT RAND MAX MIN INT PRINT SPRINTF
%token	<i>	X Y SIDE IN OUT OFF UP DOWN ACROSS
%token	<i>	HEIGHT WIDTH RADIUS
%token	<f>	NUMBER
%token	<op>	NAME VARNAME DEFNAME
%token	<p>	STRING
%token	<i>	ST '(' ')' ','

%right	<f>	'='
%left	<f>	OR
%left	<f>	AND
%nonassoc <f>	GT LT LE GE EQ NE
%left	<f>	'+' '-'
%left	<f>	'*' '/' '%'
%right	<f>	UMINUS NOT
%right	<f>	'^'

%type	<f>	expr optexpr if_expr number assign
%type	<i>	optop
%type	<p>	optstring if
%type	<op>	optname iterator name
%type	<pt>	point
%type	<i>	side optside numlist comma linetype drawtype
%type	<ap>	linedesc optdesc stringlist string stringattr sattrlist exprlist
%type	<i>	frameitem framelist coordlog
%type	<f>	string_expr

%%

top:
	  graphseq		{ if (codegen && !synerr) graph((char *) 0); }
	| /* empty */		{ codegen = 0; }
	| error			{ codegen = 0; ERROR "syntax error" WARNING; }
	;

graphseq:
	  statlist
	| graph statlist
	| graphseq graph statlist
	;
graph:
	  GRAPH			{ graph($1); endstat(); }
	;

statlist:
	  ST
	| stat ST		{ endstat(); }
	| statlist stat ST	{ endstat(); }
	;

stat:
	  FRAME framelist	{ codegen = 1; }
	| ticks			{ codegen = 1; }
	| grid			{ codegen = 1; }
	| label			{ codegen = 1; }
	| coord
	| plot			{ codegen = 1; }
	| line			{ codegen = 1; }
	| circle		{ codegen = 1; }
	| draw
	| next			{ codegen = 1; }
	| PIC			{ codegen = 1; pic($1); }
	| for
	| if
	| copy
	| numlist		{ codegen = 1; numlist(); }
	| assign
	| PRINT expr		{ fprintf(stderr, "\t%g\n", $2); }
	| PRINT string		{ fprintf(stderr, "\t%s\n", $2->sval); freeattr($2); }
	| /* empty */
	;

numlist:
	  number		{ savenum(0, $1); $$ = 1; }
	| numlist number	{ savenum($1, $2); $$ = $1+1; }
	| numlist comma number	{ savenum($1, $3); $$ = $1+1; }
	;
number:
	  NUMBER
	| '-' NUMBER %prec UMINUS	{ $$ = -$2; }
	| '+' NUMBER %prec UMINUS	{ $$ = $2; }
	;

label:
	  LABEL optside stringlist lablist	{ label($2, $3); }
	;
lablist:
	  labattr
	| lablist labattr
	| /* empty */
	;
labattr:
	  UP expr		{ labelmove($1, $2); }
	| DOWN expr		{ labelmove($1, $2); }
	| SIDE expr		{ labelmove($1, $2); /* LEFT or RIGHT only */ }
	| WIDTH expr		{ labelwid($2); }
	;

framelist:
	  framelist frameitem
	| /* empty */		{ $$ = 0; }
	;
frameitem:
	  HEIGHT expr		{ frameht($2); }
	| WIDTH expr		{ framewid($2); }
	| side linedesc		{ frameside($1, $2); }
	| linedesc		{ frameside(0, $1); }
	;
side:
	  SIDE
	;
optside:
	  side
	| /* empty */		{ $$ = 0; }
	;

linedesc:
	  linetype optexpr	{ $$ = makeattr($1, $2, (char *) 0, 0, 0); }
	;
linetype:
	  DOT | DASH | SOLID | INVIS
	;
optdesc:
	  linedesc
	| /* empty */		{ $$ = makeattr(0, 0.0, (char *) 0, 0, 0); }
	;

ticks:
	  TICKS tickdesc	{ ticks(); }
	;
tickdesc:
	  tickattr
	| tickdesc tickattr
	;
tickattr:
	  side			{ tickside($1); }
	| IN expr		{ tickdir(IN, $2, 1); }
	| OUT expr		{ tickdir(OUT, $2, 1); }
	| IN			{ tickdir(IN, 0.0, 0); }
	| OUT			{ tickdir(OUT, 0.0, 0); }
	| AT optname ticklist	{ setlist(); ticklist($2, AT); }
	| iterator		{ setlist(); ticklist($1, AT); }
	| side OFF		{ tickoff($1); }
	| OFF			{ tickoff(LEFT|RIGHT|TOP|BOT); }
	| labattr
	;
ticklist:
	  tickpoint
	| ticklist comma tickpoint
	;
tickpoint:
	  expr			{ savetick($1, (char *) 0); }
	| expr string		{ savetick($1, $2->sval); }
	;
iterator:
	  FROM optname expr TO optname expr BY optop expr optstring
			{ iterator($3, $6, $8, $9, $10); $$ = $2; }
	| FROM optname expr TO optname expr optstring
			{ iterator($3, $6, '+', 1.0, $7); $$ = $2; }
	;
optop:
	  '+'		{ $$ = '+'; }
	| '-'		{ $$ = '-'; }
	| '*'		{ $$ = '*'; }
	| '/'		{ $$ = '/'; }
	| /* empty */	{ $$ = ' '; }
	;
optstring:
	  string	{ $$ = $1->sval; }
	| /* empty */	{ $$ = (char *) 0; }
	;

grid:
	  GRID griddesc		{ ticks(); }
	;
griddesc:
	  gridattr
	| griddesc gridattr
	;
gridattr:
	  side			{ tickside($1); }
	| X			{ tickside(BOT); }
	| Y			{ tickside(LEFT); }
	| linedesc		{ griddesc($1); }
	| AT optname ticklist	{ setlist(); gridlist($2); }
	| iterator		{ setlist(); gridlist($1); }
	| TICKS OFF		{ gridtickoff(); }
	| OFF			{ gridtickoff(); }
	| labattr
	;

line:
	  LINE FROM point TO point optdesc	{ line($1, $3, $5, $6); }
	| LINE optdesc FROM point TO point	{ line($1, $4, $6, $2); }
	;
circle:
	  CIRCLE RADIUS expr AT point		{ circle($3, $5); }
	| CIRCLE AT point RADIUS expr		{ circle($5, $3); }
	| CIRCLE AT point			{ circle(0.0, $3); }
	;

stringlist:
	  string
	| stringlist string	{ $$ = addattr($1, $2); }
	;
string:
	  STRING sattrlist	{ $$ = makesattr($1); }
	| SPRINTF '(' STRING ')' sattrlist
				{ $$ = makesattr(sprntf($3, (Attr*) 0)); }
	| SPRINTF '(' STRING ',' exprlist ')' sattrlist
				{ $$ = makesattr(sprntf($3, $5)); }
	;
exprlist:
	  expr			{ $$ = makefattr(NUMBER, $1); }
	| exprlist ',' expr	{ $$ = addattr($1, makefattr(NUMBER, $3)); }
	;
sattrlist:
	  stringattr
	| sattrlist stringattr
	| /* empty */		{ $$ = (Attr *) 0; }
	;
stringattr:
	  JUST			{ setjust($1); }
	| SIZE optop expr	{ setsize($2, $3); }
	;

coord:
	  COORD optname coordlist	{ coord($2); }
	| COORD optname			{ resetcoord($2); }
	;
coordlist:
	  coorditem
	| coordlist coorditem
	;
coorditem:
	  coordlog	{ coordlog($1); }
	| X point	{ coord_x($2); }
	| Y point	{ coord_y($2); }
	| X optname expr TO expr		{ coord_x(makepoint($2, $3, $5)); }
	| Y optname expr TO expr		{ coord_y(makepoint($2, $3, $5)); }
	| X FROM optname expr TO expr		{ coord_x(makepoint($3, $4, $6)); }
	| Y FROM optname expr TO expr		{ coord_y(makepoint($3, $4, $6)); }
	;
coordlog:
	  LOG X		{ $$ = XFLAG; }
	| LOG Y		{ $$ = YFLAG; }
	| LOG X LOG Y	{ $$ = XFLAG|YFLAG; }
	| LOG Y LOG X	{ $$ = XFLAG|YFLAG; }
	| LOG LOG	{ $$ = XFLAG|YFLAG; }
	;

plot:
	  stringlist AT point		{ plot($1, $3); }
	| PLOT stringlist AT point	{ plot($2, $4); }
	| PLOT expr optstring AT point	{ plotnum($2, $3, $5); }
	;

draw:
	  drawtype optname linedesc		{ drawdesc($1, $2, $3, (char *) 0); }
	| drawtype optname optdesc string	{ drawdesc($1, $2, $3, $4->sval); }
	| drawtype optname string optdesc	{ drawdesc($1, $2, $4, $3->sval); }
	;
drawtype:
	  DRAW
	| NEW
	;

next:
	  NEXT optname AT point optdesc		{ next($2, $4, $5); }

copy:
	  COPY copylist		{ copy(); }
	;
copylist:
	  copyattr
	| copylist copyattr
	;
copyattr:
	  string		{ copyfile($1->sval); }
	| THRU DEFNAME		{ copydef($2); }
	| UNTIL string		{ copyuntil($2->sval); }
	;

for:
	  FOR name FROM expr TO expr BY optop expr DOSTR
		{ forloop($2, $4, $6, $8, $9, $10); }
	| FOR name FROM expr TO expr DOSTR
		{ forloop($2, $4, $6, '+', 1.0, $7); }
	| FOR name '=' expr TO expr BY optop expr DOSTR
		{ forloop($2, $4, $6, $8, $9, $10); }
	| FOR name '=' expr TO expr DOSTR
		{ forloop($2, $4, $6, '+', 1.0, $7); }
	;

if:
	  IF if_expr THEN ELSE		{ $$ = ifstat($2, $3, $4); }
	| IF if_expr THEN		{ $$ = ifstat($2, $3, (char *) 0); }
	;
if_expr:
	  expr
	| string_expr
	| if_expr AND string_expr	{ $$ = $1 && $3; }
	| if_expr OR string_expr	{ $$ = $1 || $3; }
	;
string_expr:
	  STRING EQ STRING	{ $$ = strcmp($1,$3) == 0; free($1); free($3); }
	| STRING NE STRING	{ $$ = strcmp($1,$3) != 0; free($1); free($3); }
	;

point:
	  optname expr comma expr		{ $$ = makepoint($1, $2, $4); }
	| optname '(' expr comma expr ')'	{ $$ = makepoint($1, $3, $5); }
	;
comma:
	  ','		{ $$ = ','; }
	;

optname:
	  NAME		{ $$ = $1; }
	| /* empty */	{ $$ = lookup(curr_coord, 1); }
	;

expr:
	  NUMBER
	| assign
	| '(' string_expr ')'	{ $$ = $2; }
	| VARNAME		{ $$ = getvar($1); }
	| expr '+' expr		{ $$ = $1 + $3; }
	| expr '-' expr		{ $$ = $1 - $3; }
	| expr '*' expr		{ $$ = $1 * $3; }
	| expr '/' expr		{ if ($3 == 0.0) {
					ERROR "division by 0" WARNING; $3 = 1; }
				  $$ = $1 / $3; }
	| expr '%' expr		{ if ((long)$3 == 0) {
					ERROR "mod division by 0" WARNING; $3 = 1; }
				  $$ = (long)$1 % (long)$3; }
	| '-' expr %prec UMINUS	{ $$ = -$2; }
	| '+' expr %prec UMINUS	{ $$ = $2; }
	| '(' expr ')'		{ $$ = $2; }
	| LOG '(' expr ')'		{ $$ = Log10($3); }
	| EXP '(' expr ')'		{ $$ = Exp($3 * log(10.0)); }
	| expr '^' expr			{ $$ = pow($1, $3); }
	| SIN '(' expr ')'		{ $$ = sin($3); }
	| COS '(' expr ')'		{ $$ = cos($3); }
	| ATAN2 '(' expr ',' expr ')'	{ $$ = atan2($3, $5); }
	| SQRT '(' expr ')'		{ $$ = Sqrt($3); }
	| RAND '(' ')'			{ $$ = (double)rand() / (double)RAND_MAX; }
	| MAX '(' expr ',' expr ')'	{ $$ = $3 >= $5 ? $3 : $5; }
	| MIN '(' expr ',' expr ')'	{ $$ = $3 <= $5 ? $3 : $5; }
	| INT '(' expr ')'	{ $$ = (long) $3; }
	| expr GT expr		{ $$ = $1 > $3; }
	| expr LT expr		{ $$ = $1 < $3; }
	| expr LE expr		{ $$ = $1 <= $3; }
	| expr GE expr		{ $$ = $1 >= $3; }
	| expr EQ expr		{ $$ = $1 == $3; }
	| expr NE expr		{ $$ = $1 != $3; }
	| expr AND expr		{ $$ = $1 && $3; }
	| expr OR expr		{ $$ = $1 || $3; }
	| NOT expr		{ $$ = !($2); }
	;
assign:
	  name '=' expr		{ $$ = setvar($1, $3); }
	;

name:
	  NAME
	| VARNAME
	;

optexpr:
	  expr
	| /* empty */		{ $$ = 0.0; }
	;