Browse Source

clean up the notebook a bit

pull/8/head
Zoltán Vörös 5 years ago
parent
commit
9d4fdf42c8
  1. 26
      consumeiterable/consumeiterable.c
  2. 2121
      docs/developing_micropython.ipynb
  3. 31
      docs/ulab.ipynb
  4. 31
      docs/umath-test.ipynb
  5. 22
      keywordfunction/keywordfunction.c
  6. 48
      makeiterable/makeiterable.c
  7. 11
      profiling/profiling.c
  8. 26
      returniterable/returniterable.c
  9. 68
      simpleclass/simpleclass.c
  10. 10
      simplefunction/simplefunction.c
  11. 2
      sliceextiterable/sliceextiterable.c
  12. 78
      sliceiterable/sliceiterable.c
  13. 86
      specialclass/specialclass.c
  14. 68
      subscriptiterable/subscriptiterable.c
  15. 54
      vector/vector.c

26
consumeiterable/consumeiterable.c

@ -1,15 +1,25 @@
/*
* This file is part of the micropython-usermod project,
*
* https://github.com/v923z/micropython-usermod
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Zoltán Vörös
*/
#include "py/obj.h"
#include "py/runtime.h"
STATIC mp_obj_t consumeiterable_sumsq(mp_obj_t o_in) {
mp_float_t _sum = 0.0, itemf;
mp_obj_iter_buf_t iter_buf;
mp_obj_t item, iterable = mp_getiter(o_in, &iter_buf);
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
itemf = mp_obj_get_float(item);
_sum += itemf*itemf;
}
return mp_obj_new_float(_sum);
mp_float_t _sum = 0.0, itemf;
mp_obj_iter_buf_t iter_buf;
mp_obj_t item, iterable = mp_getiter(o_in, &iter_buf);
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
itemf = mp_obj_get_float(item);
_sum += itemf*itemf;
}
return mp_obj_new_float(_sum);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(consumeiterable_sumsq_obj, consumeiterable_sumsq);

2121
docs/developing_micropython.ipynb

File diff suppressed because it is too large

31
docs/ulab.ipynb

@ -125,7 +125,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
"version": "3.7.3"
},
"toc": {
"base_numbering": 1,
@ -139,6 +139,35 @@
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,

31
docs/umath-test.ipynb

@ -1878,7 +1878,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
"version": "3.7.3"
},
"toc": {
"base_numbering": 1,
@ -1897,6 +1897,35 @@
},
"toc_section_display": true,
"toc_window_display": true
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,

22
keywordfunction/keywordfunction.c

