Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/function params and return #8

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
8974a71
Rename file 'extmod-generator' to 'extmod-generator.py'
IhorNehrutsa Sep 25, 2020
d550a99
Add CONST and var to example/example.py
IhorNehrutsa Sep 25, 2020
70824b3
Add defines and vars
IhorNehrutsa Sep 25, 2020
fea8d66
Update generated C code
IhorNehrutsa Sep 26, 2020
b77f4db
Update generated C code
IhorNehrutsa Sep 26, 2020
c822094
Prepare extmod-generator output file as external C module
IhorNehrutsa Oct 15, 2020
36c1571
formatting code
IhorNehrutsa Oct 15, 2020
fd8b87c
Rename mp_module_{module}_ to mod_{module}_
IhorNehrutsa Oct 15, 2020
6ba05ad
Rename SIMPLE_TYPES to CONSTANT_TYPES
IhorNehrutsa Oct 15, 2020
26d985b
Skip '__file__', '__name__', '__package__' etc attributes in module
IhorNehrutsa Oct 15, 2020
e3b551a
Modify Source.append() to pass more strings
IhorNehrutsa Oct 15, 2020
8ff4b49
Add class GenericName(object): to store name, classname and fullname …
IhorNehrutsa Oct 15, 2020
53f0071
Add '._make_new' attribute to class structure only if constructor __i…
IhorNehrutsa Oct 15, 2020
42008f2
Add consts example
IhorNehrutsa Oct 15, 2020
65dc667
Merge branch 'feature/var_define' of https://github.com/ihornehrutsa/…
IhorNehrutsa Oct 15, 2020
54b9498
Update example.py and generated files
IhorNehrutsa Oct 15, 2020
23e254a
Add module and function comments to the generated C code.
IhorNehrutsa Oct 15, 2020
0840822
Refactoring: revert Source.append(), add Source.append_str()
IhorNehrutsa Oct 16, 2020
6b47993
Add class_instance, class __doc__ and parents
IhorNehrutsa Oct 16, 2020
ace03b8
Empty function code
IhorNehrutsa Oct 16, 2020
b676000
Empty function code.
IhorNehrutsa Oct 16, 2020
92fe933
Ignore *.bak files
IhorNehrutsa Oct 31, 2020
4d7fc19
Merge branch 'feature/var_define' of https://github.com/IhorNehrutsa/…
IhorNehrutsa Oct 31, 2020
c94bab3
Rename mod_{module}_ to (module)_.
IhorNehrutsa Oct 31, 2020
72e4558
Add {module}_{classname}_type.
IhorNehrutsa Oct 31, 2020
65777e9
Review
IhorNehrutsa Oct 31, 2020
532b869
Add code attribute
IhorNehrutsa Oct 31, 2020
4be8981
Add converting PYthon parameters to C variables
IhorNehrutsa Oct 31, 2020
67948cd
Fix critical error in extmod-generator.py
IhorNehrutsa Nov 2, 2020
b213639
Update example files
IhorNehrutsa Nov 2, 2020
c5f8009
Merge branch 'feature/function_params_and_return' of https://github.c…
IhorNehrutsa Nov 3, 2020
b510a5c
strip_path(s) to fix 'example.example'
IhorNehrutsa Nov 4, 2020
12badbd
Merge branch 'feature/function_params_and_return' of https://github.c…
IhorNehrutsa Nov 4, 2020
274cfb0
Correct return type: void {module}_{classname}_print.
IhorNehrutsa Nov 13, 2020
cea2521
Create clear.bat
IhorNehrutsa Nov 20, 2022
cbbfe57
Update README.md
IhorNehrutsa Nov 20, 2022
edef3b3
Update extmod-generator.py
IhorNehrutsa Nov 20, 2022
67ffc63
example.consts
IhorNehrutsa Nov 20, 2022
6b496da
example
IhorNehrutsa Nov 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
__pycache__
!example/modexample.c
!example/qstrdefs.h
*.bak
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ Program will look for file named `module/module.py` and generate the following f

If a `module` argument is not provided, the default `example` will be used.

