Skip to content

Commit 320ded1

Browse files
committed
support obj_runCharInit() and obj_runChar()
1 parent abb154c commit 320ded1

9 files changed

Lines changed: 80 additions & 28 deletions

File tree

package/PikaStdLib/PikaDebuger_Debuger.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "dataStrs.h"
33

44
extern PikaObj* __pikaMain;
5-
static enum shell_state __obj_shellLineHandler_debuger(PikaObj* self,
5+
static enum shell_state __obj_shellLineHandler_REPL(PikaObj* self,
66
char* input_line) {
77
/* continue */
88
if (strEqu("c", input_line)) {
@@ -49,5 +49,5 @@ void PikaDebug_Debuger_set_trace(PikaObj* self) {
4949
struct shell_config cfg = {
5050
.prefix = "(pika-debug) ",
5151
};
52-
obj_shellLineProcess(self, __obj_shellLineHandler_debuger, &cfg);
52+
obj_shellLineProcess(self, __obj_shellLineHandler_REPL, &cfg);
5353
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "dataStrs.h"
33

44
extern PikaObj* __pikaMain;
5-
static enum shell_state __obj_shellLineHandler_debuger(PikaObj* self,
5+
static enum shell_state __obj_shellLineHandler_REPL(PikaObj* self,
66
char* input_line) {
77
/* continue */
88
if (strEqu("c", input_line)) {
@@ -49,5 +49,5 @@ void PikaDebug_Debuger_set_trace(PikaObj* self) {
4949
struct shell_config cfg = {
5050
.prefix = "(pika-debug) ",
5151
};
52-
obj_shellLineProcess(self, __obj_shellLineHandler_debuger, &cfg);
52+
obj_shellLineProcess(self, __obj_shellLineHandler_REPL, &cfg);
5353
}

port/linux/test/compile-test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ TEST(compiler, task) {
104104
byteCodeFrame_appendFromAsm(&bytecode_frame, pikaAsm);
105105
/* do something */
106106
byteCodeFrame_print(&bytecode_frame);
107-
printf("Asm size: %d\r\n", strGetSize(pikaAsm));
107+
printf("Asm size: %d\r\n", (int)strGetSize(pikaAsm));
108108

109109
byteCodeFrame_printAsArray(&bytecode_frame);
110110

port/linux/test/pikaMain-test.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,3 +1850,29 @@ TEST(pikaMain, iteral_oct) {
18501850
obj_deinit(self);
18511851
EXPECT_EQ(pikaMemNow(), 0);
18521852
}
1853+
1854+
TEST(pikaMain, REPL_push_mode) {
1855+
/* init */
1856+
pikaMemInfo.heapUsedMax = 0;
1857+
/* run */
1858+
PikaObj* self = newRootObj("pikaMain", New_PikaMain);
1859+
__platform_printf("BEGIN\r\n");
1860+
obj_runCharInit(self);
1861+
char lines[] =
1862+
"print('test')\n"
1863+
"for i in range(0, 10):\n"
1864+
" i\n"
1865+
"\n";
1866+
for (size_t i = 0; i < strGetSize(lines); i++) {
1867+
obj_runChar(self, lines[i]);
1868+
}
1869+
/* assert */
1870+
EXPECT_STREQ(log_buff[18], "BEGIN\r\n");
1871+
EXPECT_STREQ(log_buff[14], "test\r\n");
1872+
EXPECT_STREQ(log_buff[11], "... ");
1873+
EXPECT_STREQ(log_buff[1], "9\r\n");
1874+
EXPECT_STREQ(log_buff[0], ">>> ");
1875+
/* deinit */
1876+
obj_deinit(self);
1877+
EXPECT_EQ(pikaMemNow(), 0);
1878+
}

src/BaseObj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "PikaVM.h"
3232
#include "TinyObj.h"
3333
#include "dataMemory.h"
34+
3435
PikaObj* New_BaseObj(Args* args);
3536
void Baseobj_print(PikaObj* self, Args* args);
3637

src/PikaObj.c

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -671,19 +671,21 @@ static void __clearBuff(char* buff, int size) {
671671
}
672672
}
673673

674-
static enum shell_state __obj_runChar(PikaObj* self, char inputChar){
674+
static void __obj_runCharBeforeRun(PikaObj* self) {
675675
struct shell_config* cfg = args_getStruct(self->list, "__shcfg");
676-
__obj_shellLineHandler_t __lineHandler_fun = obj_getPtr(self, "__shhdl");
677676
/* create the line buff for the first time */
678-
if(!obj_isArgExist(self, "__shbuf")){
679-
obj_setBytes(self, "__shbuf" , NULL, PIKA_LINE_BUFF_SIZE);
680-
/* print out the prefix when first entry */
681-
__platform_printf(cfg->prefix);
682-
}
677+
obj_setBytes(self, "__shbuf", NULL, PIKA_LINE_BUFF_SIZE);
678+
obj_setInt(self, "__shinb", 0);
679+
/* print out the prefix when first entry */
680+
__platform_printf(cfg->prefix);
681+
}
683682

683+
enum shell_state obj_runChar(PikaObj* self, char inputChar) {
684+
struct shell_config* cfg = args_getStruct(self->list, "__shcfg");
685+
__obj_shellLineHandler_t __lineHandler_fun = obj_getPtr(self, "__shhdl");
684686
char* rxBuff = (char*)obj_getBytes(self, "__shbuf");
685687
char* input_line = NULL;
686-
uint8_t is_in_block = 0;
688+
int is_in_block = obj_getInt(self, "__shinb");
687689
#ifndef __linux
688690
__platform_printf("%c", inputChar);
689691
#endif
@@ -717,10 +719,11 @@ static enum shell_state __obj_runChar(PikaObj* self, char inputChar){
717719
strsDeinit(&buffs);
718720
/* go out from block */
719721
if ((rxBuff[0] != ' ') && (rxBuff[0] != '\t')) {
720-
is_in_block = 0;
722+
obj_setInt(self, "__shinb", 0);
721723
input_line = obj_getStr(self, "shell_buff");
722-
return __lineHandler_fun(self, input_line);
724+
enum shell_state state = __lineHandler_fun(self, input_line);
723725
__platform_printf(">>> ");
726+
return state;
724727
} else {
725728
__platform_printf("... ");
726729
}
@@ -730,7 +733,7 @@ static enum shell_state __obj_runChar(PikaObj* self, char inputChar){
730733
if (0 != strGetSize(rxBuff)) {
731734
/* go in block */
732735
if (rxBuff[strGetSize(rxBuff) - 1] == ':') {
733-
is_in_block = 1;
736+
obj_setInt(self, "__shinb", 1);
734737
char _n = '\n';
735738
strAppendWithSize(rxBuff, &_n, 1);
736739
obj_setStr(self, "shell_buff", rxBuff);
@@ -740,32 +743,43 @@ static enum shell_state __obj_runChar(PikaObj* self, char inputChar){
740743
}
741744
}
742745
input_line = rxBuff;
743-
return __lineHandler_fun(self, input_line);
746+
enum shell_state state = __lineHandler_fun(self, input_line);
744747
__platform_printf(cfg->prefix);
745-
746748
__clearBuff(rxBuff, PIKA_LINE_BUFF_SIZE);
747-
return SHELL_STATE_CONTINUE;
749+
return state;
748750
}
749751
return SHELL_STATE_CONTINUE;
750752
}
751753

752-
void obj_shellLineProcess(PikaObj* self,
753-
__obj_shellLineHandler_t __lineHandler_fun,
754-
struct shell_config* cfg) {
754+
static void obj_shellConfig(PikaObj* self,
755+
__obj_shellLineHandler_t __lineHandler_fun,
756+
struct shell_config* cfg) {
755757
struct shell_config cfg_stack = {0};
756758
__platform_memcpy(&cfg_stack, cfg, sizeof(cfg_stack));
757759
args_setStruct(self->list, "__shcfg", cfg_stack);
758760
obj_setPtr(self, "__shhdl", __lineHandler_fun);
761+
}
762+
763+
void obj_shellLineProcess(PikaObj* self,
764+
__obj_shellLineHandler_t __lineHandler_fun,
765+
struct shell_config* cfg) {
766+
/* config the shell */
767+
obj_shellConfig(self, __lineHandler_fun, cfg);
768+
769+
/* init the shell */
770+
__obj_runCharBeforeRun(self);
771+
772+
/* getchar and run */
759773
while (1) {
760774
char inputChar = __platform_getchar();
761-
if(SHELL_STATE_EXIT == __obj_runChar(self, inputChar)){
775+
if (SHELL_STATE_EXIT == obj_runChar(self, inputChar)) {
762776
break;
763777
}
764778
}
765779
}
766780

767-
static enum shell_state __obj_shellLineHandler_debuger(PikaObj* self,
768-
char* input_line) {
781+
static enum shell_state __obj_shellLineHandler_REPL(PikaObj* self,
782+
char* input_line) {
769783
/* exit */
770784
if (strEqu("exit()", input_line)) {
771785
/* exit pika shell */
@@ -776,11 +790,20 @@ static enum shell_state __obj_shellLineHandler_debuger(PikaObj* self,
776790
return SHELL_STATE_CONTINUE;
777791
}
778792

793+
void obj_runCharInit(PikaObj* self) {
794+
struct shell_config cfg = {
795+
.prefix = ">>> ",
796+
};
797+
obj_shellConfig(self, __obj_shellLineHandler_REPL, &cfg);
798+
/* init the shell */
799+
__obj_runCharBeforeRun(self);
800+
}
801+
779802
void pikaScriptShell(PikaObj* self) {
780803
struct shell_config cfg = {
781804
.prefix = ">>> ",
782805
};
783-
obj_shellLineProcess(self, __obj_shellLineHandler_debuger, &cfg);
806+
obj_shellLineProcess(self, __obj_shellLineHandler_REPL, &cfg);
784807
}
785808

786809
void obj_setErrorCode(PikaObj* self, int32_t errCode) {

src/PikaObj.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ int32_t obj_newMetaObj(PikaObj* self, char* objName, NewFun newFunPtr);
243243
int32_t obj_newDirectObj(PikaObj* self, char* objName, NewFun newFunPtr);
244244
int obj_runModule(PikaObj* self, char* module_name);
245245
char* obj_toStr(PikaObj* self);
246+
void obj_runCharInit(PikaObj* self);
247+
enum shell_state obj_runChar(PikaObj* self, char inputChar);
246248

247249
#define PIKA_PYTHON_BEGIN
248250
#define PIKA_PYTHON(x)

src/dataString.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ int32_t strGetTokenNum(char* strIn, char sign) {
119119
return strCountSign(strIn, sign) + 1;
120120
}
121121

122-
uint32_t strGetSize(char* pData) {
122+
size_t strGetSize(char* pData) {
123123
return strlen(pData);
124124
}
125125

src/dataString.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "PikaPlatform.h"
3131

3232
/* size */
33-
uint32_t strGetSize(char* pData);
33+
size_t strGetSize(char* pData);
3434
/* append */
3535
char* strAppend(char* strOut, char* pData);
3636
char* strAppend_unlimited(char* strOut, char* pData);

0 commit comments

Comments
 (0)