-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibraryManager.cpp
252 lines (211 loc) · 19.5 KB
/
LibraryManager.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "LibraryManager.h"
#include <stdio.h>
#include <ctime>
#include <iostream>
#include "Utils.h"
#include "Logger.h"
#define PRINT_LAMBDA(fn) [](Value *args, uint32_t argCount, const Token *relatedToken, Value &result) -> bool \
{ \
if (args == nullptr) \
return false; \
if (argCount == 1) \
{ \
fn(TEXT("{}"), args[0].ToString()); \
return false; \
} \
if (!IS_STR_VALUE(args[0])) \
{ \
for (uint32_t i = 0; i < argCount; ++i) \
fn(TEXT("{}"), args[i].ToString()); \
return false; \
} \
STD_STRING content = TO_STR_VALUE(args[0])->value; \
if (argCount != 1) /*formatting output*/ \
{ \
size_t pos = content.find(TEXT("{}")); \
size_t argpos = 1; \
while (pos != STD_STRING::npos) \
{ \
if (argpos < argCount) \
content.replace(pos, 2, args[argpos++].ToString()); \
else \
content.replace(pos, 2, TEXT("null")); \
pos = content.find(TEXT("{}")); \
} \
} \
size_t pos = content.find(TEXT("\\n")); \
while (pos != STD_STRING::npos) \
{ \
content[pos] = TCHAR('\n'); \
content.replace(pos + 1, 1, TEXT("")); /*erase a char*/ \
pos = content.find(TEXT("\\n")); \
} \
pos = content.find(TEXT("\\t")); \
while (pos != STD_STRING::npos) \
{ \
content[pos] = TCHAR('\t'); \
content.replace(pos + 1, 1, TEXT("")); /*erase a char*/ \
pos = content.find(TEXT("\\t")); \
} \
pos = content.find(TEXT("\\r")); \
while (pos != STD_STRING::npos) \
{ \
content[pos] = TCHAR('\r'); \
content.replace(pos + 1, 1, TEXT("")); /*erase a char*/ \
pos = content.find(TEXT("\\r")); \
} \
fn(TEXT("{}"), content); \
return false; \
}
namespace lwscript
{
SINGLETON_IMPL(LibraryManager)
void LibraryManager::RegisterLibrary(ClassObject *libraryClass)
{
for (const auto &lib : mLibraries)
{
if (lib->name == libraryClass->name)
Logger::Error(TEXT("Conflict library name {}"), libraryClass->name);
}
mLibraries.emplace_back(libraryClass);
}
const std::vector<ClassObject *> &LibraryManager::GetLibraries() const
{
return mLibraries;
}
LibraryManager::LibraryManager()
{
const auto SizeOfFunction = new NativeFunctionObject([](Value *args, uint32_t argCount, const Token *relatedToken, Value &result) -> bool
{
if (args == nullptr || argCount > 1)
Logger::Error(relatedToken, TEXT("[Native function 'sizeof']:Expect a argument."));
if (IS_ARRAY_VALUE(args[0]))
{
result = Value((int64_t)TO_ARRAY_VALUE(args[0])->elements.size());
return true;
}
else if (IS_DICT_VALUE(args[0]))
{
result = Value((int64_t)TO_DICT_VALUE(args[0])->elements.size());
return true;
}
else if (IS_STR_VALUE(args[0]))
{
result = Value((int64_t)TO_STR_VALUE(args[0])->value.size());
return true;
}
else
Logger::Error(relatedToken, TEXT("[Native function 'sizeof']:Expect a array,dict ot string argument."));
return false;
});
const auto InsertFunction = new NativeFunctionObject([](Value *args, uint32_t argCount, const Token *relatedToken, Value &result) -> bool
{
if (args == nullptr || argCount != 3)
Logger::Error(relatedToken, TEXT("[Native function 'insert']:Expect 3 arguments,the arg0 must be array,dict or string object.The arg1 is the index object.The arg2 is the value object."));
if (IS_ARRAY_VALUE(args[0]))
{
ArrayObject *array = TO_ARRAY_VALUE(args[0]);
if (!IS_INT_VALUE(args[1]))
Logger::Error(relatedToken, TEXT("[Native function 'insert']:Arg1 must be integer type while insert to a array"));
int64_t iIndex = TO_INT_VALUE(args[1]);
if (iIndex < 0 || iIndex >= (int64_t)array->elements.size())
Logger::Error(relatedToken, TEXT("[Native function 'insert']:Index out of array's range"));
array->elements.insert(array->elements.begin() + iIndex, 1, args[2]);
}
else if (IS_DICT_VALUE(args[0]))
{
DictObject *dict = TO_DICT_VALUE(args[0]);
for (auto [key, value] : dict->elements)
if (key == args[1])
Logger::Error(relatedToken, TEXT("[Native function 'insert']:Already exist value in the dict object of arg1") + args[1].ToString());
dict->elements[args[1]] = args[2];
}
else if (IS_STR_VALUE(args[0]))
{
auto &string = TO_STR_VALUE(args[0])->value;
if (!IS_INT_VALUE(args[1]))
Logger::Error(relatedToken, TEXT("[Native function 'insert']:Arg1 must be integer type while insert to a array"));
int64_t iIndex = TO_INT_VALUE(args[1]);
if (iIndex < 0 || iIndex >= (int64_t)string.size())
Logger::Error(relatedToken, TEXT("[Native function 'insert']:Index out of array's range"));
string.insert(iIndex, args[2].ToString());
}
else
Logger::Error(relatedToken, TEXT("[Native function 'insert']:Expect a array,dict ot string argument."));
result = args[0];
return true;
});
const auto EraseFunction = new NativeFunctionObject([](Value *args, uint32_t argCount, const Token *relatedToken, Value &result) -> bool
{
if (args == nullptr || argCount != 2)
Logger::Error(relatedToken, TEXT("[Native function 'erase']:Expect 2 arguments,the arg0 must be array,dict or string object.The arg1 is the corresponding index object."));
if (IS_ARRAY_VALUE(args[0]))
{
ArrayObject *array = TO_ARRAY_VALUE(args[0]);
if (!IS_INT_VALUE(args[1]))
Logger::Error(relatedToken, TEXT("[Native function 'erase']:Arg1 must be integer type while insert to a array"));
int64_t iIndex = TO_INT_VALUE(args[1]);
if (iIndex < 0 || iIndex >= (int64_t)array->elements.size())
Logger::Error(relatedToken, TEXT("[Native function 'erase']:Index out of array's range"));
array->elements.erase(array->elements.begin() + iIndex);
}
else if (IS_DICT_VALUE(args[0]))
{
DictObject *dict = TO_DICT_VALUE(args[0]);
bool hasValue = false;
for (auto it = dict->elements.begin(); it != dict->elements.end(); ++it)
if (it->first == args[1])
{
dict->elements.erase(it);
hasValue = true;
break;
}
if (!hasValue)
Logger::Error(relatedToken, TEXT("[Native function 'erase']:No corresponding index in dict."));
}
else if (IS_STR_VALUE(args[0]))
{
auto &string = TO_STR_VALUE(args[0])->value;
if (!IS_INT_VALUE(args[1]))
Logger::Error(relatedToken, TEXT("[Native function 'erase']:Arg1 must be integer type while insert to a array"));
int64_t iIndex = TO_INT_VALUE(args[1]);
if (iIndex < 0 || iIndex >= (int64_t)string.size())
Logger::Error(relatedToken, TEXT("[Native function 'erase']:Index out of array's range"));
string.erase(string.begin() + iIndex);
}
else
Logger::Error(relatedToken, TEXT("[Native function 'erase']:Expect a array,dict ot string argument."));
result = args[0];
return true;
});
const auto AddressOfFunction = new NativeFunctionObject([](Value *args, uint32_t argCount, const Token *relatedToken, Value &result) -> bool
{
if (args == nullptr || argCount != 1)
Logger::Error(relatedToken, TEXT("[Native function 'addressof']:Expect 1 arguments."));
if (!IS_OBJECT_VALUE(args[0]))
Logger::Error(relatedToken, TEXT("[Native function 'addressof']:The arg0 is a value,only object has address."));
result = new StrObject(PointerAddressToString(args[0].object));
return true;
});
const auto ClockFunction = new NativeFunctionObject([](Value *args, uint32_t argCount, const Token *relatedToken, Value &result) -> bool
{
result = Value((double)clock() / CLOCKS_PER_SEC);
return true;
});
auto ioClass = new ClassObject(TEXT("io"));
auto dsClass = new ClassObject(TEXT("ds"));
auto memClass = new ClassObject(TEXT("mem"));
auto timeClass = new ClassObject(TEXT("time"));
ioClass->members[TEXT("print")] = new NativeFunctionObject(PRINT_LAMBDA(Logger::Print));
ioClass->members[TEXT("println")] = new NativeFunctionObject(PRINT_LAMBDA(Logger::Println));
dsClass->members[TEXT("sizeof")] = SizeOfFunction;
dsClass->members[TEXT("insert")] = InsertFunction;
dsClass->members[TEXT("erase")] = EraseFunction;
memClass->members[TEXT("addressof")] = AddressOfFunction;
timeClass->members[TEXT("clock")] = ClockFunction;
mLibraries.emplace_back(ioClass);
mLibraries.emplace_back(dsClass);
mLibraries.emplace_back(memClass);
mLibraries.emplace_back(timeClass);
}
}