-
Notifications
You must be signed in to change notification settings - Fork 1
/
global-setup.cpp
208 lines (160 loc) · 4.9 KB
/
global-setup.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "./cparse/shunting-yard.h"
#include <cstdio>
#include <sstream>
struct lineIterator : public Iterator {
FILE* file;
packToken last;
lineIterator(FILE* fp) : file(fp) {}
packToken* next() {
char c = fgetc(file);
std::stringstream ss;
if (c == EOF || c == '\0') {
reset();
return 0;
}
// Read the line until it ends:
while (c != '\0' && c != EOF && c != '\n' && c != '\r') {
ss << c;
c = fgetc(file);
}
// If the file is over:
if (c == EOF || c == '\0') {
// Put the last character back:
ungetc(c, file);
} else {
char peek = fgetc(file);
// If the next char is not part of the End Of Line:
if ((peek != '\r' && peek != '\n') || c == peek) {
// Put it back:
ungetc(peek, file);
}
}
last = ss.str();
return &last;
}
void reset() { fseek(file, 0, SEEK_SET); }
TokenBase* clone() const {
return new lineIterator(*this);
}
};
packToken file_write(TokenMap scope) {
TokenMap _this = scope.find("this")->asMap();
std::string text = scope.find("text")->asString();
FILE* file = reinterpret_cast<FILE*>(_this["fp"].asInt());
fprintf(file, "%s", text.c_str());
return packToken::None();
}
packToken file_read(TokenMap scope) {
TokenMap _this = scope.find("this")->asMap();
FILE* file = reinterpret_cast<FILE*>(_this["fp"].asInt());
char c = fgetc(file);
std::stringstream ss;
while (c && c != EOF) {
ss << c;
c = fgetc(file);
}
return ss.str();
}
packToken file_readlines(TokenMap scope) {
TokenMap _this = scope.find("this")->asMap();
FILE* file = reinterpret_cast<FILE*>(_this["fp"].asInt());
if (file == 0) {
throw std::runtime_error("Cannot read from a closed file!");
}
return lineIterator(file);
}
packToken file_close(TokenMap scope) {
TokenMap _this = scope.find("this")->asMap();
FILE* file = reinterpret_cast<FILE*>(_this["fp"].asInt());
fclose(file);
_this["fp"] = 0;
return packToken::None();
}
struct reverseIterator : public Iterator {
TokenList list;
int64_t pos;
reverseIterator(TokenList list) : list(list) {
reset();
}
packToken* next() {
if (pos >= 0) {
size_t index = pos--;
return &(list[index]);
} else {
reset();
return 0;
}
}
void reset() { pos = list.list().size()-1; }
TokenBase* clone() const {
return new reverseIterator(*this);
}
};
packToken default_reverse(TokenMap scope) {
TokenList list = scope.find("this")->asList();
return packToken(new reverseIterator(list));
}
// The base class for the file objects.
TokenMap BASE_file;
packToken default_open(TokenMap scope) {
std::string addr = scope.find("addr")->asString();
packToken p_mode = *scope.find("mode");
std::string mode;
if (p_mode->type == NONE) {
mode = "r";
} else {
mode = p_mode.asString();
}
FILE* file = fopen(addr.c_str(), mode.c_str());
TokenMap obj_file = BASE_file.getChild();
obj_file["fp"] = reinterpret_cast<int64_t>(file);
return obj_file;
}
packToken default_rpn(TokenMap scope) {
std::string code = scope.find("value")->asString();
calculator c(code.c_str(), scope);
return c.str();
}
/* * * * * lazy built-in class * * * * */
packToken lazy_class_init(TokenMap scope) {
TokenMap _this = scope.find("this")->asMap();
_this["func"] = packToken(scope.find("func")->asFunc()->clone());
_this["args"] = scope.find("args")->asList();
return packToken::None();
}
packToken lazy_class_exec(TokenMap scope) {
TokenMap _this = scope.find("this")->asMap();
Function* func = _this["func"].asFunc();
TokenList args = _this["args"].asList();
Tuple tuple;
// Copy the tokens from this.args:
for (const packToken& item : args.list()) {
tuple.list().push_back(item);
}
// Copy the tokens received as exec() arguments:
args = scope.find("args")->asList();
for (const packToken& item : args.list()) {
tuple.list().push_back(item);
}
return Function::call(_this, func, &tuple, *(scope.parent()));
}
/* * * * * Global Startup * * * * */
struct GlobalStartup {
GlobalStartup() {
TokenMap& global = TokenMap::default_global();
global["global"] = global;
global["rpn"] = CppFunction(&default_rpn, {"value"}, "rpn");
global["open"] = CppFunction(&default_open, {"addr", "mode"}, "open");
// Add a default class to global scope:
global["Lazy"] = TokenMap();
global["Lazy"]["__init__"] = CppFunction(&lazy_class_init, {"func"}, "init");
global["Lazy"]["exec"] = CppFunction(&lazy_class_exec);
typeMap_t& type_map = calculator::type_attribute_map();
type_map[LIST]["reverse"] = CppFunction(&default_reverse, "reverse");
// Startup the BASE_file prototype:
BASE_file["readlines"] = CppFunction(file_readlines);
BASE_file["read"] = CppFunction(file_read);
BASE_file["write"] = CppFunction(file_write, {"text"}, "write");
BASE_file["close"] = CppFunction(file_close);
}
} global_startup;