summaryrefslogtreecommitdiff
path: root/appl/cmd/mash/symb.b
blob: 8d317b372183053ffb4966aadd067c6940aac4b6 (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
#
#	Symbol table routines.  A symbol table becomes copy-on-write
#	when it is cloned.  The first modification will copy the hash table.
#	Every list is then copied on first modification.
#

#
#	Copy a hash list.
#
cpsymbs(l: list of ref Symb): list of ref Symb
{
	r: list of ref Symb;
	while (l != nil) {
		r = (ref *hd l) :: r;
		l = tl l;
	}
	return r;
}

#
#	New symbol table.
#
Stab.new(): ref Stab
{
	return ref Stab(array[SHASH] of list of ref Symb, 0, 0);
}

#
#	Clone a symbol table.  Copy Stab and mark contents copy-on-write.
#
Stab.clone(t: self ref Stab): ref Stab
{
	t.copy = 1;
	t.wmask = SMASK;
	return ref *t;
}

#
#	Update symbol table entry, or add new entry.
#
Stab.update(t: self ref Stab, s: string, tag: int, v: list of string, f: ref Cmd, b: Mashbuiltin): ref Symb
{
	if (t.copy) {
		a := array[SHASH] of list of ref Symb;
		a[:] = t.tab[:];
		t.tab = a;
		t.copy = 0;
	}
	x := hash->fun1(s, SHASH);
	l := t.tab[x];
	if (t.wmask & (1 << x)) {
		l = cpsymbs(l);
		t.tab[x] = l;
		t.wmask &= ~(1 << x);
	}
	r := l;
	while (r != nil) {
		h := hd r;
		if (h.name == s) {
			case tag {
			Svalue =>
				h.value = v;
			Sfunc =>
				h.func = f;
			Sbuiltin =>
				h.builtin = b;
			}
			return h;
		}
		r = tl r;
	}
	n := ref Symb(s, v, f, b, 0);
	t.tab[x] = n :: l;
	return n;
}

#
#	Make a list of a symbol table's contents.
#
Stab.all(t: self ref Stab): list of ref Symb
{
	r: list of ref Symb;
	for (i := 0; i < SHASH; i++) {
		for (l := t.tab[i]; l != nil; l = tl l)
			r = (ref *hd l) :: r;
	}
	return r;
}

#
#	Assign a list of strings to a variable.  The distinguished value
#	"empty" is used to distinguish nil value from undefined.
#
Stab.assign(t: self ref Stab, s: string, v: list of string)
{
	if (v == nil)
		v = empty;
	t.update(s, Svalue, v, nil, nil);
}

#
#	Define a builtin.
#
Stab.defbuiltin(t: self ref Stab, s: string, b: Mashbuiltin)
{
	t.update(s, Sbuiltin, nil, nil, b);
}

#
#	Define a function.
#
Stab.define(t: self ref Stab, s: string, f: ref Cmd)
{
	t.update(s, Sfunc, nil, f, nil);
}

#
#	Symbol table lookup.
#
Stab.find(t: self ref Stab, s: string): ref Symb
{
	l := t.tab[hash->fun1(s, SHASH)];
	while (l != nil) {
		h := hd l;
		if (h.name == s)
			return h;
		l = tl l;
	}
	return nil;
}

#
#	Function lookup.
#
Stab.func(t: self ref Stab, s: string): ref Cmd
{
	v := t.find(s);
	if (v == nil)
		return nil;
	return v.func;
}

#
#	New environment.
#
Env.new(): ref Env
{
	return ref Env(Stab.new(), nil, ETop, nil, nil, nil, nil, nil, nil, 0);
}

#
#	Clone environment.  No longer top-level or interactive.
#
Env.clone(e: self ref Env): ref Env
{
	e = e.copy();
	e.flags &= ~(ETop | EInter);
	e.global = e.global.clone();
	if (e.local != nil)
		e.local = e.local.clone();
	return e;
}

#
#	Copy environment.
#
Env.copy(e: self ref Env): ref Env
{
	return ref *e;
}

#
#	Fetch $n argument.
#
Env.arg(e: self ref Env, s: string): string
{
	n := int s;
	if (e.args == nil || n >= len e.args)
		return "$" + s;
	else
		return e.args[n];
}

#
#	Lookup builtin.
#
Env.builtin(e: self ref Env, s: string): Mashbuiltin
{
	v := e.global.find(s);
	if (v == nil)
		return nil;
	return v.builtin;
}

#
#	Define a builtin.
#
Env.defbuiltin(e: self ref Env, s: string, b: Mashbuiltin)
{
	e.global.defbuiltin(s, b);
}

#
#	Define a function.
#
Env.define(e: self ref Env, s: string, f: ref Cmd)
{
	e.global.define(s, f);
}

#
#	Value of a shell variable (check locals then globals).
#
Env.dollar(e: self ref Env, s: string): ref Symb
{
	if (e.local != nil) {
		l := e.local.find(s);
		if (l != nil && l.value != nil)
			return l;
	}
	g := e.global.find(s);
	if (g != nil && g.value != nil)
		return g;
	return nil;
}

#
#	Lookup a function.
#
Env.func(e: self ref Env, s: string): ref Cmd
{
	v := e.global.find(s);
	if (v == nil)
		return nil;
	return v.func;
}

#
#	Local assignment.
#
Env.let(e: self ref Env, s: string, v: list of string)
{
	if (e.local == nil)
		e.local = Stab.new();
	e.local.assign(s, v);
}

#
#	Assignment.  Update local or define global.
#
Env.set(e: self ref Env, s: string, v: list of string)
{
	if (e.local != nil && e.local.find(s) != nil)
		e.local.assign(s, v);
	else
		e.global.assign(s, v);
}

#
#	Report undefined.
#
Env.undefined(e: self ref Env, s: string)
{
	e.report(s + ": undefined");
}