in GaiaXAnalyze/GXAnalyzeCore/GXAnalyze.cpp [1029:1203]
long GXAnalyze::getValue(string expression, void *source) {
char *input;
int inputLength = expression.length();
input = new char[inputLength + 2];
vector<GXATSNode> array;
vector<GXATSNode> arrayNum;
array.reserve(128);
arrayNum.reserve(128);
string result = "#";
strcpy(input, expression.c_str());
int p = 0;
int synCode;
int countNode = 0;
while (p < strlen(input)) {
if (input[p] == ' ') {
p++;
} else {
GXATSNode token = scanner(synCode, p, input, this);
if (isTerminalWord(token.detail)) {
result = result + terminal[token.detail];
} else {
result = result + nonTerminal[token.detail];
}
GXATSNode tokenNum;
if (token.token == "value" || token.token == "data") {
long res = this->getSourceValue(token.name, source);
if (res == 0) {
tokenNum.name = "null";
tokenNum.token = "null";
} else {
GXValue *gxv = (GXValue *) res;
if (gxv->tag == GX_TAG_FLOAT) {
tokenNum.name = to_string(gxv->float64);
if (tokenNum.name.find('.') != -1) {
tokenNum.name = regex_replace(tokenNum.name, regex("0+?$"),
""); // 除了捕捉到的组以外,其他的东西均舍弃
tokenNum.name = regex_replace(tokenNum.name, regex("[.]$"),
""); // 除了捕捉到的组以外,其他的东西均舍弃
}
tokenNum.token = "num";
} else if (gxv->tag == GX_TAG_LONG) {
tokenNum.name = to_string(gxv->intNum);
tokenNum.token = "long";
} else if (gxv->tag == GX_TAG_STRING) {
tokenNum.name = gxv->str;
tokenNum.token = "string";
} else if (gxv->tag == GX_TAG_BOOL) {
if (gxv->int32 == 1) {
tokenNum.name = "true";
} else {
tokenNum.name = "false";
}
tokenNum.token = "bool";
} else if (gxv->tag == GX_TAG_ARRAY) {
tokenNum.name = to_string((long) (gxv->ptr));
tokenNum.token = "array";
} else if (gxv->tag == GX_TAG_MAP) {
tokenNum.name = to_string((long) (gxv->ptr));
tokenNum.token = "map";
} else if (gxv->tag == GX_TAG_NULL) {
tokenNum.name = "null";
tokenNum.token = "null";
} else {
tokenNum.name = "null";
tokenNum.token = "null";
}
if (gxv->tag == GX_TAG_STRING && gxv->str != NULL) {
delete[] gxv->str;
gxv->str = NULL;
}
delete gxv;
}
} else if (token.token == "function") {
long res = this->getFunctionValue(token.name, nullptr, 0, "");
if (res == 0) {
tokenNum.name = "null";
tokenNum.token = "null";
} else {
GXValue *gxv = (GXValue *) res;
if (gxv->tag == GX_TAG_FLOAT) {
tokenNum.name = to_string(gxv->float64);
if (tokenNum.name.find('.') != -1) {
tokenNum.name = regex_replace(tokenNum.name, regex("0+?$"),
""); // 除了捕捉到的组以外,其他的东西均舍弃
tokenNum.name = regex_replace(tokenNum.name, regex("[.]$"),
""); // 除了捕捉到的组以外,其他的东西均舍弃
}
tokenNum.token = "num";
} else if (gxv->tag == GX_TAG_LONG) {
tokenNum.name = to_string(gxv->intNum);
tokenNum.token = "long";
} else if (gxv->tag == GX_TAG_STRING) {
tokenNum.name = gxv->str;
tokenNum.token = "string";
} else if (gxv->tag == GX_TAG_BOOL) {
if (gxv->int32 == 1) {
tokenNum.name = "true";
} else {
tokenNum.name = "false";
}
tokenNum.token = "bool";
} else if (gxv->tag == GX_TAG_ARRAY) {
tokenNum.name = to_string((long) (gxv->ptr));
tokenNum.token = "array";
} else if (gxv->tag == GX_TAG_MAP) {
tokenNum.name = to_string((long) (gxv->ptr));
tokenNum.token = "map";
} else if (gxv->tag == GX_TAG_NULL) {
tokenNum.name = "null";
tokenNum.token = "null";
} else {
tokenNum.name = "null";
tokenNum.token = "null";
}
if (gxv->tag == GX_TAG_STRING && gxv->str != NULL) {
delete[] gxv->str;
gxv->str = NULL;
}
delete gxv;
}
} else {
tokenNum = token;
}
if (token.token != "op") {
token.count = countNode;
tokenNum.count = countNode;
countNode++;
arrayNum.push_back(tokenNum);
}
array.push_back(token);
}
}
//释放s的内存空间
delete[]input;
result = result + "#";
long Res;
mtx.lock();
unordered_map<string, string>::iterator iter = cache.find(result);
mtx.unlock();
if (iter != cache.end()) {
if (iter->second == "(0)") {
GXATSNode res = arrayNum[0];
GXValue *pointer;
if (res.token == "string") {
pointer = new GXValue(GX_TAG_STRING, res.name);
} else if (res.token == "bool") {
if (res.name == "true") {
pointer = new GXValue(GX_TAG_BOOL, 1);
} else {
pointer = new GXValue(GX_TAG_BOOL, 0);
}
} else if (res.token == "num") {
pointer = new GXValue(GX_TAG_FLOAT, (float) atof(res.name.c_str()));
} else if (res.token == "long") {
pointer = new GXValue(GX_TAG_LONG, (int64_t) atoll(res.name.c_str()));
} else if (res.token == "map") {
pointer = new GXValue(GX_TAG_MAP, (void *) atol(res.name.c_str()));
} else if (res.token == "array") {
pointer = new GXValue(GX_TAG_ARRAY, (void *) atol(res.name.c_str()));
} else if (res.token == "null") {
pointer = new GXValue(GX_TAG_NULL, 1);
} else {
pointer = new GXValue(GX_TAG_NULL, 1);
}
Res = (long) pointer;
} else {
Res = calculateCache(iter->second, arrayNum, this, source);
}
} else {
Res = check(result, array, this, source, expression);
}
arrayNum.clear();
array.clear();
return Res;
}