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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
|
/* -*- mode: objc; coding: utf-8 -*- */
/* Toilet Lisp, a Common Lisp subset for the Étoilé runtime.
* Copyright (C) 2008 Matthias Andreas Benkard.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _XOPEN_SOURCE 600
#define _GNU_SOURCE // for RTLD_DEFAULT
#define _ISOC99_SOURCE
#import "MLKInterpretedClosure.h"
#import "MLKCons.h"
#import "MLKDynamicContext.h"
#import "MLKEnvironment.h"
#import "MLKForeignProcedure.h"
#import "MLKFuncallable.h"
#import "MLKInterpreter.h"
#import "MLKLexicalContext.h"
#import "MLKLexicalEnvironment.h"
#import "MLKLLVMCompiler.h"
#import "MLKPackage.h"
#import "MLKReader.h"
#import "MLKRoot.h"
#import "MLKSymbol.h"
#import "NSObject-MLKPrinting.h"
#import "globals.h"
#import "runtime-compatibility.h"
#import "special-symbols.h"
#import "util.h"
#import <Foundation/NSArray.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSException.h>
#import <Foundation/NSNull.h>
#import <Foundation/NSString.h>
#import <Foundation/NSValue.h>
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#else
#ifndef _BSD_SOURCE
#define _BSD_SOURCE
#endif
#undef _POSIX_C_SOURCE // needed at least on Mac OS X for RTLD_DEFAULT to be defined
#include <dlfcn.h>
#endif
#define RETURN_VALUE(thing) \
{ return [NSArray arrayWithObject:nullify(thing)]; }
static id PRIMARY (NSArray *array) __attribute__ ((pure));
static id
PRIMARY (NSArray *array)
{
if ([array count] > 0)
return [array objectAtIndex:0];
else
return nil;
}
@implementation MLKInterpreter
+(void) initialize
{
ensure_symbols ();
}
+(id) compile:(id)object inContext:(MLKLexicalContext *)context
{
return PRIMARY ([self eval:object
inLexicalContext:context
withEnvironment:[MLKLexicalEnvironment globalEnvironment]]);
}
+(NSArray*) eval:(id)program
inLexicalContext:(MLKLexicalContext *)context
withEnvironment:(MLKLexicalEnvironment *)lexenv
{
id form = [MLKForm formWithObject:program
inContext:context
forCompiler:self];
return [form interpretWithEnvironment:lexenv];
}
+(BOOL) load:(MLKStream *)stream verbose:(BOOL)verbose print:(BOOL)print
{
id eofValue = [[NSObject alloc] init];
int level = MLKIntWithInteger ([[MLKDynamicContext currentContext]
valueForSymbol:[sys intern:@"*LOAD-LEVEL*"]]);
int i;
while (YES)
{
id result;
id expansion;
//NSLog (@"; LOAD: Reding a form.");
id code;
//NSLog (@"; LOAD: Reading finished.");
NSString *formdesc;
NSAutoreleasePool *pool;
//NSLog (@"%@", MLKPrintToString(code));
//NSLog (@"%@", stream);
//NSLog (@"...");
pool = [[NSAutoreleasePool alloc] init];
code = [MLKReader readFromStream:stream
eofError:NO
eofValue:eofValue
recursive:NO
preserveWhitespace:NO];
if (code == eofValue)
break;
if (MLKInstanceP(code)
&& [code isKindOfClass:[MLKCons class]] && [code cdr])
formdesc = [NSString stringWithFormat:@"(%@ %@ ...)",
MLKPrintToString([code car]),
MLKPrintToString([[code cdr] car])];
else
formdesc = MLKPrintToString(code);
//fprintf (stderr, "; COMPILE-MINIMALLY: %s\n", [formdesc UTF8String]);
fprintf (stderr, "; ");
for (i = 0; i < level; i++)
fprintf (stderr, "| ");
fprintf (stderr, "LOAD: %s\n", [formdesc UTF8String]);
if (MLKLoadCompilesP)
{
expansion = code;
result = [MLKDefaultCompiler eval:code];
}
else
{
expansion = code;
result = [MLKInterpreter
eval:expansion
inLexicalContext:[MLKLexicalContext globalContext]
withEnvironment:[MLKLexicalEnvironment globalEnvironment]];
}
LRELEASE (pool);
if (print)
{
//FIXME
//NSLog (@"; LOAD: Fnord. Primary value: %@",
// MLKPrintToString([result objectAtIndex:0]));
}
}
return YES;
}
@end
@implementation MLKForm (MLKInterpretation)
-(NSArray *) interpret
{
return [self interpretWithEnvironment:[MLKLexicalEnvironment globalEnvironment]];
}
-(NSArray *) interpretWithEnvironment:(MLKLexicalEnvironment *)env
{
NSArray *values;
#if 0
BOOL trace = NO;
//if ([dynamicContext valueForSymbol:V_INITP])
// trace = YES;
//if (trace)
NSLog (@"; EVAL END: %@", MLKPrintToString(_form));
values = [self reallyInterpretWithEnvironment:env];
//if (trace)
NSLog (@"; EVAL: %@", MLKPrintToString(_form));
#elif 0
NS_DURING
{
values = [self reallyInterpretWithEnvironment:env];
}
NS_HANDLER
{
NSLog (@"; BROKEN EVAL: %@", MLKPrintToString(_form));
[localException raise];
}
NS_ENDHANDLER;
#else
values = [self reallyInterpretWithEnvironment:env];
#endif
return values;
}
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
NSLog (@"WARNING: Unrecognised form type: %@", self);
return [NSArray array];
}
@end
@implementation MLKSelfEvaluatingForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
RETURN_VALUE (_form);
}
@end
@implementation MLKSymbolForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
if ([_context variableIsLexical:_form])
{
// NSLog (@"Lexical?");
RETURN_VALUE ([env valueForSymbol:_form]);
}
else
{
// NSLog (@"Special?");
RETURN_VALUE ([[MLKDynamicContext currentContext] valueForSymbol:_form]);
}
}
@end
@implementation MLKCatchForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
id catchTag;
NSArray *values;
MLKDynamicContext *newctx;
catchTag = PRIMARY ([_tagForm interpretWithEnvironment:env]);
NS_DURING
{
newctx = [[MLKDynamicContext alloc] initWithParent:[MLKDynamicContext currentContext]
variables:nil
handlers:nil
restarts:nil
catchTags:[NSSet setWithObject:catchTag]
activeHandlerEnvironment:nil];
[newctx pushContext];
values = [self interpretBodyWithEnvironment:env];
NS_VALUERETURN (values, NSArray *);
}
NS_HANDLER
{
[MLKDynamicContext popContext];
LRELEASE (newctx);
if ([[localException name] isEqualToString:@"MLKThrow"])
{
id thrownTag = [[localException userInfo]
objectForKey:@"THROWN TAG"];
if (thrownTag == catchTag)
return [[localException userInfo]
objectForKey:@"THROWN OBJECTS"];
else
[localException raise];
}
else
[localException raise];
}
NS_ENDHANDLER;
[MLKDynamicContext popContext];
LRELEASE (newctx);
return nil;
}
@end
@implementation MLKForeignLambdaForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
// FIXME: Support library designators.
int (*function)();
#ifdef _WIN32
// FIXME
//EnumProcessModules (...);
//GetProcAddress (..., [_foreignName UTF8String]);
#else
function = dlsym (RTLD_DEFAULT, [_foreignName UTF8String]);
#endif
NSMutableArray *argtypes = [NSMutableArray array];
int i;
for (i = 0; i < _argc; i++)
[argtypes addObject:[NSNumber numberWithInt:_argumentTypes[i]]];
RETURN_VALUE (LAUTORELEASE ([[MLKForeignProcedure alloc]
initWithCode:function
argumentTypes:argtypes
returnType:_returnType]));
}
@end
@implementation MLKLambdaFunctionForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
return [_lambdaForm interpretWithEnvironment:env];
}
@end
@implementation MLKSimpleFunctionForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
RETURN_VALUE ([env functionForSymbol:_functionName]);
}
@end
@implementation MLKIfForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
id cndval = denullify(PRIMARY([_conditionForm interpretWithEnvironment:env]));
if (cndval)
return [_consequentForm interpretWithEnvironment:env];
else
return [_alternativeForm interpretWithEnvironment:env];
}
@end
@implementation MLKInPackageForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
id package = [MLKPackage findPackage:stringify(_packageDesignator)];
[[MLKDynamicContext currentContext]
setValue:package
forSymbol:[[MLKPackage findPackage:@"COMMON-LISP"]
intern:@"*PACKAGE*"]];
RETURN_VALUE (package);
}
@end
@implementation MLKSimpleLambdaForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
id closure = LAUTORELEASE ([[MLKInterpretedClosure alloc]
initWithForm:self
environment:env]);
RETURN_VALUE (closure);
}
@end
@implementation MLKSimpleFletForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
int i;
MLKLexicalEnvironment *newenv = [MLKLexicalEnvironment environmentWithParent:env
variables:nil
functions:nil];
for (i = 0; i < [_functionBindingForms count]; i++)
{
[[_functionBindingForms objectAtIndex:i] interpretWithEnvironment:newenv];
}
return [self interpretBodyWithEnvironment:newenv];
}
@end
@implementation MLKSimpleFunctionBindingForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
id value = [_compiler compile:[MLKCons cons:_LAMBDA with:_tail]
inContext:_context];
[env addFunction:value forSymbol:_name];
return nil;
}
@end
@implementation MLKLetForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
int i;
NSArray *values;
MLKLexicalEnvironment *newenv;
MLKDynamicContext *dynctx;
newenv = [MLKLexicalEnvironment environmentWithParent:env
variables:nil
functions:nil];
dynctx = [[MLKDynamicContext alloc] initWithParent:[MLKDynamicContext currentContext]
variables:nil
handlers:nil
restarts:nil
catchTags:nil
activeHandlerEnvironment:nil];
for (i = 0; i < [_variableBindingForms count]; i++)
{
id variable = [[_variableBindingForms objectAtIndex:i] name];
id value = PRIMARY([[_variableBindingForms objectAtIndex:i]
interpretWithEnvironment:env]);
if ([_bodyContext variableIsLexical:variable])
{
[newenv addValue:value forSymbol:variable];
}
else
{
[dynctx addValue:value forSymbol:variable];
}
}
[dynctx pushContext];
NS_DURING
{
values = [self interpretBodyWithEnvironment:newenv];
}
NS_HANDLER
{
[MLKDynamicContext popContext];
LRELEASE (dynctx);
[localException raise];
}
NS_ENDHANDLER;
[MLKDynamicContext popContext];
LRELEASE (dynctx);
return values;
}
@end
@implementation MLKVariableBindingForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
return [_valueForm interpretWithEnvironment:env];
}
@end
@implementation MLKSimpleLoopForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
while (YES)
{
[self interpretBodyWithEnvironment:env];
}
RETURN_VALUE (nil); // never reached
}
@end
@implementation MLKMultipleValueCallForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
NSMutableArray *results = [NSMutableArray array];
int i;
id <MLKFuncallable> function = PRIMARY ([_functionForm interpretWithEnvironment:env]);
for (i = 0; i < [_bodyForms count]; i++)
{
NSArray *values = [[_bodyForms objectAtIndex:i] interpretWithEnvironment:env];
[results addObjectsFromArray:values];
}
return [function applyToArray:results];
}
@end
@implementation MLKProgNForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
return [self interpretBodyWithEnvironment:env];
}
@end
@implementation MLKBodyForm (MLKInterpretation)
-(NSArray *) interpretBodyWithEnvironment:(MLKLexicalEnvironment *)env
{
int i;
NSArray *values = [NSArray array];
for (i = 0; i < [_bodyForms count]; i++)
{
values = [[_bodyForms objectAtIndex:i] interpretWithEnvironment:env];
}
return values;
}
@end
@implementation MLKProgVForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
id variables = PRIMARY([_variableListForm interpretWithEnvironment:env]);
id values = PRIMARY([_valueListForm interpretWithEnvironment:env]);
MLKDynamicContext *dynctx;
id result;
dynctx = [[MLKDynamicContext alloc]
initWithParent:[MLKDynamicContext currentContext]
variables:nil
handlers:nil
restarts:nil
catchTags:nil
activeHandlerEnvironment:nil];
for (; variables; (variables = [variables cdr], values = [values cdr]))
{
id var = [variables car];
id value = [values car];
[dynctx addValue:value forSymbol:var];
}
[dynctx pushContext];
NS_DURING
{
result = [self interpretBodyWithEnvironment:env];
}
NS_HANDLER
{
[MLKDynamicContext popContext];
LRELEASE (dynctx);
[localException raise];
}
NS_ENDHANDLER;
[MLKDynamicContext popContext];
LRELEASE (dynctx);
return result;
}
@end
@implementation MLKQuoteForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
RETURN_VALUE (_quotedData);
}
@end
@implementation MLKThrowForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
id catchTag;
NSArray *values;
NSDictionary *userInfo;
catchTag = PRIMARY([_tagForm interpretWithEnvironment:env]);
values = [_valueForm interpretWithEnvironment:env];
userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
catchTag, @"THROWN TAG",
values, @"THROWN OBJECTS", nil];
if ([[MLKDynamicContext currentContext] catchTagIsEstablished:denullify (catchTag)])
[[NSException exceptionWithName:@"MLKThrow"
reason:[NSString stringWithFormat:
@"THROW: tag %@, values %@.",
MLKPrintToString(catchTag),
MLKPrintToString(values)]
userInfo:userInfo] raise];
else
// FIXME: This should really be a condition rather than
// an exception. See CLHS THROW.
[[NSException exceptionWithName:@"MLKControlError"
reason:[NSString stringWithFormat:
@"THROW without a corresponding CATCH: tag %@, values %@.",
MLKPrintToString(catchTag),
MLKPrintToString(values)]
userInfo:userInfo] raise];
return nil;
}
@end
@implementation MLKUnwindProtectForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
NSArray *results;
NS_DURING
{
results = [_protectedForm interpretWithEnvironment:env];
}
NS_HANDLER
{
[self interpretBodyWithEnvironment:env];
[localException raise];
}
NS_ENDHANDLER;
[self interpretBodyWithEnvironment:env];
return results;
}
@end
@implementation MLKFunctionCallForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
int i;
NSMutableArray *args = [NSMutableArray array];
for (i = 0; i < [_argumentForms count]; i++)
{
id result = PRIMARY([[_argumentForms objectAtIndex:i]
interpretWithEnvironment:env]);
[args addObject:result];
}
if (![_context symbolNamesFunction:_head])
{
[NSException raise:@"MLKNoSuchOperatorException"
format:@"%@ does not name a known operator.",
MLKPrintToString(_head)];
return nil;
}
else
{
id <MLKFuncallable> function = [env functionForSymbol:_head];
return [function applyToArray:args];
}
}
@end
@implementation MLKSetQForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
int i;
id value = nil;
MLKDynamicContext *dynamicContext = [MLKDynamicContext currentContext];
for (i = 0; i < [_variables count]; i++)
{
id symbol = denullify([_variables objectAtIndex:i]);
value = PRIMARY([[_valueForms objectAtIndex:i] interpretWithEnvironment:env]);
if ([_context variableIsLexical:symbol])
[env setValue:value forSymbol:symbol];
else if ([dynamicContext bindingForSymbol:symbol])
[dynamicContext setValue:value forSymbol:symbol];
else
// FIXME: Maybe print a warning.
[[MLKDynamicContext globalContext] addValue:value
forSymbol:symbol];
}
RETURN_VALUE (value);
}
@end
@implementation MLKFSetQForm (MLKInterpretation)
-(NSArray *) reallyInterpretWithEnvironment:(MLKLexicalEnvironment *)env
{
int i;
id value = nil;
for (i = 0; i < [_functionNames count]; i++)
{
id symbol = denullify([_functionNames objectAtIndex:i]);
value = PRIMARY([[_valueForms objectAtIndex:i] interpretWithEnvironment:env]);
if ([_context symbolNamesFunction:symbol])
{
[env setFunction:value forSymbol:symbol];
}
else
{
// FIXME: Maybe print a warning.
[[MLKLexicalContext globalContext] addFunction:symbol];
[[MLKLexicalEnvironment globalEnvironment] addFunction:value
forSymbol:symbol];
}
}
RETURN_VALUE (value);
}
@end
|