Browse Source

support , in returns, statements after if, and try to make doExpression handle creating new vars

Odilitime 2 weeks ago
parent
commit
424d7ff777
1 changed files with 57 additions and 10 deletions
  1. 57
    10
      src/parsers/scripting/javascript/JSParser.cpp

+ 57
- 10
src/parsers/scripting/javascript/JSParser.cpp View File

@@ -127,7 +127,7 @@ std::vector<std::string> jsGetTokens(const std::string &source, const size_t sta
127 127
     size_t parenStart = 0;
128 128
     size_t functionStart = 0;
129 129
     for (cursor = start; cursor < source.length(); cursor++) {
130
-        //std::cout << "jsGetTokens step [" << cursor << "] char[" << source[cursor] << "] state[" << std::to_string(state) << "]\n";
130
+        //std::cout << "jsGetTokens step [" << cursor << "] char[" << source[cursor] << "] state[" << std::to_string(state) << "] scopeLevel[" << scopeLevel << "]\n";
131 131
         if (state == 0) {
132 132
             if (source[cursor] == '{') {
133 133
                 state = 1; // JSON
@@ -169,6 +169,11 @@ std::vector<std::string> jsGetTokens(const std::string &source, const size_t sta
169 169
                 //std::cout << "Entering function: " << cursor << std::endl;
170 170
                 state = 6;
171 171
                 functionStart = cursor;
172
+            } else if (source[cursor] == 'r' && source.length() > cursor + 5 && source[cursor + 1] == 'e'
173
+                       && source[cursor + 2] == 't' && source[cursor + 3] == 'u' && source[cursor + 4] == 'r'
174
+                       && source[cursor + 5] == 'n') {
175
+              // return state, have to ignore , until ; or new line
176
+              state = 9;
172 177
             }
173 178
         } else if (state == 1) {
174 179
             // inside a scope (JSON)
@@ -242,23 +247,25 @@ std::vector<std::string> jsGetTokens(const std::string &source, const size_t sta
242 247
         }
243 248
 
244 249
         // state 0 or 7, ignore states 1-6
245
-        if ((state == 0 || state == 7) && !scopeLevel) {
246
-            if (source[cursor] == '\n' || source[cursor] == ';' || endIt || (source[cursor] == ',' && state != 7)) {
250
+        if ((state == 0 || state == 7 || state == 9) && !scopeLevel) {
251
+            // , if state (not 7) or (not 9)
252
+            if (source[cursor] == '\n' || source[cursor] == ';' || endIt || (source[cursor] == ',' && state == 0)) {
247 253
                 // FIXME: ; in for loops
248 254
                 std::string token = source.substr(last ? last + 1 : last, last ? (cursor - last - 1) : cursor );
249 255
                 if (source[cursor] == '}') {
250 256
                     token += '}';
251 257
                 }
252 258
                 // scopeLevel[" << scopeLevel << "]"
253
-                //std::cout << "got token [" << token << "] ending[" << source[cursor] << "] endIt[" << endIt << "]" << std::endl;
259
+                std::cout << "got token [" << token << "] ending[" << source[cursor] << "] endIt[" << endIt << "]" << std::endl;
254 260
                 if (token.length()<3) {
255 261
                     //std::cout << "token too short [" << token << "]" << std::endl;
256 262
                 } else {
257 263
                     tokens.push_back(token);
258 264
                 }
259 265
                 last = cursor;
260
-
261
-                if (state == 7) { // allow var constructs to end normally and take us out of var construct
266
+              
267
+                // state 7 or 9
268
+                if (state != 0) { // allow var constructs to end normally and take us out of var construct
262 269
                     state = 0; // reset state
263 270
                 }
264 271
             }
@@ -266,7 +273,8 @@ std::vector<std::string> jsGetTokens(const std::string &source, const size_t sta
266 273
     }
267 274
     std::string token = source.substr(last ? last + 1 : last, last ? (cursor - last - 1) : cursor );
268 275
     //&& !token.length() // all look like complete valid tokens
269
-    if (!state ) {
276
+    // state 9 (return), perfect ok to run to the end
277
+    if (!state || state == 9) {
270 278
         if (token.length()) {
271 279
             // we can't just be discarding stuff
272 280
             tokens.push_back(token);
@@ -734,6 +742,36 @@ js_internal_storage *doExpression(js_function &rootScope, std::string token) {
734 742
                     stack = scopeTest;
735 743
                 } else {
736 744
                     if (!deref) {
745
+                        // well if it's prefixed with var, then we're creating var
746
+                        size_t hasVarPos = trimmedToken.find("var ");
747
+                        if (hasVarPos != std::string::npos) {
748
+                          std::string remainingExpr = trimmedToken.substr(hasVarPos + 4);
749
+                          std::cout << "variable name [" << remainingExpr << "] it[" << it << "]" << std::endl;
750
+                          
751
+                          // we're just initialling a blank variable
752
+                          js_internal_storage **storage = getObjectKeyPointer(remainingExpr, &rootScope);
753
+                          if (storage != nullptr) {
754
+                            // actually already exists, so stomp it
755
+                            *storage = new js_internal_storage; // FIXME;
756
+                          } else {
757
+                            // new variable
758
+                            rootScope.locals.value[remainingExpr] = new js_internal_storage;
759
+                          }
760
+                          stack = rootScope.locals.value[remainingExpr];
761
+                          /*
762
+                          rootScope.locals.value[remainingExpr] = new js_internal_storage;
763
+                          js_internal_storage *exprRes = doExpression(rootScope, remainingExpr);
764
+                          std::cout << "Expression tyoe[" << typeOfStorage(exprRes) << "]" << std::endl;
765
+                          if (typeOfStorage(exprRes) == "null")
766
+                          {
767
+                            stack = new js_internal_storage;
768
+                          } else {
769
+                            stack = exprRes;
770
+                          }
771
+                          */
772
+                          std::cout << "Done with creating new variable" << std::endl;
773
+                          continue;
774
+                        }
737 775
                         jsDisplayScope(&rootScope, 0);
738 776
                         std::cout << "is Not a Var\n";
739 777
                     } else {
@@ -2642,7 +2680,6 @@ js_internal_storage *jsParseTokens(const std::vector<std::string> &tokens, js_fu
2642 2680
         if (ttoken == "") continue; // skip empty tokens
2643 2681
         std::cout << "parse token[" << it << "]" << std::endl;
2644 2682
         if (ttoken.substr(0, 2)=="if") {
2645
-            execfile << "if not implemented\n";
2646 2683
             std::string ifStr = it.substr(2);
2647 2684
             std::cout << "ifStr1[" << ifStr << "]" << std::endl;
2648 2685
             // find (
@@ -2673,11 +2710,21 @@ js_internal_storage *jsParseTokens(const std::vector<std::string> &tokens, js_fu
2673 2710
                 // skip all elses
2674 2711
             } else {
2675 2712
                 // handle else
2676
-                std::cout << "looking for else [" << ttoken.substr(end) << "]\n";
2677
-                start = ttoken.substr(end).find("else");
2713
+                std::string lastToken = ttoken.substr(end);
2714
+                std::cout << "looking for else [" << lastToken << "]\n";
2715
+                if (lastToken[0] == '}') {
2716
+                  lastToken = ttoken.substr(end + 1);
2717
+                  std::cout << "Adjusted elseSearch past } [" << lastToken << "]\n";
2718
+                }
2719
+                start = lastToken.find("else");
2678 2720
                 if (start != std::string::npos) {
2721
+                    execfile << "if else not implemented\n";
2679 2722
                     std::cout << "false condition ELSE not implemented" << std::endl;
2723
+                    // would need to extract the else clause form lastToene
2680 2724
                 }
2725
+                // now execute remaining code
2726
+                // FIXME: there should be no expressions after an if statement
2727
+                js_internal_storage *lastRes = doExpression(*scope, lastToken);
2681 2728
             }
2682 2729
             //std::cout << "HALT if not implemented" << std::endl;
2683 2730
             //return nullptr;