Example
-------

Examples
--------
```
$ python.exe extmod-generator.py
```
[example/example.py](example/example.py) ⇒ [example/modexample.c](example/modexample.c) + [example/qstrdefs.h](example/qstrdefs.h)

```
$ python.exe extmod-generator.py example.consts
```
[example/consts/consts.py](example/consts/consts.py) ⇒ [example/consts/consts.c](example/consts/consts.c) + [example/consts/qstrdefs.h](example/consts/qstrdefs.h)

License
-------

Expand Down
1 change: 1 addition & 0 deletions clear.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
del /S *.bak
242 changes: 242 additions & 0 deletions example/consts/consts.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
/*
* This file was generated by micropython-extmod-generator https://github.com/prusnak/micropython-extmod-generator
* from Python stab file D:\microPython\micropython-extmod-generator\example\consts\consts.py
*
* The MIT License (MIT)
*
* Copyright (c) 2022
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

/*
A MODULE COMMENT
*/

#define MODULE_CONSTS_ENABLED (1) // you may relocate this line to the mpconfigport.h
#if MODULE_CONSTS_ENABLED

// Include required definitions first.
#include "py/obj.h"
#include "py/objstr.h"
#include "py/objtuple.h"
#include "py/runtime.h"
#include "py/builtin.h"

#include "objfloat.h"

/*
// Example exception for any generated function
if (some_val == 0) {
mp_raise_ValueError("'some_val' can't be zero!");
}
*/

/*
// Module constants declarations
STATIC const bool *CONST_FALSE = MP_ROM_FALSE;
STATIC const MP_DEFINE_FLOAT_OBJ(CONST_FLOAT_float_obj, 123.123);
STATIC const mp_int_t CONST_INT = 123;
STATIC const void *CONST_NONE = MP_ROM_NONE;
STATIC const MP_DEFINE_STR_OBJ(CONST_STR_str_obj, "QWERTY");
STATIC const bool *CONST_TRUE = MP_ROM_TRUE;

const mp_rom_obj_tuple_t CONST_TUPLE_1_1_tuple_obj = {{&mp_type_tuple}, 3, {
MP_ROM_INT(10),
MP_ROM_INT(20),
MP_ROM_INT(30),
},};

const mp_rom_obj_tuple_t CONST_TUPLE_2_1_tuple_obj = {{&mp_type_tuple}, 3, {
MP_ROM_INT(10),
MP_ROM_INT(20),
MP_ROM_INT(30),
},};
STATIC const MP_DEFINE_STR_OBJ(CONST_TUPLE_1_2_str_obj, "asdfg");
const mp_rom_obj_tuple_t CONST_TUPLE_1_9_tuple_obj = {{&mp_type_tuple}, 3, {
MP_ROM_INT(1),
MP_ROM_PTR(&CONST_TUPLE_2_1_tuple_obj),
MP_ROM_PTR(&CONST_TUPLE_1_2_str_obj),
},};
STATIC const MP_DEFINE_STR_OBJ(CONST_TUPLE_0_2_str_obj, "asdfg");
STATIC const MP_DEFINE_FLOAT_OBJ(CONST_TUPLE_0_7_float_obj, 123.456);
STATIC const MP_DEFINE_STR_OBJ(CONST_TUPLE_0_8_str_obj, "QWERTY");
const mp_rom_obj_tuple_t CONST_TUPLE_0_6_tuple_obj = {{&mp_type_tuple}, 10, {
MP_ROM_INT(1),
MP_ROM_PTR(&CONST_TUPLE_1_1_tuple_obj),
MP_ROM_PTR(&CONST_TUPLE_0_2_str_obj),
MP_ROM_NONE,
MP_ROM_TRUE,
MP_ROM_FALSE,
MP_ROM_FALSE,
MP_ROM_PTR(&CONST_TUPLE_0_7_float_obj),
MP_ROM_PTR(&CONST_TUPLE_0_8_str_obj),
MP_ROM_PTR(&CONST_TUPLE_1_9_tuple_obj),
},};

*/

