summaryrefslogtreecommitdiff
path: root/MLKPackage.m
blob: 66d3035a7a583f1a6be4027d0a860d31d8c40042 (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
/* -*- mode: objc; coding: utf-8 -*- */
/* Étoilisp/Mulklisp, 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 "MLKPackage.h"
#import "MLKSymbol.h"
#import "MLKError.h"

#import <Foundation/NSDictionary.h>
#import <Foundation/NSSet.h>
#import <Foundation/NSString.h>
#import <Foundation/NSArray.h>


static NSMutableDictionary *packages = nil;


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

  self = [self init];

  // FIXME: Make this thread-safe.
  if (!packages)
    packages = [[NSMutableDictionary alloc] init];

  [packages setObject:self forKey:name];

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

  _symbols = [[NSMutableDictionary alloc] init];
  _exported_symbols = [[NSMutableSet alloc] init];
  _shadowed_symbols = [[NSMutableSet alloc] init];
  _nicknames = [[NSMutableSet alloc] initWithSet:nicknames];
  ASSIGN (_name, name);

  return self;
}

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

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

  symbols = [[aPackage allSymbols] allObjects];

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

-(void) import:(MLKSymbol *)aSymbol
{
  // FIXME: Check for conflicts.

  // FIXME: What to do about exported and shadowed symbols that conflict
  // with the new one?
  [_symbols setObject:aSymbol forKey:[aSymbol name]];
}

-(void) shadow:(MLKSymbol *)aSymbol
{
  [_shadowed_symbols addObject:aSymbol];
}

-(void) unintern:(MLKSymbol *)aSymbol
{
  [_symbols removeObjectForKey:[aSymbol name]];
}

-(MLKSymbol *) intern:(NSString *)symbolName
{
  if ([[_symbols allKeys] containsObject:symbolName])
    return [_symbols objectForKey:symbolName];
  else
    {
      MLKSymbol *symbol = [[MLKSymbol alloc] initWithName:symbolName
                                             package:self];
      [_symbols setObject:symbol forKey:symbolName];
      return symbol;
    }
}

-(MLKSymbol *) findSymbol:(NSString *)symbolName
{
  if ([[_symbols allKeys] containsObject:symbolName])
    return [_symbols objectForKey:symbolName];
  else
    [[MLKError errorWithMessage:@"Symbol not found."] raise];

  return nil;
}

-(NSString *) name
{
  return _name;
}

-(NSSet *) nicknames
{
  return _nicknames;
}

-(NSSet *) exportedSymbols
{
  return _exported_symbols;
}

-(NSSet *) shadowedSymbols
{
  return _shadowed_symbols;
}

-(NSSet *) allSymbols
{
  return [NSSet setWithArray:[_symbols allValues]];
}
@end