This repository has been archived by the owner on Oct 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
env.cc
81 lines (62 loc) · 2.15 KB
/
env.cc
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
#include "env.h"
#include <boost/algorithm/string/join.hpp>
using namespace std;
void Environment::push_namespace(AST::Namespace *ns) {
ns_stack.push_back(ns->id);
}
void Environment::pop_namespace() {
ns_stack.pop_back();
}
void Environment::push_class(AST::Class *cs) {
cs_stack.push_back(cs);
push_vars();
// declare class fields
ClassFieldDeclaration &f = decl->fields[get_current_ns()][get_current_cs()->id];
for(ClassFieldDeclaration::iterator it = f.begin(); it != f.end(); it++) {
AST::FieldFeature *ff = dynamic_cast<AST::FieldFeature*>(it->second);
if(ff != NULL)
add_var(EnvironmentVar(ff->id, ff->decl_type));
}
}
void Environment::pop_class() {
pop_vars();
cs_stack.pop_back();
}
void Environment::push_method(AST::MethodFeature *mf) {
mf_stack.push_back(mf);
}
void Environment::pop_method() {
mf_stack.pop_back();
}
string Environment::get_current_ns() {
return boost::algorithm::join(ns_stack, ".");
}
void Environment::push_vars() {
vars_stack.push_back(map<string, EnvironmentVar>());
}
void Environment::pop_vars() {
vars_stack.pop_back();
}
void Environment::add_var(const EnvironmentVar &v) {
vars_stack.back()[v.id] = v;
}
EnvironmentVar *Environment::find_var(string id) {
for(EnvironmentVarStack::reverse_iterator it = vars_stack.rbegin(); it != vars_stack.rend(); it++) {
map<string, EnvironmentVar>::iterator it2 = it->find(id);
if(it2 != it->end())
return &it2->second;
}
return NULL;
}
AST::MethodFeature *Environment::find_method(std::string id) {
for(list<AST::Class*>::iterator csit = cs_stack.begin(); csit != cs_stack.end(); csit++) {
AST::Class *cs = *csit;
for(AST::FeatureList::iterator fit = cs->fl->begin(); fit != cs->fl->end(); fit++) {
AST::MethodFeature *mf = dynamic_cast<AST::MethodFeature*>(*fit);
if(mf != NULL && mf->id == id)
return mf;
}
}
// TODO maybe id is full name static-method (TestNamespace.TestClass.testMethod)
return NULL;
}