// Defining module functions
// def test_test()
STATIC mp_obj_t consts_test_test(void) {

//TODO: Your code here

return MP_ROM_NONE;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(consts_test_test_obj, consts_test_test);

// Defining classes
// class WithConsts(object):
/*
A CLASS COMMENT
*/
// WithConsts constants
STATIC const bool *WithConsts_CONST_FALSE = MP_ROM_FALSE;
STATIC const MP_DEFINE_FLOAT_OBJ(WithConsts_CONST_FLOAT_float_obj, 123.123);
STATIC const mp_int_t WithConsts_CONST_INT = 123;
STATIC const void *WithConsts_CONST_NONE = MP_ROM_NONE;
STATIC const MP_DEFINE_STR_OBJ(WithConsts_CONST_STR_str_obj, "QWERTY");
STATIC const bool *WithConsts_CONST_TRUE = MP_ROM_TRUE;

const mp_rom_obj_tuple_t WithConsts_CONST_TUPLE_1_1_tuple_obj = {{&mp_type_tuple}, 3, {
MP_ROM_INT(10),
MP_ROM_INT(20),
MP_ROM_INT(30),
},};

const mp_rom_obj_tuple_t WithConsts_CONST_TUPLE_2_1_tuple_obj = {{&mp_type_tuple}, 3, {
MP_ROM_INT(10),
MP_ROM_INT(20),
MP_ROM_INT(30),
},};
STATIC const MP_DEFINE_STR_OBJ(WithConsts_CONST_TUPLE_1_2_str_obj, "asdfg");
const mp_rom_obj_tuple_t WithConsts_CONST_TUPLE_1_9_tuple_obj = {{&mp_type_tuple}, 3, {
MP_ROM_INT(1),
MP_ROM_PTR(&WithConsts_CONST_TUPLE_2_1_tuple_obj),
MP_ROM_PTR(&WithConsts_CONST_TUPLE_1_2_str_obj),
},};
STATIC const MP_DEFINE_STR_OBJ(WithConsts_CONST_TUPLE_0_2_str_obj, "asdfg");
STATIC const MP_DEFINE_FLOAT_OBJ(WithConsts_CONST_TUPLE_0_7_float_obj, 123.456);
STATIC const MP_DEFINE_STR_OBJ(WithConsts_CONST_TUPLE_0_8_str_obj, "QWERTY");
const mp_rom_obj_tuple_t WithConsts_CONST_TUPLE_0_6_tuple_obj = {{&mp_type_tuple}, 10, {
MP_ROM_INT(1),
MP_ROM_PTR(&WithConsts_CONST_TUPLE_1_1_tuple_obj),
MP_ROM_PTR(&WithConsts_CONST_TUPLE_0_2_str_obj),
MP_ROM_NONE,
MP_ROM_TRUE,
MP_ROM_FALSE,
MP_ROM_FALSE,
MP_ROM_PTR(&WithConsts_CONST_TUPLE_0_7_float_obj),
MP_ROM_PTR(&WithConsts_CONST_TUPLE_0_8_str_obj),
MP_ROM_PTR(&WithConsts_CONST_TUPLE_1_9_tuple_obj),
},};


STATIC const mp_obj_type_t consts_WithConsts_type;

typedef struct _mp_obj_consts_WithConsts_t {
mp_obj_base_t base;
} mp_obj_consts_WithConsts_t;

// Defining WithConsts methods
// def WithConsts.test_test(self)
STATIC mp_obj_t consts_WithConsts_test_test(mp_obj_t self_obj) {
mp_obj_consts_WithConsts_t *self = MP_OBJ_TO_PTR(self_obj);


//TODO: Your code here

return MP_ROM_NONE;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(consts_WithConsts_test_test_obj, consts_WithConsts_test_test);

// WithConsts stuff
// Register class methods
STATIC const mp_rom_map_elem_t consts_WithConsts_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_test_test), MP_ROM_PTR(&consts_WithConsts_test_test_obj) },
#define USE_VALUE_WithConsts // Use VALUE from the Python stab code or NAME(aka #define NAME) from include files when this line is commented out
#ifdef USE_VALUE_WithConsts
{ MP_ROM_QSTR(MP_QSTR_CONST_FALSE), MP_ROM_FALSE },
#else
{ MP_ROM_QSTR(MP_QSTR_CONST_FALSE), MP_ROM_INT(CONST_FALSE) },
#endif
{ MP_ROM_QSTR(MP_QSTR_CONST_FLOAT), MP_ROM_PTR(&WithConsts_CONST_FLOAT_float_obj) },
#ifdef USE_VALUE_WithConsts
{ MP_ROM_QSTR(MP_QSTR_CONST_INT), MP_ROM_INT(123) },
#else
{ MP_ROM_QSTR(MP_QSTR_CONST_INT), MP_ROM_INT(CONST_INT) },
#endif
{ MP_ROM_QSTR(MP_QSTR_CONST_NONE), MP_ROM_NONE },
{ MP_ROM_QSTR(MP_QSTR_CONST_STR), MP_ROM_PTR(&WithConsts_CONST_STR_str_obj) },
#ifdef USE_VALUE_WithConsts
{ MP_ROM_QSTR(MP_QSTR_CONST_TRUE), MP_ROM_TRUE },
#else
{ MP_ROM_QSTR(MP_QSTR_CONST_TRUE), MP_ROM_INT(CONST_TRUE) },
#endif
{ MP_ROM_QSTR(MP_QSTR_CONST_TUPLE), MP_ROM_PTR(&WithConsts_CONST_TUPLE_0_6_tuple_obj) },
};
STATIC MP_DEFINE_CONST_DICT(consts_WithConsts_locals_dict, consts_WithConsts_locals_dict_table);

