-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbenv.mjs
113 lines (98 loc) · 2.22 KB
/
benv.mjs
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
import {HashMap, ArraySet} from './common.mjs';
import {BOT} from './lattice.mjs';
export default function Benv(map, global)
{
this._map = map;
this._global = global;
}
Benv.EMPTY_FRAME = HashMap.empty();
Benv.empty =
function ()
{
return new Benv(Benv.EMPTY_FRAME, true);
}
Benv.prototype.extend =
function ()
{
return new Benv(this._map, false);
}
Benv.prototype.toString =
function ()
{
return this._map.toString();
}
Benv.prototype.nice =
function ()
{
return this._map.nice();
}
Benv.prototype.equals =
function (x)
{
return (x instanceof Benv)
&& this._global === x._global
&& this._map.equals(x._map)
}
Benv.prototype.hashCode =
function ()
{
var prime = 17;
var result = 1;
result = prime * result + this._map.hashCode();
result = prime * result + this._global.hashCode();
return result;
}
Benv.prototype.diff = //DEBUG
function (x)
{
var diff = [];
var thisNames = this._map.keys();
var xNames = x._map.keys();
for (var i = 0; i < thisNames.length; i++)
{
var thisName = thisNames[i];
var thisValue = this.lookup(thisName);
var xValue = x.lookup(thisName);
if (!thisValue.equals(xValue))
{
diff.push(thisName + "\t" + thisValue + " -- " + xValue);
}
}
for (var i = 0; i < xNames.length; i++)
{
var xName = xNames[i];
var xValue = x.lookup(xName);
var thisValue = this.lookup(xName);
if (thisValue === BOT)
{
diff.push(xName + "\t" + thisValue + " -- " + xValue);
}
}
if (!this._parents.equals(x._parents))
{
diff.push("<parents> " + this._parents + " -- " + x._parents);
}
return ">>>BENV\n" + diff.join("\n") + "<<<";
}
Benv.prototype.add =
function (name, value)
{
var map = this._map.put(name, value);
return new Benv(map, this._global);
}
Benv.prototype.lookup =
function (name)
{
const addr = this._map.get(name);
return addr === undefined ? BOT : addr;
}
Benv.prototype.addresses =
function ()
{
return ArraySet.from(this._map.values());
}
Benv.prototype.narrow =
function (names)
{
return new Benv(this._map.narrow(names), this.global);
}