summaryrefslogtreecommitdiff
path: root/MLKPackage.m
blob: f867694cb5e81446d1cf6f63cc9ad0e346f585dd (plain)
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
/* -*- 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/>.
 */

#import "MLKDynamicContext.h"
#import "MLKPackage.h"
#import "MLKSymbol.h"
#import "NSObject-MLKPrinting.h"
#import "runtime-compatibility.h"
#import "util.h"

#import <Foundation/NSException.h>
#import <Foundation/NSNull.h>


static NSMutableDictionary *packages = nil;


@implementation MLKPackage
+(void) initialize
{
  MLKPackage *cl, *clUser, *sys, *toilet, *tlUser;

  if (!packages)
    {
      packages = [[NSMutableDictionary alloc] init];

      cl = [MLKPackage packageWithName:@"COMMON-LISP"
                       nicknames:[NSSet setWithObject:@"CL"]];
      clUser = [MLKPackage packageWithName:@"COMMON-LISP-USER"
                           nicknames:[NSSet setWithObject:@"CL-USER"]];;
      sys = [MLKPackage packageWithName:@"TOILET-SYSTEM"
                        nicknames:[NSSet setWithObjects:
                                           @"TL-SYS", @"SYSTEM",
                                           @"SYS", nil]];
      toilet = [MLKPackage packageWithName:@"TOILET-LISP"
                           nicknames:[NSSet setWithObjects:
                                              @"TL", @"TOILET", nil]];
      tlUser = [MLKPackage packageWithName:@"TOILET-LISP-USER"
                           nicknames:[NSSet setWithObjects:
                                              @"TL-USER",
                                              @"TOILET-USER",
                                              nil]];
 
      [MLKPackage packageWithName:@"KEYWORD" nicknames:[NSSet set]];      
    }
  else
    {
      cl = [self findPackage:@"COMMON-LISP"];
      clUser = [self findPackage:@"COMMON-LISP-USER"];
      sys = [self findPackage:@"TOILET-SYSTEM"];
      toilet = [self findPackage:@"TOILET-LISP"];
      tlUser = [self findPackage:@"TOILET-LISP-USER"];
    }

  [cl usePackage:sys];

  [tlUser usePackage:cl];
  [tlUser usePackage:toilet];
  //[tlUser usePackage:clUser];

  [clUser usePackage:cl];
  [clUser usePackage:toilet];
  [clUser usePackage:sys];

  [cl import:nil];
  [cl export:nil];
  [cl export:[cl intern:@"T"]];
  [cl export:[cl intern:@"CATCH"]];
  [cl export:[cl intern:@"THROW"]];
  [cl export:[cl intern:@"IF"]];
  [cl export:[cl intern:@"IN-PACKAGE"]];
  [cl export:[cl intern:@"LET"]];
  [cl export:[cl intern:@"LAMBDA"]];
  [cl export:[cl intern:@"FUNCALL"]];
  [cl export:[cl intern:@"FUNCTION"]];
  [cl export:[cl intern:@"PROGN"]];
  [cl export:[cl intern:@"APPLY"]];
  [cl export:[cl intern:@"PROGV"]];
  [cl export:[cl intern:@"SETQ"]];
  [cl export:[cl intern:@"SETF"]];
  [cl export:[cl intern:@"DECLARE"]];
  [cl export:[cl intern:@"QUOTE"]];
  [cl export:[cl intern:@"VALUES"]];
  [cl export:[cl intern:@"EVAL"]];
  [cl export:[cl intern:@"SPECIAL"]];
  [cl export:[cl intern:@"SET"]];
  [cl export:[cl intern:@"UNWIND-PROTECT"]];
  [cl export:[cl intern:@"MULTIPLE-VALUE-CALL"]];
  [cl export:[cl intern:@"EVAL-WHEN"]];

  [sys export:[sys intern:@"%DEFMACRO"]];
  [sys export:[sys intern:@"%LAMBDA"]];
  [sys export:[sys intern:@"%FSET"]];
  [sys export:[sys intern:@"%FSETQ"]];
  [sys export:[sys intern:@"%LOOP"]];
  [sys export:[sys intern:@"%FLET"]];
  [sys export:[sys intern:@"%MACROLET"]];
  [sys export:[sys intern:@"%FOREIGN-LAMBDA"]];

  [sys export:[sys intern:@"*SYSTEM-INITIALISED-P*"]];

  [sys export:[sys intern:@"CAR"]];
  [sys export:[sys intern:@"CDR"]];
  [sys export:[sys intern:@"RPLACA"]];
  [sys export:[sys intern:@"RPLACD"]];
  [sys export:[sys intern:@"CONS"]];
  [sys export:[sys intern:@"LOAD"]];
  [sys export:[sys intern:@"EQ"]];
  [sys export:[sys intern:@"ATOM"]];
  [sys export:[sys intern:@"LISTP"]];
  [sys export:[sys intern:@"CONSP"]];
  [sys export:[sys intern:@"SYMBOLP"]];
  [sys export:[sys intern:@"FIXNUMP"]];
  [sys export:[sys intern:@"NULL"]];
  [sys export:[sys intern:@"ADD"]];
  [sys export:[sys intern:@"SUBTRACT"]];
  [sys export:[sys intern:@"MULTIPLY"]];
  [sys export:[sys intern:@"DIVIDE"]];
  [sys export:[sys intern:@"ADD-FIXNUMS"]];
  [sys export:[sys intern:@"SUBTRACT-FIXNUMS"]];
  [sys export:[sys intern:@"MULTIPLY-FIXNUMS"]];
  [sys export:[sys intern:@"DIVIDE-FIXNUMS"]];
  [sys export:[sys intern:@"LIST"]];
  [sys export:[sys intern:@"MACROEXPAND-1"]];
  [sys export:[sys intern:@"MACROEXPAND-ALL"]];
  [sys export:[sys intern:@"EXPORT"]];
  [sys export:[sys intern:@"SHADOW"]];
  [sys export:[sys intern:@"UNEXPORT"]];
  [sys export:[sys intern:@"FIND-PACKAGE"]];
  [sys export:[sys intern:@"STRING"]];
  [sys export:[sys intern:@"GENSYM"]];
  [sys export:[sys intern:@"MAKE-SYMBOL"]];
  [sys export:[sys intern:@"IMPORT"]];
  [sys export:[sys intern:@"INTERN"]];
  [sys export:[sys intern:@"SYMBOL-NAME"]];
  [sys export:[sys intern:@"FIXNUM-EQ"]];
  [sys export:[sys intern:@"DECLARATIONS-AND-DOC-AND-FORMS"]];

  [sys export:[sys intern:@"OBJC-CLASS-OF"]];
  [sys export:[sys intern:@"OBJC-SUBCLASSP"]];
  [sys export:[sys intern:@"FIND-OBJC-CLASS"]];
  [sys export:[sys intern:@"NS-LOG"]];
  [sys export:[sys intern:@"PRIMITIVE-TYPE-OF"]];
  [sys export:[sys intern:@"SEND-BY-NAME"]];

  [cl export:[cl intern:@"*BREAK-ON-SIGNALS*"]];
  [cl export:[cl intern:@"*COMPILE-FILE-PATHNAME*"]];
  [cl export:[cl intern:@"*COMPILE-FILE-TRUENAME*"]];
  [cl export:[cl intern:@"*COMPILE-PRINT*"]];
  [cl export:[cl intern:@"*COMPILE-VERBOSE*"]];
  [cl export:[cl intern:@"*DEBUG-IO*"]];
  [cl export:[cl intern:@"*DEBUGGER-HOOK*"]];
  [cl export:[cl intern:@"*DEFAULT-PATHNAME-DEFAULTS*"]];
  [cl export:[cl intern:@"*ERROR-OUTPUT*"]];
  [cl export:[cl intern:@"*FEATURES*"]];
  [cl export:[cl intern:@"*GENSYM-COUNTER*"]];
  [cl export:[cl intern:@"*LOAD-PATHNAME*"]];
  [cl export:[cl intern:@"*LOAD-PRINT*"]];
  [cl export:[cl intern:@"*LOAD-TRUENAME*"]];
  [cl export:[cl intern:@"*LOAD-VERBOSE*"]];
  [cl export:[cl intern:@"*MACROEXPAND-HOOK*"]];
  [cl export:[cl intern:@"*MODULES*"]];
  [cl export:[cl intern:@"*PACKAGE*"]];
  [cl export:[cl intern:@"*PRINT-ARRAY*"]];
  [cl export:[cl intern:@"*PRINT-BASE*"]];
  [cl export:[cl intern:@"*PRINT-CASE*"]];
  [cl export:[cl intern:@"*PRINT-CIRCLE*"]];
  [cl export:[cl intern:@"*PRINT-ESCAPE*"]];
  [cl export:[cl intern:@"*PRINT-GENSYM*"]];
  [cl export:[cl intern:@"*PRINT-LENGTH*"]];
  [cl export:[cl intern:@"*PRINT-LEVEL*"]];
  [cl export:[cl intern:@"*PRINT-LINES*"]];
  [cl export:[cl intern:@"*PRINT-MISER-WIDTH*"]];
  [cl export:[cl intern:@"*PRINT-PPRINT-DISPATCH*"]];
  [cl export:[cl intern:@"*PRINT-PRETTY*"]];
  [cl export:[cl intern:@"*PRINT-RADIX*"]];
  [cl export:[cl intern:@"*PRINT-READABLY*"]];
  [cl export:[cl intern:@"*PRINT-RIGHT-MARGIN*"]];
  [cl export:[cl intern:@"*QUERY-IO*"]];
  [cl export:[cl intern:@"*RANDOM-STATE*"]];
  [cl export:[cl intern:@"*READ-BASE*"]];
  [cl export:[cl intern:@"*READ-DEFAULT-FLOAT-FORMAT*"]];
  [cl export:[cl intern:@"*READ-EVAL*"]];
  [cl export:[cl intern:@"*READ-SUPPRESS*"]];
  [cl export:[cl intern:@"*READTABLE*"]];
  [cl export:[cl intern:@"*STANDARD-INPUT*"]];
  [cl export:[cl intern:@"*STANDARD-OUTPUT*"]];
  [cl export:[cl intern:@"*TERMINAL-IO*"]];
  [cl export:[cl intern:@"*TRACE-OUTPUT* "]];
}

