Merge remote-tracking branch 'upstream/master' into listsort. Lots of conflict fun.

Conflicts:
	py/obj.h
	py/objbool.c
	py/objboundmeth.c
	py/objcell.c
	py/objclass.c
	py/objclosure.c
	py/objcomplex.c
	py/objdict.c
	py/objexcept.c
	py/objfun.c
	py/objgenerator.c
	py/objinstance.c
	py/objmodule.c
	py/objrange.c
	py/objset.c
	py/objslice.c
This commit is contained in:
John R. Lenton 2014-01-07 18:01:08 +00:00
commit 270112f731
54 changed files with 2684 additions and 520 deletions

14
examples/accellog.py Normal file
View File

@ -0,0 +1,14 @@
# log the accelerometer values to a file, 1 per second
f = open('motion.dat', 'w') # open the file for writing
for i in range(60): # loop 60 times
time = pyb.time() # get the current time
accel = pyb.accel() # get the accelerometer data
# write time and x,y,z values to the file
f.write('{} {} {} {}\n'.format(time, accel[0], accel[1], accel[2]))
pyb.delay(1000) # wait 1000 ms = 1 second
f.close() # close the file

43
examples/conwaylife.py Normal file
View File

@ -0,0 +1,43 @@
# do 1 iteration of Conway's Game of Life
def conway_step():
for x in range(128): # loop over x coordinates
for y in range(32): # loop over y coordinates
# count number of neigbours
num_neighbours = (lcd.get(x - 1, y - 1) +
lcd.get(x, y - 1) +
lcd.get(x + 1, y - 1) +
lcd.get(x - 1, y) +
lcd.get(x + 1, y) +
lcd.get(x + 1, y + 1) +
lcd.get(x, y + 1) +
lcd.get(x - 1, y + 1))
# check if the centre cell is alive or not
self = lcd.get(x, y)
# apply the rules of life
if self and not (2 <= num_neighbours <= 3):
lcd.reset(x, y) # not enough, or too many neighbours: cell dies
elif not self and num_neighbours == 3:
lcd.set(x, y) # exactly 3 neigbours around an empty cell: cell is born
# randomise the start
def conway_rand():
lcd.clear() # clear the LCD
for x in range(128): # loop over x coordinates
for y in range(32): # loop over y coordinates
if pyb.rand() & 1: # get a 1-bit random number
lcd.set(x, y) # set the pixel randomly
# loop for a certain number of frames, doing iterations of Conway's Game of Life
def conway_go(num_frames):
for i in range(num_frames):
conway_step() # do 1 iteration
lcd.show() # update the LCD
# PC testing
import lcd
import pyb
lcd = lcd.LCD(128, 32)
conway_rand()
conway_go(100)

36
examples/lcd.py Normal file
View File

@ -0,0 +1,36 @@
# LCD testing object for PC
# uses double buffering
class LCD:
def __init__(self, width, height):
self.width = width
self.height = height
self.buf1 = [[0 for x in range(self.width)] for y in range(self.height)]
self.buf2 = [[0 for x in range(self.width)] for y in range(self.height)]
def clear(self):
for y in range(self.height):
for x in range(self.width):
self.buf1[y][x] = self.buf2[y][x] = 0
def show(self):
print('') # blank line to separate frames
for y in range(self.height):
for x in range(self.width):
self.buf1[y][x] = self.buf2[y][x]
for y in range(self.height):
row = ''.join(['*' if self.buf1[y][x] else ' ' for x in range(self.width)])
print(row)
def get(self, x, y):
if 0 <= x < self.width and 0 <= y < self.height:
return self.buf1[y][x]
else:
return 0
def reset(self, x, y):
if 0 <= x < self.width and 0 <= y < self.height:
self.buf2[y][x] = 0
def set(self, x, y):
if 0 <= x < self.width and 0 <= y < self.height:
self.buf2[y][x] = 1

22
examples/ledangle.py Normal file
View File

@ -0,0 +1,22 @@
def led_angle(seconds_to_run_for):
# make LED objects
l1 = pyb.Led(1)
l2 = pyb.Led(2)
for i in range(20 * seconds_to_run_for):
# get x-axis
accel = pyb.accel()[0]
# turn on LEDs depending on angle
if accel < -10:
l1.on()
l2.off()
elif accel > 10:
l1.off()
l2.on()
else:
l1.off()
l2.off()
# delay so that loop runs at at 1/50ms = 20Hz
pyb.delay(50)

View File

@ -1,14 +1,22 @@
@micropython.native def mandelbrot():
def in_set(c): # returns True if c, complex, is in the Mandelbrot set
z = 0 @micropython.native
for i in range(40): def in_set(c):
z = z*z + c z = 0
if abs(z) > 60: for i in range(40):
return False z = z*z + c
return True if abs(z) > 60:
return False
return True
for v in range(31): lcd.clear()
line = []
for u in range(91): for u in range(91):
line.append('*' if in_set((u / 30 - 2) + (v / 15 - 1) * 1j) else ' ') for v in range(31):
print(''.join(line)) if in_set((u / 30 - 2) + (v / 15 - 1) * 1j):
lcd.set(u, v)
lcd.show()
# PC testing
import lcd
lcd = lcd.LCD(128, 32)
mandelbrot()

11
examples/pyb.py Normal file
View File

@ -0,0 +1,11 @@
# pyboard testing functions for PC
def delay(n):
pass
rand_seed = 1
def rand():
global rand_seed
# for these choice of numbers, see P L'Ecuyer, "Tables of linear congruential generators of different sizes and good lattice structure"
rand_seed = (rand_seed * 653276) % 8388593
return rand_seed

587
logo/vector-logo-2-BW.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 36 KiB

BIN
logo/vector-logo-2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 115 KiB

View File

@ -1,16 +1,18 @@
#include <stdio.h> #include <stdio.h>
#include <assert.h> #include <assert.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <string.h> #include <string.h>
#include "misc.h" #include "misc.h"
#include "asmx64.h"
#include "mpconfig.h" #include "mpconfig.h"
// wrapper around everything in this file // wrapper around everything in this file
#if MICROPY_EMIT_X64 #if MICROPY_EMIT_X64
#include <sys/types.h>
#include <sys/mman.h>
#include "asmx64.h"
#if defined(__OpenBSD__) || defined(__MACH__) #if defined(__OpenBSD__) || defined(__MACH__)
#define MAP_ANONYMOUS MAP_ANON #define MAP_ANONYMOUS MAP_ANON
#endif #endif

View File

@ -6,6 +6,8 @@
#include "mpconfig.h" #include "mpconfig.h"
#include "gc.h" #include "gc.h"
#if MICROPY_ENABLE_GC
// a machine word is big enough to hold a pointer // a machine word is big enough to hold a pointer
/* /*
#define BYTES_PER_WORD (8) #define BYTES_PER_WORD (8)
@ -380,3 +382,5 @@ int main(void) {
gc_dump_at(); gc_dump_at();
} }
*/ */
#endif // MICROPY_ENABLE_GC

View File

