-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathes.y
167 lines (155 loc) · 3.19 KB
/
es.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
%{
#include "es.h"
#define YYSTYPE Sexp*
%}
%token ATOM
%token DOT
%token LPAREN
%token RPAREN
%%
list: { prompt(lineno); }
| list '\n'
| list sexp '\n' { prints(eval($2, global_env)); prompt(lineno); }
;
sexp: ATOM
| LPAREN sexp DOT sexp RPAREN { $$ = mk_oblist($2, $4); }
;
%%
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <setjmp.h>
jmp_buf begin;
char *progname;
main(char argc, char *argv[])
{
lineno = 0;
progname = argv[0];
symbol_table = (Symbols*)emalloc(sizeof(Symbols));
symbol_table->sym = NULL;
symbol_table->next = NULL;
initenv();
setjmp(begin);
yyparse();
}
yylex()
{
char c;
if (needLparen > 0){
inParen++; needLparen--; needRparen++;
*processingCdr++ = waitingCdr;
waitingAtom = 1; waitingCdr = 0;
return LPAREN;
}
while (isspace((c=getchar()))){
if (c == '\n') lineno++;
if (inParen)
;
else
if (c == '\n') return c;
}
if (c == EOF)
return 0;
if (c == '('){
if (waitingAtom){
inParen++;
*processingCdr++ = waitingCdr;
waitingCdr = 0; waitingLNull = 1;
return LPAREN;
}
ungetc(c, stdin);
needLparen++;
waitingAtom = 1; waitingCdr = 1;
return DOT;
}
if (c == ')'){
if (waitingLNull){
ungetc(c, stdin);
waitingAtom = 0; waitingCdr = 1; waitingLNull = 0;
yylval = mk_nullcar();
return ATOM;
}
if (waitingCdr){
ungetc(c, stdin);
if (waitingAtom){
waitingAtom = 0; waitingCdr = 0;
yylval = (Sexp*) 0;
return ATOM;
}
waitingAtom = 1; waitingCdr = 1;
return DOT;
}
if (needRparen > 0 && *(processingCdr-1)){
ungetc(c, stdin);
inParen--; needRparen--;
waitingAtom = 0; waitingCdr = !*--processingCdr;
return RPAREN;
}
inParen--;
waitingAtom = 0; waitingCdr = !*--processingCdr;
return RPAREN;
}
if (c == '.'){
char cnext;
cnext = getchar();
if (isspace(cnext)){
waitingAtom = 1; waitingCdr = 1;
return DOT;
}
if (cnext == '('){
ungetc(cnext, stdin);
waitingAtom = 1; waitingCdr = 1;
return DOT;
}
ungetc(cnext, stdin);
}
if (isdigit(c) || c == '.' || c == '-'){
if (waitingAtom){
char sbuf[100];
double* numaddr;
getstring(c, sbuf);
waitingAtom = 0; waitingCdr = !waitingCdr; waitingLNull = 0;
if ((numaddr = atof_withtest(sbuf)) != NULL){
yylval = mk_num(*numaddr);
return ATOM;
}
yylval = mk_symbol(sbuf);
return ATOM;
}
ungetc(c, stdin);
needLparen++;
waitingAtom = 1; waitingCdr = 1;
return DOT;
}
if (!isspace(c)){
if (waitingAtom){
char sbuf[100];
getstring(c, sbuf);
waitingAtom = 0; waitingCdr = !waitingCdr; waitingLNull = 0;
yylval = mk_symbol(sbuf);
return ATOM;
}
ungetc(c, stdin);
needLparen++;
waitingAtom = 1; waitingCdr = 1;
return DOT;
}
return c;
}
yyerror(char* s)
{
warning(s, (char*) 0);
longjmp(begin, 0);
}
readerror(char* s, char* t)
{
warning(s, t);
longjmp(begin, 0);
}
warning(char *s, char* t)
{
fprintf(stderr, "%s; %s", progname, s);
if (t)
fprintf(stderr, " %s", t);
fprintf(stderr, " at es%d\n", lineno-1);
}