-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMareConverter.h
395 lines (346 loc) · 11.1 KB
/
MareConverter.h
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#ifndef MARE_CONVERTER_H_INCLUDED
#define MARE_CONVERTER_H_INCLUDED
#include "MareSet.h"
#include "MareMemory.h"
#include "VarObj.h"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "Blob.h"
namespace mare_vm {
using namespace std;
static inline vector<string> split(string const& str, char const delimiter) {
vector<string> internal;
stringstream ss(str);
string temp;
while (getline(ss, temp, delimiter)) {
internal.push_back(temp);
}
return internal;
}
/** Converting */
static inline vector<string> fromBlob(Blob const& in_){
std::string str_array( in_.begin(), in_.end() );
return split(str_array, '\n');
}
/** Converting from sfInterCode to intenalCode */
static bool fromBlob2InterCode(Blob const& in_, vector<char*>& internal) {
char* buf_p;
char* buf_no_p;
int line_size, dataIndex, lineIdx;
internal.clear();
buf_no_p = new char[2];
buf_no_p[0] = 0x01; buf_no_p[1] = '\0';
line_size = lineIdx = 0;
size_t length = in_.size();
for(size_t strIndex = 0; strIndex < length; dataIndex++) {
unsigned char tmpValue = in_[strIndex++];
if (line_size == 0){
dataIndex = 0;
if (tmpValue == 0) throw tecCONTRACT_PREPROCESS_ERROR;
if (tmpValue == 1){
internal.push_back(buf_no_p);
strIndex++;
continue;
}
line_size = tmpValue;
buf_p = new char[line_size+1];
buf_p[dataIndex] = tmpValue;
}
else if (line_size == 1){
buf_p[dataIndex] = '\0'; // char array에 end 추가
internal.push_back(buf_p);
line_size--;
}
else {
buf_p[dataIndex] = tmpValue;
line_size--;
}
}
return true;
}
/** Converting from sfSymTbls to Gtable, Ltable */
static bool fromBlob2SymTbl(Blob const& in_, vector<SymTbl>& global, vector<SymTbl>& local) {
global.clear(); local.clear();
vector<string> jStrs = fromBlob(in_);
int max = jStrs.size();
int cnt = 0;
SymTbl tmpTb;
while (cnt < max) {
string strtype = jStrs[cnt++];
if (strtype.length() < 2) throw tecCONTRACT_PREPROCESS_ERROR;
unsigned char typek = strtype.at(0);
int nums = atoi(strtype.substr(2).c_str());
for(int i=0; i<nums; i++){
string str = jStrs[cnt++];
// check str length
vector<string> params = split(str, ' ');
tmpTb.clear();
tmpTb.symKind = (SymKind)atoi(params[0].c_str());
tmpTb.dtTyp = (DtType)atoi(params[1].c_str());
tmpTb.adrs = atoi(params[2].c_str());
tmpTb.aryLen = atoi(params[3].c_str());
tmpTb.args = atoi(params[4].c_str());
tmpTb.frame = atoi(params[5].c_str());
tmpTb.name = params[6];
if (typek == Gvar)
global.push_back(tmpTb);
else if (typek == Lvar)
local.push_back(tmpTb);
else {
throw tecCONTRACT_PREPROCESS_ERROR;
}
}
}
return false;
}
static bool fromBlob2StructTbl(Blob const& in_, vector<ItemTbl>& global, map<string, int>& local) {
global.clear(); local.clear();
vector<string> jStrs = fromBlob(in_);
int max = jStrs.size();
int cnt = 0;
while (cnt < max) {
string strtype = jStrs[cnt++];
if (strtype.length() < 2) throw tecCONTRACT_PREPROCESS_ERROR;
unsigned char typek = strtype.at(0);
int nums = atoi(strtype.substr(2).c_str());
if (typek == StructItem) {
ItemTbl tmpTb;
for(int i=0; i<nums; i++){
string str = jStrs[cnt++];
// check str length
vector<string> params = split(str, ' ');
tmpTb.clear();
tmpTb.dtTyp = (DtType)atoi(params[0].c_str());
tmpTb.symId = atoi(params[1].c_str());
tmpTb.offset = atoi(params[2].c_str());
tmpTb.name = params[3].c_str();
tmpTb.initTyp = (DtType)atoi(params[4].c_str());
if (tmpTb.initTyp == NON_T) {}
else if (tmpTb.initTyp == STR_T)
tmpTb.initStr = params[5];
else
tmpTb.initVal = atoi(params[5].c_str());
global.push_back(tmpTb);
}
}
else if (typek == Other) {
for(int i=0; i<nums; i++){
string str = jStrs[cnt++];
// check str length
vector<string> params = split(str, ' ');
local.insert(make_pair(params[0], atoi(params[1].c_str())));
}
}
else {
throw tecCONTRACT_PREPROCESS_ERROR;
}
}
return false;
}
static inline VarObj toVarObj(string& valStr) {
DtType cc = (DtType)(valStr.at(0) - 48); // char to int
if (cc == STR_T) {
if (valStr.length() == 2) {
VarObj obj; obj.init(STR_T);
return obj;
}
else {
VarObj obj(cc, valStr.substr(2));
return obj;
}
}
vector<string> strs = split(valStr, ' ');
if (cc == NON_T || strs.size() == 1) {
VarObj obj; obj.init(NON_T);
return obj;
}
else if (cc == INT_T || cc == DBL_T || cc == DATETIME_T){
double dbl = atof(strs[1].c_str());
VarObj obj(cc, dbl);
return obj;
}
VarObj obj(cc, strs[1]);
return obj;
}
static inline void setMemory(int idx, string& valStr, MareMemory& mem) {
if (mem.size() < idx)
mem.resize(idx);
mem.set(idx, toVarObj(valStr));
}
/** Converting from sfStorages to DynamicMem */
static bool fromBlob2Memory(Blob const& private_, vector<SymTbl> const& global, MareMemory& mem) {
vector<string> jStrsPri = fromBlob(private_);
int max = jStrsPri.size();
int sz = global.size();
mem.resize(max);
string valStr;
int n = 0;
for (int k=0; k<sz; k++) {
if (global[k].symKind != funcId) { /* 함수가 아닌 경우 */
if (global[k].args == 0) { /* 변수인 경우 */
valStr = jStrsPri[n++];
setMemory(global[k].adrs, valStr, mem);
}
else { /* 배열형 타입인 경우 */
for (int j=0; j<global[k].args; j++) {
valStr = jStrsPri[n++];
setMemory(global[k].adrs + j, valStr, mem);
}
}
}
}
return true;
}
/** Converting from sfLiterals to nbrLITERAL, strLITERAL */
static bool fromBlob2Literal(Blob const& in_, vector<double>& dbls_, vector<string>& strs_) {
dbls_.clear(); strs_.clear();
vector<string> jStrs = fromBlob(in_);
int max = jStrs.size();
int cnt = 0;
while (cnt < max) {
string str = jStrs[cnt++];
unsigned char typek = str.at(0);
int nums = atoi(str.substr(2).c_str());
for(int i=0; i<nums; i++){
if (typek == VarStr)
strs_.push_back(jStrs[cnt++]);
else// if (typek == VarDbl)
dbls_.push_back(atof(jStrs[cnt++].c_str()));
//else throw "error:from Blob to Literal";
}
}
return true;
}
/*** To Blob ***/
static inline void appendBlob(string const& in_, Blob& obj) {
for(unsigned char cc : in_){
obj.push_back(cc);
}
obj.push_back('\n');
}
static inline void appendBlob(TknKind const& type_, int const& nums_, Blob& obj) {
obj.push_back(type_);
obj.push_back(' ');
appendBlob(to_string(nums_), obj);
}
/** Converting from intenalCode to sfInterCode */
static Blob toBlob(vector<char*> const& internalCode_){
Blob obj;
int max = internalCode_.size();
for (int i=0; i<max; i++){
char* cp = internalCode_[i];
unsigned short cnt = cp[0];
for(int idx=0; idx<cnt; idx++){
unsigned char code = cp[idx];
obj.push_back(code);
}
obj.push_back('\n');
}
return obj;
}
/** Converting from DynamicMem, Gtable to sfStorages */
static Blob toBlob(MareMemory& in_, vector<SymTbl> const& global) {
Blob outPri;
outPri.clear();
int max = global.size();
for (int i=0; i<max; i++){
SymTbl sym = global[i];
if (sym.symKind != funcId) {
if (sym.args > 1) { // 배열인 경우, 배열크기만큼 추가
for (short k=0; k<sym.args;k++){
appendBlob(in_.get(sym.adrs + k).toFullString(), outPri);
}
}
else {
appendBlob(in_.get(sym.adrs).toFullString(), outPri);
}
}
}
return outPri;
}
/** Converting from Gtable, Ltable to sfSymTbls */
static Blob toBlob(vector<SymTbl> const& global, vector<SymTbl> const& local) {
Blob obj;
int max = global.size();
if (max > 0) {
appendBlob(Gvar, max, obj);
for (int i=0; i<max; i++){
SymTbl sym = global[i];
appendBlob(sym.toFullString(), obj);
}
}
max = local.size();
if (max > 0) {
appendBlob(Lvar, max, obj);
for (int i=0; i<max; i++){
SymTbl sym = local[i];
appendBlob(sym.toFullString(), obj);
}
}
return obj;
}
/** Converting from Itable, ObjectMap to sfSymTbls */
static Blob toBlob(vector<ItemTbl> const& global, map<string, int>& local) {
Blob obj;
int max = global.size();
if (max > 0) {
appendBlob(StructItem, max, obj);
for (int i=0; i<max; i++){
ItemTbl sym = global[i];
appendBlob(sym.toFullString(), obj);
}
}
max = local.size();
if (max > 0) {
appendBlob(Other, max, obj);
map<string, int>::iterator it;
for (it=local.begin(); it!=local.end(); ++it) {
appendBlob(it->first + " " + to_string(it->second), obj);
}
}
return obj;
}
/** Converting from nbrLITERAL, strLITERAL to sfLiterals */
static Blob toBlob(vector<double> const& dbls_, vector<string> const& strs_) {
Blob obj;
int max = dbls_.size();
if (max > 0) {
string objStr;
appendBlob(VarDbl, max, obj);
for (int i=0; i<max; i++){
// 소숫점 정리
objStr = to_string_with_precision(dbls_[i]);
to_string_simple(objStr);
appendBlob(objStr, obj);
}
}
max = strs_.size();
if (max > 0) {
appendBlob(VarStr, max, obj);
for (int i=0; i<max; i++){
appendBlob(strs_[i], obj);
}
}
return obj;
}
/** Convrting from strings (funcInfos, etc.) */
static Blob toBlob(vector<string> const& strs_) {
Blob obj;
int max = strs_.size();
if (max > 0) {
for (int i=0; i<max; i++){
appendBlob(strs_[i], obj);
}
}
return obj;
}
/** Convrting from string (logs, etc.) */
static Blob toBlob(string const& in_) {
Blob obj;
appendBlob(in_, obj);
return obj;
}
}
#endif