diff --git a/examples/builtins/literal.py b/examples/builtins/literal.py new file mode 100644 index 000000000..0746781b6 --- /dev/null +++ b/examples/builtins/literal.py @@ -0,0 +1,50 @@ +b = b'abc' +c = b"abc" +d = b'"abc"' +e = b"'abc'" +f = b'abc\'def' +g = b"abc\"def" +assert len(b) == 3 +assert len(c) == 3 +assert len(d) == 5 +assert len(e) == 5 +assert len(f) == 7 +assert len(g) == 7 +assert b[0] == 97 and b[1] == 98 and b[2] == 99 +assert c[0] == 97 and c[1] == 98 and c[2] == 99 +assert d[0] == 34 and d[1] == 97 and d[2] == 98 and d[3] == 99 and d[4] == 34 +assert e[0] == 39 and e[1] == 97 and e[2] == 98 and e[3] == 99 and e[4] == 39 +assert f[0] == 97 and f[1] == 98 and f[2] == 99 and f[3] == 39 and f[4] == 100 and f[5] == 101 and f[6] == 102 +assert g[0] == 97 and g[1] == 98 and g[2] == 99 and g[3] == 34 and g[4] == 100 and g[5] == 101 and g[6] == 102 +assert type(b[0:1]) == bytes +assert type(b[0]) == int + +# Initialize the string variables +a = 'abc' +b = "abc" +c = '"abc"' +d = "'abc'" +e = 'abc\'def' +f = "abc\"def" + +# Check the length of the strings +assert len(a) == 3 +assert len(b) == 3 +assert len(c) == 5 +assert len(d) == 5 +assert len(e) == 7 +assert len(f) == 7 + +# Check the value of each character in the strings +assert a[0] == 'a' and a[1] == 'b' and a[2] == 'c' +assert b[0] == 'a' and b[1] == 'b' and b[2] == 'c' +assert c[0] == '"' and c[1] == 'a' and c[2] == 'b' and c[3] == 'c' and c[4] == '"' +assert d[0] == "'" and d[1] == 'a' and d[2] == 'b' and d[3] == 'c' and d[4] == "'" +assert e[0] == 'a' and e[1] == 'b' and e[2] == 'c' and e[3] == "'" and e[4] == 'd' and e[5] == 'e' and e[6] == 'f' +assert f[0] == 'a' and f[1] == 'b' and f[2] == 'c' and f[3] == '"' and f[4] == 'd' and f[5] == 'e' and f[6] == 'f' + +# Check the type of the slices and characters +assert type(a[0:1]) == str +assert type(a[0]) == str + +print('PASS') diff --git a/port/linux/.vscode/launch.json b/port/linux/.vscode/launch.json index 350d55ef3..797fc2ab9 100644 --- a/port/linux/.vscode/launch.json +++ b/port/linux/.vscode/launch.json @@ -11,36 +11,11 @@ "program": "${workspaceFolder}/build/test/pikascript_test", // "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain", "args": [ - // "--gtest_filter=pikaui.*" - // "--gtest_filter=doc.*" - // "--gtest_filter=packtool.*" - // "--gtest_filter=os.path" - // "--gtest_filter=packtool.packread" - // "--gtest_filter=thread.test1" - // "--gtest_filter=eventloop.test1" - // "--gtest_filter=parser.tuple_single" - // "--gtest_filter=parser.split_slice" - // "--gtest_filter=vm.var_global_run" - // "--gtest_filter=lua.eval" - // "--gtest_filter=eventloop.once1" - // "--gtest_filter=parser.fn_fn" - // "--gtest_filter=builtin.getitem" - // "--gtest_filter=except.isinstance" - // "--gtest_filter=vm.range_1" - // "--gtest_filter=eventloop.*" - // "--gtest_filter=event.event1" - // "--gtest_filter=event.event_thread" - // "--gtest_filter=_json.loads" - // "--gtest_filter=json.speed" - // "--gtest_filter=json.json_issue1" - // "--gtest_filter=json.err" - // "--gtest_filter=builtin.eval" - // "--gtest_filter=zlib.*" - // "--gtest_filter=vm.run_file" - // "--gtest_filter=modbus.rtu_request" // "--gtest_filter=builtin.write_fn" // "--gtest_filter=builtin.base_type" - "--gtest_filter=parser.comprehension" + // "--gtest_filter=parser.comprehension" + // "--gtest_filter=parser.*" + "--gtest_filter=pikaMain.slice2" ], "stopAtEntry": false, "cwd": "${workspaceFolder}", diff --git a/port/linux/test/pikaMain-test.cpp b/port/linux/test/pikaMain-test.cpp index 5f7bb8a74..d3de60723 100644 --- a/port/linux/test/pikaMain-test.cpp +++ b/port/linux/test/pikaMain-test.cpp @@ -854,12 +854,12 @@ TEST(pikaMain, bytes__index__) { "s[2] = b'q'\n" "\n"); /* collect */ - uint8_t* res = obj_getBytes(pikaMain, "res"); - uint8_t* res2 = obj_getBytes(pikaMain, "res2"); + int res = obj_getInt(pikaMain, "res"); + int res2 = obj_getInt(pikaMain, "res2"); uint8_t* s = obj_getBytes(pikaMain, "s"); /* assert */ - EXPECT_STREQ((char*)res, "s"); - EXPECT_STREQ((char*)res2, "r"); + EXPECT_EQ(res, 115); + EXPECT_EQ(res2, 114); EXPECT_EQ(s[2], 'q'); // EXPECT_STREQ(s, "teqt"); /* deinit */ @@ -2031,7 +2031,7 @@ TEST(pikaMain, neg_index) { "'test'[-2]\n" "b'test'[-2]\n"); /* assert */ - EXPECT_STREQ(log_buff[0], "b'\\x73'\r\n"); + EXPECT_STREQ(log_buff[0], "115\r\n"); EXPECT_STREQ(log_buff[1], "'s'\r\n"); EXPECT_STREQ(log_buff[2], "BEGIN\r\n"); /* deinit */ diff --git a/port/linux/test/python/builtins/literal.py b/port/linux/test/python/builtins/literal.py new file mode 100644 index 000000000..0746781b6 --- /dev/null +++ b/port/linux/test/python/builtins/literal.py @@ -0,0 +1,50 @@ +b = b'abc' +c = b"abc" +d = b'"abc"' +e = b"'abc'" +f = b'abc\'def' +g = b"abc\"def" +assert len(b) == 3 +assert len(c) == 3 +assert len(d) == 5 +assert len(e) == 5 +assert len(f) == 7 +assert len(g) == 7 +assert b[0] == 97 and b[1] == 98 and b[2] == 99 +assert c[0] == 97 and c[1] == 98 and c[2] == 99 +assert d[0] == 34 and d[1] == 97 and d[2] == 98 and d[3] == 99 and d[4] == 34 +assert e[0] == 39 and e[1] == 97 and e[2] == 98 and e[3] == 99 and e[4] == 39 +assert f[0] == 97 and f[1] == 98 and f[2] == 99 and f[3] == 39 and f[4] == 100 and f[5] == 101 and f[6] == 102 +assert g[0] == 97 and g[1] == 98 and g[2] == 99 and g[3] == 34 and g[4] == 100 and g[5] == 101 and g[6] == 102 +assert type(b[0:1]) == bytes +assert type(b[0]) == int + +# Initialize the string variables +a = 'abc' +b = "abc" +c = '"abc"' +d = "'abc'" +e = 'abc\'def' +f = "abc\"def" + +# Check the length of the strings +assert len(a) == 3 +assert len(b) == 3 +assert len(c) == 5 +assert len(d) == 5 +assert len(e) == 7 +assert len(f) == 7 + +# Check the value of each character in the strings +assert a[0] == 'a' and a[1] == 'b' and a[2] == 'c' +assert b[0] == 'a' and b[1] == 'b' and b[2] == 'c' +assert c[0] == '"' and c[1] == 'a' and c[2] == 'b' and c[3] == 'c' and c[4] == '"' +assert d[0] == "'" and d[1] == 'a' and d[2] == 'b' and d[3] == 'c' and d[4] == "'" +assert e[0] == 'a' and e[1] == 'b' and e[2] == 'c' and e[3] == "'" and e[4] == 'd' and e[5] == 'e' and e[6] == 'f' +assert f[0] == 'a' and f[1] == 'b' and f[2] == 'c' and f[3] == '"' and f[4] == 'd' and f[5] == 'e' and f[6] == 'f' + +# Check the type of the slices and characters +assert type(a[0:1]) == str +assert type(a[0]) == str + +print('PASS') diff --git a/src/PikaParser.c b/src/PikaParser.c index 6397c861a..cf22efd92 100644 --- a/src/PikaParser.c +++ b/src/PikaParser.c @@ -1905,12 +1905,22 @@ AST* AST_parseStmt(AST* ast, char* stmt) { AST_setNodeAttr(ast, (char*)"import", import); goto __exit; } - /* solve str stmt */ - if (STMT_string == stmtType) { + /* solve str/bytes stmt */ + if (STMT_string == stmtType || STMT_bytes == stmtType) { str = strsCopy(&buffs, right); /* remove the first char */ char firstChar = str[0]; - str = str + 1; + switch (stmtType) { + case STMT_string: + str = str + 1; + break; + case STMT_bytes: + str = str + 2; + break; + default: + // never reach + pika_assert(0); + } /* remove the last char */ str[strGetSize(str) - 1] = '\0'; /* replace */ @@ -1924,15 +1934,11 @@ AST* AST_parseStmt(AST* ast, char* stmt) { break; } } - AST_setNodeAttr(ast, (char*)"string", str); - goto __exit; - } - /* solve bytes stmt */ - if (STMT_bytes == stmtType) { - str = right + 1; - str = strsDeleteChar(&buffs, str, '\''); - str = strsDeleteChar(&buffs, str, '\"'); - AST_setNodeAttr(ast, (char*)"bytes", str); + if (STMT_string == stmtType) { + AST_setNodeAttr(ast, (char*)"string", str); + } else if (STMT_bytes == stmtType) { + AST_setNodeAttr(ast, (char*)"bytes", str); + } goto __exit; } /* solve number stmt */ diff --git a/src/PikaVM.c b/src/PikaVM.c index 03551c7f9..7ac13f249 100644 --- a/src/PikaVM.c +++ b/src/PikaVM.c @@ -659,7 +659,7 @@ Arg* _vm_get(VMState* vm, PikaObj* self, Arg* aKey, Arg* aObj) { uint8_t* sBytesPyload = arg_getBytes(aObj); uint8_t sByteBuff[] = " "; sByteBuff[0] = sBytesPyload[iIndex]; - return arg_newBytes(sByteBuff, 1); + return arg_newInt(sByteBuff[0]); } if (argType_isObject(eType)) { PikaObj* oArg = NULL; @@ -746,7 +746,7 @@ Arg* _vm_slice(VMState* vm, } /* __slice__ is equal to __getitem__ */ - if (iEnd - iStart == 1) { + if (iEnd - iStart == 1 && arg_getType(aObj) != ARG_TYPE_BYTES) { return _vm_get(vm, self, aStart, aObj); } @@ -764,22 +764,8 @@ Arg* _vm_slice(VMState* vm, } if (ARG_TYPE_BYTES == arg_getType(aObj)) { - Arg* aSliced = arg_newBytes(NULL, 0); - for (int i = iStart; i < iEnd; i++) { - Arg* aIndex = arg_newInt(i); - Arg* aItem = _vm_get(vm, self, aIndex, aObj); - uint8_t* sBytesOrigin = arg_getBytes(aSliced); - size_t uSizeOrigin = arg_getBytesSize(aSliced); - Arg* aSlicedNew = arg_newBytes(NULL, uSizeOrigin + 1); - pika_platform_memcpy(arg_getBytes(aSlicedNew), sBytesOrigin, - uSizeOrigin); - pika_platform_memcpy(arg_getBytes(aSlicedNew) + uSizeOrigin, - arg_getBytes(aItem), 1); - arg_deinit(aSliced); - aSliced = aSlicedNew; - arg_deinit(aItem); - arg_deinit(aIndex); - } + uint8_t* sBytesOrigin = arg_getBytes(aObj); + Arg* aSliced = arg_newBytes(sBytesOrigin + iStart, iEnd - iStart); return aSliced; }