forked from dllaurence/Nil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.llm
338 lines (270 loc) · 10.4 KB
/
parse.llm
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
;**********************************************************************
; parse.llm
;
; Implementation of the parser.
;
; Copyright 2009-2010 by Dustin Laurence. Distributed under the terms of
; the LGPLv3.
;
;**********************************************************************
#include "parse.llh"
#include "lex.llh"
#include "exp.llh"
#include "system.llh"
;**********************************************************************
; ReadParseExp
;
; The front end of the nil tokenizer. Constructs the parse expressions
; required by the parser from the tokens identified by ReadLexeme().
;
; Note that this function has lots of no-op bitcasting. The reason is
; that at least notionally the lexer owns the Token type and the parser
; owns the ParseExp type, and no other module using ParseExp's should
; need to know about Tokens. That will matter if either ever changes
; its representation.
;
;**********************************************************************
define NILCC %ParseExp
@ReadParseExp()
{
%token = call NILCC %Token @ReadLexeme()
%tokenType = extractvalue %Token %token, 0
switch %c_int %tokenType, label %CantHappen [
%c_int NIL_EXCEPTION, label %PassThrough
%c_int ASCII_NEWLINE, label %PassThrough
%c_int ASCII_SQUOTE, label %PassThrough
%c_int ASCII_LPAREN, label %PassThrough
%c_int ASCII_RPAREN, label %PassThrough
%c_int NIL_SYMBOL, label %ReturnComposite
]
CantHappen:
cant_happen()
br label %CantHappen
PassThrough:
; This is a painful way to write a no-op!
%valueWord = extractvalue %Token %token, 1
%valueExp = bitcast %Word %valueWord to %Exp
%sameType = insertvalue %ParseExp undef, %c_int %tokenType, 0
%parseExp = insertvalue %ParseExp %sameType, %Exp %valueExp, 1
ret %ParseExp %parseExp
ReturnComposite:
%lexemeWord = extractvalue %Token %token, 1
%lexeme = inttoptr %Word %lexemeWord to %c_char*
#ifndef NDEBUG
; Check to see that the string is at least one character long
; (since the lexer shouldn't ever produce an empty string)
%len = call ccc %c_size_t @strlen(%c_char* %lexeme)
%positive = icmp ugt %c_size_t %len, 0
assert(%positive)
; Could assert that it's aligned as well.
#endif
switch %c_int %tokenType, label %CompositeCantHappen [
%c_int NIL_SYMBOL, label %ReturnSymbol
]
CompositeCantHappen:
cant_happen()
br label %CompositeCantHappen
ReturnSymbol:
; Get the corresponding nil_symbol
%symbol = call NILCC %Exp @NewSymbol(%c_char* %lexeme)
%symbolExp = insertvalue %ParseExp {%c_int NIL_SYMBOL, %Exp undef},
%Exp %symbol, 1
ret %ParseExp %symbolExp
}
;**********************************************************************
; GetParseExp
;
; A buffer to allow the parser a token (%ParseExp) of lookahead using
; UngetParseExp. This greatly simplifies the interaction loop as well.
;
;**********************************************************************
; One token (expression) lookahead for now.
#define EXP_BUFFER_SIZE 1
@expBuffer = internal global [EXP_BUFFER_SIZE x %ParseExp] undef
@expBufferCount = internal global %Word 0
define NILCC %ParseExp
@GetParseExp()
{
%numBuffered = load %Word* @expBufferCount
#ifndef NDEBUG
%nonNegative = icmp sge %Word %numBuffered, 0
assert(%nonNegative)
#endif
%bufferEmpty = icmp eq %Word %numBuffered, 0
br i1 %bufferEmpty, label %BufferEmpty, label %BufferNotEmpty
BufferEmpty:
%newParseExp = call NILCC %ParseExp @ReadParseExp()
ret %ParseExp %newParseExp
BufferNotEmpty:
%index = sub %Word %numBuffered, 1
store %Word %index, %Word* @expBufferCount
%expPtr = getelementptr [EXP_BUFFER_SIZE x %ParseExp]* @expBuffer,
i64 0, %Word %index
%oldParseExp = load %ParseExp* %expPtr
ret %ParseExp %oldParseExp
}
;**********************************************************************
; UngetParseExp
;
;**********************************************************************
define NILCC void
@UngetParseExp(%ParseExp %parseExp)
{
%numBuffered = load %Word* @expBufferCount
#ifndef NDEBUG
%canAdd = icmp slt %Word %numBuffered, EXP_BUFFER_SIZE
assert(%canAdd)
#endif
%expPtr = getelementptr [EXP_BUFFER_SIZE x %ParseExp]* @expBuffer,
i64 0, %Word %numBuffered
store %ParseExp %parseExp, %ParseExp* %expPtr
%newNumBuffered = add %Word %numBuffered, 1
store %Word %newNumBuffered, %Word* @expBufferCount
ret void
}
;**********************************************************************
; Parser
;
; The beginnings of a parser module.
;
;**********************************************************************
#define CONTPROMPTARRAY_LEN 6
@contPromptArray = internal constant [CONTPROMPTARRAY_LEN x %c_char] c"---> \00"
;**********************************************************************
; ReadExp (public)
;
; The Nil parser, implemented as a mutually recursive pair of functions.
;
; Warning: here there be tygers. Recursive tygers.
;
;**********************************************************************
@quoteStrPtr = external constant %c_char*
define NILCC %ParseExp
@ReadExp()
{
br label %GetParseExp
GetParseExp:
%parseExp = call NILCC %ParseExp @GetParseExp()
%parseExpType = extractvalue %ParseExp %parseExp, 0
switch %c_int %parseExpType, label %CantHappen [
%c_int ASCII_NEWLINE, label %Newline
%c_int NIL_CELL, label %CantHappen ; Noted explicitly
%c_int NIL_SYMBOL, label %ReturnParseExp
%c_int ASCII_SQUOTE, label %QuoteNext
%c_int ASCII_LPAREN, label %BeginList
%c_int ASCII_RPAREN, label %EndList
%c_int NIL_EXCEPTION, label %ReturnParseExp
]
Newline:
%promptStr = getelementptr [CONTPROMPTARRAY_LEN x %c_char]* @contPromptArray,
i64 0, i64 0
call ccc %c_int @putstring(%c_char* %promptStr)
br label %GetParseExp
CantHappen:
cant_happen()
br label %CantHappen
ReturnParseExp:
%retParseExp = phi %ParseExp [%parseExp, %GetParseExp],
[%parseExp, %GetParseExp],
[%listParseExp, %BeginList],
[%nextParseExp, %QuoteNext]
ret %ParseExp %retParseExp
QuoteNext:
; FIXME: consider turning this into a helper function if/when we
; have another special parser character with similar behavior
; Drop single quote
%nextParseExp = call NILCC %ParseExp @ReadExp()
%nextParseExpType = extractvalue %ParseExp %nextParseExp, 0
%gotQuoteException = icmp eq %c_int %nextParseExpType, NIL_EXCEPTION
br i1 %gotQuoteException, label %ReturnParseExp, label %GotNextExp
GotNextExp:
%nextExp = extractvalue %ParseExp %nextParseExp, 1
%cdr = call NILCC %Exp @Cons(%Exp %nextExp, %Exp NIL_VALUE)
%quoteStr = load %c_char** @quoteStrPtr
%quoteSymbol = call NILCC %Exp @NewSymbol(%c_char* %quoteStr)
%quoteExp = call NILCC %Exp @Cons(%Exp %quoteSymbol, %Exp %cdr)
%quoteParseExp = insertvalue %ParseExp {%c_int NIL_CELL, %Exp undef},
%Exp %quoteExp, 1
ret %ParseExp %quoteParseExp
EndList:
%lParenParseExp = insertvalue %ParseExp {%c_int NIL_EXCEPTION, %Exp undef},
%Exp NIL_EXTRA_RPAREN, 1
ret %ParseExp %lParenParseExp
BeginList:
; Drop left paren
%listParseExp = call NILCC %ParseExp @ReadList()
%listParseExpType = extractvalue %ParseExp %listParseExp, 0
%gotException = icmp eq %c_int %listParseExpType, NIL_EXCEPTION
br i1 %gotException, label %ReturnParseExp, label %GotList
GotList:
%endParseExp = call NILCC %ParseExp @GetParseExp()
%endParseExpType = extractvalue %ParseExp %endParseExp, 0
%gotEndParen = icmp eq %c_int %endParseExpType, ASCII_RPAREN
assert(%gotEndParen)
ret %ParseExp %listParseExp
}
;**********************************************************************
; ReadList (private)
;
;**********************************************************************
define NILCC %ParseExp
@ReadList()
{
br label %GetParseExp
GetParseExp:
%parseExp = call NILCC %ParseExp @GetParseExp()
%parseExpType = extractvalue %ParseExp %parseExp, 0
switch %c_int %parseExpType, label %GetCAR [
%c_int ASCII_NEWLINE, label %Newline
%c_int ASCII_RPAREN, label %EndList
]
Newline:
%promptStr = getelementptr [CONTPROMPTARRAY_LEN x %c_char]* @contPromptArray,
i64 0, i64 0
call ccc %c_int @putstring(%c_char* %promptStr)
br label %GetParseExp
EndList:
call NILCC void @UngetParseExp(%ParseExp %parseExp)
ret %ParseExp {%c_int NIL_SYMBOL, %Exp NIL_VALUE}
GetCAR:
call NILCC void @UngetParseExp(%ParseExp %parseExp)
%carParseExp = call NILCC %ParseExp @ReadExp()
%carParseExpType = extractvalue %ParseExp %carParseExp, 0
%gotCARException = icmp eq %c_int %carParseExpType, NIL_EXCEPTION
br i1 %gotCARException, label %ReturnParseExp, label %GetCDR
ReturnParseExp:
%retParseExp = phi %ParseExp [%carParseExp, %GetCAR],
[%cdrParseExp, %GetCDR]
ret %ParseExp %retParseExp
GetCDR:
%cdrParseExp = call NILCC %ParseExp @ReadList()
%cdrParseExpType = extractvalue %ParseExp %cdrParseExp, 0
%gotCDRException = icmp eq %c_int %cdrParseExpType, NIL_EXCEPTION
br i1 %gotCDRException, label %ReturnParseExp, label %ReturnCons
ReturnCons:
%car = extractvalue %ParseExp %carParseExp, 1
%cdr = extractvalue %ParseExp %cdrParseExp, 1
%cell = call NILCC %Exp @Cons(%Exp %car, %Exp %cdr)
%cellParseExp = insertvalue %ParseExp {%c_int NIL_CELL, %Exp undef},
%Exp %cell, 1
ret %ParseExp %cellParseExp
}
;**********************************************************************
; Read
;
;**********************************************************************
define NILCC %ParseExp
@Read()
{
%firstParseExp = call NILCC %ParseExp @GetParseExp()
%firstParseExpType = extractvalue %ParseExp %firstParseExp, 0
switch %c_int %firstParseExpType, label %Other [
%c_int ASCII_NEWLINE, label %Newline
]
Other:
call NILCC void @UngetParseExp(%ParseExp %firstParseExp)
%parseExp = call NILCC %ParseExp @ReadExp()
ret %ParseExp %parseExp
Newline:
ret %ParseExp %firstParseExp
}