File: scope.c

package info (click to toggle)
nickle 2.68-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,336 kB
  • ctags: 3,288
  • sloc: ansic: 31,198; yacc: 1,860; lex: 858; sh: 830; makefile: 229
file content (285 lines) | stat: -rw-r--r-- 6,638 bytes parent folder | download | duplicates (6)
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
/* $Header$ */

/*
 * Copyright © 1988-2004 Keith Packard and Bart Massey.
 * All Rights Reserved.  See the file COPYING in this directory
 * for licensing information.
 */

#include	"nickle.h"
#include	"ref.h"

static void
NamespaceMark (void *object)
{
    NamespacePtr    namespace = object;

    MemReference (namespace->previous);
    MemReference (namespace->names);
}

DataType namespaceType = { NamespaceMark, 0, "namespaceType" };

NamespacePtr
NewNamespace (NamespacePtr previous)
{
    ENTER ();
    NamespacePtr    namespace;

    namespace = ALLOCATE (&namespaceType, sizeof (Namespace));
    namespace->previous = previous;
    namespace->names = 0;
    namespace->publish = publish_public;
    RETURN (namespace);
}

static void
NamelistMark (void *object)
{
    NamelistPtr	namelist = object;

    MemReference (namelist->next);
    MemReference (namelist->symbol);
}

DataType namelistType = { NamelistMark, 0, "namelistType" };

static NamelistPtr
NewNamelist (NamelistPtr next, SymbolPtr symbol, Publish publish)
{
    ENTER ();
    NamelistPtr   namelist;

    namelist = ALLOCATE (&namelistType, sizeof (Namelist));
    namelist->next = next;
    namelist->symbol = symbol;
    namelist->publish = publish;
    RETURN (namelist);
}

NamespacePtr	GlobalNamespace, TopNamespace, CurrentNamespace;
ReferencePtr	TopNamespaceReference;
ReferencePtr	CurrentNamespaceReference;
CommandPtr	CurrentCommands;
ReferencePtr	CurrentCommandsReference;

void
NamespaceInit (void)
{
    ENTER ();

    GlobalNamespace = NewNamespace (0);
    MemAddRoot (GlobalNamespace);
    
    TopNamespace = GlobalNamespace;
    TopNamespaceReference = NewReference ((void **) &TopNamespace);
    MemAddRoot (TopNamespaceReference);
    
    CurrentNamespace = GlobalNamespace;
    CurrentNamespaceReference = NewReference ((void **) &CurrentNamespace);
    MemAddRoot (CurrentNamespaceReference);
    
    CurrentCommands = 0;
    CurrentCommandsReference = NewReference ((void **) &CurrentCommands);
    MemAddRoot (CurrentCommandsReference);

    EXIT ();
}

static NamelistPtr
NamespaceFindNamelist (NamespacePtr namespace, Atom atom, Bool search, Bool allow_private)
{
    NamelistPtr	namelist;

    do
    {
	for (namelist = namespace->names; namelist; namelist = namelist->next)
	    if (namelist->symbol->symbol.name == atom &&
		(allow_private ||
		 namespace->publish != publish_private ||
		 namelist->publish != publish_private))
		return namelist;
	namespace = namespace->previous;
    } while (search && namespace);
    return 0;
}

SymbolPtr
NamespaceFindName (NamespacePtr namespace, Atom atom, Bool search)
{
    NamelistPtr	namelist;

    namelist = NamespaceFindNamelist (namespace, atom, search, False);
    if (namelist)
	return namelist->symbol;
    return 0;
}

Bool
NamespaceIsNamePrivate (NamespacePtr namespace, Atom atom, Bool search)
{
    return NamespaceFindNamelist (namespace, atom, search, True) != 0;
}