@ -1,3 +1,13 @@
/*
* This file is part of the micropython-usermod project,
*
* https://github.com/v923z/micropython-usermod
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Zoltán Vörös
*/
#include <stdio.h>
#include "py/obj.h"
#include "py/runtime.h"
@ -5,17 +15,17 @@
STATIC mp_obj_t keywordfunction_add_ints(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_a, MP_ARG_INT | MP_ARG_REQUIRED, {.u_int = 0} },
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_a, MP_ARG_INT | MP_ARG_REQUIRED, {.u_int = 0} },
{ MP_QSTR_b, MP_ARG_KW_ONLY | MP_ARG_INT | MP_ARG_REQUIRED, {.u_int = 0} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
int16_t a = args[0].u_int;
int16_t b = args[1].u_int;
printf("a = %d, b = %d\n", a, b);
return mp_obj_new_int(a + b);
int16_t a = args[0].u_int;
int16_t b = args[1].u_int;
printf("a = %d, b = %d\n", a, b);
return mp_obj_new_int(a + b);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(keywordfunction_add_ints_obj, 2, keywordfunction_add_ints);

48
makeiterable/makeiterable.c

@ -1,3 +1,13 @@
/*
* This file is part of the micropython-usermod project,
*
* https://github.com/v923z/micropython-usermod
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Zoltán Vörös
*/
#include <stdlib.h>
#include "py/obj.h"
#include "py/runtime.h"
@ -13,26 +23,26 @@ const mp_obj_type_t iterable_array_type;
mp_obj_t mp_obj_new_itarray_iterator(mp_obj_t , size_t , mp_obj_iter_buf_t *);
STATIC void itarray_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind;
itarray_obj_t *self = MP_OBJ_TO_PTR(self_in);
printf("itarray: ");
for(uint16_t i=0; i < self->len; i++) {
printf("%d ", self->elements[i]);
}
printf("\n");
(void)kind;
itarray_obj_t *self = MP_OBJ_TO_PTR(self_in);
printf("itarray: ");
for(uint16_t i=0; i < self->len; i++) {
printf("%d ", self->elements[i]);
}
printf("\n");
}
STATIC mp_obj_t itarray_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 1, 1, true);
itarray_obj_t *self = m_new_obj(itarray_obj_t);
self->base.type = &iterable_array_type;
self->len = mp_obj_get_int(args[0]);
uint16_t *arr = malloc(self->len * sizeof(uint16_t));
for(uint16_t i=0; i < self->len; i++) {
arr[i] = i*i;
}
self->elements = arr;
return MP_OBJ_FROM_PTR(self);
mp_arg_check_num(n_args, n_kw, 1, 1, true);
itarray_obj_t *self = m_new_obj(itarray_obj_t);
self->base.type = &iterable_array_type;
self->len = mp_obj_get_int(args[0]);
uint16_t *arr = malloc(self->len * sizeof(uint16_t));
for(uint16_t i=0; i < self->len; i++) {
arr[i] = i*i;
}
self->elements = arr;
return MP_OBJ_FROM_PTR(self);
}
STATIC mp_obj_t itarray_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
@ -72,8 +82,8 @@ mp_obj_t itarray_iternext(mp_obj_t self_in) {
mp_obj_itarray_it_t *self = MP_OBJ_TO_PTR(self_in);
itarray_obj_t *itarray = MP_OBJ_TO_PTR(self->itarray);
if (self->cur < itarray->len) {
// read the current value
uint16_t *arr = itarray->elements;
// read the current value
uint16_t *arr = itarray->elements;
mp_obj_t o_out = MP_OBJ_NEW_SMALL_INT(arr[self->cur]);
self->cur += 1;
return o_out;

11
profiling/profiling.c

@ -1,3 +1,13 @@
/*
* This file is part of the micropython-usermod project,
*
* https://github.com/v923z/micropython-usermod
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Zoltán Vörös
*/
#include <math.h>
#include <stdio.h>
#include "py/obj.h"
@ -59,4 +69,3 @@ const mp_obj_module_t profiling_user_cmodule = {
};
MP_REGISTER_MODULE(MP_QSTR_profiling, profiling_user_cmodule, MODULE_PROFILING_ENABLED);

26
returniterable/returniterable.c

@ -1,15 +1,25 @@
/*
* This file is part of the micropython-usermod project,
*
* https://github.com/v923z/micropython-usermod
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Zoltán Vörös
*/
#include "py/obj.h"
#include "py/runtime.h"
STATIC mp_obj_t powers_iterable(mp_obj_t base, mp_obj_t exponent) {
int e = mp_obj_get_int(exponent);
mp_obj_t tuple[e+1];
int b = mp_obj_get_int(base), ba = 1;
for(int i=0; i <= e; i++) {
tuple[i] = mp_obj_new_int(ba);
ba *= b;
}
return mp_obj_new_tuple(e+1, tuple);
int e = mp_obj_get_int(exponent);
mp_obj_t tuple[e+1];
int b = mp_obj_get_int(base), ba = 1;
for(int i=0; i <= e; i++) {
tuple[i] = mp_obj_new_int(ba);
ba *= b;
}
return mp_obj_new_tuple(e+1, tuple);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(powers_iterable_obj, powers_iterable);

68
simpleclass/simpleclass.c

@ -1,74 +1,84 @@
/*
* This file is part of the micropython-usermod project,
*
* https://github.com/v923z/micropython-usermod
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Zoltán Vörös
*/
#include <stdio.h>
#include "py/runtime.h"
#include "py/obj.h"
typedef struct _simpleclass_myclass_obj_t {
mp_obj_base_t base;
int16_t a;
int16_t b;
mp_obj_base_t base;
int16_t a;
int16_t b;
} simpleclass_myclass_obj_t;
const mp_obj_type_t simpleclass_myclass_type;
STATIC void myclass_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind;
simpleclass_myclass_obj_t *self = MP_OBJ_TO_PTR(self_in);
printf("myclass(%d, %d)", self->a, self->b);
(void)kind;
simpleclass_myclass_obj_t *self = MP_OBJ_TO_PTR(self_in);
printf("myclass(%d, %d)", self->a, self->b);
}
STATIC mp_obj_t myclass_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 2, 2, true);
simpleclass_myclass_obj_t *self = m_new_obj(simpleclass_myclass_obj_t);
self->base.type = &simpleclass_myclass_type;
self->a = mp_obj_get_int(args[0]);
self->b = mp_obj_get_int(args[1]);
return MP_OBJ_FROM_PTR(self);
mp_arg_check_num(n_args, n_kw, 2, 2, true);
simpleclass_myclass_obj_t *self = m_new_obj(simpleclass_myclass_obj_t);
self->base.type = &simpleclass_myclass_type;
self->a = mp_obj_get_int(args[0]);
self->b = mp_obj_get_int(args[1]);
return MP_OBJ_FROM_PTR(self);
}
// Class methods
STATIC mp_obj_t myclass_sum(mp_obj_t self_in) {
simpleclass_myclass_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_int(self->a + self->b);
simpleclass_myclass_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_int(self->a + self->b);
}
MP_DEFINE_CONST_FUN_OBJ_1(myclass_sum_obj, myclass_sum);
STATIC const mp_rom_map_elem_t myclass_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_mysum), MP_ROM_PTR(&myclass_sum_obj) },
{ MP_ROM_QSTR(MP_QSTR_mysum), MP_ROM_PTR(&myclass_sum_obj) },
};
STATIC MP_DEFINE_CONST_DICT(myclass_locals_dict, myclass_locals_dict_table);
const mp_obj_type_t simpleclass_myclass_type = {
{ &mp_type_type },
.name = MP_QSTR_simpleclass,
.print = myclass_print,
.make_new = myclass_make_new,
.locals_dict = (mp_obj_dict_t*)&myclass_locals_dict,
{ &mp_type_type },
.name = MP_QSTR_simpleclass,
.print = myclass_print,
.make_new = myclass_make_new,
.locals_dict = (mp_obj_dict_t*)&myclass_locals_dict,
};
// Module functions
STATIC mp_obj_t simpleclass_add(const mp_obj_t o_in) {
simpleclass_myclass_obj_t *class_instance = MP_OBJ_TO_PTR(o_in);
return mp_obj_new_int(class_instance->a + class_instance->b);
simpleclass_myclass_obj_t *class_instance = MP_OBJ_TO_PTR(o_in);
return mp_obj_new_int(class_instance->a + class_instance->b);
}
MP_DEFINE_CONST_FUN_OBJ_1(simpleclass_add_obj, simpleclass_add);
STATIC const mp_map_elem_t simpleclass_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_simpleclass) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_myclass), (mp_obj_t)&simpleclass_myclass_type },
{ MP_OBJ_NEW_QSTR(MP_QSTR_add), (mp_obj_t)&simpleclass_add_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_simpleclass) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_myclass), (mp_obj_t)&simpleclass_myclass_type },
{ MP_OBJ_NEW_QSTR(MP_QSTR_add), (mp_obj_t)&simpleclass_add_obj },
};
STATIC MP_DEFINE_CONST_DICT (
mp_module_simpleclass_globals,
simpleclass_globals_table
mp_module_simpleclass_globals,
simpleclass_globals_table
);
const mp_obj_module_t simpleclass_user_cmodule = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_simpleclass_globals,
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_simpleclass_globals,
};
MP_REGISTER_MODULE(MP_QSTR_simpleclass, simpleclass_user_cmodule, MODULE_SIMPLECLASS_ENABLED);