-(MLKPackage *) initWithName:(NSString *)name
                   nicknames:(NSSet *)nicknames
{
  int i;
  NSArray *e;

  self = [super init];

  [packages setObject:self forKey:name];

  e = [nicknames allObjects];
  for (i = 0; i < [e count]; i++)
    {
      [packages setObject:self forKey:[e objectAtIndex:i]];
    }

  _present_symbols = [[NSMutableSet alloc] init];
  _accessible_symbols = [[NSMutableDictionary alloc] init];
  _exported_symbols = [[NSMutableSet alloc] init];
  _shadowing_symbols = [[NSMutableSet alloc] init];
  _nicknames = [[NSMutableSet alloc] initWithSet:nicknames];
  _used_packages = [[NSMutableArray alloc] init];
  _using_packages = [[NSMutableArray alloc] init];
  LASSIGN (_name, name);

  return self;
}

+(MLKPackage *) packageWithName:(NSString *)name
                      nicknames:(NSSet *)nicknames
{
  return LAUTORELEASE ([[self alloc] initWithName:name nicknames:nicknames]);
}

+(MLKPackage *) findPackage:(NSString *)name
{
  return [packages objectForKey:name];
}

-(void) usePackage:(MLKPackage *)package
{
  int i;
  NSArray *symbols;

  symbols = [[package exportedSymbols] allObjects];

  for (i = 0; i < [symbols count]; i++)
    {
      [self inherit:[symbols objectAtIndex:i]];
    }

  [_used_packages addObject:package];
  [package->_using_packages addObject:self];
}

