88#include "mpconfig.h"
99#include "mpqstr.h"
1010#include "lexer.h"
11+ #include "lexermemzip.h"
1112#include "parse.h"
1213#include "obj.h"
1314#include "compile.h"
2223
2324extern uint32_t _heap_start ;
2425
25- #ifdef USE_READLINE
26- #include <readline/readline.h>
27- #include <readline/history.h>
28- #endif
26+ bool do_file (const char * filename );
2927
30- #if 0
31- static char * str_join (const char * s1 , int sep_char , const char * s2 ) {
32- int l1 = strlen (s1 );
33- int l2 = strlen (s2 );
34- char * s = m_new (char , l1 + l2 + 2 );
35- memcpy (s , s1 , l1 );
36- if (sep_char != 0 ) {
37- s [l1 ] = sep_char ;
38- l1 += 1 ;
28+ void flash_error (int n ) {
29+ for (int i = 0 ; i < n ; i ++ ) {
30+ led_state (PYB_LED_BUILTIN , 1 );
31+ delay (250 );
32+ led_state (PYB_LED_BUILTIN , 0 );
33+ delay (250 );
3934 }
40- memcpy (s + l1 , s2 , l2 );
41- s [l1 + l2 ] = 0 ;
42- return s ;
4335}
4436
45- static char * prompt (char * p ) {
46- #ifdef USE_READLINE
47- char * line = readline (p );
48- if (line ) {
49- add_history (line );
50- }
51- #else
52- static char buf [256 ];
53- fputs (p , stdout );
54- char * s = fgets (buf , sizeof (buf ), stdin );
55- if (!s ) {
56- return NULL ;
57- }
58- int l = strlen (buf );
59- if (buf [l - 1 ] == '\n' ) {
60- buf [l - 1 ] = 0 ;
61- } else {
62- l ++ ;
63- }
64- char * line = m_new (char , l );
65- memcpy (line , buf , l );
66- #endif
67- return line ;
68- }
69- #endif
70-
7137static const char * help_text =
7238"Welcome to Micro Python!\n\n"
7339"This is a *very* early version of Micro Python and has minimal functionality.\n\n"
@@ -215,6 +181,19 @@ mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
215181}
216182#endif
217183
184+ static qstr pyb_config_source_dir = 0 ;
185+ static qstr pyb_config_main = 0 ;
186+
187+ mp_obj_t pyb_source_dir (mp_obj_t source_dir ) {
188+ pyb_config_source_dir = mp_obj_get_qstr (source_dir );
189+ return mp_const_none ;
190+ }
191+
192+ mp_obj_t pyb_main (mp_obj_t main ) {
193+ pyb_config_main = mp_obj_get_qstr (main );
194+ return mp_const_none ;
195+ }
196+
218197mp_obj_t pyb_delay (mp_obj_t count ) {
219198 delay (mp_obj_get_int (count ));
220199 return mp_const_none ;
@@ -225,6 +204,12 @@ mp_obj_t pyb_led(mp_obj_t state) {
225204 return state ;
226205}
227206
207+ mp_obj_t pyb_run (mp_obj_t filename_obj ) {
208+ const char * filename = qstr_str (mp_obj_get_qstr (filename_obj ));
209+ do_file (filename );
210+ return mp_const_none ;
211+ }
212+
228213char * strdup (const char * str ) {
229214 uint32_t len = strlen (str );
230215 char * s2 = m_new (char , len + 1 );
@@ -316,6 +301,39 @@ int readline(vstr_t *line, const char *prompt) {
316301 }
317302}
318303
304+ bool do_file (const char * filename ) {
305+ mp_lexer_t * lex = mp_lexer_new_from_memzip_file (filename );
306+
307+ if (lex == NULL ) {
308+ printf ("could not open file '%s' for reading\n" , filename );
309+ return false;
310+ }
311+
312+ mp_parse_node_t pn = mp_parse (lex , MP_PARSE_FILE_INPUT );
313+ mp_lexer_free (lex );
314+
315+ if (pn == MP_PARSE_NODE_NULL ) {
316+ return false;
317+ }
318+
319+ mp_obj_t module_fun = mp_compile (pn , false);
320+ if (module_fun == mp_const_none ) {
321+ return false;
322+ }
323+
324+ nlr_buf_t nlr ;
325+ if (nlr_push (& nlr ) == 0 ) {
326+ rt_call_function_0 (module_fun );
327+ nlr_pop ();
328+ return true;
329+ } else {
330+ // uncaught exception
331+ mp_obj_print ((mp_obj_t )nlr .ret_val );
332+ printf ("\n" );
333+ return false;
334+ }
335+ }
336+
319337void do_repl (void ) {
320338 stdout_tx_str ("Micro Python for Teensy 3.1\r\n" );
321339 stdout_tx_str ("Type \"help()\" for more information.\r\n" );
@@ -397,24 +415,53 @@ int main(void) {
397415 rt_init ();
398416
399417#if 1
400- printf ("About to add functions()\n" );
401418 // add some functions to the python namespace
402419 {
403420 rt_store_name (qstr_from_str_static ("help" ), rt_make_function_0 (pyb_help ));
404421 mp_obj_t m = mp_obj_new_module (qstr_from_str_static ("pyb" ));
405422 rt_store_attr (m , qstr_from_str_static ("info" ), rt_make_function_0 (pyb_info ));
423+ rt_store_attr (m , qstr_from_str_static ("source_dir" ), rt_make_function_1 (pyb_source_dir ));
424+ rt_store_attr (m , qstr_from_str_static ("main" ), rt_make_function_1 (pyb_main ));
406425 rt_store_attr (m , qstr_from_str_static ("gc" ), rt_make_function_0 (pyb_gc ));
407426 rt_store_attr (m , qstr_from_str_static ("delay" ), rt_make_function_1 (pyb_delay ));
408427 rt_store_attr (m , qstr_from_str_static ("led" ), rt_make_function_1 (pyb_led ));
409428 rt_store_attr (m , qstr_from_str_static ("Led" ), rt_make_function_1 (pyb_Led ));
410429 rt_store_attr (m , qstr_from_str_static ("gpio" ), (mp_obj_t )& pyb_gpio_obj );
411430 rt_store_name (qstr_from_str_static ("pyb" ), m );
431+ rt_store_name (qstr_from_str_static ("run" ), rt_make_function_1 (pyb_run ));
412432 }
413433#endif
414434
435+ if (!do_file ("/boot.py" )) {
436+ printf ("Unable to open '/boot.py'\n" );
437+ flash_error (4 );
438+ }
439+
415440 // Turn bootup LED off
416441 led_state (PYB_LED_BUILTIN , 0 );
417442
443+ // run main script
444+ {
445+ vstr_t * vstr = vstr_new ();
446+ vstr_add_str (vstr , "/" );
447+ if (pyb_config_source_dir == 0 ) {
448+ vstr_add_str (vstr , "src" );
449+ } else {
450+ vstr_add_str (vstr , qstr_str (pyb_config_source_dir ));
451+ }
452+ vstr_add_char (vstr , '/' );
453+ if (pyb_config_main == 0 ) {
454+ vstr_add_str (vstr , "main.py" );
455+ } else {
456+ vstr_add_str (vstr , qstr_str (pyb_config_main ));
457+ }
458+ if (!do_file (vstr_str (vstr ))) {
459+ printf ("Unable to open '%s'\n" , vstr_str (vstr ));
460+ flash_error (3 );
461+ }
462+ vstr_free (vstr );
463+ }
464+
418465 do_repl ();
419466
420467 printf ("PYB: soft reboot\n" );
0 commit comments