Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
misc.cpp@0:4ab1392a0142, 2012-10-07 (annotated)
- Committer:
- gignops
- Date:
- Sun Oct 07 11:49:09 2012 +0000
- Revision:
- 0:4ab1392a0142
This project is an adaptation of tinypy code. The aim is to run python code on Mbed.; ; Have fun !
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| gignops | 0:4ab1392a0142 | 1 | #include "misc.h" |
| gignops | 0:4ab1392a0142 | 2 | |
| gignops | 0:4ab1392a0142 | 3 | tp_obj *tp_ptr(tp_obj o) { |
| gignops | 0:4ab1392a0142 | 4 | tp_obj *ptr = (tp_obj*)tp_malloc(sizeof(tp_obj)); *ptr = o; |
| gignops | 0:4ab1392a0142 | 5 | return ptr; |
| gignops | 0:4ab1392a0142 | 6 | } |
| gignops | 0:4ab1392a0142 | 7 | |
| gignops | 0:4ab1392a0142 | 8 | tp_obj _tp_dcall(TP,tp_obj fnc(TP)) { |
| gignops | 0:4ab1392a0142 | 9 | return fnc(tp); |
| gignops | 0:4ab1392a0142 | 10 | } |
| gignops | 0:4ab1392a0142 | 11 | tp_obj _tp_tcall(TP,tp_obj fnc) { |
| gignops | 0:4ab1392a0142 | 12 | if (fnc.fnc.ftype&2) { |
| gignops | 0:4ab1392a0142 | 13 | _tp_list_insert(tp,tp->params.list.val,0,fnc.fnc.info->self); |
| gignops | 0:4ab1392a0142 | 14 | } |
| gignops | 0:4ab1392a0142 | 15 | return _tp_dcall(tp,(tp_obj (*)(tp_vm *))fnc.fnc.val); |
| gignops | 0:4ab1392a0142 | 16 | } |
| gignops | 0:4ab1392a0142 | 17 | |
| gignops | 0:4ab1392a0142 | 18 | tp_obj tp_fnc_new(TP,int t, void *v, tp_obj s, tp_obj g) { |
| gignops | 0:4ab1392a0142 | 19 | tp_obj r = {TP_FNC}; |
| gignops | 0:4ab1392a0142 | 20 | _tp_fnc *info = (_tp_fnc*)tp_malloc(sizeof(_tp_fnc)); |
| gignops | 0:4ab1392a0142 | 21 | info->self = s; |
| gignops | 0:4ab1392a0142 | 22 | info->globals = g; |
| gignops | 0:4ab1392a0142 | 23 | r.fnc.ftype = t; |
| gignops | 0:4ab1392a0142 | 24 | r.fnc.info = info; |
| gignops | 0:4ab1392a0142 | 25 | r.fnc.val = v; |
| gignops | 0:4ab1392a0142 | 26 | return tp_track(tp,r); |
| gignops | 0:4ab1392a0142 | 27 | } |
| gignops | 0:4ab1392a0142 | 28 | |
| gignops | 0:4ab1392a0142 | 29 | tp_obj tp_def(TP,void *v, tp_obj g) { |
| gignops | 0:4ab1392a0142 | 30 | return tp_fnc_new(tp,1,v,tp_None,g); |
| gignops | 0:4ab1392a0142 | 31 | } |
| gignops | 0:4ab1392a0142 | 32 | |
| gignops | 0:4ab1392a0142 | 33 | tp_obj tp_fnc(TP,tp_obj v(TP)) { |
| gignops | 0:4ab1392a0142 | 34 | return tp_fnc_new(tp,0,(void*)v,tp_None,tp_None); |
| gignops | 0:4ab1392a0142 | 35 | } |
| gignops | 0:4ab1392a0142 | 36 | |
| gignops | 0:4ab1392a0142 | 37 | tp_obj tp_method(TP,tp_obj self,tp_obj v(TP)) { |
| gignops | 0:4ab1392a0142 | 38 | return tp_fnc_new(tp,2,(void*)v,self,tp_None); |
| gignops | 0:4ab1392a0142 | 39 | } |
| gignops | 0:4ab1392a0142 | 40 | |
| gignops | 0:4ab1392a0142 | 41 | tp_obj tp_data(TP,int magic,void *v) { |
| gignops | 0:4ab1392a0142 | 42 | tp_obj r = {TP_DATA}; |
| gignops | 0:4ab1392a0142 | 43 | r.data.info = (_tp_data*)tp_malloc(sizeof(_tp_data)); |
| gignops | 0:4ab1392a0142 | 44 | r.data.val = v; |
| gignops | 0:4ab1392a0142 | 45 | r.data.magic = magic; |
| gignops | 0:4ab1392a0142 | 46 | return tp_track(tp,r); |
| gignops | 0:4ab1392a0142 | 47 | } |
| gignops | 0:4ab1392a0142 | 48 | |
| gignops | 0:4ab1392a0142 | 49 | tp_obj tp_params(TP) { |
| gignops | 0:4ab1392a0142 | 50 | tp_obj r; |
| gignops | 0:4ab1392a0142 | 51 | tp->params = tp->_params.list.val->items[tp->cur]; |
| gignops | 0:4ab1392a0142 | 52 | r = tp->_params.list.val->items[tp->cur]; |
| gignops | 0:4ab1392a0142 | 53 | r.list.val->len = 0; |
| gignops | 0:4ab1392a0142 | 54 | return r; |
| gignops | 0:4ab1392a0142 | 55 | } |
| gignops | 0:4ab1392a0142 | 56 | tp_obj tp_params_n(TP,int n, tp_obj argv[]) { |
| gignops | 0:4ab1392a0142 | 57 | tp_obj r = tp_params(tp); |
| gignops | 0:4ab1392a0142 | 58 | int i; for (i=0; i<n; i++) { _tp_list_append(tp,r.list.val,argv[i]); } |
| gignops | 0:4ab1392a0142 | 59 | return r; |
| gignops | 0:4ab1392a0142 | 60 | } |
| gignops | 0:4ab1392a0142 | 61 | tp_obj tp_params_v(TP,int n,...) { |
| gignops | 0:4ab1392a0142 | 62 | int i; |
| gignops | 0:4ab1392a0142 | 63 | tp_obj r = tp_params(tp); |
| gignops | 0:4ab1392a0142 | 64 | va_list a; va_start(a,n); |
| gignops | 0:4ab1392a0142 | 65 | for (i=0; i<n; i++) { |
| gignops | 0:4ab1392a0142 | 66 | _tp_list_append(tp,r.list.val,va_arg(a,tp_obj)); |
| gignops | 0:4ab1392a0142 | 67 | } |
| gignops | 0:4ab1392a0142 | 68 | va_end(a); |
| gignops | 0:4ab1392a0142 | 69 | return r; |
| gignops | 0:4ab1392a0142 | 70 | } |