-(void) unusePackage:(MLKPackage *)package
{
  int i;
  NSArray *symbols;

  symbols = [[package exportedSymbols] allObjects];

  for (i = 0; i < [symbols count]; i++)
    {
      [self uninherit:[symbols objectAtIndex:i]];
    }

  [_used_packages removeObject:package];
  [package->_using_packages removeObject:self];
}

-(void) import:(MLKSymbol *)symbol
{
  id old_symbol;
  NSString *name;

  name = symbol ? [symbol name] : (NSString *)@"NIL";
  symbol = symbol ? (id)symbol : (id)[NSNull null];

  if ((old_symbol = [_accessible_symbols objectForKey:name]))
    {
      if (old_symbol != symbol)
        [NSException
          raise:@"MLKSymbolConflictError"
          format:@"Imported symbol %@ conflicts with accessible symbol %@ in package %@.",
                 MLKPrintToString(symbol),
                 MLKPrintToString(old_symbol),
                 MLKPrintToString(self)];
    }

  [_accessible_symbols setObject:symbol forKey:name];
  [_present_symbols addObject:symbol];

  if ([_name isEqual:@"KEYWORD"])
    {
      // Make keyword symbols self-evaluate.
      [[MLKDynamicContext globalContext] addValue:symbol forSymbol:symbol];

      // Make them external as well.
      [self export:symbol];

      // FIXME: Should finally make them constant, see CLHS 10.2, Type KEYWORD.
    }
}

-(void) inherit:(MLKSymbol *)symbol
{
  id old_symbol;
  NSString *name;

  name = symbol ? [symbol name] : (NSString *)@"NIL";
  symbol = nullify (symbol);
  old_symbol = [_accessible_symbols objectForKey:name];

  if (old_symbol && [_shadowing_symbols containsObject:old_symbol])
    return;

  if (old_symbol && old_symbol != symbol)
    [NSException
      raise:@"MLKSymbolConflictError"
      format:@"Inherited symbol %@ conflicts with accessible symbol %@ in package %@.",
             MLKPrintToString(symbol),
             MLKPrintToString(old_symbol),
             MLKPrintToString(self)];

  [_accessible_symbols setObject:symbol forKey:name];
  
  if ([_name isEqual:@"KEYWORD"])
    {
      // Make keyword symbols self-evaluate.
      [[MLKDynamicContext globalContext] addValue:symbol forSymbol:symbol];

      // Make them external as well.
      [self export:symbol];

      // FIXME: Should finally make them constant, see CLHS 10.2, Type KEYWORD.
    }
}

