Skip to content

Commit 7936348

Browse files
committed
!33 support class parameters
* update package * class_par is tested ok
1 parent 97803a6 commit 7936348

File tree

9 files changed

+146
-95
lines changed

9 files changed

+146
-95
lines changed

package/PikaMath/PikaMath.pyi

+39-36
Original file line numberDiff line numberDiff line change
@@ -18,84 +18,87 @@ class Operator(TinyObj):
1818
def __str__(self) -> str: ...
1919
def __del__(self): ...
2020

21+
2122
class Math(TinyObj):
23+
pi: float
24+
e: float
25+
2226
def __init__(self):
2327
pass
2428

25-
def ceil(self, x:float) ->int:
29+
def ceil(self, x: float) -> int:
2630
pass
27-
28-
def fabs(self, x:float) ->float:
31+
32+
def fabs(self, x: float) -> float:
2933
pass
3034

31-
def floor(self, x:float) ->int:
35+
def floor(self, x: float) -> int:
3236
pass
3337

34-
def fmod(self, x:float,y:float) ->float:
38+
def fmod(self, x: float, y: float) -> float:
3539
pass
3640

37-
def remainder(self ,x:float, y:float) ->float:
41+
def remainder(self, x: float, y: float) -> float:
3842
pass
3943

40-
def trunc(self, x:float) ->float:
44+
def trunc(self, x: float) -> float:
4145
pass
4246

43-
#幂函数和对数函数
47+
# 幂函数和对数函数
4448

45-
def exp(self, x:float) ->float:
49+
def exp(self, x: float) -> float:
4650
pass
47-
48-
def log(self, x:float) ->float:
51+
52+
def log(self, x: float) -> float:
4953
pass
50-
51-
def log2(self, x:float) ->float:
54+
55+
def log2(self, x: float) -> float:
5256
pass
5357

54-
def log10(self, x:float) ->float:
58+
def log10(self, x: float) -> float:
5559
pass
5660

57-
def pow(self, x:float, y:float) ->float:
61+
def pow(self, x: float, y: float) -> float:
5862
pass
5963

60-
def sqrt(self, x:float) ->float:
64+
def sqrt(self, x: float) -> float:
6165
pass
6266

63-
#三角函数
64-
def acos(self, x:float) ->float:
67+
# 三角函数
68+
def acos(self, x: float) -> float:
6569
pass
66-
67-
def asin(self, x:float) ->float:
70+
71+
def asin(self, x: float) -> float:
6872
pass
6973

70-
def atan(self, x:float) ->float:
74+
def atan(self, x: float) -> float:
7175
pass
7276

73-
def atan2(self, x:float,y:float) ->float:
77+
def atan2(self, x: float, y: float) -> float:
7478
pass
75-
76-
def cos(self, x:float) ->float:
79+
80+
def cos(self, x: float) -> float:
7781
pass
7882

79-
def sin(self, x:float) ->float:
83+
def sin(self, x: float) -> float:
8084
pass
81-
82-
def tan(self, x:float) ->float:
85+
86+
def tan(self, x: float) -> float:
8387
pass
84-
85-
#角度转换
86-
def degrees(self, x:float) ->float:
88+
89+
# 角度转换
90+
def degrees(self, x: float) -> float:
8791
pass
8892

89-
def radians(self, x:float) ->float:
93+
def radians(self, x: float) -> float:
9094
pass
9195

92-
#双曲函数
93-
def cosh(self, x:float) ->float:
96+
# 双曲函数
97+
def cosh(self, x: float) -> float:
9498
pass
9599

96-
def sinh(self, x:float) ->float:
100+
def sinh(self, x: float) -> float:
97101
pass
98102

99-
def tanh(self, x:float) ->float:
103+
def tanh(self, x: float) -> float:
100104
pass
101-

port/linux/.vscode/launch.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"program": "${workspaceFolder}/build/test/pikascript_test",
1212
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
1313
"args": [
14-
// "--gtest_filter=compiler*"
14+
"--gtest_filter=class.class_par"
1515
],
1616
"stopAtEntry": false,
1717
"cwd": "${workspaceFolder}",

port/linux/fast_test.sh

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
ROOT=$PWD
2+
rm $(find build -name *.gcda) -f
23
cd build && rm ./test/pikascript_test -f && ninja -j0
34
cd $ROOT
45
build/test/pikascript_test

port/linux/package/pikascript/PikaMath.pyi

+39-36
Original file line numberDiff line numberDiff line change
@@ -18,84 +18,87 @@ class Operator(TinyObj):
1818
def __str__(self) -> str: ...
1919
def __del__(self): ...
2020

