-
Notifications
You must be signed in to change notification settings - Fork 1
/
repl.c
60 lines (58 loc) · 1.25 KB
/
repl.c
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
#include "p.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include <sys/stat.h>
#include "timer.h"
#include "k.h"
#include "x.h"
#include "repl.h"
void load(char *fn) {
FILE *fp=0; U x; pr *r; struct stat t; char *p;
if(-1==stat(fn,&t)) { fprintf(stderr,"%s: %s\n",fn,strerror(errno)); return; }
fp=fopen(fn,"r");
if(!fp) { fprintf(stderr,"%s: %s\n",fn,strerror(errno)); return; }
p=xmalloc(1+t.st_size);
if(t.st_size!=fread(p,1,t.st_size,fp)) {
fprintf(stderr,"%s: error reading file\n",fn);
xfree(p);
return;
}
p[t.st_size]=0;
fclose(fp);
r=pgparse(p);
x=pgreduce(r,1);
xfree(p);
prfree(r);
kfree(x);
}
void repl() {
int c,j; size_t i,m=2; pgs *s; char *b,*p; U x;
pr *r;
printf(" ");
for(i=0;;i=0) {
b=p=xmalloc(m+2);
while((c=fgetc(stdin))&&c!='\n') {
b[i++]=c;
if(i==m) { m<<=1; b=p=xrealloc(b,m+2); }
}
if(!i) { xfree(p); printf(" "); continue; }
b[i++]='\n'; b[i]=0;
r=pgparse(p);
if(r) {
if(TIMES) {
timer_start();
for(j=0;j<TIMES;j++) x=pgreduce(r,0);
printf("%.3f\n",timer_stop());
}
else {
x=pgreduce(r,1);
xfree(r);
kfree(x);
}
}
xfree(p);
printf(" ");
}
}