@ -4,8 +4,11 @@
#include <fcntl.h> #include <fcntl.h>
#include "misc.h" #include "misc.h"
#include "mpconfig.h"
#include "lexer.h" #include "lexer.h"
#if MICROPY_ENABLE_LEXER_UNIX
typedef struct _str_buf_t { typedef struct _str_buf_t {
bool free; // free src_beg when done bool free; // free src_beg when done
const char *src_beg; // beginning of source const char *src_beg; // beginning of source
@ -78,3 +81,5 @@ mp_lexer_t *mp_import_open_file(qstr mod_name) {
vstr_printf(vstr, "%s.py", qstr_str(mod_name)); vstr_printf(vstr, "%s.py", qstr_str(mod_name));
return mp_lexer_new_from_file(vstr_str(vstr)); // TODO does lexer need to copy the string? can we free it here? return mp_lexer_new_from_file(vstr_str(vstr)); // TODO does lexer need to copy the string? can we free it here?
} }
#endif // MICROPY_ENABLE_LEXER_UNIX

View File

@ -5,11 +5,7 @@
/** types *******************************************************/ /** types *******************************************************/
typedef int bool; #include <stdbool.h>
enum {
false = 0,
true = 1
};
typedef unsigned char byte; typedef unsigned char byte;
typedef unsigned int uint; typedef unsigned int uint;

View File

@ -4,8 +4,80 @@
#include <mpconfigport.h> #include <mpconfigport.h>
#ifndef INT_FMT // Any options not explicitly set in mpconfigport.h will get default
// values below.
/*****************************************************************************/
/* Micro Python emitters */
// Whether to emit CPython byte codes (for debugging/testing)
// Enabling this overrides all other emitters
#ifndef MICROPY_EMIT_CPYTHON
#define MICROPY_EMIT_CPYTHON (0)
#endif
// Whether to emit x64 native code
#ifndef MICROPY_EMIT_X64
#define MICROPY_EMIT_X64 (0)
#endif
// Whether to emit thumb native code
#ifndef MICROPY_EMIT_THUMB
#define MICROPY_EMIT_THUMB (0)
#endif
// Whether to enable the thumb inline assembler
#ifndef MICROPY_EMIT_INLINE_THUMB
#define MICROPY_EMIT_INLINE_THUMB (0)
#endif
/*****************************************************************************/
/* Internal debugging stuff */
// Whether to collect memory allocation stats
#ifndef MICROPY_MEM_STATS
#define MICROPY_MEM_STATS (0)
#endif
// Whether to build code to show byte code
#ifndef MICROPY_SHOW_BC
#define MICROPY_SHOW_BC (0)
#endif
/*****************************************************************************/
/* Fine control over Python features */
// Whether to include the garbage collector
#ifndef MICROPY_ENABLE_GC
#define MICROPY_ENABLE_GC (0)
#endif
// Whether to include REPL helper function
#ifndef MICROPY_ENABLE_REPL_HELPERS
#define MICROPY_ENABLE_REPL_HELPERS (0)
#endif
// Whether to include lexer helper function for unix
#ifndef MICROPY_ENABLE_LEXER_UNIX
#define MICROPY_ENABLE_LEXER_UNIX (0)
#endif
// Whether to support float and complex types
#ifndef MICROPY_ENABLE_FLOAT
#define MICROPY_ENABLE_FLOAT (0)
#endif
// Whether to support slice object and correspondingly
// slice subscript operators
#ifndef MICROPY_ENABLE_SLICE
#define MICROPY_ENABLE_SLICE (1)
#endif
/*****************************************************************************/
/* Miscellaneous settings */
// printf format spec to use for machine_int_t and friends // printf format spec to use for machine_int_t and friends
#ifndef INT_FMT
#ifdef __LP64__ #ifdef __LP64__
// Archs where machine_int_t == long, long != int // Archs where machine_int_t == long, long != int
#define UINT_FMT "%lu" #define UINT_FMT "%lu"
@ -16,18 +88,3 @@
#define INT_FMT "%d" #define INT_FMT "%d"
#endif #endif
#endif //INT_FMT #endif //INT_FMT
// Any options not explicitly set in mpconfigport.h will get default
// values below.
// Whether to collect memory allocation stats
#ifndef MICROPY_MEM_STATS
#define MICROPY_MEM_STATS (1)
#endif
// Whether to support slice object and correspondingly
// slice subscript operators
#ifndef MICROPY_ENABLE_SLICE
#define MICROPY_ENABLE_SLICE (1)
#endif

View File

@ -15,15 +15,14 @@ typedef machine_int_t mp_small_int_t;
typedef machine_float_t mp_float_t; typedef machine_float_t mp_float_t;
#endif #endif
// Anything that wants to be a Micro Python object must // Anything that wants to be a Micro Python object must have
// have mp_obj_base_t as its first member (except NULL and small ints) // mp_obj_base_t as its first member (except NULL and small ints)
typedef struct _mp_obj_base_t mp_obj_base_t;
typedef struct _mp_obj_type_t mp_obj_type_t;
struct _mp_obj_type_t;
struct _mp_obj_base_t { struct _mp_obj_base_t {
const mp_obj_type_t *type; const struct _mp_obj_type_t *type;
}; };
typedef struct _mp_obj_base_t mp_obj_base_t;
// The NULL object is used to indicate the absence of an object // The NULL object is used to indicate the absence of an object
// It *cannot* be used when an mp_obj_t is expected, except where explicitly allowed // It *cannot* be used when an mp_obj_t is expected, except where explicitly allowed
@ -43,13 +42,14 @@ struct _mp_obj_base_t {
#define MP_DECLARE_CONST_FUN_OBJ(obj_name) extern const mp_obj_fun_native_t obj_name #define MP_DECLARE_CONST_FUN_OBJ(obj_name) extern const mp_obj_fun_native_t obj_name
#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, 0, 0, fun_name} #define MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, is_kw, n_args_min, n_args_max, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, is_kw, n_args_min, n_args_max, (void *)fun_name}
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, 1, 1, fun_name} #define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 0, 0, (mp_fun_0_t)fun_name)
#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, 2, 2, fun_name} #define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 1, 1, (mp_fun_1_t)fun_name)
#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, 3, 3, fun_name} #define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 2, 2, (mp_fun_2_t)fun_name)
#define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, n_args_min, (~((machine_uint_t)0)), fun_name} #define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 3, 3, (mp_fun_3_t)fun_name)
#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, n_args_min, n_args_max, fun_name} #define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, n_args_min, (~((machine_uint_t)0)), (mp_fun_var_t)fun_name)
#define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, true, 0, (~((machine_uint_t)0)), fun_name} #define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, n_args_min, n_args_max, (mp_fun_var_t)fun_name)
#define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, true, 0, (~((machine_uint_t)0)), (mp_fun_var_t)fun_name)
// Need to declare this here so we are not dependent on map.h // Need to declare this here so we are not dependent on map.h
struct _mp_map_t; struct _mp_map_t;
@ -90,7 +90,7 @@ struct _mp_obj_type_t {
mp_fun_1_t getiter; mp_fun_1_t getiter;
mp_fun_1_t iternext; mp_fun_1_t iternext;
const mp_method_t methods[]; const mp_method_t *methods;
/* /*
What we might need to add here: What we might need to add here:
@ -115,6 +115,8 @@ struct _mp_obj_type_t {
*/ */
}; };
typedef struct _mp_obj_type_t mp_obj_type_t;
// Constant objects, globally accessible // Constant objects, globally accessible
extern const mp_obj_type_t mp_const_type; extern const mp_obj_type_t mp_const_type;
@ -245,6 +247,7 @@ typedef struct _mp_obj_fun_native_t { // need this so we can define const object
// for const function objects, make an empty, const map // for const function objects, make an empty, const map
// such functions won't be able to access the global scope, but that's probably okay // such functions won't be able to access the global scope, but that's probably okay
} mp_obj_fun_native_t; } mp_obj_fun_native_t;
extern const mp_obj_type_t fun_native_type; extern const mp_obj_type_t fun_native_type;
extern const mp_obj_type_t fun_bc_type; extern const mp_obj_type_t fun_bc_type;
void mp_obj_fun_bc_get(mp_obj_t self_in, int *n_args, uint *n_state, const byte **code); void mp_obj_fun_bc_get(mp_obj_t self_in, int *n_args, uint *n_state, const byte **code);

View File

@ -32,11 +32,10 @@ static mp_obj_t bool_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args
} }
const mp_obj_type_t bool_type = { const mp_obj_type_t bool_type = {
.base = { &mp_const_type }, { &mp_const_type },
.name = "bool", "bool",
.print = bool_print, .print = bool_print,
.make_new = bool_make_new, .make_new = bool_make_new,
.methods = {{NULL, NULL},},
}; };
static const mp_obj_bool_t false_obj = {{&bool_type}, false}; static const mp_obj_bool_t false_obj = {{&bool_type}, false};

View File