// Create the class-object itself
STATIC const mp_obj_type_t consts_WithConsts_type = {
{ &mp_type_type },
.name = MP_QSTR_WithConsts,
.locals_dict = (mp_obj_dict_t*)&consts_WithConsts_locals_dict,
};


// module stuff
// Set up the module properties
STATIC const mp_rom_map_elem_t consts_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_consts) },
{ MP_ROM_QSTR(MP_QSTR_test_test), MP_ROM_PTR(&consts_test_test_obj) },
{ MP_ROM_QSTR(MP_QSTR_WithConsts), MP_ROM_PTR(&consts_WithConsts_type) },
#define USE_VALUE // Use VALUE from the Python stab code or NAME(aka #define NAME) from include files when this line is commented out
#ifdef USE_VALUE
{ MP_ROM_QSTR(MP_QSTR_CONST_FALSE), MP_ROM_FALSE },
#else
{ MP_ROM_QSTR(MP_QSTR_CONST_FALSE), MP_ROM_INT(CONST_FALSE) },
#endif
{ MP_ROM_QSTR(MP_QSTR_CONST_FLOAT), MP_ROM_PTR(&CONST_FLOAT_float_obj) },
#ifdef USE_VALUE
{ MP_ROM_QSTR(MP_QSTR_CONST_INT), MP_ROM_INT(123) },
#else
{ MP_ROM_QSTR(MP_QSTR_CONST_INT), MP_ROM_INT(CONST_INT) },
#endif
{ MP_ROM_QSTR(MP_QSTR_CONST_NONE), MP_ROM_NONE },
{ MP_ROM_QSTR(MP_QSTR_CONST_STR), MP_ROM_PTR(&CONST_STR_str_obj) },
#ifdef USE_VALUE
{ MP_ROM_QSTR(MP_QSTR_CONST_TRUE), MP_ROM_TRUE },
#else
{ MP_ROM_QSTR(MP_QSTR_CONST_TRUE), MP_ROM_INT(CONST_TRUE) },
#endif
{ MP_ROM_QSTR(MP_QSTR_CONST_TUPLE), MP_ROM_PTR(&CONST_TUPLE_0_6_tuple_obj) },
};
STATIC MP_DEFINE_CONST_DICT(consts_globals, consts_globals_table);

// Define the module object
const mp_obj_module_t consts_cmodule = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&consts_globals,
};
// Register the module
MP_REGISTER_MODULE(MP_QSTR_consts, consts_cmodule, MODULE_CONSTS_ENABLED);

