-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope.cpp
139 lines (120 loc) · 2.93 KB
/
scope.cpp
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
// scope.cpp - implementation of Scope class
//
#include "scope.h"
#include "log.h"
#include <assert.h>
namespace js2cpp {
// Binding of name to definition
AST* Binding::Definition(void) const
{
assert(type==BOUND_FUNCTION);
return value;
} // Definition
void Binding::MakeExtern(void)
{
type=BOUND_EXTERN;
}
void Binding::MakeVar(AST* init)
{
type=BOUND_VARIABLE;
value = init;
}
void Binding::MakeFunction(AST* fun)
{
type=BOUND_FUNCTION;
value = fun;
}
void Binding::MakeReference(void)
{
type=BOUND_REFERENCE;
}
// Scope of names
aScope::~aScope()
{
}
void aScope::Bind(const char* name, Binding bind)
{
decls[name] = bind;
}
void aScope::DeclareVariable(const char* vname, AST* init)
{
// init is optional initializer
// add a variable declaration to this scope.
Log("%s declared in %s\n", vname, name);
Binding binding;
binding.MakeVar(init);
Bind(vname, binding);
}
void aScope::DeclareFunction(const char* fname, AST* fun)
{
// add a literal function to this scope
Log("function %s added to scope %s\n", fname, name);
Binding binding;
binding.MakeFunction(fun);
Bind(fname, binding);
}
void aScope::DeclareLiteralFunction(AST* fun)
{
// add a literal function to this scope
Log("literal function added to scope %s\n", name);
litfuncs.insert(fun);
}
void aScope::Reference(const char* id)
{
Log("use: %s in %s\n", id, name);
Binding binding;
if (decls.find(id) == decls.end()) {
// first reference to this name in this scope
binding.MakeReference();
decls[id] = binding;
}
} // Reference
bool aScope::FindDeclaration(const char* id, Binding& decl, aScope* &owner)
// Look up the declaration of a name in this scope-chain.
// Returns true if a declaration is found, false otherwise.
{
assert(this);
assert(name);
Bindings::iterator ii = decls.find(id);
if (ii != decls.end()) {
decl = (*ii).second;
if (decl.isDeclaration()) {
owner = this;
return true;
}
}
if (!parent) {
return false;
}
// Look in parent scope (Tail recursion!)
return parent->FindDeclaration(id, decl, owner);
}
void aScope::Start(void)
{
}
void aScope::End(void)
// End of a scope, surround = surrounding scope, if any (otherwise NULL)
{
Log("ending scope %s\n", this->name);
if (parent) {
for (Bindings::iterator ii=decls.begin(); ii!=decls.end(); ++ii) {
const char* id = (const char*)(*ii).first;
Binding binding = (*ii).second;
if (binding.isReference()) {
// referenced but never defined in this scope,
// export to surrounding scope
Log("export ref: %s to %s\n", id, parent->Name());
parent->Reference(id);
}
}
}
}
aScope* aScope::AtDepth(int d)
{
assert(d <= depth);
if (d==depth) {
return this;
}
return parent->AtDepth(d);
} // AtDepth
} // namespace