@ -34,10 +34,9 @@ mp_obj_t bound_meth_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
} }
const mp_obj_type_t bound_meth_type = { const mp_obj_type_t bound_meth_type = {
.base = { &mp_const_type }, { &mp_const_type },
.name = "bound_method", "bound_method",
.call_n = bound_meth_call_n, .call_n = bound_meth_call_n,
.methods = {{NULL, NULL},},
}; };
mp_obj_t mp_obj_new_bound_meth(mp_obj_t self, mp_obj_t meth) { mp_obj_t mp_obj_new_bound_meth(mp_obj_t self, mp_obj_t meth) {

View File

@ -24,9 +24,8 @@ void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj) {
} }
const mp_obj_type_t cell_type = { const mp_obj_type_t cell_type = {
.base = { &mp_const_type }, { &mp_const_type },
.name = "cell", "cell",
.methods = {{NULL, NULL},},
}; };
mp_obj_t mp_obj_new_cell(mp_obj_t obj) { mp_obj_t mp_obj_new_cell(mp_obj_t obj) {

View File

@ -61,10 +61,9 @@ mp_map_t *mp_obj_class_get_locals(mp_obj_t self_in) {
} }
const mp_obj_type_t class_type = { const mp_obj_type_t class_type = {
.base = { &mp_const_type }, { &mp_const_type },
.name = "class", "class",
.call_n = class_call_n, .call_n = class_call_n,
.methods = {{NULL, NULL},},
}; };
mp_obj_t mp_obj_new_class(mp_map_t *class_locals) { mp_obj_t mp_obj_new_class(mp_map_t *class_locals) {

View File

@ -33,10 +33,9 @@ mp_obj_t closure_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
} }
const mp_obj_type_t closure_type = { const mp_obj_type_t closure_type = {
.base = { &mp_const_type }, { &mp_const_type },
.name = "closure", "closure",
.call_n = closure_call_n, .call_n = closure_call_n,
.methods = {{NULL, NULL},},
}; };
mp_obj_t mp_obj_new_closure(mp_obj_t fun, mp_obj_t closure_tuple) { mp_obj_t mp_obj_new_closure(mp_obj_t fun, mp_obj_t closure_tuple) {

View File

@ -85,13 +85,12 @@ static mp_obj_t complex_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
} }
const mp_obj_type_t complex_type = { const mp_obj_type_t complex_type = {
.base = { &mp_const_type }, { &mp_const_type },
.name = "complex", "complex",
.print = complex_print, .print = complex_print,
.make_new = complex_make_new, .make_new = complex_make_new,
.unary_op = complex_unary_op, .unary_op = complex_unary_op,
.binary_op = complex_binary_op, .binary_op = complex_binary_op,
.methods = { { NULL, NULL }, },
}; };
mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) { mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) {

View File

@ -66,7 +66,6 @@ const mp_obj_type_t dict_type = {
.print = dict_print, .print = dict_print,
.make_new = dict_make_new, .make_new = dict_make_new,
.binary_op = dict_binary_op, .binary_op = dict_binary_op,
.methods = {{NULL, NULL},},
}; };
mp_obj_t mp_obj_new_dict(int n_args) { mp_obj_t mp_obj_new_dict(int n_args) {

View File

@ -36,10 +36,9 @@ void exception_print(void (*print)(void *env, const char *fmt, ...), void *env,
} }
const mp_obj_type_t exception_type = { const mp_obj_type_t exception_type = {
.base = { &mp_const_type }, { &mp_const_type },
.name = "exception", "exception",
.print = exception_print, .print = exception_print,
.methods = {{NULL, NULL},},
}; };
mp_obj_t mp_obj_new_exception(qstr id) { mp_obj_t mp_obj_new_exception(qstr id) {

View File

@ -68,7 +68,6 @@ const mp_obj_type_t float_type = {
.make_new = float_make_new, .make_new = float_make_new,
.unary_op = float_unary_op, .unary_op = float_unary_op,
.binary_op = float_binary_op, .binary_op = float_binary_op,
.methods = { { NULL, NULL }, },
}; };
mp_obj_t mp_obj_new_float(mp_float_t value) { mp_obj_t mp_obj_new_float(mp_float_t value) {

View File

@ -92,13 +92,10 @@ mp_obj_t fun_native_call_n_kw(mp_obj_t self_in, int n_args, int n_kw, const mp_o
} }
const mp_obj_type_t fun_native_type = { const mp_obj_type_t fun_native_type = {
.base = { &mp_const_type }, { &mp_const_type },
.name = "function", "function",
.call_n = fun_native_call_n, .call_n = fun_native_call_n,
.call_n_kw = fun_native_call_n_kw, .call_n_kw = fun_native_call_n_kw,
.methods = {
{NULL, NULL}, // end-of-list sentinel
},
}; };
mp_obj_t rt_make_function_0(mp_fun_0_t fun) { mp_obj_t rt_make_function_0(mp_fun_0_t fun) {
@ -189,12 +186,9 @@ mp_obj_t fun_bc_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
} }
const mp_obj_type_t fun_bc_type = { const mp_obj_type_t fun_bc_type = {
.base = { &mp_const_type }, { &mp_const_type },
.name = "function", "function",
.call_n = fun_bc_call_n, .call_n = fun_bc_call_n,
.methods = {
{NULL, NULL}, // end-of-list sentinel
},
}; };
mp_obj_t mp_obj_new_fun_bc(int n_args, uint n_state, const byte *code) { mp_obj_t mp_obj_new_fun_bc(int n_args, uint n_state, const byte *code) {
@ -297,12 +291,9 @@ mp_obj_t fun_asm_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
} }
static const mp_obj_type_t fun_asm_type = { static const mp_obj_type_t fun_asm_type = {
.base = { &mp_const_type }, { &mp_const_type },
.name = "function", "function",
.call_n = fun_asm_call_n, .call_n = fun_asm_call_n,
.methods = {
{NULL, NULL}, // end-of-list sentinel
},
}; };
mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun) { mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun) {

View File

@ -37,12 +37,9 @@ mp_obj_t gen_wrap_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
} }
const mp_obj_type_t gen_wrap_type = { const mp_obj_type_t gen_wrap_type = {
.base = { &mp_const_type }, { &mp_const_type },
.name = "generator", "generator",
.call_n = gen_wrap_call_n, .call_n = gen_wrap_call_n,
.unary_op = NULL,
.binary_op = NULL,
.methods = {{NULL, NULL},},
}; };
mp_obj_t mp_obj_new_gen_wrap(uint n_locals, uint n_stack, mp_obj_t fun) { mp_obj_t mp_obj_new_gen_wrap(uint n_locals, uint n_stack, mp_obj_t fun) {
@ -88,12 +85,11 @@ mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
} }
const mp_obj_type_t gen_instance_type = { const mp_obj_type_t gen_instance_type = {
.base = { &mp_const_type }, { &mp_const_type },
.name = "generator", "generator",
.print = gen_instance_print, .print = gen_instance_print,
.getiter = gen_instance_getiter, .getiter = gen_instance_getiter,
.iternext = gen_instance_iternext, .iternext = gen_instance_iternext,
.methods = {{NULL, NULL},},
}; };
// args are in reverse order in the array // args are in reverse order in the array

View File

@ -90,9 +90,8 @@ void mp_obj_instance_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
} }
const mp_obj_type_t instance_type = { const mp_obj_type_t instance_type = {
.base = { &mp_const_type }, { &mp_const_type },
.name = "instance", "instance",
.methods = {{NULL, NULL},},
}; };
mp_obj_t mp_obj_new_instance(mp_obj_t class) { mp_obj_t mp_obj_new_instance(mp_obj_t class) {

View File

@ -34,7 +34,6 @@ const mp_obj_type_t int_type = {
.base = { &mp_const_type }, .base = { &mp_const_type },
.name = "int", .name = "int",
.make_new = int_make_new, .make_new = int_make_new,
.methods = { { NULL, NULL }, },
}; };
mp_obj_t mp_obj_new_int(machine_int_t value) { mp_obj_t mp_obj_new_int(machine_int_t value) {

View File

@ -58,6 +58,7 @@ static mp_obj_t list_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args
default: default:
nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "list takes at most 1 argument, %d given", (void*)(machine_int_t)n_args)); nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "list takes at most 1 argument, %d given", (void*)(machine_int_t)n_args));
} }
return NULL;
} }
static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
@ -277,27 +278,28 @@ static MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, list_remove);
static MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse); static MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse);
static MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, list_sort); static MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, list_sort);
static const mp_method_t list_type_methods[] = {
{ "append", &list_append_obj },
{ "clear", &list_clear_obj },
{ "copy", &list_copy_obj },
{ "count", &list_count_obj },
{ "index", &list_index_obj },
{ "insert", &list_insert_obj },
{ "pop", &list_pop_obj },
{ "remove", &list_remove_obj },
{ "reverse", &list_reverse_obj },
{ "sort", &list_sort_obj },
{ NULL, NULL }, // end-of-list sentinel
};
const mp_obj_type_t list_type = { const mp_obj_type_t list_type = {
.base = { &mp_const_type }, .base = { &mp_const_type },
.name = "list", .name = "list",
.print = list_print, .print = list_print,
.make_new = list_make_new, .make_new = list_make_new,
.unary_op = NULL,
.binary_op = list_binary_op, .binary_op = list_binary_op,
.getiter = list_getiter, .getiter = list_getiter,
.methods = { .methods = list_type_methods,
{ "append", &list_append_obj },
{ "clear", &list_clear_obj },
{ "copy", &list_copy_obj },
{ "count", &list_count_obj },
{ "index", &list_index_obj },
{ "insert", &list_insert_obj },
{ "pop", &list_pop_obj },
{ "remove", &list_remove_obj },
{ "reverse", &list_reverse_obj },
{ "sort", &list_sort_obj },
{ NULL, NULL }, // end-of-list sentinel
},
}; };
static mp_obj_list_t *list_new(uint n) { static mp_obj_list_t *list_new(uint n) {
@ -361,7 +363,6 @@ static const mp_obj_type_t list_it_type = {
.base = { &mp_const_type }, .base = { &mp_const_type },
.name = "list_iterator", .name = "list_iterator",
.iternext = list_it_iternext, .iternext = list_it_iternext,
.methods = { { NULL, NULL }, },
}; };
mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) { mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) {

View File

@ -6,6 +6,7 @@
#include "nlr.h" #include "nlr.h"
#include "misc.h" #include "misc.h"
#include "mpconfig.h" #include "mpconfig.h"
#include "mpqstr.h"
#include "obj.h" #include "obj.h"
#include "runtime.h" #include "runtime.h"
#include "map.h" #include "map.h"
@ -22,17 +23,17 @@ void module_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_
} }
const mp_obj_type_t module_type = { const mp_obj_type_t module_type = {
.base = { &mp_const_type }, { &mp_const_type },
.name = "module", "module",
.print = module_print, .print = module_print,
.methods = {{NULL, NULL},},
}; };
mp_obj_t mp_obj_new_module(qstr module_name) { mp_obj_t mp_obj_new_module(qstr module_name) {
mp_obj_module_t *o = m_new_obj(mp_obj_module_t); mp_obj_module_t *o = m_new_obj(mp_obj_module_t);
o->base.type = &module_type; o->base.type = &module_type;
o->name = module_name; o->name = module_name;
o->globals = mp_map_new(MP_MAP_QSTR, 0); o->globals = mp_map_new(MP_MAP_QSTR, 1);
mp_qstr_map_lookup(o->globals, MP_QSTR___name__, true)->value = mp_obj_new_str(module_name);
return o; return o;
} }

