From 8b47a2001fba4f0dc564934fe579265eedf69464 Mon Sep 17 00:00:00 2001 From: BLUELOVETH Date: Mon, 28 Aug 2023 12:43:43 +0800 Subject: [PATCH] support complex import --- include/pocketpy/lexer.h | 2 +- src/compiler.cpp | 3 ++- src/lexer.cpp | 2 +- tests/30_import.py | 11 ++++++++++- tests/test2/__init__.py | 2 +- tests/test2/_a.py | 1 - tests/test2/a/__init__.py | 1 + tests/test2/a/g/__init__.py | 4 ++++ tests/test2/{_b.py => b.py} | 2 +- tests/test2/utils/__init__.py | 4 ++++ tests/test2/utils/r.py | 7 +++++++ 11 files changed, 32 insertions(+), 7 deletions(-) delete mode 100644 tests/test2/_a.py create mode 100644 tests/test2/a/__init__.py create mode 100644 tests/test2/a/g/__init__.py rename tests/test2/{_b.py => b.py} (61%) create mode 100644 tests/test2/utils/__init__.py create mode 100644 tests/test2/utils/r.py diff --git a/include/pocketpy/lexer.h b/include/pocketpy/lexer.h index d35d44e8..77dcfb04 100644 --- a/include/pocketpy/lexer.h +++ b/include/pocketpy/lexer.h @@ -20,7 +20,7 @@ constexpr const char* kTokens[] = { "<<", "<<=", ">>", ">>=", /*****************************************/ ".", ",", ":", ";", "#", "(", ")", "[", "]", "{", "}", - "**", "=", ">", "<", "...", "->", "?", "@", "==", "!=", ">=", "<=", + "**", "=", ">", "<", "..", "...", "->", "?", "@", "==", "!=", ">=", "<=", "++", "--", "~", /** SPEC_BEGIN **/ "$goto", "$label", diff --git a/src/compiler.cpp b/src/compiler.cpp index b49b8b46..e5713d71 100644 --- a/src/compiler.cpp +++ b/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; } diff --git a/src/lexer.cpp b/src/lexer.cpp index 79c32f0d..765c4dea 100644 --- a/src/lexer.cpp +++ b/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(".")); diff --git a/tests/30_import.py b/tests/30_import.py index 501b672a..a7f6d0ff 100644 --- a/tests/30_import.py +++ b/tests/30_import.py @@ -5,4 +5,13 @@ except ImportError: import test1 -assert test1.add(1, 2) == 13 \ No newline at end of file +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' \ No newline at end of file diff --git a/tests/test2/__init__.py b/tests/test2/__init__.py index 50112d75..62efa2ab 100644 --- a/tests/test2/__init__.py +++ b/tests/test2/__init__.py @@ -1 +1 @@ -from ._a import D \ No newline at end of file +from .a import D \ No newline at end of file diff --git a/tests/test2/_a.py b/tests/test2/_a.py deleted file mode 100644 index 7ea0ad16..00000000 --- a/tests/test2/_a.py +++ /dev/null @@ -1 +0,0 @@ -from ._b import D \ No newline at end of file diff --git a/tests/test2/a/__init__.py b/tests/test2/a/__init__.py new file mode 100644 index 00000000..d0396b19 --- /dev/null +++ b/tests/test2/a/__init__.py @@ -0,0 +1 @@ +from ..b import D \ No newline at end of file diff --git a/tests/test2/a/g/__init__.py b/tests/test2/a/g/__init__.py new file mode 100644 index 00000000..ff8d2052 --- /dev/null +++ b/tests/test2/a/g/__init__.py @@ -0,0 +1,4 @@ +from ...utils import r + +def get_value(): + return r.value \ No newline at end of file diff --git a/tests/test2/_b.py b/tests/test2/b.py similarity index 61% rename from tests/test2/_b.py rename to tests/test2/b.py index 318ae8d0..681054fd 100644 --- a/tests/test2/_b.py +++ b/tests/test2/b.py @@ -1,7 +1,7 @@ D = 10 try: - import test + import abc # does not exist exit(1) except ImportError: pass \ No newline at end of file diff --git a/tests/test2/utils/__init__.py b/tests/test2/utils/__init__.py new file mode 100644 index 00000000..cfdbb785 --- /dev/null +++ b/tests/test2/utils/__init__.py @@ -0,0 +1,4 @@ +from .r import value + +def get_value_2(): + return value \ No newline at end of file diff --git a/tests/test2/utils/r.py b/tests/test2/utils/r.py new file mode 100644 index 00000000..f3c6116e --- /dev/null +++ b/tests/test2/utils/r.py @@ -0,0 +1,7 @@ +value = '123' + +try: + from test2.a import g +except ImportError: + # circular import + pass \ No newline at end of file