-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathScope.js
153 lines (118 loc) · 2.75 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
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
// eslint-disable-next-line no-unused-vars
const state = require("./state").$;
const { append } = require("./lang/estree/estree");
const Identifier = require("./lang/ast/Identifier");
class Scope {
constructor(prior, block) {
this.prior = prior;
this.block = block;
if (prior) {
this.root = prior.root;
} else {
this.root = this;
}
this.local = {};
this.instance = null;
this.graph = {};
this.callback = [];
}
get $instance() {
let index = this;
while (index) {
const instance = index.instance;
if (instance) {
return instance;
}
index = index.prior;
}
return null;
}
set $instance(instance) {
this.instance = instance;
}
assign(variable, evaluation, reassign = false) {
let prefix;
if (reassign) {
prefix = this.retrieve(variable.first)?.object.node;
}
if (!prefix) {
prefix = {
type: "MemberExpression",
computed: false, // false because it uses dot notation
object: {
type: "Identifier",
name: "scope",
},
property: {
type: "Identifier",
name: "local",
},
};
}
const local = new Identifier(append(prefix, variable.node));
// eslint-disable-next-line no-unused-vars
const scope = this;
// eslint-disable-next-line no-eval
return eval(`${local}=${evaluation}`);
}
retrieve(variable, exact = false) {
let index = this;
let estree = {
type: "Identifier",
name: "scope",
};
const first = variable.first;
while (index) {
if (
index.graph[first] !== undefined &&
// eslint-disable-next-line no-eval
(!exact || eval(`index.local.${variable}`) !== undefined)
) {
const local = {
type: "Identifier",
name: "local",
};
estree = append(estree, local);
return new Identifier(append(estree, variable.node));
}
const prior = {
type: "Identifier",
name: "prior",
};
estree = append(estree, prior);
index = index.prior;
}
return null;
}
instance(instance) {
let index = this;
while (index) {
const value = index.instances[instance];
if (value) {
return value;
}
index = index.prior;
}
}
retrieveObject() {
let index = this;
while (index) {
if (index.object !== undefined) {
return index.object.name;
}
index = index.prior;
}
return null;
}
retrieveGraph(instance) {
let index = this;
while (index) {
const value = index.graph[instance];
if (value) {
return value;
}
index = index.prior;
}
}
}
module.exports = Scope;