Skip to content

Commit df65e2d

Browse files
committed
support RES on pikaCompiler
1 parent 287d515 commit df65e2d

20 files changed

Lines changed: 136 additions & 101 deletions

File tree

package/PikaStdLib/PikaStdLib_SysObj.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pika_float PikaStdLib_SysObj_float(PikaObj* self, Arg* arg) {
7979
if (ARG_TYPE_FLOAT == type) {
8080
return (float)arg_getFloat(arg);
8181
}
82-
obj_setSysOut(self, "[error] convert to pika_float type faild.");
82+
obj_setSysOut(self, "[error] convert to pika_float type failed.");
8383
obj_setErrorCode(self, 1);
8484
return -99999.99999;
8585
}
@@ -105,7 +105,7 @@ int PikaStdLib_SysObj_int(PikaObj* self, Arg* arg) {
105105
uint8_t val = *arg_getBytes(arg);
106106
return val;
107107
}
108-
obj_setSysOut(self, "[error] convert to int type faild.");
108+
obj_setSysOut(self, "[error] convert to int type failed.");
109109
obj_setErrorCode(self, 1);
110110
return -999999999;
111111
}

port/linux/.vscode/launch.json

Lines changed: 1 addition & 1 deletion
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=parser.multi_import"
14+
// "--gtest_filter=pikaMain.synac_err_1"
1515
],
1616
"stopAtEntry": false,
1717
"cwd": "${workspaceFolder}",

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pika_float PikaStdLib_SysObj_float(PikaObj* self, Arg* arg) {
7979
if (ARG_TYPE_FLOAT == type) {
8080
return (float)arg_getFloat(arg);
8181
}
82-
obj_setSysOut(self, "[error] convert to pika_float type faild.");
82+
obj_setSysOut(self, "[error] convert to pika_float type failed.");
8383
obj_setErrorCode(self, 1);
8484
return -99999.99999;
8585
}
@@ -105,7 +105,7 @@ int PikaStdLib_SysObj_int(PikaObj* self, Arg* arg) {
105105
uint8_t val = *arg_getBytes(arg);
106106
return val;
107107
}
108-
obj_setSysOut(self, "[error] convert to int type faild.");
108+
obj_setSysOut(self, "[error] convert to int type failed.");
109109
obj_setErrorCode(self, 1);
110110
return -999999999;
111111
}