View File

@ -18,7 +18,6 @@ const mp_obj_type_t none_type = {
.base = { &mp_const_type }, .base = { &mp_const_type },
.name = "NoneType", .name = "NoneType",
.print = none_print, .print = none_print,
.methods = {{NULL, NULL},},
}; };
static const mp_obj_none_t none_obj = {{&none_type}}; static const mp_obj_none_t none_obj = {{&none_type}};

View File

@ -23,10 +23,9 @@ mp_obj_t range_getiter(mp_obj_t o_in) {
} }
static const mp_obj_type_t range_type = { static const mp_obj_type_t range_type = {
.base = { &mp_const_type} , { &mp_const_type} ,
.name = "range", "range",
.getiter = range_getiter, .getiter = range_getiter,
.methods = {{NULL, NULL},},
}; };
// range is a class and instances are immutable sequence objects // range is a class and instances are immutable sequence objects
@ -62,10 +61,9 @@ mp_obj_t range_it_iternext(mp_obj_t o_in) {
} }
static const mp_obj_type_t range_it_type = { static const mp_obj_type_t range_it_type = {
.base = { &mp_const_type }, { &mp_const_type },
.name = "range_iterator", "range_iterator",
.iternext = range_it_iternext, .iternext = range_it_iternext,
.methods = {{NULL, NULL},},
}; };
mp_obj_t mp_obj_new_range_iterator(int cur, int stop, int step) { mp_obj_t mp_obj_new_range_iterator(int cur, int stop, int step) {

View File

@ -55,11 +55,10 @@ static mp_obj_t set_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args)
} }
const mp_obj_type_t set_type = { const mp_obj_type_t set_type = {
.base = { &mp_const_type }, { &mp_const_type },
.name = "set", "set",
.print = set_print, .print = set_print,
.make_new = set_make_new, .make_new = set_make_new,
.methods = { { NULL, NULL }, },
}; };
mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) { mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {

View File

@ -21,10 +21,9 @@ void ellipsis_print(void (*print)(void *env, const char *fmt, ...), void *env, m
} }
const mp_obj_type_t ellipsis_type = { const mp_obj_type_t ellipsis_type = {
.base = { &mp_const_type }, { &mp_const_type },
.name = "ellipsis", "ellipsis",
.print = ellipsis_print, .print = ellipsis_print,
.methods = {{NULL, NULL},},
}; };
static const mp_obj_ellipsis_t ellipsis_obj = {{&ellipsis_type}}; static const mp_obj_ellipsis_t ellipsis_obj = {{&ellipsis_type}};
@ -52,7 +51,6 @@ const mp_obj_type_t slice_type = {
.base = { &mp_const_type }, .base = { &mp_const_type },
.name = "slice", .name = "slice",
.print = slice_print, .print = slice_print,
.methods = { { NULL, NULL }, },
}; };
// TODO: Make sure to handle "empty" values, which are signified by None in CPython // TODO: Make sure to handle "empty" values, which are signified by None in CPython

View File

