diff --git a/src/parsers/scripting/javascript/JSParser.cpp b/src/parsers/scripting/javascript/JSParser.cpp index 318b9e0..f0d8ead 100644 --- a/src/parsers/scripting/javascript/JSParser.cpp +++ b/src/parsers/scripting/javascript/JSParser.cpp @@ -127,7 +127,7 @@ std::vector jsGetTokens(const std::string &source, const size_t sta size_t parenStart = 0; size_t functionStart = 0; for (cursor = start; cursor < source.length(); cursor++) { - //std::cout << "jsGetTokens step [" << cursor << "] char[" << source[cursor] << "] state[" << std::to_string(state) << "]\n"; + //std::cout << "jsGetTokens step [" << cursor << "] char[" << source[cursor] << "] state[" << std::to_string(state) << "] scopeLevel[" << scopeLevel << "]\n"; if (state == 0) { if (source[cursor] == '{') { state = 1; // JSON @@ -169,6 +169,11 @@ std::vector jsGetTokens(const std::string &source, const size_t sta //std::cout << "Entering function: " << cursor << std::endl; state = 6; functionStart = cursor; + } else if (source[cursor] == 'r' && source.length() > cursor + 5 && source[cursor + 1] == 'e' + && source[cursor + 2] == 't' && source[cursor + 3] == 'u' && source[cursor + 4] == 'r' + && source[cursor + 5] == 'n') { + // return state, have to ignore , until ; or new line + state = 9; } } else if (state == 1) { // inside a scope (JSON) @@ -242,23 +247,25 @@ std::vector jsGetTokens(const std::string &source, const size_t sta } // state 0 or 7, ignore states 1-6 - if ((state == 0 || state == 7) && !scopeLevel) { - if (source[cursor] == '\n' || source[cursor] == ';' || endIt || (source[cursor] == ',' && state != 7)) { + if ((state == 0 || state == 7 || state == 9) && !scopeLevel) { + // , if state (not 7) or (not 9) + if (source[cursor] == '\n' || source[cursor] == ';' || endIt || (source[cursor] == ',' && state == 0)) { // FIXME: ; in for loops std::string token = source.substr(last ? last + 1 : last, last ? (cursor - last - 1) : cursor ); if (source[cursor] == '}') { token += '}'; } // scopeLevel[" << scopeLevel << "]" - //std::cout << "got token [" << token << "] ending[" << source[cursor] << "] endIt[" << endIt << "]" << std::endl; + std::cout << "got token [" << token << "] ending[" << source[cursor] << "] endIt[" << endIt << "]" << std::endl; if (token.length()<3) { //std::cout << "token too short [" << token << "]" << std::endl; } else { tokens.push_back(token); } last = cursor; - - if (state == 7) { // allow var constructs to end normally and take us out of var construct + + // state 7 or 9 + if (state != 0) { // allow var constructs to end normally and take us out of var construct state = 0; // reset state } } @@ -266,7 +273,8 @@ std::vector jsGetTokens(const std::string &source, const size_t sta } std::string token = source.substr(last ? last + 1 : last, last ? (cursor - last - 1) : cursor ); //&& !token.length() // all look like complete valid tokens - if (!state ) { + // state 9 (return), perfect ok to run to the end + if (!state || state == 9) { if (token.length()) { // we can't just be discarding stuff tokens.push_back(token); @@ -734,6 +742,36 @@ js_internal_storage *doExpression(js_function &rootScope, std::string token) { stack = scopeTest; } else { if (!deref) { + // well if it's prefixed with var, then we're creating var + size_t hasVarPos = trimmedToken.find("var "); + if (hasVarPos != std::string::npos) { + std::string remainingExpr = trimmedToken.substr(hasVarPos + 4); + std::cout << "variable name [" << remainingExpr << "] it[" << it << "]" << std::endl; + + // we're just initialling a blank variable + js_internal_storage **storage = getObjectKeyPointer(remainingExpr, &rootScope); + if (storage != nullptr) { + // actually already exists, so stomp it + *storage = new js_internal_storage; // FIXME; + } else { + // new variable + rootScope.locals.value[remainingExpr] = new js_internal_storage; + } + stack = rootScope.locals.value[remainingExpr]; + /* + rootScope.locals.value[remainingExpr] = new js_internal_storage; + js_internal_storage *exprRes = doExpression(rootScope, remainingExpr); + std::cout << "Expression tyoe[" << typeOfStorage(exprRes) << "]" << std::endl; + if (typeOfStorage(exprRes) == "null") + { + stack = new js_internal_storage; + } else { + stack = exprRes; + } + */ + std::cout << "Done with creating new variable" << std::endl; + continue; + } jsDisplayScope(&rootScope, 0); std::cout << "is Not a Var\n"; } else { @@ -2642,7 +2680,6 @@ js_internal_storage *jsParseTokens(const std::vector &tokens, js_fu if (ttoken == "") continue; // skip empty tokens std::cout << "parse token[" << it << "]" << std::endl; if (ttoken.substr(0, 2)=="if") { - execfile << "if not implemented\n"; std::string ifStr = it.substr(2); std::cout << "ifStr1[" << ifStr << "]" << std::endl; // find ( @@ -2673,11 +2710,21 @@ js_internal_storage *jsParseTokens(const std::vector &tokens, js_fu // skip all elses } else { // handle else - std::cout << "looking for else [" << ttoken.substr(end) << "]\n"; - start = ttoken.substr(end).find("else"); + std::string lastToken = ttoken.substr(end); + std::cout << "looking for else [" << lastToken << "]\n"; + if (lastToken[0] == '}') { + lastToken = ttoken.substr(end + 1); + std::cout << "Adjusted elseSearch past } [" << lastToken << "]\n"; + } + start = lastToken.find("else"); if (start != std::string::npos) { + execfile << "if else not implemented\n"; std::cout << "false condition ELSE not implemented" << std::endl; + // would need to extract the else clause form lastToene } + // now execute remaining code + // FIXME: there should be no expressions after an if statement + js_internal_storage *lastRes = doExpression(*scope, lastToken); } //std::cout << "HALT if not implemented" << std::endl; //return nullptr;