mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
new Obj by fun directly, not class loader
This commit is contained in:
parent
03ae5a44c7
commit
e5408deb06
3
port/linux/.vscode/settings.json
vendored
3
port/linux/.vscode/settings.json
vendored
@ -4,7 +4,8 @@
|
||||
"stdbool.h": "c",
|
||||
"datamemory.h": "c",
|
||||
"pikamath_operator.h": "c",
|
||||
"dataargs.h": "c"
|
||||
"dataargs.h": "c",
|
||||
"pikastdlib_sysobj.h": "c"
|
||||
},
|
||||
"python.formatting.provider": "yapf"
|
||||
}
|
@ -41,7 +41,7 @@ PikaObj *New_MYROOT(Args *args)
|
||||
obj_import(self, "LED", New_LED);
|
||||
|
||||
/* new led object bellow root object */
|
||||
obj_newObj(self, "led", "LED");
|
||||
obj_newObjFromClassLoader(self, "led", "LED");
|
||||
|
||||
/* return the object */
|
||||
return self;
|
||||
|
@ -36,7 +36,7 @@ PikaObj *New_MYROOT(Args *args)
|
||||
obj_import(self, "TEST", New_TEST);
|
||||
|
||||
/* new led object bellow root object */
|
||||
obj_newObj(self, "test", "TEST");
|
||||
obj_newObjFromClassLoader(self, "test", "TEST");
|
||||
|
||||
/* return the object */
|
||||
return self;
|
||||
|
@ -35,7 +35,7 @@ PikaObj *New_MYROOT(Args *args)
|
||||
obj_import(self, "USART", New_USART);
|
||||
|
||||
/* new led object bellow root object */
|
||||
obj_newObj(self, "usart", "USART");
|
||||
obj_newObjFromClassLoader(self, "usart", "USART");
|
||||
|
||||
/* return the object */
|
||||
return self;
|
||||
|
@ -72,8 +72,8 @@ PikaObj *New_MYROOT(Args *args)
|
||||
obj_import(self, "USART2", New_USART2);
|
||||
|
||||
/* new object bellow root object */
|
||||
obj_newObj(self, "usart1", "USART");
|
||||
obj_newObj(self, "usart2", "USART2");
|
||||
obj_newObjFromClassLoader(self, "usart1", "USART");
|
||||
obj_newObjFromClassLoader(self, "usart2", "USART2");
|
||||
|
||||
/* return the object */
|
||||
return self;
|
||||
|
@ -52,7 +52,7 @@ exit:
|
||||
}
|
||||
|
||||
void PikaStdLib_SysObj_new(PikaObj* self, char* classPath, char* objPath) {
|
||||
int32_t res = obj_newObj(self, objPath, classPath);
|
||||
int32_t res = obj_newObjFromClassLoader(self, objPath, classPath);
|
||||
if (1 == res) {
|
||||
obj_setSysOut(self, "[error] new: class not found .");
|
||||
obj_setErrorCode(self, 1);
|
||||
|
Binary file not shown.
@ -80,11 +80,8 @@ PikaObj* New_MYROOT1(Args* args) {
|
||||
import sub object. */
|
||||
PikaObj* self = New_BaseObj(args);
|
||||
|
||||
/* import LED class */
|
||||
obj_import(self, (char*)"USART", New_USART);
|
||||
|
||||
/* new led object bellow root object */
|
||||
obj_newObj(self, (char*)"usart", (char*)"USART");
|
||||
obj_newObj(self, (char*)"usart", (char*)"USART", (NewFun)New_USART);
|
||||
|
||||
/* return the object */
|
||||
return self;
|
||||
@ -126,7 +123,8 @@ TEST(object_test, test3) {
|
||||
// obj_setInt(obj, (char*)"isShow", isShow);
|
||||
// obj_setFloat(obj, (char*)"val2", 3.11);
|
||||
// obj_run(obj,
|
||||
// (char*)"res = testFloat(val1 = 3.22,val2 = val2,isShow = isShow)");
|
||||
// (char*)"res = testFloat(val1 = 3.22,val2 = val2,isShow =
|
||||
// isShow)");
|
||||
// float res = obj_getFloat(obj, (char*)"res");
|
||||
// EXPECT_FLOAT_EQ(res, 6.33);
|
||||
// obj_deinit(obj);
|
||||
@ -187,18 +185,7 @@ TEST(object_test, test10) {
|
||||
|
||||
TEST(object_test, newObject) {
|
||||
PikaObj* root = newRootObj((char*)"root", New_MYROOT1);
|
||||
obj_newObj(root, (char*)"newUart", (char*)"USART");
|
||||
obj_deinit(root);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(object_test, newObjectAndSetStr) {
|
||||
PikaObj* root = newRootObj((char*)"root", New_MYROOT1);
|
||||
obj_newObj(root, (char*)"newUart", (char*)"USART");
|
||||
obj_setStr(root, (char*)"newUart.name", (char*)"testName");
|
||||
char* name = obj_getStr(root, (char*)"newUart.name");
|
||||
printf("the name is %s\r\n", name);
|
||||
EXPECT_TRUE(strEqu((char*)"testName", name));
|
||||
obj_newObjFromClassLoader(root, (char*)"newUart", (char*)"USART");
|
||||
obj_deinit(root);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
@ -15,10 +15,10 @@ static void* getClassPtr(PikaObj* classObj, char* classPath) {
|
||||
return obj_getPtr(classObj, ptrPath);
|
||||
}
|
||||
|
||||
int32_t obj_newObjByFun(PikaObj* self,
|
||||
char* objName,
|
||||
char* className,
|
||||
void* newFunPtr) {
|
||||
int32_t obj_newObj(PikaObj* self,
|
||||
char* objName,
|
||||
char* className,
|
||||
NewFun newFunPtr) {
|
||||
/* class means subprocess init */
|
||||
Args* buffs = New_strBuff();
|
||||
|
||||
@ -75,7 +75,9 @@ int32_t obj_import(PikaObj* self, char* className, NewFun classPtr) {
|
||||
return res;
|
||||
}
|
||||
|
||||
int32_t obj_newObj(PikaObj* self, char* objPath, char* classPath) {
|
||||
int32_t obj_newObjFromClassLoader(PikaObj* self,
|
||||
char* objPath,
|
||||
char* classPath) {
|
||||
PikaObj* classLoader = obj_getObj(self, "_clsld", 0);
|
||||
Args* buffs = New_args(NULL);
|
||||
int res = 0;
|
||||
@ -90,7 +92,7 @@ int32_t obj_newObj(PikaObj* self, char* objPath, char* classPath) {
|
||||
goto exit;
|
||||
}
|
||||
char* objName = strsGetLastToken(buffs, objPath, '.');
|
||||
obj_newObjByFun(objHost, objName, classPath, NewObjPtr);
|
||||
obj_newObj(objHost, objName, classPath, NewObjPtr);
|
||||
res = 0;
|
||||
goto exit;
|
||||
|
||||
@ -152,8 +154,6 @@ static void set(PikaObj* self, Args* args) {
|
||||
|
||||
PikaObj* New_BaseObj(Args* args) {
|
||||
PikaObj* self = New_TinyObj(args);
|
||||
obj_setObjWithoutClass(self, "_clsld", New_TinyObj);
|
||||
obj_getObj(self, "_clsld", 0);
|
||||
class_defineMethod(self, "print(val:any)", print);
|
||||
class_defineMethod(self, "set(argPath:str, val:any)", set);
|
||||
return self;
|
||||
|
@ -10,10 +10,12 @@
|
||||
#include "dataMemory.h"
|
||||
|
||||
PikaObj* New_BaseObj(Args* args);
|
||||
int32_t obj_newObj(PikaObj* self, char* objPath, char* classPath);
|
||||
int32_t obj_newObjFromClassLoader(PikaObj* self,
|
||||
char* objPath,
|
||||
char* classPath);
|
||||
int32_t obj_import(PikaObj* self, char* className, NewFun classPtr);
|
||||
int32_t obj_newObjByFun(PikaObj* self,
|
||||
char* objName,
|
||||
char* className,
|
||||
void* newFunPtr);
|
||||
int32_t obj_newObj(PikaObj* self,
|
||||
char* objName,
|
||||
char* className,
|
||||
NewFun newFunPtr);
|
||||
#endif
|
||||
|
@ -462,7 +462,7 @@ Args* obj_runDirect(PikaObj* self, char* cmd) {
|
||||
classPath[i] = '_';
|
||||
}
|
||||
}
|
||||
if (0 == obj_newObj(self, newObj, classPath)) {
|
||||
if (0 == obj_newObjFromClassLoader(self, newObj, classPath)) {
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
@ -150,7 +150,7 @@ AST* pikaParseLine(char* line, Stack* blockStack) {
|
||||
for (int i = 0; i < blockDeepthLast - blockDeepth; i++) {
|
||||
QueueObj* exitBlock = obj_getObj(ast, "exitBlock", 0);
|
||||
if (NULL == exitBlock) {
|
||||
obj_newObjByFun(ast, "exitBlock", "", New_TinyObj);
|
||||
obj_newObj(ast, "exitBlock", "", New_TinyObj);
|
||||
exitBlock = obj_getObj(ast, "exitBlock", 0);
|
||||
queueObj_init(exitBlock);
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ int32_t queueObj_pushObj(QueueObj* self, char* className) {
|
||||
char *topStr = fast_itoa(buff, top);
|
||||
/* add top */
|
||||
obj_setInt(self, "top", top + 1);
|
||||
return obj_newObjByFun(self, topStr, className, New_TinyObj);
|
||||
return obj_newObj(self, topStr, className, New_TinyObj);
|
||||
}
|
||||
|
||||
PikaObj* queueObj_getCurrentObj(QueueObj* self) {
|
||||
|
@ -126,12 +126,8 @@ impl ClassInfo {
|
||||
for (_, import_info) in self.import_list.iter() {
|
||||
new_class_fn.push_str(&import_info.import_fn());
|
||||
}
|
||||
let mut if_only_import: bool = false;
|
||||
if self.this_class_name == "PikaMain" {
|
||||
if_only_import= true;
|
||||
}
|
||||
for (_, object_info) in self.object_list.iter() {
|
||||
new_class_fn.push_str(&object_info.new_object_fn(if_only_import));
|
||||
new_class_fn.push_str(&object_info.new_object_fn());
|
||||
}
|
||||
|
||||
for (_, method_info) in self.method_list.iter() {
|
||||
|
@ -32,20 +32,12 @@ impl ObjectInfo {
|
||||
import_class_name: import_class_name,
|
||||
});
|
||||
}
|
||||
pub fn new_object_fn(&self, is_only_import: bool) -> String {
|
||||
pub fn new_object_fn(&self) -> String {
|
||||
let mut new_object_fn = String::new();
|
||||
let import_fn = format!(
|
||||
" obj_import(self, \"{}\", New_{});\n",
|
||||
self.import_class_name, self.import_class_name
|
||||
);
|
||||
new_object_fn.push_str(&import_fn);
|
||||
/* do not generate new object for pikaMain class */
|
||||
if is_only_import {
|
||||
return new_object_fn;
|
||||
}
|
||||
let new_fn = format!(
|
||||
" obj_newObj(self, \"{}\", \"{}\");\n",
|
||||
self.name, self.import_class_name
|
||||
" obj_newObj(self, \"{}\", \"{}\", New_{});\n",
|
||||
self.name, self.import_class_name, self.import_class_name
|
||||
);
|
||||
new_object_fn.push_str(&new_fn);
|
||||
return new_object_fn;
|
||||
|
Loading…
x
Reference in New Issue
Block a user