@ -184,17 +184,19 @@ mp_obj_t str_format(int n_args, const mp_obj_t *args) {
static MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join); static MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join);
static MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format); static MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format);
static const mp_method_t str_type_methods[] = {
{ "join", &str_join_obj },
{ "format", &str_format_obj },
{ NULL, NULL }, // end-of-list sentinel
};
const mp_obj_type_t str_type = { const mp_obj_type_t str_type = {
.base = { &mp_const_type }, .base = { &mp_const_type },
.name = "str", .name = "str",
.print = str_print, .print = str_print,
.binary_op = str_binary_op, .binary_op = str_binary_op,
.getiter = str_getiter, .getiter = str_getiter,
.methods = { .methods = str_type_methods,
{ "join", &str_join_obj },
{ "format", &str_format_obj },
{ NULL, NULL }, // end-of-list sentinel
},
}; };
mp_obj_t mp_obj_new_str(qstr qstr) { mp_obj_t mp_obj_new_str(qstr qstr) {
@ -235,7 +237,6 @@ static const mp_obj_type_t str_it_type = {
.base = { &mp_const_type }, .base = { &mp_const_type },
.name = "str_iterator", .name = "str_iterator",
.iternext = str_it_iternext, .iternext = str_it_iternext,
.methods = { { NULL, NULL }, },
}; };
mp_obj_t mp_obj_new_str_iterator(mp_obj_str_t *str, int cur) { mp_obj_t mp_obj_new_str_iterator(mp_obj_str_t *str, int cur) {

View File

@ -103,7 +103,6 @@ const mp_obj_type_t tuple_type = {
.make_new = tuple_make_new, .make_new = tuple_make_new,
.binary_op = tuple_binary_op, .binary_op = tuple_binary_op,
.getiter = tuple_getiter, .getiter = tuple_getiter,
.methods = {{NULL, NULL},},
}; };
// the zero-length tuple // the zero-length tuple
@ -166,7 +165,6 @@ static const mp_obj_type_t tuple_it_type = {
.base = { &mp_const_type }, .base = { &mp_const_type },
.name = "tuple_iterator", .name = "tuple_iterator",
.iternext = tuple_it_iternext, .iternext = tuple_it_iternext,
.methods = {{NULL, NULL},},
}; };
static mp_obj_t mp_obj_new_tuple_iterator(mp_obj_tuple_t *tuple, int cur) { static mp_obj_t mp_obj_new_tuple_iterator(mp_obj_tuple_t *tuple, int cur) {

View File

@ -27,5 +27,4 @@ const mp_obj_type_t mp_const_type = {
.name = "type", .name = "type",
.print = type_print, .print = type_print,
.call_n = type_call_n, .call_n = type_call_n,
.methods = {{NULL, NULL},},
}; };

105
py/py.mk Normal file
View File

@ -0,0 +1,105 @@
# default settings; can be overriden in main Makefile
ifndef PY_SRC
PY_SRC = ../py
endif
ifndef BUILD
BUILD = build
endif
# to create the build directory
$(BUILD):
mkdir -p $@
# where py object files go (they have a name prefix to prevent filename clashes)
PY_BUILD = $(BUILD)/py.
# py object files
PY_O_BASENAME = \
nlrx86.o \
nlrx64.o \
nlrthumb.o \
malloc.o \
gc.o \
qstr.o \
vstr.o \
unicode.o \
lexer.o \
lexerunix.o \
parse.o \
scope.o \
compile.o \
emitcommon.o \
emitpass1.o \
emitcpy.o \
emitbc.o \
asmx64.o \
emitnx64.o \
asmthumb.o \
emitnthumb.o \
emitinlinethumb.o \
runtime.o \
map.o \
obj.o \
objbool.o \
objboundmeth.o \
objcell.o \
objclass.o \
objclosure.o \
objcomplex.o \
objdict.o \
objexcept.o \
objfloat.o \
objfun.o \
objgenerator.o \
objinstance.o \
objint.o \
objlist.o \
objmodule.o \
objnone.o \
objrange.o \
objset.o \
objslice.o \
objstr.o \
objtuple.o \
objtype.o \
builtin.o \
builtinimport.o \
vm.o \
showbc.o \
repl.o \
# prepend the build destination prefix to the py object files
PY_O = $(addprefix $(PY_BUILD), $(PY_O_BASENAME))
$(PY_BUILD)emitnx64.o: $(PY_SRC)/emitnative.c $(PY_SRC)/emit.h mpconfigport.h
$(CC) $(CFLAGS) -DN_X64 -c -o $@ $<
$(PY_BUILD)emitnthumb.o: $(PY_SRC)/emitnative.c $(PY_SRC)/emit.h mpconfigport.h
$(CC) $(CFLAGS) -DN_THUMB -c -o $@ $<
$(PY_BUILD)%.o: $(PY_SRC)/%.S
$(CC) $(CFLAGS) -c -o $@ $<
$(PY_BUILD)%.o: $(PY_SRC)/%.c mpconfigport.h
$(CC) $(CFLAGS) -c -o $@ $<
# optimising gc for speed; 5ms down to 4ms on pybv2
$(PY_BUILD)gc.o: $(PY_SRC)/gc.c
$(CC) $(CFLAGS) -O3 -c -o $@ $<
# optimising vm for speed, adds only a small amount to code size but makes a huge difference to speed (20% faster)
$(PY_BUILD)vm.o: $(PY_SRC)/vm.c
$(CC) $(CFLAGS) -O3 -c -o $@ $<
# header dependencies
$(PY_BUILD)parse.o: $(PY_SRC)/grammar.h
$(PY_BUILD)compile.o: $(PY_SRC)/grammar.h
$(PY_BUILD)/emitcpy.o: $(PY_SRC)/emit.h
$(PY_BUILD)emitbc.o: $(PY_SRC)/emit.h

View File

@ -1,6 +1,9 @@
#include "misc.h" #include "misc.h"
#include "mpconfig.h"
#include "repl.h" #include "repl.h"
#if MICROPY_ENABLE_REPL_HELPERS
bool str_startswith_word(const char *str, const char *head) { bool str_startswith_word(const char *str, const char *head) {
int i; int i;
for (i = 0; str[i] && head[i]; i++) { for (i = 0; str[i] && head[i]; i++) {
@ -42,3 +45,5 @@ bool mp_repl_is_compound_stmt(const char *line) {
} }
return n_paren > 0 || n_brack > 0 || n_brace > 0; return n_paren > 0 || n_brack > 0 || n_brace > 0;
} }
#endif // MICROPY_ENABLE_REPL_HELPERS

View File

@ -1 +1,3 @@
#if MICROPY_ENABLE_REPL_HELPERS
bool mp_repl_is_compound_stmt(const char *line); bool mp_repl_is_compound_stmt(const char *line);
#endif

View File

@ -61,7 +61,8 @@ typedef struct _mp_code_t {
} mp_code_t; } mp_code_t;
static int next_unique_code_id; static int next_unique_code_id;
static mp_code_t *unique_codes; static machine_uint_t unique_codes_alloc = 0;
static mp_code_t *unique_codes = NULL;
#ifdef WRITE_CODE #ifdef WRITE_CODE
FILE *fp_write_code = NULL; FILE *fp_write_code = NULL;
@ -126,6 +127,7 @@ void rt_init(void) {
mp_qstr_map_lookup(&map_builtins, MP_QSTR_sum, true)->value = rt_make_function_var(1, mp_builtin_sum); mp_qstr_map_lookup(&map_builtins, MP_QSTR_sum, true)->value = rt_make_function_var(1, mp_builtin_sum);
next_unique_code_id = 1; // 0 indicates "no code" next_unique_code_id = 1; // 0 indicates "no code"
unique_codes_alloc = 0;
unique_codes = NULL; unique_codes = NULL;
#ifdef WRITE_CODE #ifdef WRITE_CODE
@ -134,6 +136,7 @@ void rt_init(void) {
} }
void rt_deinit(void) { void rt_deinit(void) {
m_del(mp_code_t, unique_codes, unique_codes_alloc);
#ifdef WRITE_CODE #ifdef WRITE_CODE
if (fp_write_code != NULL) { if (fp_write_code != NULL) {
fclose(fp_write_code); fclose(fp_write_code);
@ -146,18 +149,20 @@ int rt_get_unique_code_id(void) {
} }
static void alloc_unique_codes(void) { static void alloc_unique_codes(void) {
if (unique_codes == NULL) { if (next_unique_code_id > unique_codes_alloc) {
unique_codes = m_new(mp_code_t, next_unique_code_id + 10); // XXX hack until we fix the REPL allocation problem // increase size of unique_codes table
for (int i = 0; i < next_unique_code_id; i++) { unique_codes = m_renew(mp_code_t, unique_codes, unique_codes_alloc, next_unique_code_id);
for (int i = unique_codes_alloc; i < next_unique_code_id; i++) {
unique_codes[i].kind = MP_CODE_NONE; unique_codes[i].kind = MP_CODE_NONE;
} }
unique_codes_alloc = next_unique_code_id;
} }
} }
void rt_assign_byte_code(int unique_code_id, byte *code, uint len, int n_args, int n_locals, int n_stack, bool is_generator) { void rt_assign_byte_code(int unique_code_id, byte *code, uint len, int n_args, int n_locals, int n_stack, bool is_generator) {
alloc_unique_codes(); alloc_unique_codes();
assert(1 <= unique_code_id && unique_code_id < next_unique_code_id); assert(1 <= unique_code_id && unique_code_id < next_unique_code_id && unique_codes[unique_code_id].kind == MP_CODE_NONE);
unique_codes[unique_code_id].kind = MP_CODE_BYTE; unique_codes[unique_code_id].kind = MP_CODE_BYTE;
unique_codes[unique_code_id].n_args = n_args; unique_codes[unique_code_id].n_args = n_args;
unique_codes[unique_code_id].n_locals = n_locals; unique_codes[unique_code_id].n_locals = n_locals;
@ -192,7 +197,7 @@ void rt_assign_byte_code(int unique_code_id, byte *code, uint len, int n_args, i
void rt_assign_native_code(int unique_code_id, void *fun, uint len, int n_args) { void rt_assign_native_code(int unique_code_id, void *fun, uint len, int n_args) {
alloc_unique_codes(); alloc_unique_codes();
assert(1 <= unique_code_id && unique_code_id < next_unique_code_id); assert(1 <= unique_code_id && unique_code_id < next_unique_code_id && unique_codes[unique_code_id].kind == MP_CODE_NONE);
unique_codes[unique_code_id].kind = MP_CODE_NATIVE; unique_codes[unique_code_id].kind = MP_CODE_NATIVE;
unique_codes[unique_code_id].n_args = n_args; unique_codes[unique_code_id].n_args = n_args;
unique_codes[unique_code_id].n_locals = 0; unique_codes[unique_code_id].n_locals = 0;
@ -225,7 +230,7 @@ void rt_assign_native_code(int unique_code_id, void *fun, uint len, int n_args)
void rt_assign_inline_asm_code(int unique_code_id, void *fun, uint len, int n_args) { void rt_assign_inline_asm_code(int unique_code_id, void *fun, uint len, int n_args) {
alloc_unique_codes(); alloc_unique_codes();
assert(1 <= unique_code_id && unique_code_id < next_unique_code_id); assert(1 <= unique_code_id && unique_code_id < next_unique_code_id && unique_codes[unique_code_id].kind == MP_CODE_NONE);
unique_codes[unique_code_id].kind = MP_CODE_INLINE_ASM; unique_codes[unique_code_id].kind = MP_CODE_INLINE_ASM;
unique_codes[unique_code_id].n_args = n_args; unique_codes[unique_code_id].n_args = n_args;
unique_codes[unique_code_id].n_locals = 0; unique_codes[unique_code_id].n_locals = 0;
@ -785,10 +790,12 @@ mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
} else if (MP_OBJ_IS_OBJ(base)) { } else if (MP_OBJ_IS_OBJ(base)) {
// generic method lookup // generic method lookup
mp_obj_base_t *o = base; mp_obj_base_t *o = base;
const mp_method_t *meth = &o->type->methods[0]; const mp_method_t *meth = o->type->methods;
for (; meth->name != NULL; meth++) { if (meth != NULL) {
if (strcmp(meth->name, qstr_str(attr)) == 0) { for (; meth->name != NULL; meth++) {
return mp_obj_new_bound_meth(base, (mp_obj_t)meth->fun); if (strcmp(meth->name, qstr_str(attr)) == 0) {
return mp_obj_new_bound_meth(base, (mp_obj_t)meth->fun);
}
} }
} }
} }
@ -809,12 +816,14 @@ void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
} else if (MP_OBJ_IS_OBJ(base)) { } else if (MP_OBJ_IS_OBJ(base)) {
// generic method lookup // generic method lookup
mp_obj_base_t *o = base; mp_obj_base_t *o = base;
const mp_method_t *meth = &o->type->methods[0]; const mp_method_t *meth = o->type->methods;
for (; meth->name != NULL; meth++) { if (meth != NULL) {
if (strcmp(meth->name, qstr_str(attr)) == 0) { for (; meth->name != NULL; meth++) {
dest[1] = (mp_obj_t)meth->fun; if (strcmp(meth->name, qstr_str(attr)) == 0) {
dest[0] = base; dest[1] = (mp_obj_t)meth->fun;
return; dest[0] = base;
return;
}
} }
} }
} }

View File

@ -8,6 +8,8 @@
#include "mpconfig.h" #include "mpconfig.h"
#include "bc0.h" #include "bc0.h"
#if MICROPY_SHOW_BC
#define DECODE_UINT do { unum = *ip++; if (unum > 127) { unum = ((unum & 0x3f) << 8) | (*ip++); } } while (0) #define DECODE_UINT do { unum = *ip++; if (unum > 127) { unum = ((unum & 0x3f) << 8) | (*ip++); } } while (0)
#define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0) #define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0)
#define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0) #define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0)
@ -363,3 +365,5 @@ void mp_show_byte_code(const byte *ip, int len) {
printf("\n"); printf("\n");
} }
} }
#endif // MICROPY_SHOW_BC

View File

@ -1,9 +1,16 @@
# define main target
all: all2
# include py core make definitions
include ../py/py.mk
# program for deletion
RM = /bin/rm
STMSRC=lib STMSRC=lib
#STMOTGSRC=lib-otg #STMOTGSRC=lib-otg
FATFSSRC=fatfs FATFSSRC=fatfs
CC3KSRC=cc3k CC3KSRC=cc3k
PYSRC=../py
BUILD=build
DFU=../tools/dfu.py DFU=../tools/dfu.py
TARGET=PYBOARD TARGET=PYBOARD
@ -11,7 +18,7 @@ AS = arm-none-eabi-as
CC = arm-none-eabi-gcc CC = arm-none-eabi-gcc
LD = arm-none-eabi-ld LD = arm-none-eabi-ld
CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mabi=aapcs-linux -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion -DSTM32F40XX -DHSE_VALUE=8000000 CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mabi=aapcs-linux -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion -DSTM32F40XX -DHSE_VALUE=8000000
CFLAGS = -I. -I$(PYSRC) -I$(FATFSSRC) -I$(STMSRC) -Wall -ansi -std=gnu99 -Os -DNDEBUG $(CFLAGS_CORTEX_M4) -D$(TARGET) CFLAGS = -I. -I$(PY_SRC) -I$(FATFSSRC) -I$(STMSRC) -Wall -ansi -std=gnu99 -Os -DNDEBUG $(CFLAGS_CORTEX_M4) -D$(TARGET)
#CFLAGS += -I$(STMOTGSRC) -DUSE_HOST_MODE -DUSE_OTG_MODE #CFLAGS += -I$(STMOTGSRC) -DUSE_HOST_MODE -DUSE_OTG_MODE
LDFLAGS = --nostdlib -T stm32f405.ld LDFLAGS = --nostdlib -T stm32f405.ld
@ -43,53 +50,6 @@ SRC_S = \
startup_stm32f40xx.s \ startup_stm32f40xx.s \
gchelper.s \ gchelper.s \
PY_O = \
nlrthumb.o \
gc.o \
malloc.o \
qstr.o \
vstr.o \
unicode.o \
lexer.o \
parse.o \
scope.o \
compile.o \
emitcommon.o \
emitpass1.o \
emitbc.o \
asmthumb.o \
emitnthumb.o \
emitinlinethumb.o \
runtime.o \
map.o \
obj.o \
objbool.o \
objboundmeth.o \
objcell.o \
objclass.o \
objclosure.o \
objcomplex.o \
objdict.o \
objexcept.o \
objfloat.o \
objfun.o \
objgenerator.o \
objinstance.o \
objint.o \
objlist.o \
objmodule.o \
objnone.o \
objrange.o \
objset.o \
objslice.o \
objstr.o \
objtuple.o \
objtype.o \
builtin.o \
builtinimport.o \
vm.o \
repl.o \
SRC_FATFS = \ SRC_FATFS = \
ff.c \ ff.c \
diskio.c \ diskio.c \
@ -146,10 +106,10 @@ SRC_CC3K = \
ccspi.c \ ccspi.c \
pybcc3k.c \ pybcc3k.c \
OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o) $(PY_O) $(SRC_FATFS:.c=.o) $(SRC_STM:.c=.o) $(SRC_CC3K:.c=.o)) OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o) $(SRC_FATFS:.c=.o) $(SRC_STM:.c=.o) $(SRC_CC3K:.c=.o)) $(PY_O)
#OBJ += $(addprefix $(BUILD)/, $(SRC_STM_OTG:.c=.o)) #OBJ += $(addprefix $(BUILD)/, $(SRC_STM_OTG:.c=.o))
all: $(BUILD) $(BUILD)/flash.dfu all2: $(BUILD) $(BUILD)/flash.dfu
$(BUILD)/flash.dfu: $(BUILD)/flash0.bin $(BUILD)/flash1.bin $(BUILD)/flash.dfu: $(BUILD)/flash0.bin $(BUILD)/flash1.bin
python $(DFU) -b 0x08000000:$(BUILD)/flash0.bin -b 0x08020000:$(BUILD)/flash1.bin $@ python $(DFU) -b 0x08000000:$(BUILD)/flash0.bin -b 0x08020000:$(BUILD)/flash1.bin $@
@ -164,9 +124,6 @@ $(BUILD)/flash.elf: $(OBJ)
$(LD) $(LDFLAGS) -o $@ $(OBJ) $(LD) $(LDFLAGS) -o $@ $(OBJ)
arm-none-eabi-size $@ arm-none-eabi-size $@
$(BUILD):
mkdir -p $@
$(BUILD)/%.o: %.s $(BUILD)/%.o: %.s
$(AS) -o $@ $< $(AS) -o $@ $<
@ -185,31 +142,7 @@ $(BUILD)/%.o: $(STMSRC)/%.c
$(BUILD)/%.o: $(CC3KSRC)/%.c $(BUILD)/%.o: $(CC3KSRC)/%.c
$(CC) $(CFLAGS) -c -o $@ $< $(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/%.o: $(PYSRC)/%.s
$(AS) -o $@ $<
$(BUILD)/%.o: $(PYSRC)/%.S
$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/%.o: $(PYSRC)/%.c mpconfigport.h
$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/emitnthumb.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h
$(CC) $(CFLAGS) -DN_THUMB -c -o $@ $<
# optimising gc for speed; 5ms down to 4ms
$(BUILD)/gc.o: $(PYSRC)/gc.c
$(CC) $(CFLAGS) -O3 -c -o $@ $<
# optimising vm for speed, adds only a small amount to code size but makes a huge difference to speed (20% faster)
$(BUILD)/vm.o: $(PYSRC)/vm.c
$(CC) $(CFLAGS) -O3 -c -o $@ $<
$(BUILD)/parse.o: $(PYSRC)/grammar.h
$(BUILD)/compile.o: $(PYSRC)/grammar.h
$(BUILD)/emitbc.o: $(PYSRC)/emit.h
clean: clean:
/bin/rm -rf $(BUILD) $(RM) -rf $(BUILD)
.PHONY: all clean .PHONY: all all2 clean

View File

@ -326,18 +326,20 @@ static MP_DEFINE_CONST_FUN_OBJ_1(i2c_obj_read_obj, i2c_obj_read);
static MP_DEFINE_CONST_FUN_OBJ_1(i2c_obj_readAndStop_obj, i2c_obj_readAndStop); static MP_DEFINE_CONST_FUN_OBJ_1(i2c_obj_readAndStop_obj, i2c_obj_readAndStop);
static MP_DEFINE_CONST_FUN_OBJ_1(i2c_obj_stop_obj, i2c_obj_stop); static MP_DEFINE_CONST_FUN_OBJ_1(i2c_obj_stop_obj, i2c_obj_stop);
static const mp_method_t i2c_methods[] = {
{ "start", &i2c_obj_start_obj },
{ "write", &i2c_obj_write_obj },
{ "read", &i2c_obj_read_obj },
{ "readAndStop", &i2c_obj_readAndStop_obj },
{ "stop", &i2c_obj_stop_obj },
{ NULL, NULL },
};
static const mp_obj_type_t i2c_obj_type = { static const mp_obj_type_t i2c_obj_type = {
{ &mp_const_type }, { &mp_const_type },
"I2C", "I2C",
.print = i2c_obj_print, .print = i2c_obj_print,
.methods = { .methods = i2c_methods,
{ "start", &i2c_obj_start_obj },
{ "write", &i2c_obj_write_obj },
{ "read", &i2c_obj_read_obj },
{ "readAndStop", &i2c_obj_readAndStop_obj },
{ "stop", &i2c_obj_stop_obj },
{ NULL, NULL },
}
}; };
// create the I2C object // create the I2C object

View File

@ -176,15 +176,17 @@ mp_obj_t led_obj_off(mp_obj_t self_in) {
static MP_DEFINE_CONST_FUN_OBJ_1(led_obj_on_obj, led_obj_on); static MP_DEFINE_CONST_FUN_OBJ_1(led_obj_on_obj, led_obj_on);
static MP_DEFINE_CONST_FUN_OBJ_1(led_obj_off_obj, led_obj_off); static MP_DEFINE_CONST_FUN_OBJ_1(led_obj_off_obj, led_obj_off);
static const mp_method_t led_methods[] = {
{ "on", &led_obj_on_obj },
{ "off", &led_obj_off_obj },
{ NULL, NULL },
};
static const mp_obj_type_t led_obj_type = { static const mp_obj_type_t led_obj_type = {
{ &mp_const_type }, { &mp_const_type },
"Led", "Led",
.print = led_obj_print, .print = led_obj_print,
.methods = { .methods = led_methods,
{ "on", &led_obj_on_obj },
{ "off", &led_obj_off_obj },
{ NULL, NULL },
}
}; };
mp_obj_t pyb_Led(mp_obj_t led_id) { mp_obj_t pyb_Led(mp_obj_t led_id) {

View File

@ -689,22 +689,18 @@ static MP_DEFINE_CONST_FUN_OBJ_1(file_obj_close_obj, file_obj_close);
// TODO gc hook to close the file if not already closed // TODO gc hook to close the file if not already closed
static const mp_method_t file_methods[] = {
{ "read", &file_obj_read_obj },
{ "write", &file_obj_write_obj },
{ "close", &file_obj_close_obj },
{NULL, NULL},
};
static const mp_obj_type_t file_obj_type = { static const mp_obj_type_t file_obj_type = {
{ &mp_const_type }, { &mp_const_type },
"File", "File",
file_obj_print, // print .print = file_obj_print,
NULL, // make_new .methods = file_methods,
NULL, // call_n
NULL, // unary_op
NULL, // binary_op
NULL, // getiter
NULL, // iternext
.methods = {
{ "read", &file_obj_read_obj },
{ "write", &file_obj_write_obj },
{ "close", &file_obj_close_obj },
{NULL, NULL},
}
}; };
mp_obj_t pyb_io_open(mp_obj_t o_filename, mp_obj_t o_mode) { mp_obj_t pyb_io_open(mp_obj_t o_filename, mp_obj_t o_mode) {

View File

@ -2,11 +2,11 @@
// options to control how Micro Python is built // options to control how Micro Python is built
#define MICROPY_ENABLE_FLOAT (1)
#define MICROPY_EMIT_CPYTHON (0)
#define MICROPY_EMIT_X64 (0)
#define MICROPY_EMIT_THUMB (1) #define MICROPY_EMIT_THUMB (1)
#define MICROPY_EMIT_INLINE_THUMB (1) #define MICROPY_EMIT_INLINE_THUMB (1)
#define MICROPY_ENABLE_GC (1)
#define MICROPY_ENABLE_REPL_HELPERS (1)
#define MICROPY_ENABLE_FLOAT (1)
// type definitions for the specific machine // type definitions for the specific machine

View File

@ -137,14 +137,16 @@ static mp_obj_t servo_obj_angle(mp_obj_t self_in, mp_obj_t angle) {
static MP_DEFINE_CONST_FUN_OBJ_2(servo_obj_angle_obj, servo_obj_angle); static MP_DEFINE_CONST_FUN_OBJ_2(servo_obj_angle_obj, servo_obj_angle);
static const mp_method_t servo_methods[] = {
{ "angle", &servo_obj_angle_obj },
{ NULL, NULL },
};
static const mp_obj_type_t servo_obj_type = { static const mp_obj_type_t servo_obj_type = {
{ &mp_const_type }, { &mp_const_type },
"Servo", "Servo",
.print = servo_obj_print, .print = servo_obj_print,
.methods = { .methods = servo_methods,
{ "angle", &servo_obj_angle_obj },
{ NULL, NULL },
}
}; };
mp_obj_t pyb_Servo(mp_obj_t servo_id) { mp_obj_t pyb_Servo(mp_obj_t servo_id) {

View File

@ -1,95 +1,37 @@
PYSRC=../py # define main target
BUILD=build PROG = cpy
all: $(PROG)
# include py core make definitions
include ../py/py.mk
# program for deletion
RM = /bin/rm
# compiler settings
CC = gcc CC = gcc
CFLAGS = -I. -I$(PYSRC) -Wall -Werror -ansi -std=gnu99 -Os #-DNDEBUG CFLAGS = -I. -I$(PY_SRC) -Wall -Werror -ansi -std=gnu99 -Os #-DNDEBUG
LDFLAGS = -lm LDFLAGS = -lm
# source files
SRC_C = \ SRC_C = \
main.c \ main.c \
PY_O = \ OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) $(PY_O)
nlrx86.o \
nlrx64.o \
malloc.o \
qstr.o \
vstr.o \
unicode.o \
lexer.o \
lexerunix.o \
parse.o \
scope.o \
compile.o \
emitcommon.o \
emitpass1.o \
emitcpy.o \
runtime.o \
map.o \
obj.o \
objbool.o \
objboundmeth.o \
objcell.o \
objclass.o \
objclosure.o \
objcomplex.o \
objdict.o \
objexcept.o \
objfloat.o \
objfun.o \
objgenerator.o \
objinstance.o \
objint.o \
objlist.o \
objmodule.o \
objnone.o \
objrange.o \
objset.o \
objslice.o \
objstr.o \
objtuple.o \
objtype.o \
builtin.o \
builtinimport.o \
vm.o \
showbc.o \
repl.o \
OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(PY_O))
LIB = LIB =
PROG = cpy
$(PROG): $(BUILD) $(OBJ) $(PROG): $(BUILD) $(OBJ)
$(CC) -o $@ $(OBJ) $(LIB) $(LDFLAGS) $(CC) -o $@ $(OBJ) $(LIB) $(LDFLAGS)
strip $(PROG)
$(BUILD): size $(PROG)
mkdir -p $@
$(BUILD)/%.o: %.c $(BUILD)/%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $< $(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/%.o: $(PYSRC)/%.S
$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/%.o: $(PYSRC)/%.c mpconfigport.h
$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/emitnx64.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h
$(CC) $(CFLAGS) -DN_X64 -c -o $@ $<
$(BUILD)/emitnthumb.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h
$(CC) $(CFLAGS) -DN_THUMB -c -o $@ $<
# optimising vm for speed, adds only a small amount to code size but makes a huge difference to speed (20% faster)
$(BUILD)/vm.o: $(PYSRC)/vm.c
$(CC) $(CFLAGS) -O3 -c -o $@ $<
$(BUILD)/main.o: mpconfigport.h $(BUILD)/main.o: mpconfigport.h
$(BUILD)/parse.o: $(PYSRC)/grammar.h
$(BUILD)/compile.o: $(PYSRC)/grammar.h
$(BUILD)/emitcpy.o: $(PYSRC)/emit.h
$(BUILD)/emitbc.o: $(PYSRC)/emit.h
clean: clean:
/bin/rm -rf $(BUILD) $(RM) -f $(PROG)
$(RM) -rf $(BUILD)
.PHONY: clean .PHONY: all clean

View File

@ -11,7 +11,6 @@
#include "obj.h" #include "obj.h"
#include "compile.h" #include "compile.h"
#include "runtime0.h" #include "runtime0.h"
#include "runtime.h"
void do_file(const char *file) { void do_file(const char *file) {
mp_lexer_t *lex = mp_lexer_new_from_file(file); mp_lexer_t *lex = mp_lexer_new_from_file(file);

View File

@ -1,10 +1,8 @@
// options to control how Micro Python is built // options to control how Micro Python is built
#define MICROPY_ENABLE_FLOAT (1)
#define MICROPY_EMIT_CPYTHON (1) #define MICROPY_EMIT_CPYTHON (1)
#define MICROPY_EMIT_X64 (0) #define MICROPY_ENABLE_LEXER_UNIX (1)
#define MICROPY_EMIT_THUMB (0) #define MICROPY_ENABLE_FLOAT (1)
#define MICROPY_EMIT_INLINE_THUMB (0)
// type definitions for the specific machine // type definitions for the specific machine

View File

@ -1,106 +1,39 @@
PYSRC=../py # define main target
BUILD=build PROG = py
all: $(PROG)
# include py core make definitions
include ../py/py.mk
# program for deletion
RM = /bin/rm
# compiler settings
CC = gcc CC = gcc
CFLAGS = -I. -I$(PYSRC) -Wall -Werror -ansi -std=gnu99 -Os #-DNDEBUG CFLAGS = -I. -I$(PY_SRC) -Wall -Werror -ansi -std=gnu99 -Os #-DNDEBUG
LDFLAGS = -lm LDFLAGS = -lm
# source files
SRC_C = \ SRC_C = \
main.c \ main.c \
PY_O = \ OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) $(PY_O)
nlrx86.o \
nlrx64.o \
nlrthumb.o \
malloc.o \
qstr.o \
vstr.o \
unicode.o \
lexer.o \
lexerunix.o \
parse.o \
scope.o \
compile.o \
emitcommon.o \
emitpass1.o \
emitcpy.o \
emitbc.o \
asmx64.o \
emitnx64.o \
asmthumb.o \
emitnthumb.o \
emitinlinethumb.o \
runtime.o \
map.o \
obj.o \
objbool.o \
objboundmeth.o \
objcell.o \
objclass.o \
objclosure.o \
objcomplex.o \
objdict.o \
objexcept.o \
objfloat.o \
objfun.o \
objgenerator.o \
objinstance.o \
objint.o \
objlist.o \
objmodule.o \
objnone.o \
objrange.o \
objset.o \
objslice.o \
objstr.o \
objtuple.o \
objtype.o \
builtin.o \
builtinimport.o \
vm.o \
showbc.o \
repl.o \
OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(PY_O))
LIB = -lreadline LIB = -lreadline
# the following is needed for BSD # the following is needed for BSD
#LIB += -ltermcap #LIB += -ltermcap
PROG = py
$(PROG): $(BUILD) $(OBJ) $(PROG): $(BUILD) $(OBJ)
$(CC) -o $@ $(OBJ) $(LIB) $(LDFLAGS) $(CC) -o $@ $(OBJ) $(LIB) $(LDFLAGS)
strip $(PROG) strip $(PROG)
size $(PROG) size $(PROG)
$(BUILD):
mkdir -p $@
$(BUILD)/%.o: %.c $(BUILD)/%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $< $(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/%.o: $(PYSRC)/%.S
$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/%.o: $(PYSRC)/%.c mpconfigport.h
$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/emitnx64.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h mpconfigport.h
$(CC) $(CFLAGS) -DN_X64 -c -o $@ $<
$(BUILD)/emitnthumb.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h mpconfigport.h
$(CC) $(CFLAGS) -DN_THUMB -c -o $@ $<
# optimising vm for speed, adds only a small amount to code size but makes a huge difference to speed (20% faster)
$(BUILD)/vm.o: $(PYSRC)/vm.c
$(CC) $(CFLAGS) -O3 -c -o $@ $<
$(BUILD)/main.o: mpconfigport.h $(BUILD)/main.o: mpconfigport.h
$(BUILD)/parse.o: $(PYSRC)/grammar.h
$(BUILD)/compile.o: $(PYSRC)/grammar.h
$(BUILD)/emitcpy.o: $(PYSRC)/emit.h
$(BUILD)/emitbc.o: $(PYSRC)/emit.h
clean: clean:
/bin/rm -rf $(BUILD) $(RM) -f $(PROG)
$(RM) -rf $(BUILD)
.PHONY: clean .PHONY: all clean

View File

@ -20,6 +20,52 @@
#include <readline/history.h> #include <readline/history.h>
#endif #endif
static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
if (lex == NULL) {
return;
}
if (0) {
// just tokenise
while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
mp_token_show(mp_lexer_cur(lex));
mp_lexer_to_next(lex);
}
mp_lexer_free(lex);
return;
}
mp_parse_node_t pn = mp_parse(lex, input_kind);
mp_lexer_free(lex);
if (pn == MP_PARSE_NODE_NULL) {
// parse error
return;
}
//printf("----------------\n");
//parse_node_show(pn, 0);
//printf("----------------\n");
mp_obj_t module_fun = mp_compile(pn, is_repl);
if (module_fun == mp_const_none) {
// compile error
return;
}
// execute it
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
rt_call_function_0(module_fun);
nlr_pop();
} else {
// uncaught exception
mp_obj_print((mp_obj_t)nlr.ret_val);
printf("\n");
}
}
static char *str_join(const char *s1, int sep_char, const char *s2) { static char *str_join(const char *s1, int sep_char, const char *s2) {
int l1 = strlen(s1); int l1 = strlen(s1);
int l2 = strlen(s2); int l2 = strlen(s2);
@ -80,28 +126,11 @@ static void do_repl(void) {
} }
mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", line, strlen(line), false); mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", line, strlen(line), false);
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT); execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
mp_lexer_free(lex);
if (pn != MP_PARSE_NODE_NULL) {
//mp_parse_node_show(pn, 0);
mp_obj_t module_fun = mp_compile(pn, true);
if (module_fun != mp_const_none) {
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
rt_call_function_0(module_fun);
nlr_pop();
} else {
// uncaught exception
mp_obj_print((mp_obj_t)nlr.ret_val);
printf("\n");
}
}
}
} }
} }
void do_file(const char *file) { static void do_file(const char *file) {
// hack: set dir for import based on where this file is // hack: set dir for import based on where this file is
{ {
const char * s = strrchr(file, '/'); const char * s = strrchr(file, '/');
@ -115,58 +144,17 @@ void do_file(const char *file) {
} }
mp_lexer_t *lex = mp_lexer_new_from_file(file); mp_lexer_t *lex = mp_lexer_new_from_file(file);
//const char *pysrc = "def f():\n x=x+1\n print(42)\n"; execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
//mp_lexer_t *lex = mp_lexer_from_str_len("<>", pysrc, strlen(pysrc), false); }
if (lex == NULL) {
return;
}
if (0) { static void do_str(const char *str) {
// just tokenise mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", str, strlen(str), false);
while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) { execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
mp_token_show(mp_lexer_cur(lex));
mp_lexer_to_next(lex);
}
mp_lexer_free(lex);
} else {
// compile
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT);
mp_lexer_free(lex);
if (pn != MP_PARSE_NODE_NULL) {
//printf("----------------\n");
//parse_node_show(pn, 0);
//printf("----------------\n");
mp_obj_t module_fun = mp_compile(pn, false);
//printf("----------------\n");
#if MICROPY_EMIT_CPYTHON
if (!comp_ok) {
printf("compile error\n");
}
#else
if (1 && module_fun != mp_const_none) {
// execute it
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
rt_call_function_0(module_fun);
nlr_pop();
} else {
// uncaught exception
mp_obj_print((mp_obj_t)nlr.ret_val);
printf("\n");
}
}
#endif
}
}
} }
typedef struct _test_obj_t { typedef struct _test_obj_t {
mp_obj_base_t base; mp_obj_base_t base;
bool value; int value;
} test_obj_t; } test_obj_t;
static void test_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) { static void test_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
@ -188,21 +176,17 @@ static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get); static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set); static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
static const mp_method_t test_methods[] = {
{ "get", &test_get_obj },
{ "set", &test_set_obj },
{ NULL, NULL },
};
static const mp_obj_type_t test_type = { static const mp_obj_type_t test_type = {
{ &mp_const_type }, { &mp_const_type },
"Test", "Test",
.print = test_print, .print = test_print,
.make_new = NULL, .methods = test_methods,
.call_n = NULL,
.unary_op = NULL,
.binary_op = NULL,
.getiter = NULL,
.iternext = NULL,
.methods = {
{ "get", &test_get_obj },
{ "set", &test_set_obj },
{ NULL, NULL },
}
}; };
mp_obj_t test_obj_new(int value) { mp_obj_t test_obj_new(int value) {
@ -212,6 +196,11 @@ mp_obj_t test_obj_new(int value) {
return o; return o;
} }
int usage(void) {
printf("usage: py [-c <command>] [<filename>]\n");
return 1;
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
qstr_init(); qstr_init();
rt_init(); rt_init();
@ -227,12 +216,24 @@ int main(int argc, char **argv) {
if (argc == 1) { if (argc == 1) {
do_repl(); do_repl();
} else if (argc == 2) {
do_file(argv[1]);
} else { } else {
printf("usage: py [<file>]\n"); for (int a = 1; a < argc; a++) {
return 1; if (argv[a][0] == '-') {
if (strcmp(argv[a], "-c") == 0) {
if (a + 1 >= argc) {
return usage();
}
do_str(argv[a + 1]);
a += 1;
} else {
return usage();
}
} else {
do_file(argv[a]);
}
}
} }
rt_deinit(); rt_deinit();
//printf("total bytes = %d\n", m_get_total_bytes_allocated()); //printf("total bytes = %d\n", m_get_total_bytes_allocated());

View File

@ -5,11 +5,13 @@
#define MICROPY_USE_READLINE (1) #define MICROPY_USE_READLINE (1)
#endif #endif
#define MICROPY_ENABLE_FLOAT (1)
#define MICROPY_EMIT_CPYTHON (0)
#define MICROPY_EMIT_X64 (1) #define MICROPY_EMIT_X64 (1)
#define MICROPY_EMIT_THUMB (0) #define MICROPY_EMIT_THUMB (0)
#define MICROPY_EMIT_INLINE_THUMB (0) #define MICROPY_EMIT_INLINE_THUMB (0)
#define MICROPY_MEM_STATS (1)
#define MICROPY_ENABLE_REPL_HELPERS (1)
#define MICROPY_ENABLE_LEXER_UNIX (1)
#define MICROPY_ENABLE_FLOAT (1)
// type definitions for the specific machine // type definitions for the specific machine