This repository has been archived by the owner on Oct 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
symtab.js
64 lines (54 loc) · 1.71 KB
/
symtab.js
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
/*******************************************************************************
* Copyright (c) 2013 Max Schaefer.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Max Schaefer - initial API and implementation
*******************************************************************************/
/* Simple implementation of symbol tables. Uses
* prototypal inheritance to model scoping. */
if(typeof define !== 'function') {
var define = require('amdefine')(module);
}
define(function(require, exports) {
function mangle(name) {
return '$' + name;
}
function isMangled(name) {
return name && name[0] === '$';
}
function Symtab(outer) {
var self = Object.create(outer || Symtab.prototype);
// every scope has a pointer to its outer scope, which may be null
self.outer = outer;
return self;
}
Symtab.prototype.get = function(name, deflt) {
var mangled = mangle(name);
if(!deflt || this.has(name))
return this[mangled];
this[mangled] = deflt;
return deflt;
};
Symtab.prototype.has = function(name) {
return mangle(name) in this;
};
Symtab.prototype.hasOwn = function(name) {
return this.hasOwnProperty(mangle(name));
};
Symtab.prototype.set = function(name, value) {
return this[mangle(name)] = value;
};
Symtab.prototype.values = function() {
var values = [];
for(var p in this)
if(isMangled(p))
values[values.length] = this[p];
return values;
};
exports.Symtab = Symtab;
return exports;
});