mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
support **keys in cmodule
support print('test', end='')
This commit is contained in:
parent
da2e9b3779
commit
d4a0b37958
@ -34,11 +34,7 @@ class SysObj:
|
||||
def range(a1: int, a2: int) -> any: ...
|
||||
|
||||
@staticmethod
|
||||
def print(*val): ...
|
||||
|
||||
@staticmethod
|
||||
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
|
||||
def printNoEnd(val: any): ...
|
||||
def print(*val, **ops): ...
|
||||
|
||||
@staticmethod
|
||||
def __setitem__(obj: any, key: any, val: any) -> any: ...
|
||||
|
@ -393,7 +393,7 @@ static void __print_arg(PikaObj* self, Arg* val) {
|
||||
args_deinit(print_args);
|
||||
}
|
||||
|
||||
void PikaStdLib_SysObj_print(PikaObj* self, PikaTuple* val) {
|
||||
void PikaStdLib_SysObj_print(PikaObj* self, PikaTuple* val, PikaDict* ops) {
|
||||
int arg_size = tuple_getSize(val);
|
||||
Arg* print_out_arg = arg_newStr("");
|
||||
PIKA_BOOL is_get_print = PIKA_FALSE;
|
||||
@ -410,28 +410,17 @@ void PikaStdLib_SysObj_print(PikaObj* self, PikaTuple* val) {
|
||||
obj_removeArg(self, "_buf");
|
||||
}
|
||||
}
|
||||
char* end = dict_getStr(ops, "end");
|
||||
if (NULL == end) {
|
||||
/* default */
|
||||
end = "\r\n";
|
||||
}
|
||||
if (PIKA_TRUE == is_get_print) {
|
||||
__platform_printf("%s\r\n", arg_getStr(print_out_arg));
|
||||
__platform_printf("%s%s", arg_getStr(print_out_arg), end);
|
||||
}
|
||||
arg_deinit(print_out_arg);
|
||||
}
|
||||
|
||||
void PikaStdLib_SysObj_printNoEnd(PikaObj* self, Arg* val) {
|
||||
obj_setErrorCode(self, 0);
|
||||
Args* print_args = New_args(NULL);
|
||||
args_setArg(print_args, arg_copy(val));
|
||||
char* res = args_print(print_args, "val");
|
||||
if (NULL == res) {
|
||||
obj_setSysOut(self, "Error: print: can not print val");
|
||||
obj_setErrorCode(self, 1);
|
||||
args_deinit(print_args);
|
||||
return;
|
||||
}
|
||||
/* not empty */
|
||||
__platform_printf("%s", res);
|
||||
args_deinit(print_args);
|
||||
}
|
||||
|
||||
char* PikaStdLib_SysObj_cformat(PikaObj* self, char* fmt, PikaTuple* var) {
|
||||
#if PIKA_SYNTAX_FORMAT_ENABLE
|
||||
Args buffs = {0};
|
||||
|
2
port/linux/.vscode/launch.json
vendored
2
port/linux/.vscode/launch.json
vendored
@ -11,7 +11,7 @@
|
||||
"program": "${workspaceFolder}/build/test/pikascript_test",
|
||||
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
|
||||
"args": [
|
||||
"--gtest_filter=vm.keyword_3"
|
||||
// "--gtest_filter=sysObj.print"
|
||||
],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${workspaceFolder}",
|
||||
|
@ -34,11 +34,7 @@ class SysObj:
|
||||
def range(a1: int, a2: int) -> any: ...
|
||||
|
||||
@staticmethod
|
||||
def print(*val): ...
|
||||
|
||||
@staticmethod
|
||||
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
|
||||
def printNoEnd(val: any): ...
|
||||
def print(*val, **ops): ...
|
||||
|
||||
@staticmethod
|
||||
def __setitem__(obj: any, key: any, val: any) -> any: ...
|
||||
|
@ -393,7 +393,7 @@ static void __print_arg(PikaObj* self, Arg* val) {
|
||||
args_deinit(print_args);
|
||||
}
|
||||
|
||||
void PikaStdLib_SysObj_print(PikaObj* self, PikaTuple* val) {
|
||||
void PikaStdLib_SysObj_print(PikaObj* self, PikaTuple* val, PikaDict* ops) {
|
||||
int arg_size = tuple_getSize(val);
|
||||
Arg* print_out_arg = arg_newStr("");
|
||||
PIKA_BOOL is_get_print = PIKA_FALSE;
|
||||
@ -410,28 +410,17 @@ void PikaStdLib_SysObj_print(PikaObj* self, PikaTuple* val) {
|
||||
obj_removeArg(self, "_buf");
|
||||
}
|
||||
}
|
||||
char* end = dict_getStr(ops, "end");
|
||||
if (NULL == end) {
|
||||
/* default */
|
||||
end = "\r\n";
|
||||
}
|
||||
if (PIKA_TRUE == is_get_print) {
|
||||
__platform_printf("%s\r\n", arg_getStr(print_out_arg));
|
||||
__platform_printf("%s%s", arg_getStr(print_out_arg), end);
|
||||
}
|
||||
arg_deinit(print_out_arg);
|
||||
}
|
||||
|
||||
void PikaStdLib_SysObj_printNoEnd(PikaObj* self, Arg* val) {
|
||||
obj_setErrorCode(self, 0);
|
||||
Args* print_args = New_args(NULL);
|
||||
args_setArg(print_args, arg_copy(val));
|
||||
char* res = args_print(print_args, "val");
|
||||
if (NULL == res) {
|
||||
obj_setSysOut(self, "Error: print: can not print val");
|
||||
obj_setErrorCode(self, 1);
|
||||
args_deinit(print_args);
|
||||
return;
|
||||
}
|
||||
/* not empty */
|
||||
__platform_printf("%s", res);
|
||||
args_deinit(print_args);
|
||||
}
|
||||
|
||||
char* PikaStdLib_SysObj_cformat(PikaObj* self, char* fmt, PikaTuple* var) {
|
||||
#if PIKA_SYNTAX_FORMAT_ENABLE
|
||||
Args buffs = {0};
|
||||
|
@ -1358,25 +1358,25 @@ TEST(pikaMain, class_demo_1_file) {
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
#if !PIKA_NANO_ENABLE
|
||||
TEST(pikaMain, get_native_method) {
|
||||
/* init */
|
||||
pikaMemInfo.heapUsedMax = 0;
|
||||
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||
__platform_printf("BEGIN\r\n");
|
||||
/* do some thing */
|
||||
Args args = {0};
|
||||
args_setStr(&args, "val", "test");
|
||||
obj_runNativeMethod(pikaMain, "printNoEnd", &args);
|
||||
args_deinit_stack(&args);
|
||||
/* assert */
|
||||
EXPECT_STREQ(log_buff[0], "test");
|
||||
EXPECT_STREQ(log_buff[1], "BEGIN\r\n");
|
||||
/* deinit */
|
||||
obj_deinit(pikaMain);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
#endif
|
||||
// #if !PIKA_NANO_ENABLE
|
||||
// TEST(pikaMain, get_native_method) {
|
||||
// /* init */
|
||||
// pikaMemInfo.heapUsedMax = 0;
|
||||
// PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||
// __platform_printf("BEGIN\r\n");
|
||||
// /* do some thing */
|
||||
// Args args = {0};
|
||||
// args_setStr(&args, "val", "t");
|
||||
// obj_runNativeMethod(pikaMain, "ord", &args);
|
||||
// args_deinit_stack(&args);
|
||||
// /* assert */
|
||||
// EXPECT_STREQ(log_buff[0], "test\r\n");
|
||||
// EXPECT_STREQ(log_buff[1], "BEGIN\r\n");
|
||||
// /* deinit */
|
||||
// obj_deinit(pikaMain);
|
||||
// EXPECT_EQ(pikaMemNow(), 0);
|
||||
// }
|
||||
// #endif
|
||||
|
||||
TEST(pikaMain, hex_list) {
|
||||
/* init */
|
||||
|
@ -76,3 +76,18 @@ TEST(sysObj, setattr) {
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !PIKA_NANO_ENABLE
|
||||
TEST(sysobj, print_no_end) {
|
||||
char* line =
|
||||
"print('test', end='')\n";
|
||||
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
||||
obj_run(self, line);
|
||||
/* collect */
|
||||
/* assert */
|
||||
EXPECT_STREQ(log_buff[0], "test");
|
||||
/* deinit */
|
||||
obj_deinit(self);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
#endif
|
||||
|
34
src/PikaVM.c
34
src/PikaVM.c
@ -287,7 +287,10 @@ Arg* __vm_get(PikaObj* self, Arg* key, Arg* obj) {
|
||||
/* const pool */
|
||||
};
|
||||
pikaVM_runByteCode(arg_obj, (uint8_t*)bytes);
|
||||
return arg_copy(args_getArg(arg_obj->list, "__res"));
|
||||
Arg* __res = args_getArg(arg_obj->list, "__res");
|
||||
if (NULL != __res) {
|
||||
return arg_copy(__res);
|
||||
}
|
||||
}
|
||||
return arg_newNull();
|
||||
}
|
||||
@ -652,12 +655,13 @@ static int VMState_loadArgsFromMethodArg(VMState* vm,
|
||||
tuple = New_tuple();
|
||||
strPopLastToken(type_list, ',');
|
||||
}
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
if (arg_def[0] == '*' && arg_def[1] == '*') {
|
||||
/* get keyword dict name */
|
||||
keyword_dict_name = arg_def + 2;
|
||||
break;
|
||||
dict = New_dict();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -669,14 +673,22 @@ static int VMState_loadArgsFromMethodArg(VMState* vm,
|
||||
char* arg_def = strPopLastToken(type_list, ',');
|
||||
strPopLastToken(arg_def, ':');
|
||||
arg_name = arg_def;
|
||||
} else {
|
||||
/* clear the variable arg name */
|
||||
arg_name = "";
|
||||
}
|
||||
Arg* call_arg = stack_popArg_alloc(&(vm->stack));
|
||||
if (arg_name[0] != '*') {
|
||||
|
||||
/* only normal arg use format name */
|
||||
if (arg_name != NULL && arg_name[0] != '*') {
|
||||
call_arg = arg_setName(call_arg, arg_name);
|
||||
}
|
||||
/* load the keyword arg */
|
||||
if (call_arg != NULL && arg_getIsKeyword(call_arg)) {
|
||||
if (NULL == dict) {
|
||||
dict = New_dict();
|
||||
}
|
||||
arg_setIsKeyword(call_arg, PIKA_FALSE);
|
||||
dict_setArg(dict, call_arg);
|
||||
continue;
|
||||
}
|
||||
/* load the variable arg */
|
||||
if (tuple != NULL && (arg_num - i > variable_arg_start)) {
|
||||
list_append(&tuple->super, call_arg);
|
||||
@ -684,14 +696,6 @@ static int VMState_loadArgsFromMethodArg(VMState* vm,
|
||||
arg_deinit(call_arg);
|
||||
continue;
|
||||
}
|
||||
if (arg_getIsKeyword(call_arg)) {
|
||||
if (NULL == dict) {
|
||||
dict = New_dict();
|
||||
}
|
||||
/* load the keyword arg */
|
||||
dict_setArg(dict, call_arg);
|
||||
continue;
|
||||
}
|
||||
/* load normal arg */
|
||||
args_setArg(args, call_arg);
|
||||
}
|
||||
|
@ -131,6 +131,7 @@ Arg* arg_setNameHash(Arg* self, Hash nameHash) {
|
||||
}
|
||||
|
||||
Arg* arg_setName(Arg* self, char* name) {
|
||||
pika_assert(NULL != name);
|
||||
return arg_setNameHash(self, hash_time33(name));
|
||||
}
|
||||
|
||||
|
@ -88,6 +88,9 @@ PIKA_RES args_setStr(Args* self, char* name, char* strIn) {
|
||||
}
|
||||
|
||||
PIKA_RES args_pushArg(Args* self, Arg* arg) {
|
||||
if (NULL == arg) {
|
||||
return PIKA_RES_ERR_ARG_NO_FOUND;
|
||||
}
|
||||
Arg* new_arg = NULL;
|
||||
if (!arg_getSerialized(arg)) {
|
||||
new_arg = arg_copy(arg);
|
||||
@ -584,6 +587,9 @@ void* list_getPtr(PikaList* self, int index) {
|
||||
}
|
||||
|
||||
PIKA_RES list_append(PikaList* self, Arg* arg) {
|
||||
if (NULL == arg) {
|
||||
return PIKA_RES_ERR_ARG_NO_FOUND;
|
||||
}
|
||||
int top = args_getInt(&self->super, "top");
|
||||
char buff[11];
|
||||
char* topStr = fast_itoa(buff, top);
|
||||
@ -595,6 +601,7 @@ PIKA_RES list_append(PikaList* self, Arg* arg) {
|
||||
}
|
||||
|
||||
size_t list_getSize(PikaList* self) {
|
||||
pika_assert(NULL != self);
|
||||
return args_getInt(&self->super, "top");
|
||||
}
|
||||
|
||||
@ -698,12 +705,19 @@ exit:
|
||||
return res;
|
||||
}
|
||||
|
||||
/* tuple */
|
||||
PikaTuple* args_getTuple(Args* self, char* name) {
|
||||
PikaObj* tuple_obj = args_getPtr(self, name);
|
||||
return obj_getPtr(tuple_obj, "list");
|
||||
}
|
||||
|
||||
char* args_cacheStr(Args* self, char* str){
|
||||
/* dict */
|
||||
PikaDict* args_getDict(Args* self, char* name) {
|
||||
PikaObj* tuple_obj = args_getPtr(self, name);
|
||||
return obj_getPtr(tuple_obj, "dict");
|
||||
}
|
||||
|
||||
char* args_cacheStr(Args* self, char* str) {
|
||||
args_setStr(self, "__str_cache", str);
|
||||
return args_getStr(self, "__str_cache");
|
||||
}
|
||||
|
@ -185,6 +185,7 @@ char* strsFormatArg(Args* out_buffs, char* fmt, Arg* arg);
|
||||
PikaList* New_list(void);
|
||||
PikaTuple* New_tuple(void);
|
||||
PikaTuple* args_getTuple(Args* self, char* name);
|
||||
PikaDict* args_getDict(Args* self, char* name);
|
||||
|
||||
char* strsFormatList(Args* out_buffs, char* fmt, PikaList* list);
|
||||
char* args_cacheStr(Args* self, char* str);
|
||||
|
@ -21,7 +21,7 @@ class SysObj(BaseObj):
|
||||
@PIKA_C_MACRO_IF("!PIKA_NODE_ENABLE")
|
||||
def __setitem__(obj: any, key: any, val: any, obj_str: str): ...
|
||||
def __getitem__(obj: any, key: any) -> any: ...
|
||||
def print(self, *val): ...
|
||||
def print(self, *val, **options): ...
|
||||
|
||||
class RangeObj(TinyObj):
|
||||
def __next__() -> any:
|
||||
|
@ -55,6 +55,10 @@ impl ArgList {
|
||||
/* if not get ':', get the name */
|
||||
None => String::from(arg_define.to_string()),
|
||||
};
|
||||
if arg_name.starts_with("**") {
|
||||
/* is the dict variable parameter */
|
||||
arg_name = arg_name.strip_prefix("**").unwrap().to_string();
|
||||
}
|
||||
if arg_name.starts_with("*") {
|
||||
/* is the tuple variable parameter */
|
||||
arg_name = arg_name.strip_prefix("*").unwrap().to_string();
|
||||
@ -64,9 +68,11 @@ impl ArgList {
|
||||
Some(name) => name,
|
||||
/* if not get ':', ignore the arg */
|
||||
None => {
|
||||
if arg_define.starts_with("*") {
|
||||
if arg_define.starts_with("*") && arg_define.chars().nth(1).unwrap() != '*' {
|
||||
/* is the tuple variable parameter */
|
||||
String::from("@tupleVarPar")
|
||||
} else if arg_define.starts_with("*") {
|
||||
String::from("@dictVarPar")
|
||||
} else {
|
||||
String::from("")
|
||||
}
|
||||
|
@ -29,6 +29,9 @@ impl PyType {
|
||||
if self.type_name == "@tupleVarPar" {
|
||||
return "PikaTuple*".to_string();
|
||||
}
|
||||
if self.type_name == "@dictVarPar" {
|
||||
return "PikaDict*".to_string();
|
||||
}
|
||||
return "PikaObj*".to_string();
|
||||
}
|
||||
|
||||
@ -126,6 +129,9 @@ impl PyType {
|
||||
if self.type_name == "@tupleVarPar" {
|
||||
return "args_getTuple".to_string();
|
||||
}
|
||||
if self.type_name == "@dictVarPar" {
|
||||
return "args_getDict".to_string();
|
||||
}
|
||||
return "args_getPtr".to_string();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user