@@ -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
152166LibObj * 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
418435PikaMaker * 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
441464int 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
606640int32_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}
0 commit comments