SymbolPtr
NamespaceAddName (NamespacePtr namespace, SymbolPtr symbol, Publish publish)
{
    NamelistPtr namelist;
    NamelistPtr	*prev;

    /*
     * Remove old symbol
     */
    for (prev = &namespace->names; (namelist = *prev); prev = &namelist->next)
	if (namelist->symbol->symbol.name == symbol->symbol.name)
	{
	    *prev = namelist->next;
	    break;
	}

    namelist = NewNamelist (namespace->names, symbol, publish);
    namespace->names = namelist;
    return symbol;
}

Bool
NamespaceRemoveName (NamespacePtr namespace, Atom atom)
{
    NamelistPtr	namelist, *prev;

    for (prev = &namespace->names; (namelist = *prev); prev = &namelist->next)
	if (namelist->symbol->symbol.name == atom)
	{
	    *prev = namelist->next;
	    return True;
	    break;
	}
    return False;
}

void
NamespaceImport (NamespacePtr namespace, NamespacePtr import, Publish publish)
{
    NamelistPtr	namelist;

    for (namelist = import->names; namelist; namelist = namelist->next)
	if (namelist->publish == publish_public)
	    NamespaceAddName (namespace, namelist->symbol, publish);
}

static void
CommandMark (void *object)
{
    CommandPtr    command = object;

    MemReference (command->previous);
    MemReference (command->func);
}

DataType commandType = { CommandMark, 0, "commandType" };

CommandPtr
NewCommand (CommandPtr previous, Atom name, Value func, Bool names)
{
    ENTER ();
    CommandPtr    command;

    command = ALLOCATE (&commandType, sizeof (*command));
    command->previous = previous;
    command->name = name;
    command->func = func;
    command->names = names;
    RETURN (command);
}

CommandPtr
CommandFind (CommandPtr command, Atom name)
{
    for(; command; command = command->previous)
	if (command->name == name)
	    return command;
    return 0;
}

CommandPtr
CommandRemove (CommandPtr command, Atom name)
{
    ENTER ();
    CommandPtr    *prev;

    for (prev = &command; *prev; prev = &(*prev)->previous)
	if ((*prev)->name == name)
	{
	    *prev = (*prev)->previous;
	    break;
	}
    RETURN (command);
}

Bool
NamespaceLocate (Value		names, 
		 NamespacePtr	*namespacep,
		 SymbolPtr	*symbolp,
		 Publish	*publishp,
		 Bool		complain)
{
    int		    i;
    NamespacePtr    namespace;
    FramePtr	    f;
    CodePtr	    c;
    Value	    string;
    NamelistPtr	    namelist = 0;
    SymbolPtr	    symbol;
    Bool	    search = True;
    
    if (!ValueIsArray(names) || names->array.ndim != 1 || 
	ArrayLimits(&names->array)[0] == 0)
    {
	RaiseStandardException (exception_invalid_argument, 3,
				NewStrString ("not non-empty array of strings"),
				NewInt (0), names);
	return False;
    }
    GetNamespace (&namespace, &f, &c);
    for (i = 0; i < ArrayLimits(&names->array)[0]; i++)
    {
	string = ArrayValue (&names->array, i);
	if (aborting)
	    return False;
	if (!ValueIsString(string))
	{
	    RaiseStandardException (exception_invalid_argument, 3,
				    NewStrString ("not string"),
				    NewInt (0), string);
	    return False;
	}
	namelist = NamespaceFindNamelist (namespace, 
					  AtomId (StringChars (&string->string)),
					  search, True);
	search = False;
	if (!namelist)
	{
	    if (complain)
		FilePrintf (FileStdout, "No symbol %v in namespace\n",
			    string);
	    return False;
	}
	symbol = namelist->symbol;
	if (i != ArrayLimits(&names->array)[0] - 1)
	{
	    if (symbol->symbol.class != class_namespace)
	    {
		RaiseStandardException (exception_invalid_argument, 3,
					NewStrString ("not namespace"),
					NewInt(i), string);
		return False;
	    }
	    namespace = symbol->namespace.namespace;
	}
    }
    *namespacep = namespace;
    *symbolp = namelist->symbol;
    *publishp = namelist->publish;;
    return True;
}