-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope.cpp
122 lines (103 loc) · 2.97 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
#include "scope.hpp"
#include "interpreter.hpp"
// OBJECT CLASS DEFINITION
obj_ptr Object::getSlot(const std::string& ObjectName) const {
auto slot=Objects.find(ObjectName);
if(slot==Objects.end()) {
if(UpperScope.get()!=nullptr)
return UpperScope->getSlot(ObjectName);
return nullptr;
}
return slot->second;
}
obj_ptr Object::operator()(obj_ptr function_scope, Arguments& args) {
std::cerr<<"Not callable"<<std::endl;
return std::shared_ptr<Object>(this);
}
obj_ptr Object::clone() const {
obj_ptr new_obj=std::make_shared<Object>(callable);
cloneScope(new_obj);
return new_obj;
}
void Object::cloneScope(const obj_ptr& new_obj) const {
for(auto &&o:Objects) {
new_obj->addIntoSlot(o.first, o.second->clone());
}
}
// ARGUMENTS CLASS DEFINITION
Arguments::Arguments() { it=tokens.begin(); };
Arguments::Arguments(tokenizerBase& tok) {
addTilClose(tok);
}
void Arguments::addTilClose(tokenizerBase& tok) {
size_t closing=1;
token currToken=tok.nextToken();
if(currToken==token::closeArguments) --closing;
if(currToken==token::openArguments) ++closing;
while(closing>0) {
tokens.emplace_back(currToken, tok.flush());
currToken=tok.nextToken();
if(currToken==token::closeArguments) --closing;
if(currToken==token::openArguments) ++closing;
}
tokens.emplace_back(token::endOfBlock, "");
it=tokens.begin();
tok.flush(); // flush the trailing )
}
obj_ptr Arguments::execute(obj_ptr& scope) {
tokenizerBuilder exec(tokens);
Interpreter interp(exec, false, scope);
return interp.lastScope();
}
// make a chain of
// arg1 := args_values1;
// arg2 := args_values2;
// ...
// arg(n-1) := args_values(n-1);
// argn;
obj_ptr Arguments::execute(obj_ptr& scope, Arguments& args_values) {
tokenizerBuilder exec;
auto it=tokens.begin();
while(it!=tokens.end() && it->first!=token::nextArgument) {
exec.addToken(it->first, it->second);
++it;
}
while(it!=tokens.end()) {
++it;
if(it!=tokens.end()) {
exec.addToken(token::symbol, ":=");
while(!args_values.eof() && args_values.currToken()!=token::terminator
&& args_values.currToken()!=token::endOfBlock
&& args_values.currToken()!=token::nextArgument) {
exec.addToken(args_values.currToken(), args_values.flush());
args_values.move();
}
args_values.move();
exec.addToken(token::terminator, "");
}
while(it!=tokens.end() && it->first!=token::nextArgument) {
exec.addToken(it->first, it->second);
++it;
}
}
exec.restart();
Interpreter interp(exec, false, scope);
return interp.lastScope();
}
obj_ptr Arguments::exec_curr_part(obj_ptr& scope) {
tokenizerBuilder body;
while(it!=tokens.end() && it->first!=token::nextArgument) {
body.addToken(it->first, it->second);
++it;
}
if(it!=tokens.end())
++it;
body.addToken(token::terminator, "");
body.restart();
Interpreter body_exec(body, false, scope);
return body_exec.lastScope()->clone();
}
void Arguments::next_argument() {
while(it!=tokens.end() && it->first!=token::nextArgument) ++it;
if(it!=tokens.end()) ++it;
}