Skip to content

Commit

Permalink
fix parse issue when met xx_import
Browse files Browse the repository at this point in the history
  • Loading branch information
pikasTech committed Jun 28, 2022
1 parent 4ade324 commit 2eb911c
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 8 deletions.
2 changes: 1 addition & 1 deletion port/linux/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [
// "--gtest_filter=parser.optissue1"
// "--gtest_filter=module.import_as_issue1"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
Expand Down
10 changes: 10 additions & 0 deletions port/linux/package/pikascript/import_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import PikaStdLib
import PikaStdData

class Test():
def func(self):
return 'hello'


def func():
return 'hello'
1 change: 1 addition & 0 deletions port/linux/package/pikascript/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import cjson_test
import test_module4
import pika_lua
import import_test
from PikaStdData import String as S

mem = PikaStdLib.MemChecker()
Expand Down
4 changes: 2 additions & 2 deletions port/linux/test/cJSON-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,8 @@ TEST(cJSON, module) {
/* run */
__platform_printf("BEGIN\r\n");
pikaVM_run(pikaMain,
"import cjson_test\n"
"cjson_test.test_start()\n");
"import cjson_test as ctest\n"
"ctest.test_start()\n");
/* collect */
/* assert */

Expand Down
24 changes: 24 additions & 0 deletions port/linux/test/module-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,27 @@ TEST(module, __init__2) {
EXPECT_EQ(pikaMemNow(), 0);
}
#endif

#if PIKA_SYNTEX_IMPORT_EX_ENABLE
TEST(module, import_as_issue1) {
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
extern unsigned char pikaModules_py_a[];
obj_linkLibrary(pikaMain, pikaModules_py_a);
__platform_printf("BEGIN\r\n");
/* run */
obj_run(pikaMain,
"import import_test as my_import\n"
"print(my_import.func())\n"
"print(import_test.func())\n");
/* collect */
/* assert */
EXPECT_STREQ(log_buff[0], "hello\r\n");
EXPECT_STREQ(log_buff[1], "hello\r\n");
EXPECT_STREQ(log_buff[2], "BEGIN\r\n");
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
#endif
18 changes: 18 additions & 0 deletions port/linux/test/parse-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3227,3 +3227,21 @@ TEST(parser, optissue2) {
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}

TEST(lexser, import_issue1) {
/* init */
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();

/* run */
char* tokens = Lexer_getTokens(buffs, "my_import = import_test");
char* printTokens = Lexer_printTokens(buffs, tokens);
printf("%s\n", printTokens);

/* assert */
EXPECT_STREQ(printTokens, "{sym}my_import{opt}={sym}import_test");

/* deinit */
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
16 changes: 12 additions & 4 deletions src/PikaParser.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,12 @@ char* Lexer_getTokens(Args* outBuffs, char* stmt) {
tokens_arg = Lexer_setToken(tokens_arg, TOKEN_operator, content);
continue;
}

// not the string operator
if ((cn1 >= 'a' && cn1 <= 'z') || (cn1 >= 'A' && cn1 <= 'Z') ||
(cn1 >= '0' && cn1 <= '9') || cn1 == '_' || cn1 == '.') {
goto after_match_string_operator;
}
/* not */
if ('n' == c0) {
if (('o' == c1) && ('t' == c2) && (' ' == c3)) {
Expand Down Expand Up @@ -603,6 +609,8 @@ char* Lexer_getTokens(Args* outBuffs, char* stmt) {
continue;
}
}
after_match_string_operator:

/* skip spaces */
if (' ' == c0) {
/* not get symbal */
Expand Down Expand Up @@ -1594,7 +1602,7 @@ AST* AST_parseLine(char* line, Stack* block_stack) {
obj_setStr(ast, "return", "");
goto block_matched;
}

#if PIKA_SYNTEX_EXCEPTION_ENABLE
if (strEqu(line_start, "raise")) {
obj_setStr(ast, "raise", "");
Expand All @@ -1612,7 +1620,7 @@ AST* AST_parseLine(char* line, Stack* block_stack) {
goto block_matched;
}
#endif

if (strIsStartWith(line_start, "global ")) {
stmt = "";
char* global_list = line_start + 7;
Expand Down Expand Up @@ -1763,8 +1771,8 @@ static char* Parser_linePreProcess(Args* buffs_p, char* line) {
/* process EOL */
line = strsDeleteChar(buffs_p, line, '\r');
line = Parser_removeAnnotation(line);
#if PIKA_SYNTEX_IMPORT_EX_ENABLE
line = Parser_PreProcess_import(buffs_p, line);
#if PIKA_SYNTEX_IMPORT_EX_ENABLE
line = Parser_PreProcess_import(buffs_p, line);
line = Parser_PreProcess_from(buffs_p, line);
#endif
exit:
Expand Down
2 changes: 1 addition & 1 deletion src/PikaVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
#define PIKA_VERSION_MINOR 8
#define PIKA_VERSION_MICRO 8

#define PIKA_EDIT_TIME "2022/06/27 11:29:46"
#define PIKA_EDIT_TIME "2022/06/28 16:32:49"

0 comments on commit 2eb911c

Please sign in to comment.