Browse Source

jsGetObject() refactor, jsParseTokens return implementation, parseArray() actually call itself for arrays in arrays, jsGetArray()

master
Odilitime 2 years ago
parent
commit
9a75a4aafa
1 changed files with 41 additions and 11 deletions
  1. 41
    11
      src/parsers/scripting/javascript/JSParser.cpp

+ 41
- 11
src/parsers/scripting/javascript/JSParser.cpp View File

@@ -756,7 +756,7 @@ js_internal_storage *doExpression(js_function &rootScope, std::string token) {
stack = jb;
state = 0;
} else if (state == 5) {
/*
js_function *objectScope = new js_function;
js_object *newObject = new js_object;
//doAssignment(*objectScope, it);
@@ -769,6 +769,9 @@ js_internal_storage *doExpression(js_function &rootScope, std::string token) {
}
//std::cout << "JSON object done" << std::endl;
stack = newObject;
*/
stack = jsGetObject(rootScope, it);
std::cout << "doExpression getObject got [" << stack << "]\n";
state = 0;
} else {
std::cout << "doExpression unknown state[" << std::to_string(state) << "]\n";
@@ -1280,6 +1283,17 @@ js_function *makeFunctionFromString(const std::string body, const std::string pr
return func;
}

js_array *jsGetArray(js_function &rootScope, std::string token) {
js_function *objectScope = new js_function;
js_array *jsArr = new js_array;
parseJSON(*objectScope, token);
for(auto it2 : objectScope->locals.value) {
jsArr->value.push_back(*it2.second);
}
return jsArr;
}


void parseArray(js_function &rootScope, std::string token) {
std::vector<std::string> json_keys;
std::vector<std::string> json_values;
@@ -1379,10 +1393,13 @@ void parseArray(js_function &rootScope, std::string token) {
if (!parenLevel) {
std::string JSON = token.substr(parenStart + 1, cursor - parenStart);
std::cout << "PA Need to deJSON [" << JSON << "]" << std::endl;
/*
// well we can get a scope back
js_function *objectScope = new js_function;
parseJSON(*objectScope, JSON);
js_object *newObject = new js_object;
*/
js_object *newObject = jsGetObject(rootScope, JSON);
rootScope.locals.value[json_keys.back()] = newObject;
last = cursor + 1;
state = 0;
@@ -1398,11 +1415,11 @@ void parseArray(js_function &rootScope, std::string token) {
case ']':
parenLevel--;
if (!parenLevel) {
std::string JSON = token.substr(parenStart + 1, cursor - parenStart);
std::cout << "Need to deArray [" << JSON << "]" << std::endl;
std::string arrStr = token.substr(parenStart + 1, cursor - parenStart);
std::cout << "Need to deArray [" << arrStr << "]" << std::endl;
// well we can get a scope back
js_function *objectScope = new js_function;
parseJSON(*objectScope, JSON);
parseArray(*objectScope, arrStr);
js_object *newObject = new js_object;
rootScope.locals.value[json_keys.back()] = newObject;
last = cursor + 1;
@@ -1446,7 +1463,18 @@ void parseArray(js_function &rootScope, std::string token) {
}
}

//
js_object *jsGetObject(js_function &rootScope, std::string token) {
js_function *objectScope = new js_function;
js_object *jsonObj = new js_object;
parseJSON(*objectScope, token);
for(auto it2 : objectScope->locals.value) {
jsonObj->value[it2.first] = it2.second;
}
return jsonObj;
}

// we still need rootScope to read any vars from
// FIXME: take js_object and start
void parseJSON(js_function &rootScope, std::string token) {
std::vector<std::string> json_keys;
std::vector<std::string> json_values;
@@ -1458,7 +1486,6 @@ void parseJSON(js_function &rootScope, std::string token) {
size_t parenLevel = 0;
unsigned char keyState = 0;
unsigned char valueState = 0;
js_function *func = nullptr;
std::cout << "parseJSON start[" << token << "]\n";
for (cursor = 0; cursor < token.length(); cursor++) {
std::cout << "parseJSON step cursor[" << cursor << "/" << token.length() << "] char[" << token[cursor] << "] keyState[" << std::to_string(keyState) << "] valueState[" << std::to_string(valueState) << "]\n";
@@ -1602,9 +1629,12 @@ void parseJSON(js_function &rootScope, std::string token) {
std::string JSON = token.substr(parenStart + 1, cursor - parenStart);
//std::cout << "parsingJSON - PJ Need to deJSON [" << JSON << "]" << std::endl;
// well we can get a scope back
/*
js_function *objectScope = new js_function;
parseJSON(*objectScope, JSON);
js_object *newObject = new js_object;
*/
js_object *newObject = jsGetObject(rootScope, JSON);
assignfile << "JSON." << json_keys.back() << "=" << "_NTRJSON" << "\n";
rootScope.locals.value[json_keys.back()] = newObject;
last = cursor + 1;
@@ -2149,6 +2179,8 @@ bool doAssignment(js_function &rootScope, std::string token) {
}
}
} else if (state == 2) {
js_object *newObject = jsGetObject(rootScope, it);
/*
js_function *objectScope = new js_function;
js_object *newObject = new js_object;
//doAssignment(*objectScope, it);
@@ -2159,6 +2191,7 @@ bool doAssignment(js_function &rootScope, std::string token) {
//std::cout << "[" << it2.first << "=" << it2.second << "]" << std::endl;
newObject->value[it2.first] = it2.second;
}
*/
//std::cout << "JSON object done" << std::endl;
std::string tLeft = trim(left);
assignfile << tLeft << "=" << "_NTRJSON" << "\n";
@@ -2645,11 +2678,8 @@ js_internal_storage *jsParseTokens(const std::vector<std::string> &tokens, js_fu
scope->locals.value[funcName] = newFunc;
execfile << "function declaration [" << funcName << "](" << prototype << ") tokens[" << funcTokens.size() << "]\n";
} else if (ttoken.substr(0, 6)=="return") {
execfile << "return not implemented\n";
// js expression here
// probably don't need to do anything here atm
std::cout << "HALT return not implemented" << std::endl;
return nullptr;
std::string afterReturn = ttoken.substr(7);
return doExpression(*scope, afterReturn);
} else if (ttoken.find("=") != std::string::npos) {
execfile << "assignment - " << it << "\n";
// has = so it's an expression

Loading…
Cancel
Save