How do I make a port of MicroPython for Casio calculators?

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Zezombye
Posts: 33
Joined: Mon Jul 30, 2018 8:29 pm

Re: How do I make a port of MicroPython for Casio calculators?

Post by Zezombye » Fri Aug 31, 2018 11:58 pm

Thanks; as this editor is aimed at students, this kind of error reporting is very important. In my case, I spotted my error right away, but a student might not.

I could probably do it myself, though all I need is the column (as I already have the line), then I'd hook into the mp_obj_print_exception() and pull the faulty line directly from the file.

Anyway, I currently am trying to import cleanly my file. Right now I'm just putting the characters in the shell, but as you can see in the screenshot it makes the line number wrong. It also requires to put another newline if the script doesn't end with 0 indentation. I'd like that, when I type "import test", it imports all the text from test.py.

I defined mp_import_stat_t() in main.c to be the following:

Code: Select all

mp_import_stat_t mp_import_stat(const char *path) {
	return MP_IMPORT_STAT_FILE;
}
Note that I don't have folders, so there are only files.

Now, typing "import test" gives me an OSError 2 (which is normal, as it can't find the file). My question is: what is the function that actually reads from the file? I tried to follow the rabbit hole of pyexec_file() in lib/utils/pyexec.c but I got nowhere. Thanks in advance.

jickster
Posts: 546
Joined: Thu Sep 07, 2017 8:57 pm

Re: How do I make a port of MicroPython for Casio calculators?

Post by jickster » Sat Sep 01, 2018 12:20 am

Zezombye wrote:Thanks; as this editor is aimed at students, this kind of error reporting is very important. In my case, I spotted my error right away, but a student might not.

I could probably do it myself, though all I need is the column (as I already have the line), then I'd hook into the mp_obj_print_exception() and pull the faulty line directly from the file.

Anyway, I currently am trying to import cleanly my file. Right now I'm just putting the characters in the shell, but as you can see in the screenshot it makes the line number wrong. It also requires to put another newline if the script doesn't end with 0 indentation. I'd like that, when I type "import test", it imports all the text from test.py.

I defined mp_import_stat_t() in main.c to be the following:

Code: Select all

mp_import_stat_t mp_import_stat(const char *path) {
	return MP_IMPORT_STAT_FILE;
}
Note that I don't have folders, so there are only files.

Now, typing "import test" gives me an OSError 2 (which is normal, as it can't find the file). My question is: what is the function that actually reads from the file? I tried to follow the rabbit hole of pyexec_file() in lib/utils/pyexec.c but I got nowhere. Thanks in advance.

You’re never going to be able to follow it like that because it compiles to byte code and then is executed in VM.c

Start with builtinimport (partial name).


Sent from my iPhone using Tapatalk Pro

Zezombye
Posts: 33
Joined: Mon Jul 30, 2018 8:29 pm

Re: How do I make a port of MicroPython for Casio calculators?

Post by Zezombye » Sat Sep 01, 2018 2:40 pm

I looked in builtinimport.c, and in objmodule.c but I didn't find anything relating to actually opening the file. I guess there is an fopen() somewhere, however the only instance of fopen() is in emitglue.c and it writes to the file, it doesn't read from it.

jickster
Posts: 546
Joined: Thu Sep 07, 2017 8:57 pm

Re: How do I make a port of MicroPython for Casio calculators?

Post by jickster » Sat Sep 01, 2018 4:20 pm

Zezombye wrote:I looked in builtinimport.c, and in objmodule.c but I didn't find anything relating to actually opening the file. I guess there is an fopen() somewhere, however the only instance of fopen() is in emitglue.c and it writes to the file, it doesn't read from it.
First of all do you have an actual OS with a file system that supports fopen?




Sent from my iPhone using Tapatalk Pro

User avatar
pythoncoder
Posts: 3119
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How do I make a port of MicroPython for Casio calculators?

Post by pythoncoder » Sun Sep 02, 2018 6:36 am

jickster wrote:
Fri Aug 31, 2018 6:28 pm
...
Calling it a bug may be too harsh. At the very least, doubling is not a good idea for super memory-constrained systems.
I would call it a compromise. If you multiply the size by N, what is the optimum value of N? Too large and you potentially waste RAM: you might multiply the size by N when you only need one extra element. But if N is too small it's non optimal for the case where the size is steadily growing: you do more allocations than necessary, increasing fragmentation and raising the probability of GC cycles. Fragmentation can break an application long before free memory runs out.

I guess this is an established CS issue and there is know-how on how N should be determined. Alas I don't know the answer.
Peter Hinch

Zezombye
Posts: 33
Joined: Mon Jul 30, 2018 8:29 pm

Re: How do I make a port of MicroPython for Casio calculators?

Post by Zezombye » Sun Sep 02, 2018 11:49 am

First of all do you have an actual OS with a file system that supports fopen?
Not fopen() itself, however there are equivalent functions:
- Bfile_OpenFile (gets the file handle)
- Bfile_ReadFile (reads from the file)
- Bfile_CloseFile (closes the open file handle).

The OS does have a file system, although it is somewhat limited (names of 8 characters max + 3 for extension, only 1 level of folder deep, etc).

As for the list doubling problem, I don't understand how this isn't a bug. If I understand:
- List of size 1: 4 bytes
- List of size 2 (adding to list of size 1) : 8 bytes
- List of size 3 : 16 bytes
- List of size 4 : 32 bytes
...
- List of size 30 : 2Gb?
(though I managed to add to a list of size 117 max, so I'm probably wrong)

jickster
Posts: 546
Joined: Thu Sep 07, 2017 8:57 pm

Re: How do I make a port of MicroPython for Casio calculators?

Post by jickster » Sun Sep 02, 2018 5:23 pm

Zezombye wrote:
First of all do you have an actual OS with a file system that supports fopen?
Not fopen() itself, however there are equivalent functions:
- Bfile_OpenFile (gets the file handle)
- Bfile_ReadFile (reads from the file)
- Bfile_CloseFile (closes the open file handle).

The OS does have a file system, although it is somewhat limited (names of 8 characters max + 3 for extension, only 1 level of folder deep, etc).

As for the list doubling problem, I don't understand how this isn't a bug. If I understand:
- List of size 1: 4 bytes
- List of size 2 (adding to list of size 1) : 8 bytes
- List of size 3 : 16 bytes
- List of size 4 : 32 bytes
...
- List of size 30 : 2Gb?
(though I managed to add to a list of size 117 max, so I'm probably wrong)
Since you have your own openfile function you should be able to use the UNIX port for a guide on how to implement files.

As for the list doubling, it only doubles when you append a item to a full list. It is only a problem when you have very little memory which you normally do when if you are running Micro python.



Sent from my iPhone using Tapatalk Pro

jickster
Posts: 546
Joined: Thu Sep 07, 2017 8:57 pm

Re: How do I make a port of MicroPython for Casio calculators?

Post by jickster » Sun Sep 02, 2018 7:14 pm

Zezombye wrote:
Sun Sep 02, 2018 11:49 am
First of all do you have an actual OS with a file system that supports fopen?
Not fopen() itself, however there are equivalent functions:
- Bfile_OpenFile (gets the file handle)
- Bfile_ReadFile (reads from the file)
- Bfile_CloseFile (closes the open file handle).

The OS does have a file system, although it is somewhat limited (names of 8 characters max + 3 for extension, only 1 level of folder deep, etc).
According to lexer.h, each port has to implement two functions i.e. they are port-specific

mp_import_stat_t mp_import_stat(const char *path);
mp_lexer_t *mp_lexer_new_from_file(const char *filename);

Code: Select all

// platform specific import function; must be implemented for a specific port
// TODO tidy up, rename, or put elsewhere

//mp_lexer_t *mp_import_open_file(qstr mod_name);

typedef enum {
    MP_IMPORT_STAT_NO_EXIST,
    MP_IMPORT_STAT_DIR,
    MP_IMPORT_STAT_FILE,
} mp_import_stat_t;

mp_import_stat_t mp_import_stat(const char *path);
mp_lexer_t *mp_lexer_new_from_file(const char *filename);

#if MICROPY_HELPER_LEXER_UNIX
mp_lexer_t *mp_lexer_new_from_fd(qstr filename, int fd, bool close_fd);
#endif

These two functions are called in the call tree of mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args)

Code: Select all

STATIC mp_import_stat_t mp_import_stat_any(const char *path) {
    #if MICROPY_MODULE_FROZEN
    mp_import_stat_t st = mp_frozen_stat(path);
    if (st != MP_IMPORT_STAT_NO_EXIST) {
        return st;
    }
    #endif
    return mp_import_stat(path);
}

Code: Select all

    // If we can compile scripts then load the file and compile and execute it.
    #if MICROPY_ENABLE_COMPILER
    {
        mp_lexer_t *lex = mp_lexer_new_from_file(file_str);
        do_load_from_lexer(module_obj, lex);
        return;
    }

Zezombye
Posts: 33
Joined: Mon Jul 30, 2018 8:29 pm

Re: How do I make a port of MicroPython for Casio calculators?

Post by Zezombye » Sun Sep 02, 2018 7:28 pm

Thanks! I implemented mp_import_stat but didn't know about mp_lexer_new_from_file. I'll see in the Unix port how it implements it.

Also, I put you in the credits for my port as a thanks for your help, if you don't mind :)

jickster
Posts: 546
Joined: Thu Sep 07, 2017 8:57 pm

Re: How do I make a port of MicroPython for Casio calculators?

Post by jickster » Sun Sep 02, 2018 8:09 pm

Zezombye wrote:Thanks! I implemented mp_import_stat but didn't know about mp_lexer_new_from_file. I'll see in the Unix port how it implements it.

Also, I put you in the credits for my port as a thanks for your help, if you don't mind :)

A video showing it working would be nice


Sent from my iPhone using Tapatalk Pro

Post Reply