Explorar el Código

support complex import

BLUELOVETH hace 2 años
padre
commit
8b47a2001f

+ 1 - 1
include/pocketpy/lexer.h

@@ -20,7 +20,7 @@ constexpr const char* kTokens[] = {
     "<<", "<<=", ">>", ">>=",
     /*****************************************/
     ".", ",", ":", ";", "#", "(", ")", "[", "]", "{", "}",
-    "**", "=", ">", "<", "...", "->", "?", "@", "==", "!=", ">=", "<=",
+    "**", "=", ">", "<", "..", "...", "->", "?", "@", "==", "!=", ">=", "<=",
     "++", "--", "~",
     /** SPEC_BEGIN **/
     "$goto", "$label",

+ 2 - 1
src/compiler.cpp

@@ -502,7 +502,8 @@ __SUBSCR_END:
 
         while(true){
             switch(curr().type){
-                case TK("."): dots++; break;
+                case TK("."): dots+=1; break;
+                case TK(".."): dots+=2; break;
                 case TK("..."): dots+=3; break;
                 default: goto __EAT_DOTS_END;
             }

+ 1 - 1
src/lexer.cpp

@@ -345,7 +345,7 @@ static bool is_unicode_Lo_char(uint32_t c) {
                         if(matchchar('.')) {
                             add_token(TK("..."));
                         } else {
-                            SyntaxError("invalid token '..'");
+                            add_token(TK(".."));
                         }
                     } else {
                         add_token(TK("."));

+ 10 - 1
tests/30_import.py

@@ -5,4 +5,13 @@ except ImportError:
 
 import test1
 
-assert test1.add(1, 2) == 13
+assert test1.add(1, 2) == 13
+
+from test2.a.g import get_value
+assert get_value() == '123'
+
+import test2
+assert test2.a.g.get_value() == '123'
+
+from test2.utils import get_value_2
+assert get_value_2() == '123'

+ 1 - 1
tests/test2/__init__.py

@@ -1 +1 @@
-from ._a import D
+from .a import D

+ 0 - 1
tests/test2/_a.py

@@ -1 +0,0 @@
-from ._b import D

+ 1 - 0
tests/test2/a/__init__.py

@@ -0,0 +1 @@
+from ..b import D

+ 4 - 0
tests/test2/a/g/__init__.py

@@ -0,0 +1,4 @@
+from ...utils import r
+
+def get_value():
+    return r.value

+ 1 - 1
tests/test2/_b.py → tests/test2/b.py

@@ -1,7 +1,7 @@
 D = 10
 
 try:
-    import test
+    import abc  # does not exist
     exit(1)
 except ImportError:
     pass

+ 4 - 0
tests/test2/utils/__init__.py

@@ -0,0 +1,4 @@
+from .r import value
+
+def get_value_2():
+    return value

+ 7 - 0
tests/test2/utils/r.py

@@ -0,0 +1,7 @@
+value = '123'
+
+try:
+    from test2.a import g
+except ImportError:
+    # circular import
+    pass