/
parser.c
526 lines (460 loc) · 13.2 KB
/
parser.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Jonathan(Jon) DuBois 2015. This file is part of LNZ. //
////////////////////////////////////////////////////////////////////////////////
#include "lnz.h"
#include "pep.h"
int isWhitespace( u8 c ){
if( !c || isspace( c ) )
return 1;
return 0;
}
int isReserved( u8 c ){
if( strchr( "[].\\/=;", (int)c ) )
return 1;
return 0;
}
int isName( u8 c ){
if( !isReserved( c ) && !isWhitespace( c ) )
return 1;
return 0;
}
int isNumber( u8 c ){
if( c >= '0' && c <= '9' )
return 1;
else
return 0;
}
// This returns the parsed expression as a node, or NULL. If NULL is returned, error is made to point at an error message. All named indices less than or equal to global
// are considered global; all other indices should be bound lambdas.
u32 parseExpression( LNZprogram* p, const u8* string, u64 length, const char** error ){
*error = NULL;
const u8* s = string;
u64 l = length;
// Eat up whitespace in front and back.
while( l && isWhitespace( *s ) ){
--l;
++s;
}
while( l && isWhitespace( s[ l - 1 ] ) )
--l;
if( !l ){
*error = "Syntax error: null expression.";
return s - string;
}
// Handle lambdas
if( *s == '\\' ){
++s;
--l;
while( l && isWhitespace( *s ) ){
--l;
++s;
}
if( !l || !isName( *s ) ){
*error = "Syntax error: malformed lambda, expected an identifier.";
return s - string;
}
// Eat identifiers, add them to the nametables, and build up an empty lambda at
// top.
u64 lambdaCount = 0;
u32 cur, top;
do{
u64 namelen = 0;
while( namelen < l && isName( s[ namelen ] ) )
++namelen;
u32 newnode = mallocNode( p );
p->heap[ newnode ].type = LNZ_LAMBDA_TYPE;
p->heap[ newnode ].references = 1;
if( lambdaCount )
p->heap[ cur ].data = newnode;
else
top = newnode;
++lambdaCount;
cur = newnode;
addNamePointerPair( p, s, namelen, newnode );
// Eat trailing namespace
while( namelen < l && isWhitespace( s[ namelen ] ) )
++namelen;
if( namelen == l || ( s[ namelen ] != '.' && !isName( s[ namelen ] ) ) ){
*error = "Syntax error: malformed lambda, expected '.' or an identifier.";
return ( s - string ) + namelen;
}
s += namelen;
l -= namelen;
}while( *s != '.' );
// Parse the body.
u32 pe = parseExpression( p, s + 1, l - 1, error );
if( *error != NULL )
return ( s - string ) + 1 + pe;
p->heap[ cur ].data = pe;
// Pop scope.
while( lambdaCount ){
--lambdaCount;
popNamePointerPair( p );
}
return top;
}
// Handle applications,free variables, numbers and strings.
u64 applicationCount = 0;
u32 top;
// Read one expression at a time; must be parenthetical or a name.
do{
u32 arg;
u64 namelen = 0;
// Handle strings.
if( s[ namelen ] == '\'' ){
++namelen;
u32 nullstring = mallocNode( p );
p->heap[ nullstring ].type = LNZ_DATA_TYPE;
p->heap[ nullstring ].references = 0; // size.
p->heap[ nullstring ].data = 0;
arg = mallocNode( p );
p->heap[ arg ].type = LNZ_STRING_TYPE;
p->heap[ arg ].references = 1; // size.
p->heap[ arg ].data = ( (u64)nullstring ) << 32;
p->heap[ arg ].data += ( (u64)nullstring );
u64 bsc = 0;
while( namelen < l ){
if( s[ namelen ] == '\'' ){
while( bsc >= 2 ){
bsc -= 2;
addStringChar( p, arg, '\\' );
}
if( bsc ){
addStringChar( p, arg, '\'' );
bsc = 0;
}else
break;
}else if( s[ namelen ] == '\\' ){
++bsc;
}else{
while( bsc >= 2 ){
bsc -= 2;
addStringChar( p, arg, '\\' );
}
bsc = 0;
addStringChar( p, arg, s[ namelen ] );
}
++namelen;
}
if( namelen == l || s[ namelen ] != '\'' ){
*error = "Syntax error: malformed string.";
return ( s - string ) + namelen;
}
++namelen;
// Handle parenthetical subexpressions.
}else if( *s == '[' ){
namelen = 1;
u64 f = 1;
// scan for parentheses.
u64 bsc = 0;
u64 qm = 0;
while( namelen < l ){
if( s[ namelen ] == '\\' )
++bsc;
else if( s[ namelen ] == '\'' ){
if( !( bsc % 2 ) && qm )
qm = 0;
else if( !( bsc % 2 ) && !qm )
qm = 1;
bsc = 0;
}else{
if( !qm && s[ namelen ] == '[' )
++f;
if( !qm && s[ namelen ] == ']' )
--f;
bsc = 0;
}
++namelen;
if( !f )
break;
}
if( f || namelen < 2 ){
*error = "Syntax error: unbalanced parentheses.";
return s - string;
}
arg = parseExpression( p, s + 1, namelen - 2, error );
if( *error != NULL )
return ( s - string ) + 1 + arg;
}else{
if( !l || !isName( *s ) ){
*error = "Syntax error: malformed expression.";
return s - string;
}
// Handle identifiers and integers.
while( namelen < l && isName( s[ namelen ] ) )
++namelen;
u64 isint = 1;
u64 isminus = 0;
u64 i = 0;
if( s[ i ] == '-' ){
++i;
isminus = 1;
if( namelen == 1 )
isint = 0;
}
for( ; i < namelen; ++i )
if( !isNumber( s[ i ] ) )
isint = 0;
if( isint ){
arg = mallocNode( p );
u32 dn = mallocNode( p );
p->heap[ arg ].type = LNZ_INT_TYPE + isminus;
p->heap[ arg ].references = 1;
p->heap[ arg ].data = ( ( (u64)dn ) << 32 ) + ( (u64)dn );
p->heap[ dn ].type = LNZ_DATA_TYPE;
p->heap[ dn ].references = 1;
p->heap[ dn ].data = 0;
i = 0;
if( s[ i ] == '-' )
++i;
while( i < namelen && isNumber( s[ i ] ) ){
multiplyNumberByInt( p, arg, 10 );
addIntToNumber( p, arg, s[ i ] - '0' );
++i;
}
}else{
u64 nind = getIndex( p->names, s, namelen );
if( !nind ){
*error = "Unknown identifier.";
return s - string;
}
u32 pntr = getPointerFromName( p, s, namelen );
// if global or not a lambda, treat as an identifier, otherwise a free variable.
if( nind <= p->global || p->heap[ pntr ].type != LNZ_LAMBDA_TYPE )
arg = *( ( u32* )( getName( p->pointers, nind, NULL ) ) );
else{
arg = mallocNode( p );
p->heap[ arg ].type = LNZ_FREE_TYPE;
p->heap[ arg ].references = 1;
p->heap[ arg ].data = *( ( u32* )( getName( p->pointers, nind, NULL ) ) );
}
}
}
u32 newnode;
if( applicationCount == 1 )
newnode = top;
else{
newnode = mallocNode( p );
p->heap[ newnode ].data = 0;
}
p->heap[ newnode ].type = LNZ_APPLICATION_TYPE;
p->heap[ newnode ].references = 1;
p->heap[ newnode ].data += ( (u64)arg ) << 32;
if( applicationCount > 1 ){
p->heap[ newnode ].data += top;
} else if( !applicationCount )
p->heap[ newnode ].data >>= 32;
top = newnode;
++applicationCount;
// Eat up trailing whitespace.
while( namelen < l && isWhitespace( s[ namelen ] ) )
++namelen;
s += namelen;
l -= namelen;
}while( l );
if( applicationCount == 1 ){
u32 ans = p->heap[ top ].data;
freeNode( p, top );
return ans;
}else{
return top;
}
}
u64 parseLine( LNZprogram* p, const u8* string, u64 length, const char **error ){
const u8* s = string;
u64 l = length;
// Eat up white space and parse an lvalue.
while( l && isWhitespace( *s ) ){
++s;
--l;
}
u64 namelen = 0;
while( namelen < l && isName( s[ namelen ] ) )
++namelen;
if( !namelen || namelen >= l ){
*error = "Syntax error: malformed equation";
return s - string;
}
const u8* name = s;
s += namelen;
l -= namelen;
// Eat up white space and parse an =.
while( l && isWhitespace( *s ) ){
++s;
--l;
}
if( !l || *s != '=' ){
*error = "Syntax error: malformed equation, expected an =.";
return s - string;
}
++s;
--l;
// scan for semicolon.
u64 exprlen = 0;
int qm = 0;
u64 bsc = 0;
while( exprlen < l ){
if( s[ exprlen ] == '\\' )
++bsc;
else if( s[ exprlen ] == '\'' ){
if( !( bsc % 2 ) && qm )
qm = 0;
else if( !( bsc % 2 ) && !qm )
qm = 1;
bsc = 0;
}else{
bsc = 0;
if( !qm && s[ exprlen ] == ';' )
break;
}
++exprlen;
}
if( !exprlen || exprlen == l ){
*error = "Syntax error: malformed equation, expected an expression and a ;.";
return s - string;
}
u32 expr = parseExpression( p, s, exprlen, error );
if( *error != NULL )
return ( s - string ) + expr;
if( getIndex( p->names, name, namelen ) ){
*error = "Redefinition of an identifier.";
return name - string;
}
addNamePointerPair( p, name, namelen, expr );
++p->global;
s += exprlen + 1;
l -= exprlen + 1;
while( l && isWhitespace( *s ) ){
++s;
--l;
}
return s - string;
}
LNZprogram* parseProgram( const char* filename, const u8* string, u64 length, const char** error ){
LNZprogram* ans = newProgram();
const u8* s = string;
u64 l = length;
do{
u64 prsd = parseLine( ans, s, l, error );
s += prsd;
l -= prsd;
if( *error != NULL ){
u64 line = 1;
u64 chr = 1;
const u8* c = string;
while( c != s ){
if( *c == '\n' ){
chr = 1;
++line;
}else
++chr;
++c;
}
u64 pep = rand() % ( sizeof( pepTalks ) / sizeof( pepTalks[ 0 ] ) );
char* err = LNZmalloc( strlen( filename ) + strlen( *error ) +
strlen( pepTalks[ pep ] ) + 256 );
sprintf( err, "%s:%u:%u\n%s\n%s\n\n", filename, (int)line, (int)chr,
*error, pepTalks[ pep ] );
*error = err;
deleteProgram( ans );
return NULL;
}
}while( l );
return ans;
}
// Returns 0 or the index of the name of the expression.
u32 indexHelper( const LNZprogram* p, u32 expr, const LNZprogram* names ){
u32 ind = 0;
if( names == NULL )
return getIndex( p->pointers, (const u8*)( &expr ), sizeof( u32 ) );
else{
for( int i = names->pointers->size - 1; i >= 0; --i )
if( nodesEqual( p, expr, names, *( (const u32*)( names->pointers->revdict[ i ] ) ) ) ){
ind = i + 1;
break;
}
}
return ind;
}
void printExpression( const LNZprogram* p, u32 expression, u32 level,
const LNZprogram* names ){
u64 ind = indexHelper( p, expression, names );
if( ind && level ){
u64 len;
const u8* name;
if( names != NULL )
name = getName( names->names, ind, &len );
else
name = getName( p->names, ind, &len );
for( u64 i = 0; i < len; ++i )
putchar( (int)name[ i ] );
}else{
if( p->heap[ expression ].type == LNZ_LAMBDA_TYPE ){
printf( "\\" );
ind = 0;
while( p->heap[ expression ].type == LNZ_LAMBDA_TYPE && !ind ){
printf( "l%u", expression );
expression = p->heap[ expression ].data;
++level;
ind = getIndex( p->pointers, (const u8*)( &expression ), sizeof( u32 ) );
if( p->heap[ expression ].type == LNZ_LAMBDA_TYPE && !ind )
printf( " " );
else{
printf( "." );
printExpression( p, expression, level + 1, names );
}
}
}else if( p->heap[ expression ].type == LNZ_APPLICATION_TYPE ){
u32 low = p->heap[ expression ].data & (u32)( -1 );
u32 high = p->heap[ expression ].data >> 32;
ind = indexHelper( p, high, names );
u64 lind = indexHelper( p, low, names );
if( p->heap[ low ].type == LNZ_LAMBDA_TYPE && !lind ){
printf( "[" );
printExpression( p, low, level + 1, names );
printf( "]" );
}else
printExpression( p, low, level + 1, names );
if( ( p->heap[ high ].type == LNZ_APPLICATION_TYPE ||
p->heap[ high ].type == LNZ_LAMBDA_TYPE ) && !ind ){
printf( " [" );
printExpression( p, high, level + 1, names );
printf( "]" );
}else{
printf( " " );
printExpression( p, high, level + 1, names );
}
}else if( p->heap[ expression ].type == LNZ_STRING_TYPE ){
u32 len = p->heap[ p->heap[ expression ].data >> 32 ].references;
u32 w = p->heap[ expression ].data & (u32)-1;
u64 shft = 0;
putchar( '\'' );
while( len ){
int pc = (int)( ( p->heap[ w ].data & ( ( (u64)255 ) << shft ) ) >> shft );
if( pc == '\'' || pc == '\\' )
putchar( '\\' );
putchar( pc );
shft += 8;
if( shft >= 64 ){
shft = 0;
w = p->heap[ w ].references;
}
--len;
}
putchar( '\'' );
}else if( p->heap[ expression ].type == LNZ_INT_TYPE ||
p->heap[ expression ].type == LNZ_NEGATIVE_INT_TYPE ){
u32 back = p->heap[ expression ].data >> 32;
u32 len = p->heap[ back ].references;
if( p->heap[ expression ].type == LNZ_NEGATIVE_INT_TYPE &&
( len > 1 || p->heap[ back ].data ) )
putchar( '-' );
u64 numlen = 0;
char* num = numberToString( p, expression, &numlen );
for( u64 i = 0; i < numlen; ++i )
putchar( num[ i ] );
LNZfree( num );
}else if( p->heap[ expression ].type == LNZ_FREE_TYPE )
printf( "l%u", (u32)p->heap[ expression ].data );
}
}