@@ -109,64 +109,6 @@ static int handle_uncaught_exception(mp_obj_base_t *exc) {
109
109
return 1 ;
110
110
}
111
111
112
- #define LEX_SRC_STR (1)
113
- #define LEX_SRC_STDIN (4)
114
-
115
- // Returns standard error codes: 0 for success, 1 for all other errors,
116
- // except if FORCED_EXIT bit is set then script raised SystemExit and the
117
- // value of the exit is in the lower 8 bits of the return value
118
- static int execute_from_lexer (int source_kind , const void * source , mp_parse_input_kind_t input_kind , bool is_repl ) {
119
- mp_hal_set_interrupt_char (CHAR_CTRL_C );
120
-
121
- nlr_buf_t nlr ;
122
- if (nlr_push (& nlr ) == 0 ) {
123
- // create lexer based on source kind
124
- mp_lexer_t * lex ;
125
- if (source_kind == LEX_SRC_STR ) {
126
- const char * line = source ;
127
- lex = mp_lexer_new_from_str_len (MP_QSTR__lt_stdin_gt_ , line , strlen (line ), false);
128
- } else { // LEX_SRC_STDIN
129
- lex = mp_lexer_new_from_fd (MP_QSTR__lt_stdin_gt_ , 0 , false);
130
- }
131
-
132
- qstr source_name = lex -> source_name ;
133
-
134
- #if MICROPY_PY___FILE__
135
- if (input_kind == MP_PARSE_FILE_INPUT ) {
136
- mp_store_global (MP_QSTR___file__ , MP_OBJ_NEW_QSTR (source_name ));
137
- }
138
- #endif
139
-
140
- mp_parse_tree_t parse_tree = mp_parse (lex , input_kind );
141
-
142
- #if defined(MICROPY_UNIX_COVERAGE )
143
- // allow to print the parse tree in the coverage build
144
- if (mp_verbose_flag >= 3 ) {
145
- printf ("----------------\n" );
146
- mp_parse_node_print (& mp_plat_print , parse_tree .root , 0 );
147
- printf ("----------------\n" );
148
- }
149
- #endif
150
-
151
- mp_obj_t module_fun = mp_compile (& parse_tree , source_name , is_repl );
152
-
153
- if (!mp_compile_only ) {
154
- // execute it
155
- mp_call_function_0 (module_fun );
156
- }
157
-
158
- mp_hal_set_interrupt_char (-1 );
159
- mp_handle_pending (true);
160
- nlr_pop ();
161
- return 0 ;
162
-
163
- } else {
164
- // uncaught exception
165
- mp_hal_set_interrupt_char (-1 );
166
- mp_handle_pending (false);
167
- return handle_uncaught_exception (nlr .ret_val );
168
- }
169
- }
170
112
171
113
#if MICROPY_USE_READLINE == 1
172
114
#include "shared/readline/readline.h"
@@ -226,7 +168,7 @@ static int do_repl(void) {
226
168
line = line3 ;
227
169
}
228
170
229
- ret = execute_from_lexer ( LEX_SRC_STR , line , MP_PARSE_SINGLE_INPUT , true);
171
+ ret = convert_pyexec_result ( pyexec_str_single ( line , true) );
230
172
free (line );
231
173
if (ret & FORCED_EXIT ) {
232
174
return ret ;
@@ -237,10 +179,10 @@ static int do_repl(void) {
237
179
}
238
180
239
181
240
- static int do_file ( const char * file ) {
241
- int ret = pyexec_file ( file );
242
- // pyexec returns 1 for success, 0 for exception, PYEXEC_FORCED_EXIT for SystemExit
243
- // Convert to unix port's expected codes: 0 for success, 1 for exception, FORCED_EXIT|val for SystemExit
182
+ // Convert pyexec return codes to unix port's expected codes
183
+ // pyexec returns 1 for success, 0 for exception, PYEXEC_FORCED_EXIT for SystemExit
184
+ // Convert to unix port's expected codes: 0 for success, 1 for exception, FORCED_EXIT|val for SystemExit
185
+ static int convert_pyexec_result ( int ret ) {
244
186
if (ret == 1 ) {
245
187
return 0 ; // success
246
188
} else if (ret & PYEXEC_FORCED_EXIT ) {
@@ -250,19 +192,16 @@ static int do_file(const char *file) {
250
192
}
251
193
}
252
194
195
+ static int do_file (const char * file ) {
196
+ return convert_pyexec_result (pyexec_file (file ));
197
+ }
198
+
253
199
static int do_str (const char * str ) {
254
200
vstr_t vstr ;
255
- vstr_init (& vstr , strlen (str ));
256
- vstr_add_strn (& vstr , str , strlen (str ));
257
- int ret = pyexec_vstr (& vstr , false);
258
- vstr_clear (& vstr );
259
- if (ret == 1 ) {
260
- return 0 ; // success
261
- } else if (ret & PYEXEC_FORCED_EXIT ) {
262
- return ret ; // SystemExit with exit value in lower 8 bits
263
- } else {
264
- return 1 ; // exception
265
- }
201
+ size_t len = strlen (str );
202
+ vstr_init_fixed_buf (& vstr , len , (char * )str );
203
+ vstr .len = len ;
204
+ return convert_pyexec_result (pyexec_vstr (& vstr , false));
266
205
}
267
206
268
207
static void print_help (char * * argv ) {
@@ -703,7 +642,7 @@ MP_NOINLINE int main_(int argc, char **argv) {
703
642
ret = do_repl ();
704
643
prompt_write_history ();
705
644
} else {
706
- ret = execute_from_lexer ( LEX_SRC_STDIN , NULL , MP_PARSE_FILE_INPUT , false );
645
+ ret = convert_pyexec_result ( pyexec_stdin () );
707
646
}
708
647
}
709
648
0 commit comments