-
Notifications
You must be signed in to change notification settings - Fork 1
/
pcalc.y
348 lines (283 loc) · 9.52 KB
/
pcalc.y
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
%{
/* =====[ PCALC.C ]=========================================================
Description: Programmers calculator.
Revisions:
REV DATE BY DESCRIPTION
---- -------- ---------- --------------------------------------
0.00 xx.xx.94 Peter Glen Initial version.
0.00 08/28/98 Peter Glen Command line rework
0.00 08/28/98 Peter Glen Date added.
0.00 08/21/98 Peter Glen Unix port.
======================================================================= */
/* -------- System includes: -------------------------------------------- */
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <memory.h>
#include <signal.h>
#include <setjmp.h>
#include <unistd.h>
#include "hocdecl.h" /* data declarations, function prototypes */
#include "symbol.h"
#include "help.h"
#include "store.h"
#include "print.h"
int fNibble = 0;
int fSilent = 0;
extern FILE * yyin ;
%}
%union { /* stack object type */
int intval ; /* actual value */
long lngval ; /* actual value */
double val ; /* actual value */
Symbol *sym ; /* symbol table ptr */
}
%token <val> NUMBER
%token <sym> VAR BUILTIN IBUILTIN UNDEF STR
%token <sym> TO FROM STORE RESTORE STRVAR
%type <val> expr
%type <val> asgn
%type <sym> junk
%type <sym> str
%right '='
%left '|'
%left '&'
%left '<' '>'
%left '+' '-'
%left '*' '/' '%'
%left UNARYMINUS
%right '^' /* exponentiation */
%%
list:
| list
| list asgn
| list junk
| list expr {
print_num($2);
}
| list STORE VAR {
store("pcalc.var", $3->name, $3->u.val);
}
| list STORE VAR TO STR {
store($5->name, $3->name, $3->u.val);
}
| list RESTORE VAR {
restore("pcalc.var",
$3->name, &($3->u.val));
}
| list RESTORE VAR FROM STR {
restore($5->name,
$3->name, &($3->u.val));
}
| list error { yyerrok ; }
;
junk: IBUILTIN str { (*($1->u.iptr))($2->u.str) ; }
| IBUILTIN { }
| IBUILTIN VAR { (*($1->u.iptr))($2->u.val) ; }
| IBUILTIN expr { (*($1->u.iptr))($2) ; }
| STR { printf("%s", $1->name);}
| STRVAR { printf("%s", $1->u.str);}
;
asgn: VAR '=' expr { $$ = $1->u.val = $3 ; $1->type = VAR ; }
| STRVAR '=' STR { $1->u.str = $3->name; $1->type = STRVAR ;}
;
expr: NUMBER
| VAR { $$ = $1->u.val }
| BUILTIN '(' expr ')' { $$ = (*($1->u.ptr))($3) ; }
| BUILTIN expr { $$ = (*($1->u.ptr))($2) ; }
| '~' expr { $$ = ~((long)$2) ; }
| expr '|' expr { $$ = (long)$1 | (long)$3 ;}
| expr '&' expr { $$ = (long)$1 & (long)$3 ;}
| expr 'X' expr { $$ = (long)$1 ^ (long)$3 ;}
| expr '<' expr { $$ = (long)$1 << (long)$3 ; }
| expr '>' expr { $$ = (long)$1 >> (long)$3 ; }
| expr '+' expr { $$ = $1 + $3 ; }
| expr '-' expr { $$ = $1 - $3 ; }
| expr '*' expr { $$ = $1 * $3 ; }
| expr '/' expr {
if ($3 == 0.0)
execerror("division by zero", "") ;
$$ = $1 / $3 ;
}
| expr '%' expr { $$ = (long)$1 % (long)$3 ; }
| expr '^' expr { $$ = Pow( $1, $3) ; }
| '(' expr ')' { $$ = $2 ; }
| '-' expr %prec UNARYMINUS { $$ = -$2 ; }
;
str: STR { }
|STRVAR { }
;
%%
int parse_comline(int argc, char *argv[]);
char *progname ;
int lineno = 1;
jmp_buf begin ;
/*-------------------------------------------------------------------------
** MAIN (pcalc.y)
*/
int (*ptr_getchar)();
int (*ptr_ungetc)();
int main(int argc, char *argv[])
{
int args;
char template_local[] = "pcalc.tmp.XXXXXX",
template_global[] = "/tmp/pcalc.tmp.XXXXXX",
*template = NULL;
args = parse_comline(argc, argv);
progname = argv[0];
if(!argv[1])
{
printf ("\nProgrammer's calculator by Peter Glen. Version " VERSION "\n\n");
help_help();
exit(0);
}
if(*argv[1] == 'f' && argv[1][1] == '\0')
{
funchelp(); exit(0);
}
if(*argv[1] == 'l' && argv[1][1] == '\0')
{
cbhelp(); exit(0);
}
if(*argv[1] == 'o' && argv[1][1] == '\0')
{
ophelp(); exit(0);
}
if(*argv[1] == 'c'&& argv[1][1] == '\0')
{
conshelp(); exit(0);
}
if(*argv[1] == '@' )
{
// we got file:
yyin = fopen(&argv[1][1], "rt");
if(!yyin)
{
fprintf(stderr, "Cannot find file.\n");
exit(0);
}
}
else
{
// we got command line, write to a file, fake file:
int cnt;
int tmpfile;
template = template_local;
tmpfile = mkstemp(template);
if(tmpfile == -1)
{
template = template_global;
tmpfile = mkstemp(template);
if(tmpfile == -1)
{
fprintf(stderr, "cannot create tmp file\n"); exit(0);
}
}
/* XXX: hack! unlink here because if parsing fails, flex will
* exit and we won't be able to unlink the file below */
unlink(template);
yyin = fdopen(tmpfile, "r+");
for(cnt = args+1; cnt < argc; cnt++)
fprintf(yyin, "%s ", argv[cnt]);
fprintf(yyin, "\n");
//write(tmpfile, "\x1a", 1);
rewind(yyin);
}
init_sym() ;
setjmp(begin) ;
signal( SIGFPE, fpecatch) ; /* Turbo-C uses "ssignal()" */
yyparse() ;
if(yyin)
{
if (template)
unlink(template); /* unlink before we close to avoid race */
fclose(yyin); /* this closes tmpfile too */
}
return 0 ;
}
/*-------------------------------------------------------------------------
** EXECERROR
*/
void execerror( char *s, char *t)
{
warning( s, t) ;
longjmp( begin, 0) ;
}
/*-------------------------------------------------------------------------
** FPECATCH
*/
void fpecatch( int aa)
{
execerror("floating point exception", (char *) 0) ;
}
/*-------------------------------------------------------------------------
** YYERROR
*/
void yyerror( char *s) /* called by yacc syntax error */
{
warning( s, (char *) 0) ;
exit(0);
}
/*-------------------------------------------------------------------------
** WARNING
*/
void warning( char *s, char *t)
{
fprintf( stderr, "%s: %s", progname, s) ;
//fprintf( stderr, "pcalc WARNING: %s", s) ;
if (t)
fprintf( stderr, " %s", t) ;
fprintf( stderr, " near line %d\n", lineno) ;
}
/*-------------------------------------------------------------------------*/
int parse_comline(int argc, char *argv[])
{
int i, j = 0;
for(i=1; i < argc; ++i)
{
if (*argv[i] == '-')
{
switch(argv[i][1])
{
case 'h' :
printf (
"\nProgrammer's calculator by Peter Glen.\n\n"
"Usage: pcalc <stuff to calculate>\n"
" pcalc @script\n"
"\nOptions:\n"
" -n nibble mode (space out binary display)\n"
" -s,-S silent mode\n"
" -b,-B silent mode\n"
" -v version\n"
" -h help\n"
"\nFor more info, run pcalc without any options, or see the README.\n\n");
case 'n' : /* nibble mode */
fNibble = 1;
j++;
break;
case 'S' : /* quiet mode */
case 's' :
fSilent = 1;
j++;
break;
case 'B' :
case 'b' :
fSilent = 1;
j++;
break;
case 'v' :
puts("Programmer's calculator by Peter Glen. Version " VERSION);
exit(0);
break;
default:
// break on first non switch entry:
break;
}
}
}
return(j);
}
/* EOF */