Skip to content

Commit b669710

Browse files
committed
!39 Support d = {'a':x, 'b':y, 'c':z}
* Support d = {'a':x, 'b':y, 'c':z} * add parse and ins for dict literal
1 parent 74e04fd commit b669710

10 files changed

Lines changed: 150 additions & 21 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ Support class and method define, encapsulation-inheritance-polymorphism and moud
236236
| l = list() || Can be disabled in [configuration](https://pikadoc.readthedocs.io/en/latest/%E4%BC%98%E5%8C%96%E5%86%85%E5%AD%98%E5%8D%A0%E7%94%A8%E3%80%81%E9%85%8D%E7%BD%AE%20libc.html). |
237237
| l = [a, b, c] || Can be disabled in [configuration](https://pikadoc.readthedocs.io/en/latest/%E4%BC%98%E5%8C%96%E5%86%85%E5%AD%98%E5%8D%A0%E7%94%A8%E3%80%81%E9%85%8D%E7%BD%AE%20libc.html). |
238238
| d = dict() || Can be disabled in [configuration](https://pikadoc.readthedocs.io/en/latest/%E4%BC%98%E5%8C%96%E5%86%85%E5%AD%98%E5%8D%A0%E7%94%A8%E3%80%81%E9%85%8D%E7%BD%AE%20libc.html). |
239-
| d = {a:"x", b:"y", c:"z"} | - | |
239+
| d = {'a':x, 'b':y, 'c':z} | | Can be disabled in [configuration](https://pikadoc.readthedocs.io/en/latest/%E4%BC%98%E5%8C%96%E5%86%85%E5%AD%98%E5%8D%A0%E7%94%A8%E3%80%81%E9%85%8D%E7%BD%AE%20libc.html).|
240240

241241
#### Exception
242242

@@ -249,7 +249,7 @@ Support class and method define, encapsulation-inheritance-polymorphism and moud
249249
| Syntax | str | bytes | list (PikaStdData.List) |Comment |
250250
| --- | --- | --- | --- | --- |
251251
| test[i] |||| Can be disabled in [configuration](https://pikadoc.readthedocs.io/en/latest/%E4%BC%98%E5%8C%96%E5%86%85%E5%AD%98%E5%8D%A0%E7%94%A8%E3%80%81%E9%85%8D%E7%BD%AE%20libc.html). |
252-
| test[a : b : c] ||| - | Can be disabled in [configuration](https://pikadoc.readthedocs.io/en/latest/%E4%BC%98%E5%8C%96%E5%86%85%E5%AD%98%E5%8D%A0%E7%94%A8%E3%80%81%E9%85%8D%E7%BD%AE%20libc.html). |
252+
| test[a : b] ||| - | Can be disabled in [configuration](https://pikadoc.readthedocs.io/en/latest/%E4%BC%98%E5%8C%96%E5%86%85%E5%AD%98%E5%8D%A0%E7%94%A8%E3%80%81%E9%85%8D%E7%BD%AE%20libc.html). |
253253
| test[a :] ||| - |Can be disabled in [configuration](https://pikadoc.readthedocs.io/en/latest/%E4%BC%98%E5%8C%96%E5%86%85%E5%AD%98%E5%8D%A0%E7%94%A8%E3%80%81%E9%85%8D%E7%BD%AE%20libc.html). |
254254

255255
#### Other keywords/Syntax

README_zh.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ https://github.com/maxgerhardt/pikascript-pio-bluepill
241241
| l = list() || Can be disabled in [configuration](https://pikadoc.readthedocs.io/zh/latest/%E4%BC%98%E5%8C%96%E5%86%85%E5%AD%98%E5%8D%A0%E7%94%A8%E3%80%81%E9%85%8D%E7%BD%AE%20libc.html). |
242242
| l = [a, b, c] || Can be disabled in [configuration](https://pikadoc.readthedocs.io/zh/latest/%E4%BC%98%E5%8C%96%E5%86%85%E5%AD%98%E5%8D%A0%E7%94%A8%E3%80%81%E9%85%8D%E7%BD%AE%20libc.html). |
243243
| d = dict() || Can be disabled in [configuration](https://pikadoc.readthedocs.io/zh/latest/%E4%BC%98%E5%8C%96%E5%86%85%E5%AD%98%E5%8D%A0%E7%94%A8%E3%80%81%E9%85%8D%E7%BD%AE%20libc.html). |
244-
| d = {a:"x", b:"y", c:"z"} | - | |
244+
| d = {'a':x, 'b':y, 'c':z} | | Can be disabled in [configuration](https://pikadoc.readthedocs.io/zh/latest/%E4%BC%98%E5%8C%96%E5%86%85%E5%AD%98%E5%8D%A0%E7%94%A8%E3%80%81%E9%85%8D%E7%BD%AE%20libc.html).|
245245

246246
#### Exception
247247

examples/BuiltIn/dict.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import PikaStdData
2-
d = PikaStdData.Dict()
3-
d['a'] = 1
4-
d['b'] = 2
5-
d['c'] = 'test'
2+
d = {'a':1, 'b':2, 'c':'test'}
63
print(d)
74
for item in d:
85
print(item)

port/linux/.vscode/launch.json

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,14 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7-
{
8-
"name": "Python: Current File",
9-
"type": "python",
10-
"request": "launch",
11-
"program": "${file}",
12-
"console": "integratedTerminal",
13-
"justMyCode": true
14-
},
157
{
168
"name": "test",
179
"type": "cppdbg",
1810
"request": "launch",
1911
"program": "${workspaceFolder}/build/test/pikascript_test",
2012
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
2113
"args": [
22-
// "--gtest_filter=module.import_as_issue1"
14+
// "--gtest_filter=parser.dict_literal1"
2315
],
2416
"stopAtEntry": false,
2517
"cwd": "${workspaceFolder}",

port/linux/test/parse-test.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3245,3 +3245,51 @@ TEST(lexser, import_issue1) {
32453245
args_deinit(buffs);
32463246
EXPECT_EQ(pikaMemNow(), 0);
32473247
}
3248+
3249+
TEST(lexser, dict_literal1) {
3250+
/* init */
3251+
pikaMemInfo.heapUsedMax = 0;
3252+
Args* buffs = New_strBuff();
3253+
3254+
/* run */
3255+
char* tokens = Lexer_getTokens(
3256+
buffs,
3257+
"tinydict = {'name': 'runoob', 'likes': 123, 'url': 'www.runoob.com'}");
3258+
char* printTokens = Lexer_printTokens(buffs, tokens);
3259+
printf("%s\n", printTokens);
3260+
3261+
/* assert */
3262+
EXPECT_STREQ(printTokens,
3263+
"{sym}tinydict{opt}={dvd}{{lit}'name'{dvd}:{lit}'runoob'{dvd},"
3264+
"{lit}'likes'{dvd}:{lit}123{dvd},{lit}'url'{dvd}:{lit}'www."
3265+
"runoob.com'{dvd}}");
3266+
3267+
/* deinit */
3268+
args_deinit(buffs);
3269+
EXPECT_EQ(pikaMemNow(), 0);
3270+
}
3271+
3272+
#if PIKA_BUILTIN_DICT_ENABLE
3273+
TEST(parser, dict_literal1) {
3274+
pikaMemInfo.heapUsedMax = 0;
3275+
Args* buffs = New_strBuff();
3276+
char* lines =
3277+
"tinydict = {'name': 'runoob', 'likes': 123, 'url': "
3278+
"'www.runoob.com'}";
3279+
printf("%s\n", lines);
3280+
char* pikaAsm = Parser_multiLineToAsm(buffs, lines);
3281+
printf("%s", pikaAsm);
3282+
EXPECT_STREQ(pikaAsm,
3283+
"B0\n"
3284+
"1 STR name\n"
3285+
"1 STR runoob\n"
3286+
"1 STR likes\n"
3287+
"1 NUM 123\n"
3288+
"1 STR url\n"
3289+
"1 STR www.runoob.com\n"
3290+
"0 DCT \n"
3291+
"0 OUT tinydict\n");
3292+
args_deinit(buffs);
3293+
EXPECT_EQ(pikaMemNow(), 0);
3294+
}
3295+
#endif

src/PikaParser.c

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,17 @@ static enum StmtType Lexer_matchStmtType(char* right) {
183183
char* rightWithoutSubStmt = __removeTokensBetween(&buffs, right, "(", ")");
184184
rightWithoutSubStmt =
185185
__removeTokensBetween(&buffs, rightWithoutSubStmt, "[", "]");
186+
rightWithoutSubStmt =
187+
__removeTokensBetween(&buffs, rightWithoutSubStmt, "{", "}");
186188

187189
uint8_t is_get_operator = 0;
188190
uint8_t is_get_method = 0;
189191
uint8_t is_get_string = 0;
190192
uint8_t is_get_bytes = 0;
191193
uint8_t is_get_number = 0;
192194
uint8_t is_get_symbol = 0;
193-
uint8_t is_get_index = 0;
195+
uint8_t is_get_list = 0;
196+
uint8_t is_get_dict = 0;
194197
uint8_t is_get_import = 0;
195198
ParserState_forEachToken(ps, rightWithoutSubStmt) {
196199
ParserState_iterStart(&ps);
@@ -200,7 +203,11 @@ static enum StmtType Lexer_matchStmtType(char* right) {
200203
goto iter_continue;
201204
}
202205
if (strEqu(ps.token1.pyload, "[")) {
203-
is_get_index = 1;
206+
is_get_list = 1;
207+
goto iter_continue;
208+
}
209+
if (strEqu(ps.token1.pyload, "{")) {
210+
is_get_dict = 1;
204211
goto iter_continue;
205212
}
206213
if (ps.token1.type == TOKEN_operator) {
@@ -240,10 +247,14 @@ static enum StmtType Lexer_matchStmtType(char* right) {
240247
stmtType = STMT_operator;
241248
goto exit;
242249
}
243-
if (is_get_index) {
250+
if (is_get_list) {
244251
stmtType = STMT_list;
245252
goto exit;
246253
}
254+
if (is_get_dict) {
255+
stmtType = STMT_dict;
256+
goto exit;
257+
}
247258
if (is_get_method) {
248259
stmtType = STMT_method;
249260
goto exit;
@@ -467,7 +478,7 @@ char* Lexer_getTokens(Args* outBuffs, char* stmt) {
467478

468479
/* match devider*/
469480
if (('(' == c0) || (')' == c0) || (',' == c0) || ('[' == c0) ||
470-
(']' == c0) || (':' == c0)) {
481+
(']' == c0) || (':' == c0) || ('{' == c0) || ('}' == c0)) {
471482
tokens_arg =
472483
Lexer_setSymbel(tokens_arg, stmt, i, &symbol_start_index);
473484
char content[2] = {0};
@@ -1309,6 +1320,46 @@ AST* AST_parseStmt(AST* ast, char* stmt) {
13091320
}
13101321
#endif
13111322

1323+
#if PIKA_BUILTIN_DICT_ENABLE
1324+
/* solve list stmt */
1325+
if (STMT_dict == stmtType) {
1326+
obj_setStr(ast, (char*)"dict", "dict");
1327+
char* subStmts = strsCut(&buffs, right, '{', '}');
1328+
subStmts = strsAppend(&buffs, subStmts, ",");
1329+
Arg* subStmt = arg_setStr(NULL, "", "");
1330+
char* subStmt_str = NULL;
1331+
ParserState_forEachToken(ps, subStmts) {
1332+
ParserState_iterStart(&ps);
1333+
if (ps.branket_deepth > 0) {
1334+
/* in brankets */
1335+
/* append token to subStmt */
1336+
subStmt = arg_strAppend(subStmt, ps.token1.pyload);
1337+
subStmt_str = arg_getStr(subStmt);
1338+
} else {
1339+
/* not in brankets */
1340+
if (strEqu(ps.token1.pyload, ",") ||
1341+
strEqu(ps.token1.pyload, ":")) {
1342+
/* found "," or ":" push subStmt */
1343+
queueObj_pushObj(ast, (char*)"stmt");
1344+
subStmt_str = arg_getStr(subStmt);
1345+
AST_parseStmt(queueObj_getCurrentObj(ast), subStmt_str);
1346+
/* clear subStmt */
1347+
arg_deinit(subStmt);
1348+
subStmt = arg_setStr(NULL, "", "");
1349+
} else {
1350+
/* not "," append subStmt */
1351+
subStmt = arg_strAppend(subStmt, ps.token1.pyload);
1352+
subStmt_str = arg_getStr(subStmt);
1353+
}
1354+
}
1355+
ParserState_iterEnd(&ps);
1356+
}
1357+
ParserState_deinit(&ps);
1358+
arg_deinit(subStmt);
1359+
goto exit;
1360+
}
1361+
#endif
1362+
13121363
/* solve method stmt */
13131364
if (STMT_method == stmtType) {
13141365
method = strsGetFirstToken(&buffs, right, '(');
@@ -1987,6 +2038,7 @@ char* AST_appandPikaASM(AST* ast, AST* subAst, Args* outBuffs, char* pikaAsm) {
19872038
}
19882039
char* method = obj_getStr(subAst, "method");
19892040
char* list = obj_getStr(subAst, "list");
2041+
char* dict = obj_getStr(subAst, "dict");
19902042
char* operator= obj_getStr(subAst, "operator");
19912043
char* ref = obj_getStr(subAst, "ref");
19922044
char* left = obj_getStr(subAst, "left");
@@ -1995,6 +2047,10 @@ char* AST_appandPikaASM(AST* ast, AST* subAst, Args* outBuffs, char* pikaAsm) {
19952047
char* num = obj_getStr(subAst, "num");
19962048
char* import = obj_getStr(subAst, "import");
19972049
char* buff = args_getBuff(&buffs, PIKA_SPRINTF_BUFF_SIZE);
2050+
if (NULL != dict) {
2051+
__platform_sprintf(buff, "%d DCT \n", deepth);
2052+
pikaAsm = strsAppend(&buffs, pikaAsm, buff);
2053+
}
19982054
if (NULL != list) {
19992055
__platform_sprintf(buff, "%d LST \n", deepth);
20002056
pikaAsm = strsAppend(&buffs, pikaAsm, buff);

src/PikaParser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ enum StmtType {
4949
STMT_operator,
5050
STMT_import,
5151
STMT_list,
52+
STMT_dict,
5253
STMT_none,
5354
};
5455

src/PikaVM.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ void PikaStdData_List_append(PikaObj* self, Arg* arg);
432432
void PikaStdData_List___init__(PikaObj* self);
433433
PikaObj* New_PikaStdData_List(Args* args);
434434
#endif
435+
435436
static Arg* VM_instruction_handler_LST(PikaObj* self, VMState* vs, char* data) {
436437
#if PIKA_BUILTIN_LIST_ENABLE
437438
uint8_t arg_num = VMState_getInputArgNum(vs);
@@ -456,6 +457,38 @@ static Arg* VM_instruction_handler_LST(PikaObj* self, VMState* vs, char* data) {
456457
#endif
457458
}
458459

460+
#if PIKA_BUILTIN_DICT_ENABLE
461+
void PikaStdData_Dict_set(PikaObj* self, Arg* arg, char* key);
462+
void PikaStdData_Dict___init__(PikaObj* self);
463+
PikaObj* New_PikaStdData_Dict(Args* args);
464+
#endif
465+
466+
static Arg* VM_instruction_handler_DCT(PikaObj* self, VMState* vs, char* data) {
467+
#if PIKA_BUILTIN_DICT_ENABLE
468+
uint8_t arg_num = VMState_getInputArgNum(vs);
469+
PikaObj* dict = newNormalObj(New_PikaStdData_Dict);
470+
PikaStdData_Dict___init__(dict);
471+
Stack stack = {0};
472+
stack_init(&stack);
473+
/* load to local stack to change sort */
474+
for (int i = 0; i < arg_num; i++) {
475+
Arg* arg = stack_popArg(&(vs->stack));
476+
stack_pushArg(&stack, arg);
477+
}
478+
for (int i = 0; i < arg_num / 2; i++) {
479+
Arg* key_arg = stack_popArg(&stack);
480+
Arg* val_arg = stack_popArg(&stack);
481+
PikaStdData_Dict_set(dict, val_arg, arg_getStr(key_arg));
482+
arg_deinit(key_arg);
483+
arg_deinit(val_arg);
484+
}
485+
stack_deinit(&stack);
486+
return arg_setPtr(NULL, "", ARG_TYPE_OBJECT, dict);
487+
#else
488+
return VM_instruction_handler_NON(self, vs, data);
489+
#endif
490+
}
491+
459492
static Arg* VM_instruction_handler_RUN(PikaObj* self, VMState* vs, char* data) {
460493
Args buffs = {0};
461494
Arg* return_arg = NULL;

src/PikaVersion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
#define PIKA_VERSION_MINOR 8
33
#define PIKA_VERSION_MICRO 8
44

5-
#define PIKA_EDIT_TIME "2022/06/28 16:32:49"
5+
#define PIKA_EDIT_TIME "2022/06/30 12:05:31"

src/__instruction_table.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,5 @@ def_ins(RIS)
8686
def_ins(GER)
8787
/* set error code */
8888
def_ins(SER)
89+
/* dict */
90+
def_ins(DCT)

0 commit comments

Comments
 (0)