Skip to content

Commit e860275

Browse files
committed
support __str__ in for print
1 parent 6ebae91 commit e860275

File tree

10 files changed

+147
-89
lines changed

10 files changed

+147
-89
lines changed

package/PikaMath/PikaMath.pyi

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,18 @@
1-
#api
21
from PikaObj import *
32

4-
class Operator(TinyObj):
5-
def plusInt(num1: int, num2: int) -> int:
6-
pass
7-
8-
def plusFloat(num1: float, num2: float) -> float:
9-
pass
10-
11-
def minusInt(num1: int, num2: int) -> int:
12-
pass
13-
14-
def minusFloat(num1: float, num2: float) -> float:
15-
pass
16-
17-
def equalInt(num1: int, num2: int) -> int:
18-
pass
19-
20-
def equalFloat(num1: float, num2: float) -> int:
21-
pass
22-
23-
def graterThanInt(num1: int, num2: int) -> int:
24-
pass
253

26-
def graterThanFloat(num1: float, num2: float) -> int:
27-
pass
28-
29-
def lessThanInt(num1: int, num2: int) -> int:
30-
pass
31-
32-
def lessThanFloat(num1: float, num2: float) -> int:
33-
pass
34-
35-
def AND(flag1: int, flag2: int) -> int:
36-
pass
37-
38-
def OR(flag1: int, flag2: int) -> int:
39-
pass
40-
41-
def NOT(flag: int) -> int:
42-
pass
4+
class Operator(TinyObj):
5+
def plusInt(self, num1: int, num2: int) -> int: ...
6+
def plusFloat(self, num1: float, num2: float) -> float: ...
7+
def minusInt(self, num1: int, num2: int) -> int: ...
8+
def minusFloat(self, num1: float, num2: float) -> float: ...
9+
def equalInt(self, num1: int, num2: int) -> int: ...
10+
def equalFloat(self, num1: float, num2: float) -> int: ...
11+
def graterThanInt(self, num1: int, num2: int) -> int: ...
12+
def graterThanFloat(self, num1: float, num2: float) -> int: ...
13+
def lessThanInt(self, num1: int, num2: int) -> int: ...
14+
def lessThanFloat(self, num1: float, num2: float) -> int: ...
15+
def AND(self, flag1: int, flag2: int) -> int: ...
16+
def OR(self, flag1: int, flag2: int) -> int: ...
17+
def NOT(self, flag: int) -> int: ...
18+
def __str__(self) -> str: ...

package/PikaMath/PikaMath_Operator.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@ float PikaMath_Operator_minusFloat(PikaObj* self, float num1, float num2) {
3333
int PikaMath_Operator_minusInt(PikaObj* self, int num1, int num2) {
3434
return num1 - num2;
3535
}
36+
3637
float PikaMath_Operator_plusFloat(PikaObj* self, float num1, float num2) {
3738
return num1 + num2;
3839
}
40+
3941
int PikaMath_Operator_plusInt(PikaObj* self, int num1, int num2) {
4042
return num1 + num2;
41-
}
43+
}
44+
45+
char* PikaMath_Operator___str__(PikaObj *self){
46+
obj_setStr(self, "__buf", "test");
47+
return obj_getStr(self, "__buf");
48+
}

