1111#include "storage.h"
1212#include "mma.h"
1313#include "usb.h"
14+ #include "ff.h"
1415
1516static void impl02_c_version () {
1617 int x = 0 ;
@@ -63,8 +64,10 @@ void __fatal_error(const char *msg) {
6364 }
6465}
6566
67+ #include "nlr.h"
6668#include "misc.h"
6769#include "lexer.h"
70+ #include "lexerstm.h"
6871#include "mpyconfig.h"
6972#include "parse.h"
7073#include "compile.h"
@@ -88,10 +91,8 @@ py_obj_t pyb_sw() {
8891 }
8992}
9093
91- #include "ff.h"
9294FATFS fatfs0 ;
9395
94- #include "nlr.h"
9596
9697/*
9798void g(uint i) {
@@ -293,7 +294,7 @@ int main() {
293294 //sys_tick_delay_ms(1000);
294295
295296 // Python!
296- if (0 ) {
297+ if (1 ) {
297298 //const char *pysrc = "def f():\n x=x+1\nprint(42)\n";
298299 const char * pysrc =
299300 // impl01.py
@@ -323,19 +324,17 @@ int main() {
323324 " x = x + 1\n"
324325 "f()\n";
325326 */
326- /*
327327 "print('in python!')\n"
328328 "x = 0\n"
329329 "while x < 4:\n"
330330 " pyb_led(True)\n"
331331 " pyb_delay(201)\n"
332332 " pyb_led(False)\n"
333333 " pyb_delay(201)\n"
334- " x = x + 1\n"
334+ " x += 1\n"
335335 "print('press me!')\n"
336336 "while True:\n"
337337 " pyb_led(pyb_sw())\n" ;
338- */
339338 /*
340339 // impl16.py
341340 "@micropython.asm_thumb\n"
@@ -372,6 +371,7 @@ int main() {
372371 "except:\n"
373372 " print(x)\n";
374373 */
374+ /*
375375 // impl19.py
376376 "# for loop\n"
377377 "def f():\n"
@@ -380,29 +380,27 @@ int main() {
380380 " for z in range(400):\n"
381381 " pass\n"
382382 "f()\n";
383+ */
383384
384- py_lexer_t * lex = py_lexer_from_str_len ("<>" , pysrc , strlen (pysrc ), false);
385+ py_lexer_str_buf_t py_lexer_str_buf ;
386+ py_lexer_t * lex = py_lexer_new_from_str_len ("<stdin>" , pysrc , strlen (pysrc ), false, & py_lexer_str_buf );
385387
386- if (0 ) {
387- while (!py_lexer_is_kind (lex , PY_TOKEN_END )) {
388- py_token_show (py_lexer_cur (lex ));
389- py_lexer_to_next (lex );
390- sys_tick_delay_ms (1000 );
391- }
392- } else {
393- // nalloc=1740;6340;6836 -> 140;4600;496 bytes for lexer, parser, compiler
394- printf ("lex; al=%u\n" , m_get_total_bytes_allocated ());
395- sys_tick_delay_ms (1000 );
396- py_parse_node_t pn = py_parse (lex , 0 );
397- //printf("----------------\n");
388+ // nalloc=1740;6340;6836 -> 140;4600;496 bytes for lexer, parser, compiler
389+ printf ("lex; al=%u\n" , m_get_total_bytes_allocated ());
390+ sys_tick_delay_ms (1000 );
391+ py_parse_node_t pn = py_parse (lex , PY_PARSE_FILE_INPUT );
392+ py_lexer_free (lex );
393+ if (pn != PY_PARSE_NODE_NULL ) {
398394 printf ("pars;al=%u\n" , m_get_total_bytes_allocated ());
399395 sys_tick_delay_ms (1000 );
400396 //parse_node_show(pn, 0);
401- py_compile (pn , false);
397+ bool comp_ok = py_compile (pn , false);
402398 printf ("comp;al=%u\n" , m_get_total_bytes_allocated ());
403399 sys_tick_delay_ms (1000 );
404400
405- if (1 ) {
401+ if (!comp_ok ) {
402+ printf ("compile error\n" );
403+ } else {
406404 // execute it!
407405
408406 // add some functions to the python namespace
0 commit comments