-(void) uninherit:(MLKSymbol *)symbol
{
  NSString *name;

  name = symbol ? [symbol name] : (NSString *)@"NIL";

  if ([_accessible_symbols objectForKey:name] == symbol
      && ![_present_symbols containsObject:symbol])
    [_accessible_symbols removeObjectForKey:name];
}

-(void) export:(MLKSymbol *)symbol
{
  int i;
  NSString *name;

  name = symbol ? [symbol name] : (NSString *)@"NIL";
  symbol = nullify (symbol);

  for (i = 0; i < [_using_packages count]; i++)
    {
      MLKPackage *package = [_using_packages objectAtIndex:i];
      id old_symbol = [package->_accessible_symbols objectForKey:name];

      if (old_symbol
          && (old_symbol != symbol)
          && ![_shadowing_symbols containsObject:old_symbol])
        [NSException
          raise:@"MLKSymbolConflictError"
          format:@"Exported symbol %@ conflicts with accessible symbol %@ in package %@.",
                 MLKPrintToString(symbol),
                 MLKPrintToString(old_symbol),
                 MLKPrintToString(package)];
    }

  for (i = 0; i < [_using_packages count]; i++)
    {
      [[_using_packages objectAtIndex:i]
        inherit:(symbol == (id)[NSNull null] ? nil : (id)symbol)];
    }

  [_exported_symbols addObject:symbol];
}

-(void) unexport:(MLKSymbol *)symbol
{
  int i;

  symbol = nullify (symbol);

  [_exported_symbols removeObject:symbol];

  for (i = 0; i < [_using_packages count]; i++)
    {
      [[_using_packages objectAtIndex:i] uninherit:symbol];
    }
}

-(void) shadow:(NSString *)symbolName
{
  MLKSymbol *symbol;

  symbol = [_accessible_symbols objectForKey:symbolName];
  if (!symbol || ![_present_symbols containsObject:symbol])
    {
      //NSLog (@"Shadowing %@", symbol);
      symbol = [MLKSymbol symbolWithName:symbolName package:self];
      [_accessible_symbols setObject:symbol forKey:symbolName];
      [_present_symbols addObject:symbol];
    }
  [_shadowing_symbols addObject:symbol];
}

-(void) unintern:(MLKSymbol *)aSymbol
{
  // FIXME: Check for conflicts.
  if ([_present_symbols containsObject:aSymbol])
    {
      [_present_symbols removeObject:aSymbol];
      [_accessible_symbols removeObjectForKey:[aSymbol name]];
      [_shadowing_symbols removeObject:aSymbol];

      if (self == [aSymbol homePackage])
        [aSymbol setHomePackage:nil];
    }
}

-(MLKSymbol *) intern:(NSString *)symbolName
{
  MLKSymbol *symbol;
  if ((symbol = [_accessible_symbols objectForKey:symbolName]))
    return (symbol == (id)[NSNull null] ? nil : (id)symbol);
  else
    {
      //NSLog (@"Interning %@", symbolName);
      MLKSymbol *symbol = [[MLKSymbol alloc] initWithName:symbolName
                                             package:self];
      [self import:symbol];
      return symbol;
    }
}

-(MLKSymbol *) findSymbol:(NSString *)symbolName
{
  MLKSymbol *symbol;
  if ((symbol = [_accessible_symbols objectForKey:symbolName]))
    return (symbol == (id)[NSNull null] ? nil : (id)symbol);
  else
    [NSException raise:@"MLKNoSuchSymbolError"
                 format:@"The package %@ does not contain a symbol named %@.",
                        self,
                        symbolName];

  return nil;
}

-(NSString *) name
{
  return _name;
}

-(NSSet *) nicknames
{
  return _nicknames;
}

-(NSSet *) exportedSymbols
{
  return _exported_symbols;
}

-(NSSet *) shadowingSymbols
{
  return _shadowing_symbols;
}

-(NSSet *) allSymbols
{
  return [NSSet setWithArray:[_accessible_symbols allValues]];
}

-(NSArray *) usedPackages
{
  return _used_packages;
}

-(NSArray *) usingPackages
{
  return _using_packages;
}

-(NSString *) descriptionForLisp
{
  return [NSString stringWithFormat:@"#<Package %@>", MLKPrintToString ([self name])];
}

-(void) dealloc
{
  LRELEASE (_present_symbols);
  LRELEASE (_accessible_symbols);
  LRELEASE (_exported_symbols);
  LRELEASE (_shadowing_symbols);
  LRELEASE (_nicknames);
  LRELEASE (_used_packages);
  LRELEASE (_using_packages);
  LRELEASE (_name);
  [super dealloc];
}
@end