pass class demo 1, run.sh not pass

This commit is contained in:
lyon 2022-03-02 15:37:17 +08:00
parent a966d43ad0
commit ee91552bbb
2 changed files with 35 additions and 2 deletions

View File

@ -1126,3 +1126,29 @@ TEST(pikaMain, class_def_print) {
obj_deinit(self); obj_deinit(self);
EXPECT_EQ(pikaMemNow(), 0); EXPECT_EQ(pikaMemNow(), 0);
} }
TEST(pikaMain, class_demo_1) {
/* init */
pikaMemInfo.heapUsedMax = 0;
/* run */
PikaObj* self = newRootObj((char*)"pikaMain", New_PikaMain);
__platform_printf((char*)"BEGIN\r\n");
obj_run(self, (char*)
"class MyClass:\n"
" i = 12345\n"
" def f(self):\n"
" return 'hello world'\n"
" \n"
"x = MyClass()\n"
" \n"
"print(x.i)\n"
"print(x.f())\n"
);
/* assert */
EXPECT_STREQ(log_buff[0], (char*)"hello world\r\n");
EXPECT_STREQ(log_buff[1], (char*)"12345\r\n");
EXPECT_STREQ(log_buff[2], (char*)"BEGIN\r\n");
/* deinit */
obj_deinit(self);
EXPECT_EQ(pikaMemNow(), 0);
}

View File

@ -1441,8 +1441,15 @@ char* AST_toPikaASM(AST* ast, Args* outBuffs) {
if (strEqu(obj_getStr(ast, "block"), "class")) { if (strEqu(obj_getStr(ast, "block"), "class")) {
Args* buffs = New_strBuff(); Args* buffs = New_strBuff();
char* declear = obj_getStr(ast, "declear"); char* declear = obj_getStr(ast, "declear");
char* thisClass = strsGetFirstToken(buffs, declear, '('); char* thisClass = NULL;
char* superClass = strsCut(buffs, declear, '(', ')'); char* superClass = NULL;
if (strIsContain(declear, '(')) {
thisClass = strsGetFirstToken(buffs, declear, '(');
superClass = strsCut(buffs, declear, '(', ')');
} else {
thisClass = declear;
superClass = "";
}
if (strEqu("", superClass)) { if (strEqu("", superClass)) {
/* default superClass */ /* default superClass */
superClass = "PikaStdLib.PikaObj"; superClass = "PikaStdLib.PikaObj";