-
Notifications
You must be signed in to change notification settings - Fork 26
/
scope.js
78 lines (73 loc) · 2.22 KB
/
scope.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
define(function() {
function noVariableError(name) {
var err = new Error("no variable called " + name);
err.range = arguments[arguments.length - 1];
throw err;
}
var nullScope = {
assign: noVariableError,
update: noVariableError,
get: noVariableError,
getTypeOf: function(name, range) { return "undefined"; },
log: function() {}
};
function Scope(prev, name, range, log) {
this.prev = prev || nullScope;
this.name = name;
this.log = log || this.prev.log;
this.vars = {};
this.range = range;
this.declRanges = {};
this.log("enter");
}
Scope.prototype = {
assign: function(name, operator, value, range) {
if (name in this.vars) {
var oldValue = this.vars[name];
eval("this.vars[name] " + operator + " value");
this.log("assign", name, operator, value, oldValue, range);
return this.vars[name];
}
return this.prev.assign(name, operator, value, range);
},
update: function(name, operator, prefix, range) {
if (name in this.vars) {
var oldValue = this.vars[name];
eval("this.vars[name]" + operator);
this.log("update", name, operator, prefix, oldValue, range);
return prefix ? this.vars[name] : oldValue;
}
return this.prev.update(name, operator, prefix, range);
},
declare: function(name, value, range) {
this.vars[name] = value;
this.declRanges[name] = range;
this.log("declare", name, value, range);
return value;
},
getTypeOf: function(name, range) {
if (name in this.vars) {
this.log("getTypeOf", name, this.vars[name], range);
return typeof this.vars[name];
}
return this.prev.getTypeOf(name, range);
},
get: function(name, range) {
if (name in this.vars) {
this.log("get", name, this.vars[name], range);
return this.vars[name];
}
return this.prev.get(name, range);
},
leave: function() {
this.log("leave");
}
};
Scope.logToConsole = function() {
var args = ["SCOPE:" + this.name];
args = args.concat(Array.prototype.slice.call(arguments));
if (window.console)
console.log.apply(console, args);
};
return Scope;
});