21+
2122
class Math(TinyObj):
23+
pi: float
24+
e: float
25+
2226
def __init__(self):
2327
pass
2428

25-
def ceil(self, x:float) ->int:
29+
def ceil(self, x: float) -> int:
2630
pass
27-
28-
def fabs(self, x:float) ->float:
31+
32+
def fabs(self, x: float) -> float:
2933
pass
3034

31-
def floor(self, x:float) ->int:
35+
def floor(self, x: float) -> int:
3236
pass
3337

34-
def fmod(self, x:float,y:float) ->float:
38+
def fmod(self, x: float, y: float) -> float:
3539
pass
3640

37-
def remainder(self ,x:float, y:float) ->float:
41+
def remainder(self, x: float, y: float) -> float:
3842
pass
3943

40-
def trunc(self, x:float) ->float:
44+
def trunc(self, x: float) -> float:
4145
pass
4246

43-
#幂函数和对数函数
47+
# 幂函数和对数函数
4448

45-
def exp(self, x:float) ->float:
49+
def exp(self, x: float) -> float:
4650
pass
47-
48-
def log(self, x:float) ->float:
51+
52+
def log(self, x: float) -> float:
4953
pass
50-
51-
def log2(self, x:float) ->float:
54+
55+
def log2(self, x: float) -> float:
5256
pass
5357

54-
def log10(self, x:float) ->float:
58+
def log10(self, x: float) -> float:
5559
pass
5660

57-
def pow(self, x:float, y:float) ->float:
61+
def pow(self, x: float, y: float) -> float:
5862
pass
5963

60-
def sqrt(self, x:float) ->float:
64+
def sqrt(self, x: float) -> float:
6165
pass
6266

63-
#三角函数
64-
def acos(self, x:float) ->float:
67+
# 三角函数
68+
def acos(self, x: float) -> float:
6569
pass
66-
67-
def asin(self, x:float) ->float:
70+
71+
def asin(self, x: float) -> float:
6872
pass
6973

70-
def atan(self, x:float) ->float:
74+
def atan(self, x: float) -> float:
7175
pass
7276

73-
def atan2(self, x:float,y:float) ->float:
77+
def atan2(self, x: float, y: float) -> float:
7478
pass
75-
76-
def cos(self, x:float) ->float:
79+
80+
def cos(self, x: float) -> float:
7781
pass
7882

79-
def sin(self, x:float) ->float:
83+
def sin(self, x: float) -> float:
8084
pass
81-
82-
def tan(self, x:float) ->float:
85+
86+
def tan(self, x: float) -> float:
8387
pass
84-
85-
#角度转换
86-
def degrees(self, x:float) ->float:
88+
89+
# 角度转换
90+
def degrees(self, x: float) -> float:
8791
pass
8892

89-
def radians(self, x:float) ->float:
93+
def radians(self, x: float) -> float:
9094
pass
9195

92-
#双曲函数
93-
def cosh(self, x:float) ->float:
96+
# 双曲函数
97+
def cosh(self, x: float) -> float:
9498
pass
9599

96-
def sinh(self, x:float) ->float:
100+
def sinh(self, x: float) -> float:
97101
pass
98102

99-
def tanh(self, x:float) ->float:
103+
def tanh(self, x: float) -> float:
100104
pass
101-

port/linux/test/class-test.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "test_common.h"
2+
3+
TEST(class, class_par) {
4+
/* init */
5+
pikaMemInfo.heapUsedMax = 0;
6+
/* run */
7+
PikaObj* self = newRootObj("pikaMain", New_PikaMain);
8+
__platform_printf("BEGIN\r\n");
9+
obj_run(self, "pi = PikaMath.Math.pi\n");
10+
/* assert */
11+
double pi = obj_getFloat(self, "pi");
12+
EXPECT_DOUBLE_EQ(pi, 3.141592653589793115997963468544185161590576171875l);
13+
/* deinit */
14+
obj_deinit(self);
15+
EXPECT_EQ(pikaMemNow(), 0);
16+
}

src/PikaObj.c

+35-5
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,18 @@ int64_t obj_getInt(PikaObj* self, char* argPath) {
194194
}
195195

196196
Arg* obj_getArg(PikaObj* self, char* argPath) {
197-
PikaObj* obj = obj_getHostObj(self, argPath);
197+
PIKA_BOOL isClass = 0;
198+
PikaObj* obj = obj_getHostObjWithIsClass(self, argPath, &isClass);
198199
if (NULL == obj) {
199200
return NULL;
200201
}
201202
char* argName = strPointToLastToken(argPath, '.');
202203
Arg* res = args_getArg(obj->list, argName);
204+
if (isClass) {
205+
obj_setArg(self, "_buf", res);
206+
res = obj_getArg(self, "_buf");
207+
obj_deinit(obj);
208+
}
203209
return res;
204210
}
205211

