-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
341 lines (302 loc) · 6.64 KB
/
parser.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
%{
/*
Copyright 2008, 2009 Sam Chapin
This file is part of Gospel.
Gospel is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License
as published by the Free Software Foundation.
Gospel 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with Gospel. If not, see <http://www.gnu.org/licenses/>.
*/
#include "core.h"
#include "objects.h"
#include <stdio.h>
vector nreverse(vector);
vector listToVector(vector);
obj appendSymbols(vector);
obj keywordMessage(obj target, pair args);
void yyerror(YYLTYPE *, int *, int *, void **, void *, char const *);
#define fail(...) do { \
printf(__VA_ARGS__); \
fflush(stdout); \
*parserOutput = oNull; \
YYABORT; \
} while (0)
%}
%token INTEGER
%token SYMBOL
%token STRING
%token NAME
%token OPERATOR
%token KEYWORD
%token KEYWORDS
%token ADDSLOT
%token SETSLOT
%token ENDOFFILE
%glr-parser
%locations
%pure-parser
%lex-param {int *nesting}
%lex-param {void *scanner}
%parse-param {int *lineNumber}
%parse-param {int *nesting}
%parse-param {void **parserOutput}
%parse-param {void *scanner}
//%debug
%initial-action {
//yydebug = 1;
@$.first_line = @$.last_line = *lineNumber;
}
%%
input:
gap ENDOFFILE
{ *parserOutput = oEndOfFile; YYACCEPT; }
| gap
{ *parserOutput = oNull; *lineNumber = @1.last_line; }
| gap line gap
{ *parserOutput = $2; *lineNumber = @3.last_line; }
| error
{ *parserOutput = eSyntaxError; *lineNumber = @1.last_line; }
;
line:
statements
{ $$ = expressionSequence(listToVector(nreverse($1))); }
;
statements:
statement
{ $$ = list($1); }
| statements '.' statement
{ $$ = cons($3, $1); }
;
declaration:
unaryTarget signature '{' body '}'
{ pair sig = $2;
$$ = message($1,
sAddMethod_as_,
newVector(2,
car(sig),
blockLiteral(listToVector(cdr(sig)),
listToVector($4)))); }
;
statement:
cascade
| carets
{ $$ = message(oDefaultMessageTarget, sReturn_atDepth_, newVector(2, oNull, integer((int)$1))); }
| carets cascade
{ $$ = message(oDefaultMessageTarget, sReturn_atDepth_, newVector(2, $2, integer((int)$1))); }
;
carets:
'^'
{ $$ = (void *)1; }
| carets '^'
{ $$ = $1 + 1; }
;
signature:
NAME
{ $$ = list($1); }
| OPERATOR NAME
{ $$ = cons($1, list($2)); }
| keywordsignature
{ pair keywords = nreverse($1);
$$ = cons(appendSymbols(map(car, keywords)), map(cdr, keywords)); }
;
keywordsignature:
KEYWORD gap NAME
{ $$ = list(cons($1, $3)); }
| keywordsignature KEYWORD gap NAME
{ $$ = cons(cons($2, $4), $1); }
;
cascade:
expr
| cascade ';' gap message
{ $$ = call(quote($4), sCascading_, newVector(1, quote($1))); }
;
message:
unaryMessage
| binaryMessage
| keywordMessage
| declaration
| assignment
;
parens:
'(' body ')'
{ $$ = expressionSequence(listToVector($2)); }
;
expr:
parens
| literal
| unaryMessage
| binaryMessage
| keywordMessage
| assignment
| declaration
;
assignment:
unaryTarget param ADDSLOT gap expr
{ $$ = message($1, sAddSlot_as_, newVector(2, $2, $5)); }
| unaryTarget param SETSLOT gap expr
{ $$ = message($1, sSetSlot_to_, newVector(2, $2, $5)); }
;
gap:
| gap '\n'
;
unaryMessage:
unaryTarget NAME
{ $$ = message($1, $2, emptyVector); }
;
unaryTarget:
{ $$ = oDefaultMessageTarget; }
| parens
| literal
| unaryMessage
| declaration
;
untargettedBinaryMessage:
OPERATOR gap binaryArgument
{ $$ = message(oDefaultMessageTarget, $1, newVector(1, $3)); }
;
binaryMessage:
binaryTarget OPERATOR gap binaryArgument
{ $$ = message($1, $2, newVector(1, $4)); }
;
binaryTarget:
unaryTarget
| binaryMessage
;
binaryArgument:
parens
| literal
| unaryMessage
| untargettedBinaryMessage
| untargettedKeywordMessage
;
untargettedKeywordMessage:
keywords
{ $$ = keywordMessage(oDefaultMessageTarget, nreverse($1)); }
;
keywordMessage:
binaryTarget keywords
{ $$ = keywordMessage($1, nreverse($2)); }
;
keywords:
completeKeywords
| cutOffKeywords
;
completeKeywords:
KEYWORD gap keywordArgument
{ $$ = list(cons($1, $3)); }
| completeKeywords KEYWORD gap keywordArgument
{ $$ = cons(cons($2, $4), $1); }
;
cutOffKeywords:
keywordsPrecedingCutoff KEYWORD gap untargettedKeywordMessage
{ $$ = cons(cons($2, $4), $1); }
;
keywordsPrecedingCutoff:
{ $$ = emptyList; }
| completeKeywords
;
keywordArgument:
parens
| literal
| unaryMessage
| binaryMessage
;
literal:
INTEGER
| SYMBOL
| STRING
| '[' gap ']'
{ $$ = vectorObject(emptyVector); }
| '[' gap list gap ']'
{ $$ = message(oInternals, sVectorLiteral, listToVector(nreverse($3))); }
| '{' body '}'
{ $$ = blockLiteral(emptyVector, listToVector($2)); }
| '{' params '|' body '}'
{ $$ = blockLiteral(listToVector(nreverse($2)), listToVector($4)); }
;
list:
cascade
{ $$ = list($1); }
| list gap ',' gap cascade
{ $$ = cons($5, $1); }
;
params:
{ $$ = emptyList; }
| params param
{ $$ = cons($2, $1); }
;
param:
NAME
| OPERATOR
| KEYWORD
| KEYWORDS
;
body:
gap
{ $$ = list(oNull); }
| gap lines gap
{ $$ = nreverse($2); }
;
lines:
line
{ $$ = list($1); }
| lines '\n' line
{ $$ = cons($3, $1); }
;
%%
pair nreverse(pair l) {
pair next, last = emptyList;
while (l) {
next = cdr(l);
setcdr(l, last);
last = l;
l = next;
}
return last;
}
int length(pair l) {
int i = 0;
while (l) {
l = cdr(l);
i++;
}
return i;
}
vector listToVector(vector list) {
vector v = makeVector(length(list));
for (int i = 0; list; i++, list = cdr(list)) setIdx(v, i, car(list));
return v;
}
pair map(void *fn, pair list) {
return list ? cons(((void *(*)(void *))fn)(car(list)), map(fn, cdr(list))) : list;
}
obj appendSymbols(pair symbols) {
obj s = string("");
for (; symbols; symbols = cdr(symbols))
s = appendStrings(s, car(symbols));
setProto(s, oSymbol);
return intern(s);
}
obj keywordMessage(obj target, pair args) {
return message(target,
appendSymbols(map(car, args)),
listToVector(map(cdr, args)));
}
void yyerror (YYLTYPE *location,
int *line,
int *nesting,
void **parserOutput,
void *scanner,
char const *msg) {
/*
if (location->first_line == (*line = location->last_line))
printf("\nSyntax error at line %d.\n", location->last_line);
else
printf("\nSyntax error between lines %d and %d.\n", location->first_line, location->last_line);
*/
}