How to properly feed lexer from file?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
chrizztus
Posts: 16
Joined: Thu Feb 23, 2017 3:59 pm

How to properly feed lexer from file?

Post by chrizztus » Tue Oct 16, 2018 2:57 pm

Hi,
on my nrf52832 we have a tiny littlefs file system of 100kB size. My goal is to upload python files and execute them using

Code: Select all

execfile('uploaded.py')
Therefore I figured out I need to implement mp_lexer_new_from_file(const char* filename) function.
But: since I have constrained RAM resources I cannot create a, lets say 8kB buffer with the content of the file in order to pass it to mp_lexer_new_from_str_len.
Then I looked into lexer.h and saw the mp_lexer_new function that receives a reader (stream source) as 2nd argument.
Now, you might guess where this topic is going to I'd like to know whether and more specifically how I can pass the file content byte by byte to feed the lexer? Are there any examples I have missed?
Thanks in advance
Christian

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: How to properly feed lexer from file?

Post by stijn » Tue Oct 16, 2018 3:09 pm

chrizztus wrote:
Tue Oct 16, 2018 2:57 pm
how I can pass the file content byte by byte to feed the lexer?
There's already an implementation for that: see See py/reader.h and py/reader.c, specifically the mp_reader_new_file_from_fd function which does exactly that. And which might be available out of the box if you #define MICROPY_READER_POSIX (1), not sure if that is possible for your port?

User avatar
chrizztus
Posts: 16
Joined: Thu Feb 23, 2017 3:59 pm

Re: How to properly feed lexer from file?

Post by chrizztus » Wed Oct 17, 2018 6:52 am

stijn wrote:
Tue Oct 16, 2018 3:09 pm
not sure if that is possible for your port?
unfortunately not since as I said I'm using littlefs filesystem [1]. But the posix implementation looks promising for my needs. Thanks for pointing that out.

[1] https://github.com/ARMmbed/littlefs

Post Reply