@@ -410,7 +416,10 @@ static PikaObj* __obj_initSubObj(PikaObj* obj, char* name) {
410416
return res;
411417
}
412418

413-
static PikaObj* __obj_getObjDirect(PikaObj* self, char* name) {
419+
static PikaObj* __obj_getObjDirect(PikaObj* self,
420+
char* name,
421+
PIKA_BOOL* pIsClass) {
422+
*pIsClass = PIKA_FALSE;
414423
if (NULL == self) {
415424
return NULL;
416425
}
@@ -424,11 +433,24 @@ static PikaObj* __obj_getObjDirect(PikaObj* self, char* name) {
424433
if (argType_isObject(type)) {
425434
return args_getPtr(self->list, name);
426435
}
436+
/* found class */
437+
if (type == ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR) {
438+
*pIsClass = PIKA_TRUE;
439+
PikaObj* method_args_obj = New_TinyObj(NULL);
440+
Arg* cls_obj_arg = obj_runMethodArg(self, method_args_obj,
441+
args_getArg(self->list, name));
442+
obj_deinit(method_args_obj);
443+
obj_runNativeMethod(arg_getPtr(cls_obj_arg), "__init__", NULL);
444+
PikaObj* res = arg_getPtr(cls_obj_arg);
445+
arg_deinit(cls_obj_arg);
446+
return res;
447+
}
427448
return NULL;
428449
}
429450

430451
static PikaObj* __obj_getObjWithKeepDeepth(PikaObj* self,
431452
char* objPath,
453+
PIKA_BOOL* pIsClass,
432454
int32_t keepDeepth) {
433455
char objPath_buff[PIKA_PATH_BUFF_SIZE];
434456
__platform_memcpy(objPath_buff, objPath, strGetSize(objPath) + 1);
@@ -437,7 +459,7 @@ static PikaObj* __obj_getObjWithKeepDeepth(PikaObj* self,
437459
PikaObj* obj = self;
438460
for (int32_t i = 0; i < token_num - keepDeepth; i++) {
439461
char* token = strPopToken(token_buff, objPath_buff, '.');
440-
obj = __obj_getObjDirect(obj, token);
462+
obj = __obj_getObjDirect(obj, token, pIsClass);
441463
if (obj == NULL) {
442464
goto exit;
443465
}
@@ -448,11 +470,19 @@ static PikaObj* __obj_getObjWithKeepDeepth(PikaObj* self,
448470
}
449471

450472
PikaObj* obj_getObj(PikaObj* self, char* objPath) {
451-
return __obj_getObjWithKeepDeepth(self, objPath, 0);
473+
PIKA_BOOL isClass = 0;
474+
return __obj_getObjWithKeepDeepth(self, objPath, &isClass, 0);
452475
}
453476

454477
PikaObj* obj_getHostObj(PikaObj* self, char* objPath) {
455-
return __obj_getObjWithKeepDeepth(self, objPath, 1);
478+
PIKA_BOOL isClass = 0;
479+
return __obj_getObjWithKeepDeepth(self, objPath, &isClass, 1);
480+
}
481+
482+
PikaObj* obj_getHostObjWithIsClass(PikaObj* self,
483+
char* objPath,
484+
PIKA_BOOL* pIsClass) {
485+
return __obj_getObjWithKeepDeepth(self, objPath, pIsClass, 1);
456486
}
457487

458488
Method methodArg_getPtr(Arg* method_arg) {

src/PikaObj.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,15 @@ int32_t obj_load(PikaObj* self, Args* args, char* name);
124124
int32_t obj_addOther(PikaObj* self, char* subObjectName, void* new_projcetFun);
125125
PikaObj* obj_getObj(PikaObj* self, char* objPath);
126126
PikaObj* obj_getHostObj(PikaObj* self, char* objPath);
127+
PikaObj* obj_getHostObjWithIsClass(PikaObj* self,
128+
char* objPath,
129+
PIKA_BOOL* pIsClass);
127130

128131
// subProcess
129132
int32_t obj_freeObj(PikaObj* self, char* subObjectName);
130133

131134
/* method */
132-
int32_t class_defineMethod(PikaObj* self,
133-
char* declearation,
134-
Method methodPtr);
135+
int32_t class_defineMethod(PikaObj* self, char* declearation, Method methodPtr);
135136

136137
int32_t class_defineObjectMethod(PikaObj* self,
137138
char* declearation,

0 commit comments

Comments
 (0)