How to load and compile python file when there are no system file ?

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
soibac35
Posts: 6
Joined: Thu Aug 24, 2017 2:48 pm

How to load and compile python file when there are no system file ?

Post by soibac35 » Thu Sep 14, 2017 5:26 pm

Hello,

So i try to load a script i wrote to a cortext M4 chip. The basic print and all are working, however, i cant get my add-on module to work due to no system file on the chip (using crossworks). i cant load the file using file name. Is there a way to do this.

Here the code that i have:

Code: Select all

 	//load and compile python src file
 	
void compile_pyfile(const char* fileName)
{
	mp_lexer_t *lex = mp_lexer_new_from_file(fileName);
	mp_parse_tree_t pt = mp_parse(lex, MP_PARSE_FILE_INPUT);
	mp_obj_t module_fun = mp_compile(&pt, lex->source_name, MP_EMIT_OPT_NONE, false);
	mp_call_function_0(module_fun);
	}
Here is the code of a main function in bare-arm project:

Code: Select all

do_str():
[..]
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
        qstr source_name = lex->source_name;
        mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
        mp_obj_t module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, true);
        mp_call_function_0(module_fun);
So let say i want to load a python code with file name as TestModule, such as :

Code: Select all

import TestModule;

def helloC():
    print('Call hello function from C')
    TestModule.hello()
    TestModule.hello_XYZ('XYZ')
How would i do that ?

Thanks in advance

Post Reply