10
simplefunction/simplefunction.c

@ -1,3 +1,13 @@
/*
* This file is part of the micropython-usermod project,
*
* https://github.com/v923z/micropython-usermod
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Zoltán Vörös
*/
#include "py/obj.h"
#include "py/runtime.h"

2
sliceextiterable/sliceextiterable.c

@ -2,6 +2,7 @@
#include "py/obj.h"
#include "py/runtime.h"
#if 0
typedef struct _sliceextitarray_obj_t {
mp_obj_base_t base;
mp_fun_1_t iternext;
@ -129,3 +130,4 @@ mp_obj_t mp_obj_new_sliceextitarray_iterator(mp_obj_t sliceitarray, size_t cur,
o->cur = cur;
return MP_OBJ_FROM_PTR(o);
}
#endif

78
sliceiterable/sliceiterable.c

@ -1,3 +1,13 @@
/*
* This file is part of the micropython-usermod project,
*
* https://github.com/v923z/micropython-usermod
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Zoltán Vörös
*/
#include <stdlib.h>
#include "py/obj.h"
#include "py/runtime.h"
@ -13,31 +23,31 @@ const mp_obj_type_t sliceiterable_array_type;
mp_obj_t mp_obj_new_sliceitarray_iterator(mp_obj_t , size_t , mp_obj_iter_buf_t *);
STATIC void sliceitarray_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind;
sliceitarray_obj_t *self = MP_OBJ_TO_PTR(self_in);
printf("sliceitarray: ");
for(uint16_t i=0; i < self->len; i++) {
printf("%d ", self->elements[i]);
}
printf("\n");
(void)kind;
sliceitarray_obj_t *self = MP_OBJ_TO_PTR(self_in);
printf("sliceitarray: ");
for(uint16_t i=0; i < self->len; i++) {
printf("%d ", self->elements[i]);
}
printf("\n");
}
sliceitarray_obj_t *create_new_sliceitarray(uint16_t len) {
sliceitarray_obj_t *self = m_new_obj(sliceitarray_obj_t);
self->base.type = &sliceiterable_array_type;
self->len = len;
uint16_t *arr = malloc(self->len * sizeof(uint16_t));
self->elements = arr;
return self;
sliceitarray_obj_t *self = m_new_obj(sliceitarray_obj_t);
self->base.type = &sliceiterable_array_type;
self->len = len;
uint16_t *arr = malloc(self->len * sizeof(uint16_t));
self->elements = arr;
return self;
}
STATIC mp_obj_t sliceitarray_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 1, 1, true);
sliceitarray_obj_t *self = create_new_sliceitarray(mp_obj_get_int(args[0]));
for(uint16_t i=0; i < self->len; i++) {
self->elements[i] = i*i;
}
return MP_OBJ_FROM_PTR(self);
mp_arg_check_num(n_args, n_kw, 1, 1, true);
sliceitarray_obj_t *self = create_new_sliceitarray(mp_obj_get_int(args[0]));
for(uint16_t i=0; i < self->len; i++) {
self->elements[i] = i*i;
}
return MP_OBJ_FROM_PTR(self);
}
STATIC mp_obj_t sliceitarray_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
@ -51,23 +61,23 @@ STATIC mp_obj_t sliceitarray_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t v
#if MICROPY_PY_BUILTINS_SLICE
if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
mp_bound_slice_t slice;
if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) {
return mp_const_none;
mp_seq_get_fast_slice_indexes(self->len, index, &slice);
printf("start: %ld, stop: %ld, step: %ld\n", slice.start, slice.stop, slice.step);
uint16_t len = (slice.stop - slice.start) / slice.step;
sliceitarray_obj_t *res = create_new_sliceitarray(len);
for(size_t i=0; i < len; i++) {
res->elements[i] = self->elements[slice.start+i*slice.step];
}
sliceitarray_obj_t *res = create_new_sliceitarray(slice.stop - slice.start);
for(size_t i=0; i < slice.stop-slice.start; i++) {
res->elements[i] = self->elements[i+slice.start];
}
return MP_OBJ_FROM_PTR(res);
}
#endif
// we have a single index, return a single number
size_t idx = mp_obj_get_int(index);
return MP_OBJ_NEW_SMALL_INT(self->elements[idx]);
} else { // do not deal with assignment, bail out
return mp_const_none;
}
return mp_const_none;
// we have a single index, return a single number
size_t idx = mp_obj_get_int(index);
return MP_OBJ_NEW_SMALL_INT(self->elements[idx]);
} else { // do not deal with assignment, bail out
return mp_const_none;
}
return mp_const_none;
}
const mp_obj_type_t sliceiterable_array_type = {
@ -104,8 +114,8 @@ mp_obj_t sliceitarray_iternext(mp_obj_t self_in) {
mp_obj_sliceitarray_it_t *self = MP_OBJ_TO_PTR(self_in);
sliceitarray_obj_t *sliceitarray = MP_OBJ_TO_PTR(self->sliceitarray);
if (self->cur < sliceitarray->len) {
// read the current value
uint16_t *arr = sliceitarray->elements;
// read the current value
uint16_t *arr = sliceitarray->elements;
mp_obj_t o_out = MP_OBJ_NEW_SMALL_INT(arr[self->cur]);
self->cur += 1;
return o_out;

86
specialclass/specialclass.c

@ -1,33 +1,43 @@
/*
* This file is part of the micropython-usermod project,
*
* https://github.com/v923z/micropython-usermod
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Zoltán Vörös
*/
#include <stdio.h>
#include "py/runtime.h"
#include "py/obj.h"
#include "py/binary.h"
typedef struct _specialclass_myclass_obj_t {
mp_obj_base_t base;
int16_t a;
int16_t b;
mp_obj_base_t base;
int16_t a;
int16_t b;
} specialclass_myclass_obj_t;
const mp_obj_type_t specialclass_myclass_type;
STATIC void myclass_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind;
specialclass_myclass_obj_t *self = MP_OBJ_TO_PTR(self_in);
printf("myclass(%d, %d)", self->a, self->b);
(void)kind;
specialclass_myclass_obj_t *self = MP_OBJ_TO_PTR(self_in);
printf("myclass(%d, %d)", self->a, self->b);
}
mp_obj_t create_new_myclass(uint16_t a, uint16_t b) {
specialclass_myclass_obj_t *out = m_new_obj(specialclass_myclass_obj_t);
out->base.type = &specialclass_myclass_type;
out->a = a;
out->b = b;
return MP_OBJ_FROM_PTR(out);
specialclass_myclass_obj_t *out = m_new_obj(specialclass_myclass_obj_t);
out->base.type = &specialclass_myclass_type;
out->a = a;
out->b = b;
return MP_OBJ_FROM_PTR(out);
}
STATIC mp_obj_t myclass_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 2, 2, true);
return create_new_myclass(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]));
mp_arg_check_num(n_args, n_kw, 2, 2, true);
return create_new_myclass(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]));
}
STATIC const mp_rom_map_elem_t myclass_locals_dict_table[] = {
@ -36,7 +46,7 @@ STATIC const mp_rom_map_elem_t myclass_locals_dict_table[] = {
STATIC MP_DEFINE_CONST_DICT(myclass_locals_dict, myclass_locals_dict_table);
STATIC mp_obj_t specialclass_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
specialclass_myclass_obj_t *self = MP_OBJ_TO_PTR(self_in);
specialclass_myclass_obj_t *self = MP_OBJ_TO_PTR(self_in);
switch (op) {
case MP_UNARY_OP_BOOL: return mp_obj_new_bool((self->a > 0) && (self->b > 0));
case MP_UNARY_OP_LEN: return mp_obj_new_int(2);
@ -46,42 +56,42 @@ STATIC mp_obj_t specialclass_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
STATIC mp_obj_t specialclass_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) {
specialclass_myclass_obj_t *left_hand_side = MP_OBJ_TO_PTR(lhs);
specialclass_myclass_obj_t *right_hand_side = MP_OBJ_TO_PTR(rhs);
switch (op) {
case MP_BINARY_OP_EQUAL:
return mp_obj_new_bool((left_hand_side->a == right_hand_side->a) && (left_hand_side->b == right_hand_side->b));
case MP_BINARY_OP_ADD:
return create_new_myclass(left_hand_side->a + right_hand_side->a, left_hand_side->b + right_hand_side->b);
case MP_BINARY_OP_MULTIPLY:
return create_new_myclass(left_hand_side->a * right_hand_side->a, left_hand_side->b * right_hand_side->b);
default:
return MP_OBJ_NULL; // op not supported
}
specialclass_myclass_obj_t *right_hand_side = MP_OBJ_TO_PTR(rhs);
switch (op) {
case MP_BINARY_OP_EQUAL:
return mp_obj_new_bool((left_hand_side->a == right_hand_side->a) && (left_hand_side->b == right_hand_side->b));
case MP_BINARY_OP_ADD:
return create_new_myclass(left_hand_side->a + right_hand_side->a, left_hand_side->b + right_hand_side->b);
case MP_BINARY_OP_MULTIPLY:
return create_new_myclass(left_hand_side->a * right_hand_side->a, left_hand_side->b * right_hand_side->b);
default:
return MP_OBJ_NULL; // op not supported
}
}
const mp_obj_type_t specialclass_myclass_type = {
{ &mp_type_type },
.name = MP_QSTR_specialclass,
.print = myclass_print,
.make_new = myclass_make_new,
.unary_op = specialclass_unary_op,
.binary_op = specialclass_binary_op,
.locals_dict = (mp_obj_dict_t*)&myclass_locals_dict,
{ &mp_type_type },
.name = MP_QSTR_specialclass,
.print = myclass_print,
.make_new = myclass_make_new,
.unary_op = specialclass_unary_op,
.binary_op = specialclass_binary_op,
.locals_dict = (mp_obj_dict_t*)&myclass_locals_dict,
};
STATIC const mp_map_elem_t specialclass_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_specialclass) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_myclass), (mp_obj_t)&specialclass_myclass_type },
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_specialclass) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_myclass), (mp_obj_t)&specialclass_myclass_type },
};
STATIC MP_DEFINE_CONST_DICT (
mp_module_specialclass_globals,
specialclass_globals_table
mp_module_specialclass_globals,
specialclass_globals_table
);
const mp_obj_module_t specialclass_user_cmodule = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_specialclass_globals,
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_specialclass_globals,
};
MP_REGISTER_MODULE(MP_QSTR_specialclass, specialclass_user_cmodule, MODULE_SPECIALCLASS_ENABLED);