#endif // MODULE_CONSTS_ENABLED
40 changes: 40 additions & 0 deletions example/consts/consts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'''
A MODULE COMMENT
'''

CONST_STR = 'QWERTY'
CONST_BYTES = b'ZXCVB'
CONST_INT = 123
CONST_FLOAT = 123.123
CONST_FALSE = False
CONST_TRUE = True
CONST_DICT = {5:'five', 6:'six'}
CONST_LIST = [1, (10, 20, 30), 'asdfg', None, True, False, CONST_FALSE, 123.456, CONST_STR, [1, (10, 20, 30), 'asdfg']]
CONST_TUPLE = (1, (10, 20, 30), 'asdfg', None, True, False, CONST_FALSE, 123.456, CONST_STR, (1, (10, 20, 30), 'asdfg'))
CONST_DICT2 = {1:'one', 2:'two', 3:CONST_LIST, 4:CONST_TUPLE, 5:CONST_DICT}
CONST_SET = {'qux', 'foo', 'bar', 'baz'}
CONST_NONE = None

def test_test():
pass

class WithConsts():
'''
A CLASS COMMENT
'''

CONST_STR = 'QWERTY'
CONST_BYTES = b'ZXCVB'
CONST_INT = 123
CONST_FLOAT = 123.123
CONST_FALSE = False
CONST_TRUE = True
CONST_DICT = {5:'five', 6:'six'}
CONST_LIST = [1, (10, 20, 30), 'asdfg', None, True, False, CONST_FALSE, 123.456, CONST_STR, [1, (10, 20, 30), 'asdfg']]
CONST_TUPLE = (1, (10, 20, 30), 'asdfg', None, True, False, CONST_FALSE, 123.456, CONST_STR, (1, (10, 20, 30), 'asdfg'))
CONST_DICT2 = {1:'one', 2:'two', 3:CONST_LIST, 4:CONST_TUPLE, 5:CONST_DICT}
CONST_SET = {'qux', 'foo', 'bar', 'baz'}
CONST_NONE = None

def test_test(self):
pass
19 changes: 19 additions & 0 deletions example/consts/consts_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import consts

print(type(consts.CONST_NONE), consts.CONST_NONE)
print(type(consts.CONST_INT), consts.CONST_INT)
print(type(consts.CONST_FALSE), consts.CONST_FALSE)
print(type(consts.CONST_TRUE), consts.CONST_TRUE)
print(type(consts.CONST_FLOAT), consts.CONST_FLOAT)
print(type(consts.CONST_STR), consts.CONST_STR)
print(type(consts.CONST_BYTES), consts.CONST_BYTES)
print(type(consts.CONST_TUPLE), consts.CONST_TUPLE)
print(type(consts.CONST_LIST), consts.CONST_LIST)
print(type(consts.CONST_DICT), consts.CONST_DICT)
print(type(consts.CONST_SET), consts. CONST_SET)

print(dir(consts.WithConsts))
print(consts.WithConsts)
print(consts.WithConsts.CONST_TUPLE)
print(consts.WithConsts.CONST_LIST)
print(consts.WithConsts.CONST_DICT)
9 changes: 9 additions & 0 deletions example/consts/objfloat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

// This is lifted from objfloat.c, because mp_obj_float_t is not exposed there (there is no header file)
typedef struct _mp_obj_float_t {
mp_obj_base_t base;
mp_float_t value;
} mp_obj_float_t;

#define MP_DEFINE_FLOAT_OBJ(obj_name, f) mp_obj_float_t obj_name = {{&mp_type_float}, f}

12 changes: 12 additions & 0 deletions example/consts/qstrdefs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#if MODULE_CONSTS_ENABLED
Q(CONST_FALSE)
Q(CONST_FLOAT)
Q(CONST_INT)
Q(CONST_NONE)
Q(CONST_STR)
Q(CONST_TRUE)
Q(CONST_TUPLE)
Q(WithConsts)
Q(consts)
Q(test_test)
#endif
Loading