port/linux/package/pikascript/pikascript-lib/pika_cjson/pika_cjson.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ PikaObj* pika_cjson_Parse(PikaObj* self, char* value) {
66
cJSON* item = cJSON_Parse(value);
77
if (NULL == item) {
88
obj_setErrorCode(self, 3);
9-
__platform_printf("Error: cJSON parse faild.\r\n");
9+
__platform_printf("Error: cJSON parse failed.\r\n");
1010
return NULL;
1111
}
1212
PikaObj* cjson_obj = newNormalObj(New_pika_cjson_cJSON);

port/linux/package/pikascript/pikascript-lib/pika_lua/pika_lua.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void pika_lua_eval(PikaObj* self, char* cmd) {
2020
int res = luaL_dostring(pika_L, cmd);
2121
if (LUA_OK != res) {
2222
obj_setErrorCode(self, PIKA_RES_ERR_OPERATION_FAILED);
23-
obj_setSysOut(self, "Error: Lua dostring faild.\r\n");
23+
obj_setSysOut(self, "Error: Lua dostring failed.\r\n");
2424
}
2525
}
2626

src/PikaCompiler.c

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,16 @@ static void __handler_instructArray_output_file(InstructArray* self,
6161
__platform_fclose()
6262
*/
6363

64-
int pikaCompile(char* output_file_name, char* py_lines) {
64+
PIKA_RES pikaCompile(char* output_file_name, char* py_lines) {
65+
PIKA_RES res = PIKA_RES_OK;
6566
ByteCodeFrame bytecode_frame = {0};
6667

6768
FILE* bytecode_f = __platform_fopen(output_file_name, "wb+");
69+
if (NULL == bytecode_f) {
70+
__platform_printf("Error: open file %s failed.\r\n", output_file_name);
71+
res = PIKA_RES_ERR_IO_ERROR;
72+
goto exit;
73+
}
6874
/* main process */
6975

7076
/* step 1, get size of const pool and instruct array */
@@ -73,7 +79,11 @@ int pikaCompile(char* output_file_name, char* py_lines) {
7379
bytecode_frame.instruct_array.output_f = bytecode_f;
7480
bytecode_frame.instruct_array.output_redirect_fun =
7581
__handler_instructArray_output_none;
76-
Parser_linesToBytes(&bytecode_frame, py_lines);
82+
res = Parser_linesToBytes(&bytecode_frame, py_lines);
83+
if (PIKA_RES_OK != res) {
84+
__platform_printf(" Error: Syntax error.\r\n");
85+
goto exit;
86+
}
7787
uint32_t const_pool_size = bytecode_frame.const_pool.size;
7888
uint32_t instruct_array_size = bytecode_frame.instruct_array.size;
7989
byteCodeFrame_deinit(&bytecode_frame);
@@ -105,12 +115,15 @@ int pikaCompile(char* output_file_name, char* py_lines) {
105115
bytecode_frame.instruct_array.output_redirect_fun =
106116
__handler_instructArray_output_none;
107117
Parser_linesToBytes(&bytecode_frame, py_lines);
108-
byteCodeFrame_deinit(&bytecode_frame);
109118

110119
/* deinit */
111-
__platform_fclose(bytecode_f);
120+
exit:
121+
byteCodeFrame_deinit(&bytecode_frame);
122+
if (NULL != bytecode_f) {
123+
__platform_fclose(bytecode_f);
124+
}
112125
/* succeed */
113-
return 0;
126+
return res;
114127
};
115128

116129
/*
@@ -120,12 +133,12 @@ int pikaCompile(char* output_file_name, char* py_lines) {
120133
__platform_fwrite()
121134
__platform_fclose()
122135
*/
123-
int pikaCompileFileWithOutputName(char* output_file_name,
124-
char* input_file_name) {
136+
PIKA_RES pikaCompileFileWithOutputName(char* output_file_name,
137+
char* input_file_name) {
125138
Args buffs = {0};
126139
Arg* input_file_arg = arg_loadFile(NULL, input_file_name);
127140
if (NULL == input_file_arg) {
128-
return 1;
141+
return PIKA_RES_ERR_IO_ERROR;
129142
}
130143
char* lines = (char*)arg_getBytes(input_file_arg);
131144
/* replace the "\r\n" to "\n" */
@@ -134,19 +147,20 @@ int pikaCompileFileWithOutputName(char* output_file_name,
134147
lines = strsReplace(&buffs, lines, "\n\n", "\n");
135148
/* add '\n' at the end */
136149
lines = strsAppend(&buffs, lines, "\n\n");
137-
pikaCompile(output_file_name, lines);
150+
PIKA_RES res = pikaCompile(output_file_name, lines);
138151
arg_deinit(input_file_arg);
139152
strsDeinit(&buffs);
140-
return 0;
153+
return res;
141154
}
142155

143-
int pikaCompileFile(char* input_file_name) {
156+
PIKA_RES pikaCompileFile(char* input_file_name) {
144157
Args buffs = {0};
145158
char* output_file_name = strsGetFirstToken(&buffs, input_file_name, '.');
146159
output_file_name = strsAppend(&buffs, input_file_name, ".o");
147-
pikaCompileFileWithOutputName(output_file_name, input_file_name);
160+
PIKA_RES res =
161+
pikaCompileFileWithOutputName(output_file_name, input_file_name);
148162
strsDeinit(&buffs);
149-
return 0;
163+
return res;
150164
}
151165

152166
LibObj* New_LibObj(Args* args) {
@@ -400,7 +414,8 @@ int Lib_loadLibraryFileToArray(char* origin_file_name, char* out_folder) {
400414
return res;
401415
}
402416

403-
static void __Maker_compileModuleWithInfo(PikaMaker* self, char* module_name) {
417+
static PIKA_RES __Maker_compileModuleWithInfo(PikaMaker* self,
418+
char* module_name) {
404419
Args buffs = {0};
405420
char* input_file_name = strsAppend(&buffs, module_name, ".py");
406421
char* input_file_path =
@@ -411,13 +426,16 @@ static void __Maker_compileModuleWithInfo(PikaMaker* self, char* module_name) {
411426
output_file_path =
412427
strsAppend(&buffs, obj_getStr(self, "pwd"), "pikascript-api/");
413428
output_file_path = strsAppend(&buffs, output_file_path, output_file_name);
414-
pikaCompileFileWithOutputName(output_file_path, input_file_path);
429+
PIKA_RES res =
430+
pikaCompileFileWithOutputName(output_file_path, input_file_path);
415431
strsDeinit(&buffs);
432+
return res;
416433
}
417434

418435
PikaMaker* New_PikaMaker(void) {
419436
PikaMaker* self = New_TinyObj(NULL);
420437
obj_setStr(self, "pwd", "");
438+
obj_setInt(self, "err", 0);
421439
return self;
422440
}
423441

@@ -432,10 +450,15 @@ void pikaMaker_setState(PikaMaker* self, char* module_name, char* state) {
432450
obj_setStr(module_obj, "state", state);
433451
}
434452

435-
void pikaMaker_compileModule(PikaMaker* self, char* module_name) {
436-
__Maker_compileModuleWithInfo(self, module_name);
453+
PIKA_RES pikaMaker_compileModule(PikaMaker* self, char* module_name) {
454+
PIKA_RES res = __Maker_compileModuleWithInfo(self, module_name);
437455
/* update compile info */
438-
pikaMaker_setState(self, module_name, "compiled");
456+
if (PIKA_RES_OK == res) {
457+
pikaMaker_setState(self, module_name, "compiled");
458+
} else {
459+
pikaMaker_setState(self, module_name, "failed");
460+
}
461+
return res;
439462
}
440463

441464
int pikaMaker_getDependencies(PikaMaker* self, char* module_name) {
@@ -589,18 +612,29 @@ char* pikaMaker_getFirstNocompiled(PikaMaker* self) {
589612
return obj_getStr(self, "res");
590613
}
591614

592-
void pikaMaker_compileModuleWithDepends(PikaMaker* self, char* module_name) {
593-
pikaMaker_compileModule(self, module_name);
615+
PIKA_RES pikaMaker_compileModuleWithDepends(PikaMaker* self,
616+
char* module_name) {
617+
PIKA_RES res = PIKA_RES_OK;
618+
res = pikaMaker_compileModule(self, module_name);
619+
if (PIKA_RES_OK != res) {
620+
obj_setInt(self, "err", res);
621+
return res;
622+
}
594623
pikaMaker_getDependencies(self, module_name);
595624
while (1) {
596625
char* uncompiled = pikaMaker_getFirstNocompiled(self);
597626
/* compiled all modules */
598627
if (NULL == uncompiled) {
599628
break;
600629
}
601-
pikaMaker_compileModule(self, uncompiled);
630+
res = pikaMaker_compileModule(self, uncompiled);
631+
if (PIKA_RES_OK != res) {
632+
obj_setInt(self, "err", res);
633+
return res;
634+
}
602635
pikaMaker_getDependencies(self, uncompiled);
603636
}
637+
return PIKA_RES_OK;
604638
}
605639

606640
int32_t __foreach_handler_linkCompiledModules(Arg* argEach, Args* context) {
@@ -624,7 +658,13 @@ int32_t __foreach_handler_linkCompiledModules(Arg* argEach, Args* context) {
624658
return 0;
625659
}
626660

627-
void pikaMaker_linkCompiledModulesFullPath(PikaMaker* self, char* lib_path) {
661+
PIKA_RES pikaMaker_linkCompiledModulesFullPath(PikaMaker* self,
662+
char* lib_path) {
663+
PIKA_RES compile_err = obj_getInt(self, "err");
664+
if (PIKA_RES_OK != compile_err) {
665+
__platform_printf(" Error: compile failed, link aborted.\r\n");
666+
return compile_err;
667+
}
628668
Args context = {0};
629669
LibObj* lib = New_LibObj(NULL);
630670
Args buffs = {0};
@@ -643,11 +683,13 @@ void pikaMaker_linkCompiledModulesFullPath(PikaMaker* self, char* lib_path) {
643683
Lib_loadLibraryFileToArray(lib_file_path, folder_path);
644684
LibObj_deinit(lib);
645685
strsDeinit(&buffs);
686+
return PIKA_RES_OK;
646687
}
647688

648-
void pikaMaker_linkCompiledModules(PikaMaker* self, char* lib_name) {
689+
PIKA_RES pikaMaker_linkCompiledModules(PikaMaker* self, char* lib_name) {
649690
Args buffs = {0};
650691
char* lib_file_path = strsAppend(&buffs, "pikascript-api/", lib_name);
651-
pikaMaker_linkCompiledModulesFullPath(self, lib_file_path);
692+
PIKA_RES res = pikaMaker_linkCompiledModulesFullPath(self, lib_file_path);
652693
strsDeinit(&buffs);
694+
return res;
653695
}

src/PikaCompiler.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
#include "PikaObj.h"
44
#include "stdint.h"
55

6-
int pikaCompileFile(char* input_file_name);
7-
int pikaCompileFileWithOutputName(char* output_file_name,
8-
char* input_file_name);
9-
int pikaCompile(char* output_file_name, char* py_lines);
6+
PIKA_RES pikaCompileFile(char* input_file_name);
7+
PIKA_RES pikaCompileFileWithOutputName(char* output_file_name,
8+
char* input_file_name);
9+
PIKA_RES pikaCompile(char* output_file_name, char* py_lines);
1010

1111
LibObj* New_LibObj(Args* args);
1212
void LibObj_deinit(LibObj* self);
@@ -22,14 +22,14 @@ int LibObj_loadLibraryFile(LibObj* self, char* input_file_name);
2222
int Lib_loadLibraryFileToArray(char* origin_file_name, char* pikascript_root);
2323
PikaMaker* New_PikaMaker(void);
2424
void pikaMaker_setPWD(PikaMaker* self, char* pwd);
25-
void pikaMaker_compileModule(PikaMaker* self, char* module_name);
25+
PIKA_RES pikaMaker_compileModule(PikaMaker* self, char* module_name);
2626
int pikaMaker_getDependencies(PikaMaker* self, char* module_name);
2727
void pikaMaker_printStates(PikaMaker* self);
2828
char* pikaMaker_getFirstNocompiled(PikaMaker* self);
29-
void pikaMaker_compileModuleWithDepends(PikaMaker* self, char* module_name);
30-
void pikaMaker_linkCompiledModules(PikaMaker* self, char* lib_name);
29+
PIKA_RES pikaMaker_compileModuleWithDepends(PikaMaker* self, char* module_name);
30+
PIKA_RES pikaMaker_linkCompiledModulesFullPath(PikaMaker* self, char* lib_path);
31+
PIKA_RES pikaMaker_linkCompiledModules(PikaMaker* self, char* lib_name);
3132
int LibObj_loadLibrary(LibObj* self, uint8_t* library_bytes);
32-
void pikaMaker_linkCompiledModulesFullPath(PikaMaker* self, char* lib_path);
3333

3434
#define LIB_VERSION_NUMBER 2
3535
#define LIB_INFO_BLOCK_SIZE 32

src/PikaObj.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ void _temp_obj_shellLineProcess(PikaObj* self, ShellConfig* cfg);
270270
__platform_fwrite()
271271
__platform_fclose()
272272
*/
273-
int pikaCompile(char* output_file_name, char* py_lines);
274273
Method obj_getNativeMethod(PikaObj* self, char* method_name);
275274
PIKA_RES obj_runNativeMethod(PikaObj* self, char* method_name, Args* args);
276275
Arg* obj_newObjInPackage(NewFun newObjFun);

src/PikaParser.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2506,18 +2506,12 @@ static char* _Parser_linesToBytesOrAsm(Args* outBuffs,
25062506
return out_ASM;
25072507
};
25082508

2509-
char* Parser_linesToBytes(ByteCodeFrame* bf, char* py_lines) {
2510-
return _Parser_linesToBytesOrAsm(NULL, bf, py_lines);
2511-
}
2512-
2513-
int bytecodeFrame_fromLines(ByteCodeFrame* bytecode_frame, char* multi_line) {
2514-
if (NULL == Parser_linesToBytes(bytecode_frame, multi_line)) {
2515-
/* error */
2516-
return 1;
2509+
PIKA_RES Parser_linesToBytes(ByteCodeFrame* bf, char* py_lines) {
2510+
if (1 == (uintptr_t)_Parser_linesToBytesOrAsm(NULL, bf, py_lines)) {
2511+
return PIKA_RES_OK;
25172512
}
2518-
/* succeed */
2519-
return 0;
2520-
};
2513+
return PIKA_RES_ERR_SYNTAX_ERROR;
2514+
}
25212515

25222516
char* Parser_linesToAsm(Args* outBuffs, char* multi_line) {
25232517
return _Parser_linesToBytesOrAsm(outBuffs, NULL, multi_line);
@@ -3024,7 +3018,7 @@ ByteCodeFrame* byteCodeFrame_appendFromAsm(ByteCodeFrame* self, char* pikaAsm) {
30243018
char* Parser_linesToArray(char* lines) {
30253019
ByteCodeFrame bytecode_frame;
30263020
byteCodeFrame_init(&bytecode_frame);
3027-
bytecodeFrame_fromLines(&bytecode_frame, lines);
3021+
Parser_linesToBytes(&bytecode_frame, lines);
30283022
/* do something */
30293023
byteCodeFrame_print(&bytecode_frame);
30303024

src/PikaParser.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,11 @@ struct Cursor {
100100

101101
char* Parser_fileToAsm(Args* outBuffs, char* filename);
102102
char* Parser_linesToAsm(Args* outBuffs, char* multiLine);
103-
char* Parser_linesToBytes(ByteCodeFrame* bf, char* py_lines);
103+
PIKA_RES Parser_linesToBytes(ByteCodeFrame* bf, char* py_lines);
104104
char* Parser_linesToArray(char* lines);
105105

106106
char* instructUnit_fromAsmLine(Args* outBuffs, char* pikaAsm);
107107
ByteCodeFrame* byteCodeFrame_appendFromAsm(ByteCodeFrame* bf, char* pikaAsm);
108-
int bytecodeFrame_fromLines(ByteCodeFrame* bytecode_frame, char* python_lines);
109108

110109
#define Cursor_forEach(cursor) \
111110
_Cursor_beforeIter(&cursor); \

0 commit comments

Comments
 (0)