68
subscriptiterable/subscriptiterable.c

@ -1,3 +1,13 @@
/*
* This file is part of the micropython-usermod project,
*
* https://github.com/v923z/micropython-usermod
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Zoltán Vörös
*/
#include <stdlib.h>
#include "py/obj.h"
#include "py/runtime.h"
@ -13,26 +23,26 @@ const mp_obj_type_t subiterable_array_type;
mp_obj_t mp_obj_new_subitarray_iterator(mp_obj_t , size_t , mp_obj_iter_buf_t *);
STATIC void subitarray_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind;
subitarray_obj_t *self = MP_OBJ_TO_PTR(self_in);
printf("subitarray: ");
for(uint16_t i=0; i < self->len; i++) {
printf("%d ", self->elements[i]);
}
printf("\n");
(void)kind;
subitarray_obj_t *self = MP_OBJ_TO_PTR(self_in);
printf("subitarray: ");
for(uint16_t i=0; i < self->len; i++) {
printf("%d ", self->elements[i]);
}
printf("\n");
}
STATIC mp_obj_t subitarray_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 1, 1, true);
subitarray_obj_t *self = m_new_obj(subitarray_obj_t);
self->base.type = &subiterable_array_type;
self->len = mp_obj_get_int(args[0]);
uint16_t *arr = malloc(self->len * sizeof(uint16_t));
for(uint16_t i=0; i < self->len; i++) {
arr[i] = i*i;
}
self->elements = arr;
return MP_OBJ_FROM_PTR(self);
mp_arg_check_num(n_args, n_kw, 1, 1, true);
subitarray_obj_t *self = m_new_obj(subitarray_obj_t);
self->base.type = &subiterable_array_type;
self->len = mp_obj_get_int(args[0]);
uint16_t *arr = malloc(self->len * sizeof(uint16_t));
for(uint16_t i=0; i < self->len; i++) {
arr[i] = i*i;
}
self->elements = arr;
return MP_OBJ_FROM_PTR(self);
}
STATIC mp_obj_t subitarray_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
@ -40,17 +50,17 @@ STATIC mp_obj_t subitarray_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
}
STATIC mp_obj_t subitarray_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
subitarray_obj_t *self = MP_OBJ_TO_PTR(self_in);
size_t idx = mp_obj_get_int(index);
if(self->len <= idx) {
mp_raise_msg(&mp_type_IndexError, "index is out of range");
}
subitarray_obj_t *self = MP_OBJ_TO_PTR(self_in);
size_t idx = mp_obj_get_int(index);
if(self->len <= idx) {
mp_raise_msg(&mp_type_IndexError, "index is out of range");
}
if (value == MP_OBJ_SENTINEL) { // simply return the value at index, no assignment
return MP_OBJ_NEW_SMALL_INT(self->elements[idx]);
} else { // value was passed, replace the element at index
self->elements[idx] = mp_obj_get_int(value);
}
return mp_const_none;
return MP_OBJ_NEW_SMALL_INT(self->elements[idx]);
} else { // value was passed, replace the element at index
self->elements[idx] = mp_obj_get_int(value);
}
return mp_const_none;
}
const mp_obj_type_t subiterable_array_type = {
@ -87,8 +97,8 @@ mp_obj_t subitarray_iternext(mp_obj_t self_in) {
mp_obj_subitarray_it_t *self = MP_OBJ_TO_PTR(self_in);
subitarray_obj_t *subitarray = MP_OBJ_TO_PTR(self->subitarray);
if (self->cur < subitarray->len) {
// read the current value
uint16_t *arr = subitarray->elements;
// read the current value
uint16_t *arr = subitarray->elements;
mp_obj_t o_out = MP_OBJ_NEW_SMALL_INT(arr[self->cur]);
self->cur += 1;
return o_out;

54
vector/vector.c

@ -1,3 +1,13 @@
/*
* This file is part of the micropython-usermod project,
*
* https://github.com/v923z/micropython-usermod
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Zoltán Vörös
*/
#include <math.h>
#include <stdio.h>
#include "py/obj.h"
@ -12,40 +22,37 @@ typedef struct _vector_obj_t {
} vector_obj_t;
STATIC mp_obj_t vector_length(mp_obj_t o_in) {
if(!mp_obj_is_type(o_in, &vector_type)) {
mp_raise_TypeError("argument is not a vector");
}
vector_obj_t *vector = MP_OBJ_TO_PTR(o_in);
return mp_obj_new_float(sqrtf(vector->x*vector->x + vector->y*vector->y + vector->z*vector->z));
if(!mp_obj_is_type(o_in, &vector_type)) {
mp_raise_TypeError("argument is not a vector");
}
vector_obj_t *vector = MP_OBJ_TO_PTR(o_in);
return mp_obj_new_float(sqrtf(vector->x*vector->x + vector->y*vector->y + vector->z*vector->z));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(vector_length_obj, vector_length);
STATIC void vector_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind;
vector_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_print_str(print, ", ");
mp_print_float(print, self->x, 'f', 0, 0, 10, 6);
printf("vector(%f, %f, %f)\n", (double)self->x, (double)self->y, (double)self->z);
(void)kind;
vector_obj_t *self = MP_OBJ_TO_PTR(self_in);
printf("vector(%f, %f, %f)\n", (double)self->x, (double)self->y, (double)self->z);
}
STATIC mp_obj_t vector_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 3, 3, true);
vector_obj_t *vector = m_new_obj(vector_obj_t);
vector->base.type = &vector_type;
vector->x = mp_obj_get_float(args[0]);
vector->y = mp_obj_get_float(args[1]);
vector->z = mp_obj_get_float(args[2]);
return MP_OBJ_FROM_PTR(vector);
mp_arg_check_num(n_args, n_kw, 3, 3, true);
vector_obj_t *vector = m_new_obj(vector_obj_t);
vector->base.type = &vector_type;
vector->x = mp_obj_get_float(args[0]);
vector->y = mp_obj_get_float(args[1]);
vector->z = mp_obj_get_float(args[2]);
return MP_OBJ_FROM_PTR(vector);
}
const mp_obj_type_t vector_type = {
{ &mp_type_type },
.name = MP_QSTR_vector,
.print = vector_print,
.make_new = vector_make_new,
{ &mp_type_type },
.name = MP_QSTR_vector,
.print = vector_print,
.make_new = vector_make_new,
};
STATIC const mp_rom_map_elem_t vector_module_globals_table[] = {
@ -61,4 +68,3 @@ const mp_obj_module_t vector_user_cmodule = {
};
MP_REGISTER_MODULE(MP_QSTR_vector, vector_user_cmodule, MODULE_VECTOR_ENABLED);

Loading…
Cancel
Save