package/PikaStdLib/PikaStdTask_Task.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
extern PikaObj* __pikaMain;
55
void PikaStdTask_Task___init__(PikaObj* self) {
66
PIKA_PYTHON_BEGIN
7+
/* clang-format off */
78
PIKA_PYTHON(
89
calls.__init__()
910
is_period = 0
1011
)
12+
/* clang-format on */
1113
const uint8_t bytes[] = {
1214
0x0c, 0x00, /* instruct array size */
1315
0x00, 0x82, 0x01, 0x00, 0x00, 0x85, 0x10, 0x00, 0x00, 0x04, 0x12, 0x00,
@@ -25,10 +27,12 @@ void PikaStdTask_Task___init__(PikaObj* self) {
2527
void PikaStdTask_Task_call_always(PikaObj* self, Arg* fun_todo) {
2628
obj_setArg(self, "fun_todo", fun_todo);
2729
PIKA_PYTHON_BEGIN
30+
/* clang-format off */
2831
PIKA_PYTHON(
2932
calls.append('always')
3033
calls.append(fun_todo)
3134
)
35+
/* clang-format on */
3236
const uint8_t bytes[] = {
3337
0x10, 0x00, /* instruct array size */
3438
0x10, 0x83, 0x01, 0x00, 0x00, 0x02, 0x08, 0x00, 0x10, 0x81, 0x15,
@@ -46,11 +50,13 @@ void PikaStdTask_Task_call_when(PikaObj* self, Arg* fun_todo, Arg* fun_when) {
4650
obj_setArg(self, "fun_todo", fun_todo);
4751
obj_setArg(self, "fun_when", fun_when);
4852
PIKA_PYTHON_BEGIN
53+
/* clang-format off */
4954
PIKA_PYTHON(
5055
calls.append('when')
5156
calls.append(fun_when)
5257
calls.append(fun_todo)
5358
)
59+
/* clang-format on */
5460
const uint8_t bytes[] = {
5561
0x18, 0x00, /* instruct array size */
5662
0x10, 0x83, 0x01, 0x00, 0x00, 0x02, 0x06, 0x00, 0x10, 0x81, 0x13, 0x00,
@@ -73,13 +79,15 @@ void PikaStdTask_Task_call_period_ms(PikaObj* self,
7379
obj_setArg(self, "fun_todo", fun_todo);
7480
obj_setInt(self, "period_ms", period_ms);
7581
PIKA_PYTHON_BEGIN
82+
/* clang-format off */
7683
PIKA_PYTHON(
7784
calls.append('period_ms')
7885
calls.append(period_ms)
7986
calls.append(fun_todo)
8087
calls.append(0)
8188
is_period = 1
8289
)
90+
/* clang-format on */
8391
const uint8_t bytes[] =
8492
{
8593
0x28, 0x00, /* instruct array size */
@@ -102,6 +110,7 @@ void PikaStdTask_Task_run_once(PikaObj* self) {
102110
/* transfer the tick to pikaMain */
103111
obj_setInt(__pikaMain, "__tick", obj_getInt(self, "tick"));
104112
PIKA_PYTHON_BEGIN
113+
/* clang-format off */
105114
PIKA_PYTHON(
106115
len = __calls.len()
107116
mode = 'none'
@@ -139,6 +148,7 @@ void PikaStdTask_Task_run_once(PikaObj* self) {
139148
__calls[i] = __tick + period_ms
140149
info_index = 0
141150
)
151+
/* clang-format on */
142152
const uint8_t bytes[] = {
143153
0x00, 0x02, /* instruct array size */
144154
0x00, 0x82, 0x01, 0x00, 0x00, 0x04, 0x0d, 0x00, 0x00, 0x83, 0x11, 0x00,
@@ -208,9 +218,11 @@ void PikaStdTask_Task_run_once(PikaObj* self) {
208218
void __Task_update_tick(PikaObj* self) {
209219
if (obj_getInt(self, "is_perod")) {
210220
PIKA_PYTHON_BEGIN
221+
/* clang-format off */
211222
PIKA_PYTHON(
212223
platformGetTick()
213224
)
225+
/* clang-format on */
214226
const uint8_t bytes[] = {
215227
0x04, 0x00, /* instruct array size */
216228
0x00, 0x82, 0x01, 0x00, /* instruct array */
Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,18 @@
1-
#api
21
from PikaObj import *
32

4-
class Operator(TinyObj):
5-
def plusInt(self, num1: int, num2: int) -> int:
6-
pass
7-
8-
def plusFloat(self, num1: float, num2: float) -> float:
9-
pass
10-
11-
def minusInt(self, num1: int, num2: int) -> int:
12-
pass
13-
14-
def minusFloat(self, num1: float, num2: float) -> float:
15-
pass
16-
17-
def equalInt(self, num1: int, num2: int) -> int:
18-
pass
19-
20-
def equalFloat(self, num1: float, num2: float) -> int:
21-
pass
22-
23-
def graterThanInt(self, num1: int, num2: int) -> int:
24-
pass
253

26-
def graterThanFloat(self, num1: float, num2: float) -> int:
27-
pass
28-
29-
def lessThanInt(self, num1: int, num2: int) -> int:
30-
pass
31-
32-
def lessThanFloat(self, num1: float, num2: float) -> int:
33-
pass
34-
35-
def AND(self, flag1: int, flag2: int) -> int:
36-
pass
37-
38-
def OR(self, flag1: int, flag2: int) -> int:
39-
pass
40-
41-
def NOT(self, flag: int) -> int:
42-
pass
4+
class Operator(TinyObj):
5+
def plusInt(self, num1: int, num2: int) -> int: ...
6+
def plusFloat(self, num1: float, num2: float) -> float: ...
7+
def minusInt(self, num1: int, num2: int) -> int: ...
8+
def minusFloat(self, num1: float, num2: float) -> float: ...
9+
def equalInt(self, num1: int, num2: int) -> int: ...
10+
def equalFloat(self, num1: float, num2: float) -> int: ...
11+
def graterThanInt(self, num1: int, num2: int) -> int: ...
12+
def graterThanFloat(self, num1: float, num2: float) -> int: ...
13+
def lessThanInt(self, num1: int, num2: int) -> int: ...
14+
def lessThanFloat(self, num1: float, num2: float) -> int: ...
15+
def AND(self, flag1: int, flag2: int) -> int: ...
16+
def OR(self, flag1: int, flag2: int) -> int: ...
17+
def NOT(self, flag: int) -> int: ...
18+
def __str__(self) -> str: ...

port/linux/package/pikascript/pikascript-lib/PikaMath/PikaMath_Operator.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@ float PikaMath_Operator_minusFloat(PikaObj* self, float num1, float num2) {
3333
int PikaMath_Operator_minusInt(PikaObj* self, int num1, int num2) {
3434
return num1 - num2;
3535
}
36+
3637
float PikaMath_Operator_plusFloat(PikaObj* self, float num1, float num2) {
3738
return num1 + num2;
3839
}
40+
3941
int PikaMath_Operator_plusInt(PikaObj* self, int num1, int num2) {
4042
return num1 + num2;
41-
}
43+
}
44+
45+
char* PikaMath_Operator___str__(PikaObj *self){
46+
obj_setStr(self, "__buf", "test");
47+
return obj_getStr(self, "__buf");
48+
}

port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdTask_Task.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
extern PikaObj* __pikaMain;
55
void PikaStdTask_Task___init__(PikaObj* self) {
66
PIKA_PYTHON_BEGIN
7+
/* clang-format off */
78
PIKA_PYTHON(
89
calls.__init__()
910
is_period = 0
1011
)
12+
/* clang-format on */
1113
const uint8_t bytes[] = {
1214
0x0c, 0x00, /* instruct array size */
1315
0x00, 0x82, 0x01, 0x00, 0x00, 0x85, 0x10, 0x00, 0x00, 0x04, 0x12, 0x00,
@@ -25,10 +27,12 @@ void PikaStdTask_Task___init__(PikaObj* self) {
2527
void PikaStdTask_Task_call_always(PikaObj* self, Arg* fun_todo) {
2628
obj_setArg(self, "fun_todo", fun_todo);
2729
PIKA_PYTHON_BEGIN
30+
/* clang-format off */
2831
PIKA_PYTHON(
2932
calls.append('always')
3033
calls.append(fun_todo)
3134
)
35+
/* clang-format on */
3236
const uint8_t bytes[] = {
3337
0x10, 0x00, /* instruct array size */
3438
0x10, 0x83, 0x01, 0x00, 0x00, 0x02, 0x08, 0x00, 0x10, 0x81, 0x15,
@@ -46,11 +50,13 @@ void PikaStdTask_Task_call_when(PikaObj* self, Arg* fun_todo, Arg* fun_when) {
4650
obj_setArg(self, "fun_todo", fun_todo);
4751
obj_setArg(self, "fun_when", fun_when);
4852
PIKA_PYTHON_BEGIN
53+
/* clang-format off */
4954
PIKA_PYTHON(
5055
calls.append('when')
5156
calls.append(fun_when)
5257
calls.append(fun_todo)
5358
)
59+
/* clang-format on */
5460
const uint8_t bytes[] = {
5561
0x18, 0x00, /* instruct array size */
5662
0x10, 0x83, 0x01, 0x00, 0x00, 0x02, 0x06, 0x00, 0x10, 0x81, 0x13, 0x00,
@@ -73,13 +79,15 @@ void PikaStdTask_Task_call_period_ms(PikaObj* self,
7379
obj_setArg(self, "fun_todo", fun_todo);
7480
obj_setInt(self, "period_ms", period_ms);
7581
PIKA_PYTHON_BEGIN
82+
/* clang-format off */
7683
PIKA_PYTHON(
7784
calls.append('period_ms')
7885
calls.append(period_ms)
7986
calls.append(fun_todo)
8087
calls.append(0)
8188
is_period = 1
8289
)
90+
/* clang-format on */
8391
const uint8_t bytes[] =
8492
{
8593
0x28, 0x00, /* instruct array size */
@@ -102,6 +110,7 @@ void PikaStdTask_Task_run_once(PikaObj* self) {
102110
/* transfer the tick to pikaMain */
103111
obj_setInt(__pikaMain, "__tick", obj_getInt(self, "tick"));
104112
PIKA_PYTHON_BEGIN
113+
/* clang-format off */
105114
PIKA_PYTHON(
106115
len = __calls.len()
107116
mode = 'none'
@@ -139,6 +148,7 @@ void PikaStdTask_Task_run_once(PikaObj* self) {
139148
__calls[i] = __tick + period_ms
140149
info_index = 0
141150
)
151+
/* clang-format on */
142152
const uint8_t bytes[] = {
143153
0x00, 0x02, /* instruct array size */
144154
0x00, 0x82, 0x01, 0x00, 0x00, 0x04, 0x0d, 0x00, 0x00, 0x83, 0x11, 0x00,
@@ -208,9 +218,11 @@ void PikaStdTask_Task_run_once(PikaObj* self) {
208218
void __Task_update_tick(PikaObj* self) {
209219
if (obj_getInt(self, "is_perod")) {
210220
PIKA_PYTHON_BEGIN
221+
/* clang-format off */
211222
PIKA_PYTHON(
212223
platformGetTick()
213224
)
225+
/* clang-format on */
214226
const uint8_t bytes[] = {
215227
0x04, 0x00, /* instruct array size */
216228
0x00, 0x82, 0x01, 0x00, /* instruct array */

port/linux/test/compile-test.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,13 @@ TEST(make, compile_link_all) {
555555
pikaMaker_setPWD(maker, "package/pikascript/");
556556
pikaMaker_compileModuleWithDepends(maker, "main");
557557
pikaMaker_printStates(maker);
558-
pikaMaker_linkCompiledModules(maker, "pikaModules.py.a") ;
558+
pikaMaker_linkCompiledModules(maker, "pikaModules.py.a");
559559
obj_deinit(maker);
560560
EXPECT_EQ(pikaMemNow(), 0);
561561
}
562+
563+
TEST(compiler, __str__) {
564+
char* lines = "__res = __str__()";
565+
Parser_compilePyToBytecodeArray(lines);
566+
EXPECT_EQ(pikaMemNow(), 0);
567+
}

port/linux/test/pikaMain-test.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,3 +1744,40 @@ TEST(pikaMain, cmodule_in_py) {
17441744
obj_deinit(pikaMain);
17451745
EXPECT_EQ(pikaMemNow(), 0);
17461746
}
1747+
1748+
TEST(pikaMain, class___str___print) {
1749+
/* init */
1750+
pikaMemInfo.heapUsedMax = 0;
1751+
/* run */
1752+
PikaObj* self = newRootObj("pikaMain", New_PikaMain);
1753+
__platform_printf("BEGIN\r\n");
1754+
obj_run(self,
1755+
"class Test():\n"
1756+
" def __str__(self):\n"
1757+
" return 'test'\n"
1758+
"test = Test()\n"
1759+
"print(test)\n");
1760+
/* assert */
1761+
EXPECT_STREQ(log_buff[0], "test\r\n");
1762+
EXPECT_STREQ(log_buff[1], "BEGIN\r\n");
1763+
/* deinit */
1764+
obj_deinit(self);
1765+
EXPECT_EQ(pikaMemNow(), 0);
1766+
}
1767+
1768+
TEST(pikaMain, CModule__str__) {
1769+
/* init */
1770+
pikaMemInfo.heapUsedMax = 0;
1771+
/* run */
1772+
PikaObj* self = newRootObj("pikaMain", New_PikaMain);
1773+
__platform_printf("BEGIN\r\n");
1774+
obj_run(self,
1775+
"op = PikaMath.Operator()\n"
1776+
"print(op)\n");
1777+
/* assert */
1778+
EXPECT_STREQ(log_buff[0], "test\r\n");
1779+
EXPECT_STREQ(log_buff[1], "BEGIN\r\n");
1780+
/* deinit */
1781+
obj_deinit(self);
1782+
EXPECT_EQ(pikaMemNow(), 0);
1783+
}

0 commit comments

Comments
 (0)