Co-exist with C++ (issue #85)
This commit is contained in:
parent
e03c0533fe
commit
7a16fadbf8
@ -45,7 +45,7 @@ asm_thumb_t *asm_thumb_new(uint max_num_labels) {
|
||||
return as;
|
||||
}
|
||||
|
||||
void asm_thumb_free(asm_thumb_t *as, bool free_code) {
|
||||
void asm_thumb_free(asm_thumb_t *as, MP_BOOL free_code) {
|
||||
if (free_code) {
|
||||
m_del(byte, as->code_base, as->code_size);
|
||||
}
|
||||
@ -56,9 +56,9 @@ void asm_thumb_free(asm_thumb_t *as, bool free_code) {
|
||||
{
|
||||
Label *lab = &g_array_index(as->label, Label, i);
|
||||
if (lab->unresolved != NULL)
|
||||
g_array_free(lab->unresolved, true);
|
||||
g_array_free(lab->unresolved, MP_TRUE);
|
||||
}
|
||||
g_array_free(as->label, true);
|
||||
g_array_free(as->label, MP_TRUE);
|
||||
}
|
||||
*/
|
||||
m_del_obj(asm_thumb_t, as);
|
||||
@ -87,7 +87,7 @@ void asm_thumb_end_pass(asm_thumb_t *as) {
|
||||
int i;
|
||||
for (i = 0; i < as->label->len; ++i)
|
||||
if (g_array_index(as->label, Label, i).unresolved != NULL)
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
@ -44,7 +44,7 @@
|
||||
typedef struct _asm_thumb_t asm_thumb_t;
|
||||
|
||||
asm_thumb_t *asm_thumb_new(uint max_num_labels);
|
||||
void asm_thumb_free(asm_thumb_t *as, bool free_code);
|
||||
void asm_thumb_free(asm_thumb_t *as, MP_BOOL free_code);
|
||||
void asm_thumb_start_pass(asm_thumb_t *as, int pass);
|
||||
void asm_thumb_end_pass(asm_thumb_t *as);
|
||||
uint asm_thumb_get_code_size(asm_thumb_t *as);
|
||||
|
12
py/asmx64.c
12
py/asmx64.c
@ -94,7 +94,7 @@ struct _asm_x64_t {
|
||||
};
|
||||
|
||||
// for allocating memory, see src/v8/src/platform-linux.cc
|
||||
void *alloc_mem(uint req_size, uint *alloc_size, bool is_exec) {
|
||||
void *alloc_mem(uint req_size, uint *alloc_size, MP_BOOL is_exec) {
|
||||
req_size = (req_size + 0xfff) & (~0xfff);
|
||||
int prot = PROT_READ | PROT_WRITE | (is_exec ? PROT_EXEC : 0);
|
||||
void *ptr = mmap(NULL, req_size, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
@ -119,7 +119,7 @@ asm_x64_t* asm_x64_new(uint max_num_labels) {
|
||||
return as;
|
||||
}
|
||||
|
||||
void asm_x64_free(asm_x64_t* as, bool free_code) {
|
||||
void asm_x64_free(asm_x64_t* as, MP_BOOL free_code) {
|
||||
if (free_code) {
|
||||
// need to un-mmap
|
||||
//m_free(as->code_base);
|
||||
@ -131,9 +131,9 @@ void asm_x64_free(asm_x64_t* as, bool free_code) {
|
||||
{
|
||||
Label* lab = &g_array_index(as->label, Label, i);
|
||||
if (lab->unresolved != NULL)
|
||||
g_array_free(lab->unresolved, true);
|
||||
g_array_free(lab->unresolved, MP_TRUE);
|
||||
}
|
||||
g_array_free(as->label, true);
|
||||
g_array_free(as->label, MP_TRUE);
|
||||
}
|
||||
*/
|
||||
m_del_obj(asm_x64_t, as);
|
||||
@ -154,7 +154,7 @@ void asm_x64_end_pass(asm_x64_t *as) {
|
||||
as->code_size = as->code_offset;
|
||||
//as->code_base = m_new(byte, as->code_size); need to allocale executable memory
|
||||
uint actual_alloc;
|
||||
as->code_base = alloc_mem(as->code_size, &actual_alloc, true);
|
||||
as->code_base = alloc_mem(as->code_size, &actual_alloc, MP_TRUE);
|
||||
printf("code_size: %u\n", as->code_size);
|
||||
}
|
||||
|
||||
@ -165,7 +165,7 @@ void asm_x64_end_pass(asm_x64_t *as) {
|
||||
int i;
|
||||
for (i = 0; i < as->label->len; ++i)
|
||||
if (g_array_index(as->label, Label, i).unresolved != NULL)
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
@ -27,7 +27,7 @@
|
||||
typedef struct _asm_x64_t asm_x64_t;
|
||||
|
||||
asm_x64_t* asm_x64_new(uint max_num_labels);
|
||||
void asm_x64_free(asm_x64_t* as, bool free_code);
|
||||
void asm_x64_free(asm_x64_t* as, MP_BOOL free_code);
|
||||
void asm_x64_start_pass(asm_x64_t *as, int pass);
|
||||
void asm_x64_end_pass(asm_x64_t *as);
|
||||
uint asm_x64_get_code_size(asm_x64_t* as);
|
||||
|
2
py/bc.h
2
py/bc.h
@ -1,2 +1,2 @@
|
||||
mp_obj_t mp_execute_byte_code(const byte *code, const mp_obj_t *args, uint n_args, uint n_state);
|
||||
bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **sp_in_out);
|
||||
MP_BOOL mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **sp_in_out);
|
||||
|
@ -58,7 +58,7 @@ mp_obj_t mp_builtin___import__(int n, mp_obj_t *args) {
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t module_fun = mp_compile(pn, false);
|
||||
mp_obj_t module_fun = mp_compile(pn, MP_FALSE);
|
||||
|
||||
if (module_fun == mp_const_none) {
|
||||
// TODO handle compile error correctly
|
||||
|
224
py/compile.c
224
py/compile.c
@ -39,9 +39,9 @@ typedef enum {
|
||||
#define EMIT_OPT_ASM_THUMB (4)
|
||||
|
||||
typedef struct _compiler_t {
|
||||
bool is_repl;
|
||||
MP_BOOL is_repl;
|
||||
pass_kind_t pass;
|
||||
bool had_error; // try to keep compiler clean from nlr
|
||||
MP_BOOL had_error; // try to keep compiler clean from nlr
|
||||
|
||||
int next_label;
|
||||
|
||||
@ -50,9 +50,9 @@ typedef struct _compiler_t {
|
||||
int except_nest_level;
|
||||
|
||||
int n_arg_keyword;
|
||||
bool have_star_arg;
|
||||
bool have_dbl_star_arg;
|
||||
bool have_bare_star;
|
||||
MP_BOOL have_star_arg;
|
||||
MP_BOOL have_dbl_star_arg;
|
||||
MP_BOOL have_bare_star;
|
||||
int param_pass;
|
||||
int param_pass_num_dict_params;
|
||||
int param_pass_num_default_params;
|
||||
@ -261,36 +261,36 @@ void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
}
|
||||
|
||||
#if MICROPY_EMIT_CPYTHON
|
||||
static bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
|
||||
static MP_BOOL cpython_c_tuple_is_const(mp_parse_node_t pn) {
|
||||
if (!MP_PARSE_NODE_IS_LEAF(pn)) {
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
if (MP_PARSE_NODE_IS_ID(pn)) {
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
return true;
|
||||
return MP_TRUE;
|
||||
}
|
||||
|
||||
static void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
|
||||
static void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, MP_BOOL bytes) {
|
||||
const char *str = qstr_str(qstr);
|
||||
int len = strlen(str);
|
||||
bool has_single_quote = false;
|
||||
bool has_double_quote = false;
|
||||
MP_BOOL has_single_quote = MP_FALSE;
|
||||
MP_BOOL has_double_quote = MP_FALSE;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (str[i] == '\'') {
|
||||
has_single_quote = true;
|
||||
has_single_quote = MP_TRUE;
|
||||
} else if (str[i] == '"') {
|
||||
has_double_quote = true;
|
||||
has_double_quote = MP_TRUE;
|
||||
}
|
||||
}
|
||||
if (bytes) {
|
||||
vstr_printf(vstr, "b");
|
||||
}
|
||||
bool quote_single = false;
|
||||
MP_BOOL quote_single = MP_FALSE;
|
||||
if (has_single_quote && !has_double_quote) {
|
||||
vstr_printf(vstr, "\"");
|
||||
} else {
|
||||
quote_single = true;
|
||||
quote_single = MP_TRUE;
|
||||
vstr_printf(vstr, "'");
|
||||
}
|
||||
for (int i = 0; i < len; i++) {
|
||||
@ -319,8 +319,8 @@ static void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vst
|
||||
case MP_PARSE_NODE_SMALL_INT: vstr_printf(vstr, "%d", arg); break;
|
||||
case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
|
||||
case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
|
||||
case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
|
||||
case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
|
||||
case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, MP_FALSE); break;
|
||||
case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, MP_TRUE); break;
|
||||
case MP_PARSE_NODE_TOKEN:
|
||||
switch (arg) {
|
||||
case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
|
||||
@ -339,33 +339,33 @@ static void cpython_c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_
|
||||
n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
|
||||
}
|
||||
int total = n;
|
||||
bool is_const = true;
|
||||
MP_BOOL is_const = MP_TRUE;
|
||||
if (!MP_PARSE_NODE_IS_NULL(pn)) {
|
||||
total += 1;
|
||||
if (!cpython_c_tuple_is_const(pn)) {
|
||||
is_const = false;
|
||||
is_const = MP_FALSE;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
|
||||
is_const = false;
|
||||
is_const = MP_FALSE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (total > 0 && is_const) {
|
||||
bool need_comma = false;
|
||||
MP_BOOL need_comma = MP_FALSE;
|
||||
vstr_t *vstr = vstr_new();
|
||||
vstr_printf(vstr, "(");
|
||||
if (!MP_PARSE_NODE_IS_NULL(pn)) {
|
||||
cpython_c_tuple_emit_const(comp, pn, vstr);
|
||||
need_comma = true;
|
||||
need_comma = MP_TRUE;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (need_comma) {
|
||||
vstr_printf(vstr, ", ");
|
||||
}
|
||||
cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
|
||||
need_comma = true;
|
||||
need_comma = MP_TRUE;
|
||||
}
|
||||
if (total == 1) {
|
||||
vstr_printf(vstr, ",)");
|
||||
@ -412,25 +412,25 @@ void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
c_tuple(comp, MP_PARSE_NODE_NULL, pns);
|
||||
}
|
||||
|
||||
static bool node_is_const_false(mp_parse_node_t pn) {
|
||||
static MP_BOOL node_is_const_false(mp_parse_node_t pn) {
|
||||
return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
|
||||
// untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_ARG(pn) == 1);
|
||||
}
|
||||
|
||||
static bool node_is_const_true(mp_parse_node_t pn) {
|
||||
static MP_BOOL node_is_const_true(mp_parse_node_t pn) {
|
||||
return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE) || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_ARG(pn) == 1);
|
||||
}
|
||||
|
||||
#if MICROPY_EMIT_CPYTHON
|
||||
// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
|
||||
static void cpython_c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label, bool is_nested) {
|
||||
static void cpython_c_if_cond(compiler_t *comp, mp_parse_node_t pn, MP_BOOL jump_if, int label, MP_BOOL is_nested) {
|
||||
if (node_is_const_false(pn)) {
|
||||
if (jump_if == false) {
|
||||
if (jump_if == MP_FALSE) {
|
||||
EMIT(jump, label);
|
||||
}
|
||||
return;
|
||||
} else if (node_is_const_true(pn)) {
|
||||
if (jump_if == true) {
|
||||
if (jump_if == MP_TRUE) {
|
||||
EMIT(jump, label);
|
||||
}
|
||||
return;
|
||||
@ -438,42 +438,42 @@ static void cpython_c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if
|
||||
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
|
||||
int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
|
||||
if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
|
||||
if (jump_if == false) {
|
||||
if (jump_if == MP_FALSE) {
|
||||
int label2 = comp_next_label(comp);
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
|
||||
cpython_c_if_cond(comp, pns->nodes[i], MP_TRUE, label2, MP_TRUE);
|
||||
}
|
||||
cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
|
||||
cpython_c_if_cond(comp, pns->nodes[n - 1], MP_FALSE, label, MP_TRUE);
|
||||
EMIT(label_assign, label2);
|
||||
} else {
|
||||
for (int i = 0; i < n; i++) {
|
||||
cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
|
||||
cpython_c_if_cond(comp, pns->nodes[i], MP_TRUE, label, MP_TRUE);
|
||||
}
|
||||
}
|
||||
return;
|
||||
} else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
|
||||
if (jump_if == false) {
|
||||
if (jump_if == MP_FALSE) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
|
||||
cpython_c_if_cond(comp, pns->nodes[i], MP_FALSE, label, MP_TRUE);
|
||||
}
|
||||
} else {
|
||||
int label2 = comp_next_label(comp);
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
|
||||
cpython_c_if_cond(comp, pns->nodes[i], MP_FALSE, label2, MP_TRUE);
|
||||
}
|
||||
cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
|
||||
cpython_c_if_cond(comp, pns->nodes[n - 1], MP_TRUE, label, MP_TRUE);
|
||||
EMIT(label_assign, label2);
|
||||
}
|
||||
return;
|
||||
} else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
|
||||
cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
|
||||
cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, MP_TRUE);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// nothing special, fall back to default compiling for node and jump
|
||||
compile_node(comp, pn);
|
||||
if (jump_if == false) {
|
||||
if (jump_if == MP_FALSE) {
|
||||
EMIT(pop_jump_if_false, label);
|
||||
} else {
|
||||
EMIT(pop_jump_if_true, label);
|
||||
@ -481,17 +481,17 @@ static void cpython_c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if
|
||||
}
|
||||
#endif
|
||||
|
||||
static void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
|
||||
static void c_if_cond(compiler_t *comp, mp_parse_node_t pn, MP_BOOL jump_if, int label) {
|
||||
#if MICROPY_EMIT_CPYTHON
|
||||
cpython_c_if_cond(comp, pn, jump_if, label, false);
|
||||
cpython_c_if_cond(comp, pn, jump_if, label, MP_FALSE);
|
||||
#else
|
||||
if (node_is_const_false(pn)) {
|
||||
if (jump_if == false) {
|
||||
if (jump_if == MP_FALSE) {
|
||||
EMIT(jump, label);
|
||||
}
|
||||
return;
|
||||
} else if (node_is_const_true(pn)) {
|
||||
if (jump_if == true) {
|
||||
if (jump_if == MP_TRUE) {
|
||||
EMIT(jump, label);
|
||||
}
|
||||
return;
|
||||
@ -499,30 +499,30 @@ static void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int la
|
||||
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
|
||||
int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
|
||||
if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
|
||||
if (jump_if == false) {
|
||||
if (jump_if == MP_FALSE) {
|
||||
int label2 = comp_next_label(comp);
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
c_if_cond(comp, pns->nodes[i], true, label2);
|
||||
c_if_cond(comp, pns->nodes[i], MP_TRUE, label2);
|
||||
}
|
||||
c_if_cond(comp, pns->nodes[n - 1], false, label);
|
||||
c_if_cond(comp, pns->nodes[n - 1], MP_FALSE, label);
|
||||
EMIT(label_assign, label2);
|
||||
} else {
|
||||
for (int i = 0; i < n; i++) {
|
||||
c_if_cond(comp, pns->nodes[i], true, label);
|
||||
c_if_cond(comp, pns->nodes[i], MP_TRUE, label);
|
||||
}
|
||||
}
|
||||
return;
|
||||
} else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
|
||||
if (jump_if == false) {
|
||||
if (jump_if == MP_FALSE) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
c_if_cond(comp, pns->nodes[i], false, label);
|
||||
c_if_cond(comp, pns->nodes[i], MP_FALSE, label);
|
||||
}
|
||||
} else {
|
||||
int label2 = comp_next_label(comp);
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
c_if_cond(comp, pns->nodes[i], false, label2);
|
||||
c_if_cond(comp, pns->nodes[i], MP_FALSE, label2);
|
||||
}
|
||||
c_if_cond(comp, pns->nodes[n - 1], true, label);
|
||||
c_if_cond(comp, pns->nodes[n - 1], MP_TRUE, label);
|
||||
EMIT(label_assign, label2);
|
||||
}
|
||||
return;
|
||||
@ -534,7 +534,7 @@ static void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int la
|
||||
|
||||
// nothing special, fall back to default compiling for node and jump
|
||||
compile_node(comp, pn);
|
||||
if (jump_if == false) {
|
||||
if (jump_if == MP_FALSE) {
|
||||
EMIT(pop_jump_if_false, label);
|
||||
} else {
|
||||
EMIT(pop_jump_if_true, label);
|
||||
@ -803,7 +803,7 @@ void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
|
||||
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
|
||||
if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
|
||||
// bare star
|
||||
comp->have_bare_star = true;
|
||||
comp->have_bare_star = MP_TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -819,18 +819,18 @@ qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint
|
||||
}
|
||||
|
||||
// save variables (probably don't need to do this, since we can't have nested definitions..?)
|
||||
bool old_have_bare_star = comp->have_bare_star;
|
||||
MP_BOOL old_have_bare_star = comp->have_bare_star;
|
||||
int old_param_pass = comp->param_pass;
|
||||
int old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
|
||||
int old_param_pass_num_default_params = comp->param_pass_num_default_params;
|
||||
|
||||
// compile default parameters
|
||||
comp->have_bare_star = false;
|
||||
comp->have_bare_star = MP_FALSE;
|
||||
comp->param_pass = 1; // pass 1 does any default parameters after bare star
|
||||
comp->param_pass_num_dict_params = 0;
|
||||
comp->param_pass_num_default_params = 0;
|
||||
apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
|
||||
comp->have_bare_star = false;
|
||||
comp->have_bare_star = MP_FALSE;
|
||||
comp->param_pass = 2; // pass 2 does any default parameters before bare star
|
||||
comp->param_pass_num_dict_params = 0;
|
||||
comp->param_pass_num_default_params = 0;
|
||||
@ -876,12 +876,12 @@ qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint
|
||||
// nodes[1] has parent classes, if any
|
||||
if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
|
||||
// no parent classes
|
||||
EMIT(call_function, 2, 0, false, false);
|
||||
EMIT(call_function, 2, 0, MP_FALSE, MP_FALSE);
|
||||
} else {
|
||||
// have a parent class or classes
|
||||
// TODO what if we have, eg, *a or **a in the parent list?
|
||||
compile_node(comp, pns->nodes[1]);
|
||||
EMIT(call_function, 2 + list_len(pns->nodes[1], PN_arglist), 0, false, false);
|
||||
EMIT(call_function, 2 + list_len(pns->nodes[1], PN_arglist), 0, MP_FALSE, MP_FALSE);
|
||||
}
|
||||
|
||||
// return its name (the 'C' in class C(...):")
|
||||
@ -889,14 +889,14 @@ qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint
|
||||
}
|
||||
|
||||
// returns true if it was a built-in decorator (even if the built-in had an error)
|
||||
static bool compile_built_in_decorator(compiler_t *comp, int name_len, mp_parse_node_t *name_nodes, uint *emit_options) {
|
||||
static MP_BOOL compile_built_in_decorator(compiler_t *comp, int name_len, mp_parse_node_t *name_nodes, uint *emit_options) {
|
||||
if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
|
||||
if (name_len != 2) {
|
||||
printf("SyntaxError: invalid micropython decorator\n");
|
||||
return true;
|
||||
return MP_TRUE;
|
||||
}
|
||||
|
||||
qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
|
||||
@ -916,7 +916,7 @@ static bool compile_built_in_decorator(compiler_t *comp, int name_len, mp_parse_
|
||||
printf("SyntaxError: invalid micropython decorator '%s'\n", qstr_str(attr));
|
||||
}
|
||||
|
||||
return true;
|
||||
return MP_TRUE;
|
||||
}
|
||||
|
||||
void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
@ -974,7 +974,7 @@ void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
|
||||
// call each decorator
|
||||
for (int i = 0; i < n - num_built_in_decorators; i++) {
|
||||
EMIT(call_function, 1, 0, false, false);
|
||||
EMIT(call_function, 1, 0, MP_FALSE, MP_FALSE);
|
||||
}
|
||||
|
||||
// store func/class object into name
|
||||
@ -1094,7 +1094,7 @@ void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
if (comp->scope_cur->kind != SCOPE_FUNCTION) {
|
||||
printf("SyntaxError: 'return' outside function\n");
|
||||
comp->had_error = true;
|
||||
comp->had_error = MP_TRUE;
|
||||
return;
|
||||
}
|
||||
if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
|
||||
@ -1106,7 +1106,7 @@ void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns_test_if_expr->nodes[1];
|
||||
|
||||
int l_fail = comp_next_label(comp);
|
||||
c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
|
||||
c_if_cond(comp, pns_test_if_else->nodes[0], MP_FALSE, l_fail); // condition
|
||||
compile_node(comp, pns_test_if_expr->nodes[0]); // success value
|
||||
EMIT(return_value);
|
||||
EMIT(label_assign, l_fail);
|
||||
@ -1143,13 +1143,13 @@ void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
// eg a -> q1=q2=a
|
||||
// a.b.c -> q1=a, q2=a.b.c
|
||||
void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q1, qstr *q2) {
|
||||
bool is_as = false;
|
||||
MP_BOOL is_as = MP_FALSE;
|
||||
if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
|
||||
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
|
||||
// a name of the form x as y; unwrap it
|
||||
*q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
|
||||
pn = pns->nodes[0];
|
||||
is_as = true;
|
||||
is_as = MP_TRUE;
|
||||
}
|
||||
if (MP_PARSE_NODE_IS_ID(pn)) {
|
||||
// just a simple name
|
||||
@ -1220,7 +1220,7 @@ void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
#if MICROPY_EMIT_CPYTHON
|
||||
EMIT(load_const_verbatim_str, "('*',)");
|
||||
#else
|
||||
EMIT(load_const_str, qstr_from_str_static("*"), false);
|
||||
EMIT(load_const_str, qstr_from_str_static("*"), MP_FALSE);
|
||||
EMIT(build_tuple, 1);
|
||||
#endif
|
||||
|
||||
@ -1262,7 +1262,7 @@ void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
|
||||
mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
|
||||
qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
|
||||
EMIT(load_const_str, id2, false);
|
||||
EMIT(load_const_str, id2, MP_FALSE);
|
||||
}
|
||||
EMIT(build_tuple, n);
|
||||
#endif
|
||||
@ -1315,12 +1315,12 @@ void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
|
||||
void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
int l_end = comp_next_label(comp);
|
||||
c_if_cond(comp, pns->nodes[0], true, l_end);
|
||||
c_if_cond(comp, pns->nodes[0], MP_TRUE, l_end);
|
||||
EMIT(load_id, MP_QSTR_AssertionError);
|
||||
if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
|
||||
// assertion message
|
||||
compile_node(comp, pns->nodes[1]);
|
||||
EMIT(call_function, 1, 0, false, false);
|
||||
EMIT(call_function, 1, 0, MP_FALSE, MP_FALSE);
|
||||
}
|
||||
EMIT(raise_varargs, 1);
|
||||
EMIT(label_assign, l_end);
|
||||
@ -1332,7 +1332,7 @@ void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
int l_end = comp_next_label(comp);
|
||||
|
||||
int l_fail = comp_next_label(comp);
|
||||
c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
|
||||
c_if_cond(comp, pns->nodes[0], MP_FALSE, l_fail); // if condition
|
||||
|
||||
compile_node(comp, pns->nodes[1]); // if block
|
||||
//if (!(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))) { // optimisation; doesn't align with CPython
|
||||
@ -1355,7 +1355,7 @@ void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
|
||||
l_fail = comp_next_label(comp);
|
||||
c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
|
||||
c_if_cond(comp, pns_elif2->nodes[0], MP_FALSE, l_fail); // elif condition
|
||||
|
||||
compile_node(comp, pns_elif2->nodes[1]); // elif block
|
||||
if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
|
||||
@ -1368,7 +1368,7 @@ void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
// a single elif block
|
||||
|
||||
l_fail = comp_next_label(comp);
|
||||
c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
|
||||
c_if_cond(comp, pns_elif->nodes[0], MP_FALSE, l_fail); // elif condition
|
||||
|
||||
compile_node(comp, pns_elif->nodes[1]); // elif block
|
||||
if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
|
||||
@ -1399,7 +1399,7 @@ void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
int done_label = comp_next_label(comp);
|
||||
EMIT(setup_loop, break_label);
|
||||
EMIT(label_assign, continue_label);
|
||||
c_if_cond(comp, pns->nodes[0], false, done_label); // condition
|
||||
c_if_cond(comp, pns->nodes[0], MP_FALSE, done_label); // condition
|
||||
compile_node(comp, pns->nodes[1]); // body
|
||||
if (!EMIT(last_emit_was_return_value)) {
|
||||
EMIT(jump, continue_label);
|
||||
@ -1416,7 +1416,7 @@ void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
EMIT(label_assign, top_label);
|
||||
compile_node(comp, pns->nodes[1]); // body
|
||||
EMIT(label_assign, continue_label);
|
||||
c_if_cond(comp, pns->nodes[0], true, top_label); // condition
|
||||
c_if_cond(comp, pns->nodes[0], MP_TRUE, top_label); // condition
|
||||
#endif
|
||||
|
||||
// break/continue apply to outer loop (if any) in the else block
|
||||
@ -1732,7 +1732,7 @@ void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
// for REPL, evaluate then print the expression
|
||||
EMIT(load_id, MP_QSTR___repl_print__);
|
||||
compile_node(comp, pns->nodes[0]);
|
||||
EMIT(call_function, 1, 0, false, false);
|
||||
EMIT(call_function, 1, 0, MP_FALSE, MP_FALSE);
|
||||
EMIT(pop_top);
|
||||
|
||||
} else {
|
||||
@ -1837,7 +1837,7 @@ void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
int stack_size = EMIT(get_stack_size);
|
||||
int l_fail = comp_next_label(comp);
|
||||
int l_end = comp_next_label(comp);
|
||||
c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
|
||||
c_if_cond(comp, pns_test_if_else->nodes[0], MP_FALSE, l_fail); // condition
|
||||
compile_node(comp, pns->nodes[0]); // success value
|
||||
EMIT(jump, l_end);
|
||||
EMIT(label_assign, l_fail);
|
||||
@ -1898,7 +1898,7 @@ void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
int stack_size = EMIT(get_stack_size);
|
||||
int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
|
||||
compile_node(comp, pns->nodes[0]);
|
||||
bool multi = (num_nodes > 3);
|
||||
MP_BOOL multi = (num_nodes > 3);
|
||||
int l_fail = 0;
|
||||
if (multi) {
|
||||
l_fail = comp_next_label(comp);
|
||||
@ -2042,15 +2042,15 @@ void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
}
|
||||
}
|
||||
|
||||
void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_struct_t *pns, bool is_method_call) {
|
||||
void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_struct_t *pns, MP_BOOL is_method_call) {
|
||||
// function to call is on top of stack
|
||||
|
||||
int old_n_arg_keyword = comp->n_arg_keyword;
|
||||
bool old_have_star_arg = comp->have_star_arg;
|
||||
bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
|
||||
MP_BOOL old_have_star_arg = comp->have_star_arg;
|
||||
MP_BOOL old_have_dbl_star_arg = comp->have_dbl_star_arg;
|
||||
comp->n_arg_keyword = 0;
|
||||
comp->have_star_arg = false;
|
||||
comp->have_dbl_star_arg = false;
|
||||
comp->have_star_arg = MP_FALSE;
|
||||
comp->have_dbl_star_arg = MP_FALSE;
|
||||
|
||||
compile_node(comp, pns->nodes[0]); // arguments to function call; can be null
|
||||
|
||||
@ -2082,7 +2082,7 @@ void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
|
||||
mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
|
||||
EMIT(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
|
||||
compile_trailer_paren_helper(comp, pns_paren, true);
|
||||
compile_trailer_paren_helper(comp, pns_paren, MP_TRUE);
|
||||
i += 1;
|
||||
} else {
|
||||
compile_node(comp, pns->nodes[i]);
|
||||
@ -2153,7 +2153,7 @@ void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_
|
||||
|
||||
compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
|
||||
EMIT(get_iter);
|
||||
EMIT(call_function, 1, 0, false, false);
|
||||
EMIT(call_function, 1, 0, MP_FALSE, MP_FALSE);
|
||||
}
|
||||
|
||||
void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
@ -2252,23 +2252,23 @@ void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
|
||||
|
||||
// first element sets whether it's a dict or set
|
||||
bool is_dict;
|
||||
MP_BOOL is_dict;
|
||||
if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
|
||||
// a dictionary
|
||||
EMIT(build_map, 1 + n);
|
||||
compile_node(comp, pns->nodes[0]);
|
||||
EMIT(store_map);
|
||||
is_dict = true;
|
||||
is_dict = MP_TRUE;
|
||||
} else {
|
||||
// a set
|
||||
compile_node(comp, pns->nodes[0]); // 1st value of set
|
||||
is_dict = false;
|
||||
is_dict = MP_FALSE;
|
||||
}
|
||||
|
||||
// process rest of elements
|
||||
for (int i = 0; i < n; i++) {
|
||||
mp_parse_node_t pn = nodes[i];
|
||||
bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
|
||||
MP_BOOL is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
|
||||
compile_node(comp, pn);
|
||||
if (is_dict) {
|
||||
if (!is_key_value) {
|
||||
@ -2314,7 +2314,7 @@ void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
}
|
||||
|
||||
void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
compile_trailer_paren_helper(comp, pns, false);
|
||||
compile_trailer_paren_helper(comp, pns, MP_FALSE);
|
||||
}
|
||||
|
||||
void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
@ -2401,7 +2401,7 @@ void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
printf("SyntaxError?: can't have multiple *x\n");
|
||||
return;
|
||||
}
|
||||
comp->have_star_arg = true;
|
||||
comp->have_star_arg = MP_TRUE;
|
||||
compile_node(comp, pns->nodes[0]);
|
||||
}
|
||||
|
||||
@ -2410,7 +2410,7 @@ void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
|
||||
printf("SyntaxError?: can't have multiple **x\n");
|
||||
return;
|
||||
}
|
||||
comp->have_dbl_star_arg = true;
|
||||
comp->have_dbl_star_arg = MP_TRUE;
|
||||
compile_node(comp, pns->nodes[0]);
|
||||
}
|
||||
|
||||
@ -2475,8 +2475,8 @@ void compile_node(compiler_t *comp, mp_parse_node_t pn) {
|
||||
case MP_PARSE_NODE_SMALL_INT: EMIT(load_const_small_int, arg); break;
|
||||
case MP_PARSE_NODE_INTEGER: EMIT(load_const_int, arg); break;
|
||||
case MP_PARSE_NODE_DECIMAL: EMIT(load_const_dec, arg); break;
|
||||
case MP_PARSE_NODE_STRING: EMIT(load_const_str, arg, false); break;
|
||||
case MP_PARSE_NODE_BYTES: EMIT(load_const_str, arg, true); break;
|
||||
case MP_PARSE_NODE_STRING: EMIT(load_const_str, arg, MP_FALSE); break;
|
||||
case MP_PARSE_NODE_BYTES: EMIT(load_const_str, arg, MP_TRUE); break;
|
||||
case MP_PARSE_NODE_TOKEN:
|
||||
if (arg == MP_TOKEN_NEWLINE) {
|
||||
// this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
|
||||
@ -2501,7 +2501,7 @@ void compile_node(compiler_t *comp, mp_parse_node_t pn) {
|
||||
}
|
||||
}
|
||||
|
||||
void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_name, pn_kind_t pn_star, pn_kind_t pn_dbl_star, bool allow_annotations) {
|
||||
void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_name, pn_kind_t pn_star, pn_kind_t pn_dbl_star, MP_BOOL allow_annotations) {
|
||||
// TODO verify that *k and **k are last etc
|
||||
qstr param_name = 0;
|
||||
mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
|
||||
@ -2544,7 +2544,7 @@ void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_ki
|
||||
if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
|
||||
// bare star
|
||||
// TODO see http://www.python.org/dev/peps/pep-3102/
|
||||
comp->have_bare_star = true;
|
||||
comp->have_bare_star = MP_TRUE;
|
||||
//assert(comp->scope_cur->num_dict_params == 0);
|
||||
} else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
|
||||
// named star
|
||||
@ -2577,23 +2577,23 @@ void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_ki
|
||||
if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
|
||||
// TODO this parameter has an annotation
|
||||
}
|
||||
bool added;
|
||||
MP_BOOL added;
|
||||
id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
|
||||
if (!added) {
|
||||
printf("SyntaxError?: same name used for parameter; %s\n", qstr_str(param_name));
|
||||
return;
|
||||
}
|
||||
id_info->param = true;
|
||||
id_info->param = MP_TRUE;
|
||||
id_info->kind = ID_INFO_KIND_LOCAL;
|
||||
}
|
||||
}
|
||||
|
||||
void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
|
||||
compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
|
||||
compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, MP_TRUE);
|
||||
}
|
||||
|
||||
void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
|
||||
compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
|
||||
compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, MP_FALSE);
|
||||
}
|
||||
|
||||
void compile_scope_comp_iter(compiler_t *comp, mp_parse_node_t pn_iter, mp_parse_node_t pn_inner_expr, int l_top, int for_depth) {
|
||||
@ -2614,7 +2614,7 @@ void compile_scope_comp_iter(compiler_t *comp, mp_parse_node_t pn_iter, mp_parse
|
||||
} else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
|
||||
// if condition
|
||||
mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
|
||||
c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
|
||||
c_if_cond(comp, pns_comp_if->nodes[0], MP_FALSE, l_top);
|
||||
pn_iter = pns_comp_if->nodes[1];
|
||||
goto tail_recursion;
|
||||
} else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
|
||||
@ -2708,7 +2708,7 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
|
||||
// work out number of parameters, keywords and default parameters, and add them to the id_info array
|
||||
// must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
|
||||
if (comp->pass == PASS_1) {
|
||||
comp->have_bare_star = false;
|
||||
comp->have_bare_star = MP_FALSE;
|
||||
apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
|
||||
}
|
||||
|
||||
@ -2728,7 +2728,7 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
|
||||
// work out number of parameters, keywords and default parameters, and add them to the id_info array
|
||||
// must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
|
||||
if (comp->pass == PASS_1) {
|
||||
comp->have_bare_star = false;
|
||||
comp->have_bare_star = MP_FALSE;
|
||||
apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
|
||||
}
|
||||
|
||||
@ -2745,7 +2745,7 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
|
||||
|
||||
qstr qstr_arg = qstr_from_str_static(".0");
|
||||
if (comp->pass == PASS_1) {
|
||||
bool added;
|
||||
MP_BOOL added;
|
||||
id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
|
||||
assert(added);
|
||||
id_info->kind = ID_INFO_KIND_LOCAL;
|
||||
@ -2782,14 +2782,14 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
|
||||
assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
|
||||
|
||||
if (comp->pass == PASS_1) {
|
||||
bool added;
|
||||
MP_BOOL added;
|
||||
id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
|
||||
assert(added);
|
||||
id_info->kind = ID_INFO_KIND_LOCAL;
|
||||
id_info = scope_find_or_add_id(scope, MP_QSTR___locals__, &added);
|
||||
assert(added);
|
||||
id_info->kind = ID_INFO_KIND_LOCAL;
|
||||
id_info->param = true;
|
||||
id_info->param = MP_TRUE;
|
||||
scope->num_params = 1; // __locals__ is the parameter
|
||||
}
|
||||
|
||||
@ -3005,11 +3005,11 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
|
||||
}
|
||||
}
|
||||
|
||||
mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl) {
|
||||
mp_obj_t mp_compile(mp_parse_node_t pn, MP_BOOL is_repl) {
|
||||
compiler_t *comp = m_new(compiler_t, 1);
|
||||
|
||||
comp->is_repl = is_repl;
|
||||
comp->had_error = false;
|
||||
comp->had_error = MP_FALSE;
|
||||
|
||||
comp->break_label = 0;
|
||||
comp->continue_label = 0;
|
||||
@ -3030,7 +3030,7 @@ mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl) {
|
||||
comp->emit_inline_asm_method_table = NULL;
|
||||
uint max_num_labels = 0;
|
||||
for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
|
||||
if (false) {
|
||||
if (MP_FALSE) {
|
||||
#if MICROPY_EMIT_INLINE_THUMB
|
||||
} else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
|
||||
compile_scope_inline_asm(comp, s, PASS_1);
|
||||
@ -3064,7 +3064,7 @@ mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl) {
|
||||
#endif
|
||||
#endif // !MICROPY_EMIT_CPYTHON
|
||||
for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
|
||||
if (false) {
|
||||
if (MP_FALSE) {
|
||||
// dummy
|
||||
|
||||
#if MICROPY_EMIT_INLINE_THUMB
|
||||
@ -3126,7 +3126,7 @@ mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl) {
|
||||
}
|
||||
}
|
||||
|
||||
bool had_error = comp->had_error;
|
||||
MP_BOOL had_error = comp->had_error;
|
||||
m_del_obj(compiler_t, comp);
|
||||
|
||||
if (had_error) {
|
||||
|
@ -1 +1 @@
|
||||
mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl);
|
||||
mp_obj_t mp_compile(mp_parse_node_t pn, MP_BOOL is_repl);
|
||||
|
10
py/emit.h
10
py/emit.h
@ -17,10 +17,10 @@ typedef enum {
|
||||
typedef struct _emit_t emit_t;
|
||||
|
||||
typedef struct _emit_method_table_t {
|
||||
void (*set_native_types)(emit_t *emit, bool do_native_types);
|
||||
void (*set_native_types)(emit_t *emit, MP_BOOL do_native_types);
|
||||
void (*start_pass)(emit_t *emit, pass_kind_t pass, scope_t *scope);
|
||||
void (*end_pass)(emit_t *emit);
|
||||
bool (*last_emit_was_return_value)(emit_t *emit);
|
||||
MP_BOOL (*last_emit_was_return_value)(emit_t *emit);
|
||||
int (*get_stack_size)(emit_t *emit);
|
||||
void (*set_stack_size)(emit_t *emit, int size);
|
||||
|
||||
@ -37,7 +37,7 @@ typedef struct _emit_method_table_t {
|
||||
void (*load_const_int)(emit_t *emit, qstr qstr);
|
||||
void (*load_const_dec)(emit_t *emit, qstr qstr);
|
||||
void (*load_const_id)(emit_t *emit, qstr qstr);
|
||||
void (*load_const_str)(emit_t *emit, qstr qstr, bool bytes);
|
||||
void (*load_const_str)(emit_t *emit, qstr qstr, MP_BOOL bytes);
|
||||
void (*load_const_verbatim_str)(emit_t *emit, const char *str); // only needed for emitcpy
|
||||
void (*load_fast)(emit_t *emit, qstr qstr, int local_num);
|
||||
void (*load_deref)(emit_t *emit, qstr qstr, int local_num);
|
||||
@ -99,8 +99,8 @@ typedef struct _emit_method_table_t {
|
||||
void (*unpack_ex)(emit_t *emit, int n_left, int n_right);
|
||||
void (*make_function)(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params);
|
||||
void (*make_closure)(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params);
|
||||
void (*call_function)(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg);
|
||||
void (*call_method)(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg);
|
||||
void (*call_function)(emit_t *emit, int n_positional, int n_keyword, MP_BOOL have_star_arg, MP_BOOL have_dbl_star_arg);
|
||||
void (*call_method)(emit_t *emit, int n_positional, int n_keyword, MP_BOOL have_star_arg, MP_BOOL have_dbl_star_arg);
|
||||
void (*return_value)(emit_t *emit);
|
||||
void (*raise_varargs)(emit_t *emit, int n_args);
|
||||
void (*yield_value)(emit_t *emit);
|
||||
|
18
py/emitbc.c
18
py/emitbc.c
@ -17,7 +17,7 @@
|
||||
struct _emit_t {
|
||||
pass_kind_t pass;
|
||||
int stack_size;
|
||||
bool last_emit_was_return_value;
|
||||
MP_BOOL last_emit_was_return_value;
|
||||
|
||||
scope_t *scope;
|
||||
|
||||
@ -135,13 +135,13 @@ static void emit_write_byte_1_signed_label(emit_t* emit, byte b1, int label) {
|
||||
c[2] = code_offset >> 8;
|
||||
}
|
||||
|
||||
static void emit_bc_set_native_types(emit_t *emit, bool do_native_types) {
|
||||
static void emit_bc_set_native_types(emit_t *emit, MP_BOOL do_native_types) {
|
||||
}
|
||||
|
||||
static void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
|
||||
emit->pass = pass;
|
||||
emit->stack_size = 0;
|
||||
emit->last_emit_was_return_value = false;
|
||||
emit->last_emit_was_return_value = MP_FALSE;
|
||||
emit->scope = scope;
|
||||
if (pass == PASS_2) {
|
||||
memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(uint));
|
||||
@ -182,7 +182,7 @@ static void emit_bc_end_pass(emit_t *emit) {
|
||||
}
|
||||
}
|
||||
|
||||
bool emit_bc_last_emit_was_return_value(emit_t *emit) {
|
||||
MP_BOOL emit_bc_last_emit_was_return_value(emit_t *emit) {
|
||||
return emit->last_emit_was_return_value;
|
||||
}
|
||||
|
||||
@ -211,7 +211,7 @@ static void emit_pre(emit_t *emit, int stack_size_delta) {
|
||||
if (emit->stack_size > emit->scope->stack_size) {
|
||||
emit->scope->stack_size = emit->stack_size;
|
||||
}
|
||||
emit->last_emit_was_return_value = false;
|
||||
emit->last_emit_was_return_value = MP_FALSE;
|
||||
}
|
||||
|
||||
static void emit_bc_label_assign(emit_t *emit, int l) {
|
||||
@ -274,7 +274,7 @@ static void emit_bc_load_const_id(emit_t *emit, qstr qstr) {
|
||||
emit_write_byte_1_qstr(emit, MP_BC_LOAD_CONST_ID, qstr);
|
||||
}
|
||||
|
||||
static void emit_bc_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
|
||||
static void emit_bc_load_const_str(emit_t *emit, qstr qstr, MP_BOOL bytes) {
|
||||
emit_pre(emit, 1);
|
||||
if (bytes) {
|
||||
emit_write_byte_1_qstr(emit, MP_BC_LOAD_CONST_BYTES, qstr);
|
||||
@ -613,7 +613,7 @@ static void emit_bc_make_closure(emit_t *emit, scope_t *scope, int n_dict_params
|
||||
emit_write_byte_1_uint(emit, MP_BC_MAKE_CLOSURE, scope->unique_code_id);
|
||||
}
|
||||
|
||||
static void emit_bc_call_function(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
|
||||
static void emit_bc_call_function(emit_t *emit, int n_positional, int n_keyword, MP_BOOL have_star_arg, MP_BOOL have_dbl_star_arg) {
|
||||
int s = 0;
|
||||
if (have_star_arg) {
|
||||
s += 1;
|
||||
@ -639,7 +639,7 @@ static void emit_bc_call_function(emit_t *emit, int n_positional, int n_keyword,
|
||||
emit_write_byte_1_uint(emit, op, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints
|
||||
}
|
||||
|
||||
static void emit_bc_call_method(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
|
||||
static void emit_bc_call_method(emit_t *emit, int n_positional, int n_keyword, MP_BOOL have_star_arg, MP_BOOL have_dbl_star_arg) {
|
||||
int s = 0;
|
||||
if (have_star_arg) {
|
||||
s += 1;
|
||||
@ -667,7 +667,7 @@ static void emit_bc_call_method(emit_t *emit, int n_positional, int n_keyword, b
|
||||
|
||||
static void emit_bc_return_value(emit_t *emit) {
|
||||
emit_pre(emit, -1);
|
||||
emit->last_emit_was_return_value = true;
|
||||
emit->last_emit_was_return_value = MP_TRUE;
|
||||
emit_write_byte_1(emit, MP_BC_RETURN_VALUE);
|
||||
}
|
||||
|
||||
|
32
py/emitcpy.c
32
py/emitcpy.c
@ -20,7 +20,7 @@ struct _emit_t {
|
||||
int pass;
|
||||
int byte_code_offset;
|
||||
int stack_size;
|
||||
bool last_emit_was_return_value;
|
||||
MP_BOOL last_emit_was_return_value;
|
||||
|
||||
scope_t *scope;
|
||||
|
||||
@ -35,14 +35,14 @@ emit_t *emit_cpython_new(uint max_num_labels) {
|
||||
return emit;
|
||||
}
|
||||
|
||||
static void emit_cpy_set_native_types(emit_t *emit, bool do_native_types) {
|
||||
static void emit_cpy_set_native_types(emit_t *emit, MP_BOOL do_native_types) {
|
||||
}
|
||||
|
||||
static void emit_cpy_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
|
||||
emit->pass = pass;
|
||||
emit->byte_code_offset = 0;
|
||||
emit->stack_size = 0;
|
||||
emit->last_emit_was_return_value = false;
|
||||
emit->last_emit_was_return_value = MP_FALSE;
|
||||
emit->scope = scope;
|
||||
if (pass == PASS_2) {
|
||||
memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(int));
|
||||
@ -56,7 +56,7 @@ static void emit_cpy_end_pass(emit_t *emit) {
|
||||
}
|
||||
}
|
||||
|
||||
static bool emit_cpy_last_emit_was_return_value(emit_t *emit) {
|
||||
static MP_BOOL emit_cpy_last_emit_was_return_value(emit_t *emit) {
|
||||
return emit->last_emit_was_return_value;
|
||||
}
|
||||
|
||||
@ -85,7 +85,7 @@ static void emit_pre(emit_t *emit, int stack_size_delta, int byte_code_size) {
|
||||
if (emit->stack_size > emit->scope->stack_size) {
|
||||
emit->scope->stack_size = emit->stack_size;
|
||||
}
|
||||
emit->last_emit_was_return_value = false;
|
||||
emit->last_emit_was_return_value = MP_FALSE;
|
||||
if (emit->pass == PASS_3 && byte_code_size > 0) {
|
||||
if (emit->byte_code_offset >= 1000) {
|
||||
printf("%d ", emit->byte_code_offset);
|
||||
@ -173,26 +173,26 @@ static void emit_cpy_load_const_id(emit_t *emit, qstr qstr) {
|
||||
}
|
||||
}
|
||||
|
||||
static void print_quoted_str(qstr qstr, bool bytes) {
|
||||
static void print_quoted_str(qstr qstr, MP_BOOL bytes) {
|
||||
const char *str = qstr_str(qstr);
|
||||
int len = strlen(str);
|
||||
bool has_single_quote = false;
|
||||
bool has_double_quote = false;
|
||||
MP_BOOL has_single_quote = MP_FALSE;
|
||||
MP_BOOL has_double_quote = MP_FALSE;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (str[i] == '\'') {
|
||||
has_single_quote = true;
|
||||
has_single_quote = MP_TRUE;
|
||||
} else if (str[i] == '"') {
|
||||
has_double_quote = true;
|
||||
has_double_quote = MP_TRUE;
|
||||
}
|
||||
}
|
||||
if (bytes) {
|
||||
printf("b");
|
||||
}
|
||||
bool quote_single = false;
|
||||
MP_BOOL quote_single = MP_FALSE;
|
||||
if (has_single_quote && !has_double_quote) {
|
||||
printf("\"");
|
||||
} else {
|
||||
quote_single = true;
|
||||
quote_single = MP_TRUE;
|
||||
printf("'");
|
||||
}
|
||||
for (int i = 0; i < len; i++) {
|
||||
@ -213,7 +213,7 @@ static void print_quoted_str(qstr qstr, bool bytes) {
|
||||
}
|
||||
}
|
||||
|
||||
static void emit_cpy_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
|
||||
static void emit_cpy_load_const_str(emit_t *emit, qstr qstr, MP_BOOL bytes) {
|
||||
emit_pre(emit, 1, 3);
|
||||
if (emit->pass == PASS_3) {
|
||||
printf("LOAD_CONST ");
|
||||
@ -681,7 +681,7 @@ static void emit_cpy_unpack_ex(emit_t *emit, int n_left, int n_right) {
|
||||
}
|
||||
}
|
||||
|
||||
static void emit_cpy_call_function(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
|
||||
static void emit_cpy_call_function(emit_t *emit, int n_positional, int n_keyword, MP_BOOL have_star_arg, MP_BOOL have_dbl_star_arg) {
|
||||
int s = 0;
|
||||
if (have_star_arg) {
|
||||
s += 1;
|
||||
@ -708,13 +708,13 @@ static void emit_cpy_call_function(emit_t *emit, int n_positional, int n_keyword
|
||||
}
|
||||
}
|
||||
|
||||
static void emit_cpy_call_method(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
|
||||
static void emit_cpy_call_method(emit_t *emit, int n_positional, int n_keyword, MP_BOOL have_star_arg, MP_BOOL have_dbl_star_arg) {
|
||||
emit_cpy_call_function(emit, n_positional, n_keyword, have_star_arg, have_dbl_star_arg);
|
||||
}
|
||||
|
||||
static void emit_cpy_return_value(emit_t *emit) {
|
||||
emit_pre(emit, -1, 1);
|
||||
emit->last_emit_was_return_value = true;
|
||||
emit->last_emit_was_return_value = MP_TRUE;
|
||||
if (emit->pass == PASS_3) {
|
||||
printf("RETURN_VALUE\n");
|
||||
}
|
||||
|
@ -75,12 +75,12 @@ static void emit_inline_thumb_label(emit_inline_asm_t *emit, int label_num, qstr
|
||||
asm_thumb_label_assign(emit->as, label_num);
|
||||
}
|
||||
|
||||
static bool check_n_arg(qstr op, int n_args, int wanted_n_args) {
|
||||
static MP_BOOL check_n_arg(qstr op, int n_args, int wanted_n_args) {
|
||||
if (wanted_n_args == n_args) {
|
||||
return true;
|
||||
return MP_TRUE;
|
||||
} else {
|
||||
printf("SyntaxError: '%s' expects %d arguments'\n", qstr_str(op), wanted_n_args);
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,7 +52,7 @@
|
||||
#define REG_TEMP2 (REG_RSI)
|
||||
#define ASM_MOV_REG_TO_LOCAL(reg, local_num) asm_x64_mov_r64_to_local(emit->as, (reg), (local_num))
|
||||
#define ASM_MOV_IMM_TO_REG(imm, reg) asm_x64_mov_i64_to_r64_optimised(emit->as, (imm), (reg))
|
||||
#define ASM_MOV_IMM_TO_LOCAL_USING(imm, local_num, reg_temp) do { asm_x64_mov_i64_to_r64_optimised(emit->as, (imm), (reg_temp)); asm_x64_mov_r64_to_local(emit->as, (reg_temp), (local_num)); } while (false)
|
||||
#define ASM_MOV_IMM_TO_LOCAL_USING(imm, local_num, reg_temp) do { asm_x64_mov_i64_to_r64_optimised(emit->as, (imm), (reg_temp)); asm_x64_mov_r64_to_local(emit->as, (reg_temp), (local_num)); } while (MP_FALSE)
|
||||
#define ASM_MOV_LOCAL_TO_REG(local_num, reg) asm_x64_mov_local_to_r64(emit->as, (local_num), (reg))
|
||||
#define ASM_MOV_REG_TO_REG(reg_src, reg_dest) asm_x64_mov_r64_to_r64(emit->as, (reg_src), (reg_dest))
|
||||
#define ASM_MOV_LOCAL_ADDR_TO_REG(local_num, reg) asm_x64_mov_local_addr_to_r64(emit->as, (local_num), (reg))
|
||||
@ -75,7 +75,7 @@
|
||||
#define REG_TEMP2 (REG_R2)
|
||||
#define ASM_MOV_REG_TO_LOCAL(reg, local_num) asm_thumb_mov_local_reg(emit->as, (local_num), (reg))
|
||||
#define ASM_MOV_IMM_TO_REG(imm, reg) asm_thumb_mov_reg_i32_optimised(emit->as, (reg), (imm))
|
||||
#define ASM_MOV_IMM_TO_LOCAL_USING(imm, local_num, reg_temp) do { asm_thumb_mov_reg_i32_optimised(emit->as, (reg_temp), (imm)); asm_thumb_mov_local_reg(emit->as, (local_num), (reg_temp)); } while (false)
|
||||
#define ASM_MOV_IMM_TO_LOCAL_USING(imm, local_num, reg_temp) do { asm_thumb_mov_reg_i32_optimised(emit->as, (reg_temp), (imm)); asm_thumb_mov_local_reg(emit->as, (local_num), (reg_temp)); } while (MP_FALSE)
|
||||
#define ASM_MOV_LOCAL_TO_REG(local_num, reg) asm_thumb_mov_reg_local(emit->as, (reg), (local_num))
|
||||
#define ASM_MOV_REG_TO_REG(reg_src, reg_dest) asm_thumb_mov_reg_reg(emit->as, (reg_dest), (reg_src))
|
||||
#define ASM_MOV_LOCAL_ADDR_TO_REG(local_num, reg) asm_thumb_mov_reg_local_addr(emit->as, (reg), (local_num))
|
||||
@ -110,7 +110,7 @@ typedef struct _stack_info_t {
|
||||
struct _emit_t {
|
||||
int pass;
|
||||
|
||||
bool do_viper_types;
|
||||
MP_BOOL do_viper_types;
|
||||
|
||||
int local_vtype_alloc;
|
||||
vtype_kind_t *local_vtype;
|
||||
@ -121,7 +121,7 @@ struct _emit_t {
|
||||
int stack_start;
|
||||
int stack_size;
|
||||
|
||||
bool last_emit_was_return_value;
|
||||
MP_BOOL last_emit_was_return_value;
|
||||
|
||||
scope_t *scope;
|
||||
|
||||
@ -134,7 +134,7 @@ struct _emit_t {
|
||||
|
||||
emit_t *EXPORT_FUN(new)(uint max_num_labels) {
|
||||
emit_t *emit = m_new(emit_t, 1);
|
||||
emit->do_viper_types = false;
|
||||
emit->do_viper_types = MP_FALSE;
|
||||
emit->local_vtype = NULL;
|
||||
emit->stack_info = NULL;
|
||||
#if N_X64
|
||||
@ -145,7 +145,7 @@ emit_t *EXPORT_FUN(new)(uint max_num_labels) {
|
||||
return emit;
|
||||
}
|
||||
|
||||
static void emit_native_set_viper_types(emit_t *emit, bool do_viper_types) {
|
||||
static void emit_native_set_viper_types(emit_t *emit, MP_BOOL do_viper_types) {
|
||||
emit->do_viper_types = do_viper_types;
|
||||
}
|
||||
|
||||
@ -153,7 +153,7 @@ static void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
|
||||
emit->pass = pass;
|
||||
emit->stack_start = 0;
|
||||
emit->stack_size = 0;
|
||||
emit->last_emit_was_return_value = false;
|
||||
emit->last_emit_was_return_value = MP_FALSE;
|
||||
emit->scope = scope;
|
||||
|
||||
if (emit->local_vtype == NULL) {
|
||||
@ -269,7 +269,7 @@ static void emit_native_end_pass(emit_t *emit) {
|
||||
}
|
||||
}
|
||||
|
||||
static bool emit_native_last_emit_was_return_value(emit_t *emit) {
|
||||
static MP_BOOL emit_native_last_emit_was_return_value(emit_t *emit) {
|
||||
return emit->last_emit_was_return_value;
|
||||
}
|
||||
|
||||
@ -292,13 +292,13 @@ static void adjust_stack(emit_t *emit, int stack_size_delta) {
|
||||
/*
|
||||
static void emit_pre_raw(emit_t *emit, int stack_size_delta) {
|
||||
adjust_stack(emit, stack_size_delta);
|
||||
emit->last_emit_was_return_value = false;
|
||||
emit->last_emit_was_return_value = MP_FALSE;
|
||||
}
|
||||
*/
|
||||
|
||||
// this must be called at start of emit functions
|
||||
static void emit_pre(emit_t *emit) {
|
||||
emit->last_emit_was_return_value = false;
|
||||
emit->last_emit_was_return_value = MP_FALSE;
|
||||
// settle the stack
|
||||
/*
|
||||
if (regs_needed != 0) {
|
||||
@ -391,7 +391,7 @@ static void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int re
|
||||
}
|
||||
|
||||
static void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
|
||||
emit->last_emit_was_return_value = false;
|
||||
emit->last_emit_was_return_value = MP_FALSE;
|
||||
emit_access_stack(emit, 1, vtype, reg_dest);
|
||||
adjust_stack(emit, -1);
|
||||
}
|
||||
@ -618,7 +618,7 @@ static void emit_native_load_const_id(emit_t *emit, qstr qstr) {
|
||||
}
|
||||
}
|
||||
|
||||
static void emit_native_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
|
||||
static void emit_native_load_const_str(emit_t *emit, qstr qstr, MP_BOOL bytes) {
|
||||
emit_pre(emit);
|
||||
if (emit->do_viper_types) {
|
||||
// not implemented properly
|
||||
@ -1134,7 +1134,7 @@ static void emit_native_make_closure(emit_t *emit, scope_t *scope, int n_dict_pa
|
||||
assert(0);
|
||||
}
|
||||
|
||||
static void emit_native_call_function(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
|
||||
static void emit_native_call_function(emit_t *emit, int n_positional, int n_keyword, MP_BOOL have_star_arg, MP_BOOL have_dbl_star_arg) {
|
||||
// call special viper runtime routine with type info for args, and wanted type info for return
|
||||
assert(n_keyword == 0 && !have_star_arg && !have_dbl_star_arg);
|
||||
/*
|
||||
@ -1170,7 +1170,7 @@ static void emit_native_call_function(emit_t *emit, int n_positional, int n_keyw
|
||||
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
|
||||
}
|
||||
|
||||
static void emit_native_call_method(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
|
||||
static void emit_native_call_method(emit_t *emit, int n_positional, int n_keyword, MP_BOOL have_star_arg, MP_BOOL have_dbl_star_arg) {
|
||||
assert(n_keyword == 0 && !have_star_arg && !have_dbl_star_arg);
|
||||
/*
|
||||
if (n_positional == 0) {
|
||||
@ -1205,7 +1205,7 @@ static void emit_native_return_value(emit_t *emit) {
|
||||
} else {
|
||||
assert(vtype == VTYPE_PYOBJ);
|
||||
}
|
||||
emit->last_emit_was_return_value = true;
|
||||
emit->last_emit_was_return_value = MP_TRUE;
|
||||
#if N_X64
|
||||
//asm_x64_call_ind(emit->as, 0, REG_RAX); to seg fault for debugging with gdb
|
||||
asm_x64_exit(emit->as);
|
||||
|
@ -42,7 +42,7 @@ static void emit_pass1_end_pass(emit_t *emit) {
|
||||
|
||||
static void emit_pass1_load_id(emit_t *emit, qstr qstr) {
|
||||
// name adding/lookup
|
||||
bool added;
|
||||
MP_BOOL added;
|
||||
id_info_t *id = scope_find_or_add_id(emit->scope, qstr, &added);
|
||||
if (added) {
|
||||
if (qstr == MP_QSTR_AssertionError) {
|
||||
@ -73,7 +73,7 @@ static void emit_pass1_load_id(emit_t *emit, qstr qstr) {
|
||||
|
||||
static id_info_t *get_id_for_modification(scope_t *scope, qstr qstr) {
|
||||
// name adding/lookup
|
||||
bool added;
|
||||
MP_BOOL added;
|
||||
id_info_t *id = scope_find_or_add_id(scope, qstr, &added);
|
||||
if (added) {
|
||||
if (scope->kind == SCOPE_MODULE || scope->kind == SCOPE_CLASS) {
|
||||
|
80
py/lexer.c
80
py/lexer.c
@ -35,7 +35,7 @@ struct _mp_lexer_t {
|
||||
mp_token_t tok_cur;
|
||||
};
|
||||
|
||||
bool str_strn_equal(const char *str, const char *strn, int len) {
|
||||
MP_BOOL str_strn_equal(const char *str, const char *strn, int len) {
|
||||
uint i = 0;
|
||||
|
||||
while (i < len && *str == *strn) {
|
||||
@ -70,74 +70,74 @@ void mp_token_show_error_prefix(const mp_token_t *tok) {
|
||||
printf("(%s:%d:%d) ", tok->src_name, tok->src_line, tok->src_column);
|
||||
}
|
||||
|
||||
bool mp_token_show_error(const mp_token_t *tok, const char *msg) {
|
||||
MP_BOOL mp_token_show_error(const mp_token_t *tok, const char *msg) {
|
||||
printf("(%s:%d:%d) %s\n", tok->src_name, tok->src_line, tok->src_column, msg);
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
|
||||
#define CUR_CHAR(lex) ((lex)->chr0)
|
||||
|
||||
static bool is_end(mp_lexer_t *lex) {
|
||||
static MP_BOOL is_end(mp_lexer_t *lex) {
|
||||
return lex->chr0 == MP_LEXER_CHAR_EOF;
|
||||
}
|
||||
|
||||
static bool is_physical_newline(mp_lexer_t *lex) {
|
||||
static MP_BOOL is_physical_newline(mp_lexer_t *lex) {
|
||||
return lex->chr0 == '\n' || lex->chr0 == '\r';
|
||||
}
|
||||
|
||||
static bool is_char(mp_lexer_t *lex, char c) {
|
||||
static MP_BOOL is_char(mp_lexer_t *lex, char c) {
|
||||
return lex->chr0 == c;
|
||||
}
|
||||
|
||||
static bool is_char_or(mp_lexer_t *lex, char c1, char c2) {
|
||||
static MP_BOOL is_char_or(mp_lexer_t *lex, char c1, char c2) {
|
||||
return lex->chr0 == c1 || lex->chr0 == c2;
|
||||
}
|
||||
|
||||
static bool is_char_or3(mp_lexer_t *lex, char c1, char c2, char c3) {
|
||||
static MP_BOOL is_char_or3(mp_lexer_t *lex, char c1, char c2, char c3) {
|
||||
return lex->chr0 == c1 || lex->chr0 == c2 || lex->chr0 == c3;
|
||||
}
|
||||
|
||||
/*
|
||||
static bool is_char_following(mp_lexer_t *lex, char c) {
|
||||
static MP_BOOL is_char_following(mp_lexer_t *lex, char c) {
|
||||
return lex->chr1 == c;
|
||||
}
|
||||
*/
|
||||
|
||||
static bool is_char_following_or(mp_lexer_t *lex, char c1, char c2) {
|
||||
static MP_BOOL is_char_following_or(mp_lexer_t *lex, char c1, char c2) {
|
||||
return lex->chr1 == c1 || lex->chr1 == c2;
|
||||
}
|
||||
|
||||
static bool is_char_following_following_or(mp_lexer_t *lex, char c1, char c2) {
|
||||
static MP_BOOL is_char_following_following_or(mp_lexer_t *lex, char c1, char c2) {
|
||||
return lex->chr2 == c1 || lex->chr2 == c2;
|
||||
}
|
||||
|
||||
static bool is_char_and(mp_lexer_t *lex, char c1, char c2) {
|
||||
static MP_BOOL is_char_and(mp_lexer_t *lex, char c1, char c2) {
|
||||
return lex->chr0 == c1 && lex->chr1 == c2;
|
||||
}
|
||||
|
||||
static bool is_whitespace(mp_lexer_t *lex) {
|
||||
static MP_BOOL is_whitespace(mp_lexer_t *lex) {
|
||||
return unichar_isspace(lex->chr0);
|
||||
}
|
||||
|
||||
static bool is_letter(mp_lexer_t *lex) {
|
||||
static MP_BOOL is_letter(mp_lexer_t *lex) {
|
||||
return unichar_isalpha(lex->chr0);
|
||||
}
|
||||
|
||||
static bool is_digit(mp_lexer_t *lex) {
|
||||
static MP_BOOL is_digit(mp_lexer_t *lex) {
|
||||
return unichar_isdigit(lex->chr0);
|
||||
}
|
||||
|
||||
static bool is_following_digit(mp_lexer_t *lex) {
|
||||
static MP_BOOL is_following_digit(mp_lexer_t *lex) {
|
||||
return unichar_isdigit(lex->chr1);
|
||||
}
|
||||
|
||||
// TODO UNICODE include unicode characters in definition of identifiers
|
||||
static bool is_head_of_identifier(mp_lexer_t *lex) {
|
||||
static MP_BOOL is_head_of_identifier(mp_lexer_t *lex) {
|
||||
return is_letter(lex) || lex->chr0 == '_';
|
||||
}
|
||||
|
||||
// TODO UNICODE include unicode characters in definition of identifiers
|
||||
static bool is_tail_of_identifier(mp_lexer_t *lex) {
|
||||
static MP_BOOL is_tail_of_identifier(mp_lexer_t *lex) {
|
||||
return is_head_of_identifier(lex) || is_digit(lex);
|
||||
}
|
||||
|
||||
@ -280,12 +280,12 @@ static const char *tok_kw[] = {
|
||||
NULL,
|
||||
};
|
||||
|
||||
static void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool first_token) {
|
||||
static void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, MP_BOOL first_token) {
|
||||
// skip white space and comments
|
||||
bool had_physical_newline = false;
|
||||
MP_BOOL had_physical_newline = MP_FALSE;
|
||||
while (!is_end(lex)) {
|
||||
if (is_physical_newline(lex)) {
|
||||
had_physical_newline = true;
|
||||
had_physical_newline = MP_TRUE;
|
||||
next_char(lex);
|
||||
} else if (is_whitespace(lex)) {
|
||||
next_char(lex);
|
||||
@ -369,22 +369,22 @@ static void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
|
||||
// a string or bytes literal
|
||||
|
||||
// parse type codes
|
||||
bool is_raw = false;
|
||||
bool is_bytes = false;
|
||||
MP_BOOL is_raw = MP_FALSE;
|
||||
MP_BOOL is_bytes = MP_FALSE;
|
||||
if (is_char(lex, 'u')) {
|
||||
next_char(lex);
|
||||
} else if (is_char(lex, 'b')) {
|
||||
is_bytes = true;
|
||||
is_bytes = MP_TRUE;
|
||||
next_char(lex);
|
||||
if (is_char(lex, 'r')) {
|
||||
is_raw = true;
|
||||
is_raw = MP_TRUE;
|
||||
next_char(lex);
|
||||
}
|
||||
} else if (is_char(lex, 'r')) {
|
||||
is_raw = true;
|
||||
is_raw = MP_TRUE;
|
||||
next_char(lex);
|
||||
if (is_char(lex, 'b')) {
|
||||
is_bytes = true;
|
||||
is_bytes = MP_TRUE;
|
||||
next_char(lex);
|
||||
}
|
||||
}
|
||||
@ -628,7 +628,7 @@ mp_lexer_t *mp_lexer_new(const char *src_name, void *stream_data, mp_lexer_strea
|
||||
}
|
||||
|
||||
// preload first token
|
||||
mp_lexer_next_token_into(lex, &lex->tok_cur, true);
|
||||
mp_lexer_next_token_into(lex, &lex->tok_cur, MP_TRUE);
|
||||
|
||||
return lex;
|
||||
}
|
||||
@ -644,44 +644,44 @@ void mp_lexer_free(mp_lexer_t *lex) {
|
||||
}
|
||||
|
||||
void mp_lexer_to_next(mp_lexer_t *lex) {
|
||||
mp_lexer_next_token_into(lex, &lex->tok_cur, false);
|
||||
mp_lexer_next_token_into(lex, &lex->tok_cur, MP_FALSE);
|
||||
}
|
||||
|
||||
const mp_token_t *mp_lexer_cur(const mp_lexer_t *lex) {
|
||||
return &lex->tok_cur;
|
||||
}
|
||||
|
||||
bool mp_lexer_is_kind(mp_lexer_t *lex, mp_token_kind_t kind) {
|
||||
MP_BOOL mp_lexer_is_kind(mp_lexer_t *lex, mp_token_kind_t kind) {
|
||||
return lex->tok_cur.kind == kind;
|
||||
}
|
||||
|
||||
/*
|
||||
bool mp_lexer_is_str(mp_lexer_t *lex, const char *str) {
|
||||
MP_BOOL mp_lexer_is_str(mp_lexer_t *lex, const char *str) {
|
||||
return mp_token_is_str(&lex->tok_cur, str);
|
||||
}
|
||||
|
||||
bool mp_lexer_opt_kind(mp_lexer_t *lex, mp_token_kind_t kind) {
|
||||
MP_BOOL mp_lexer_opt_kind(mp_lexer_t *lex, mp_token_kind_t kind) {
|
||||
if (mp_lexer_is_kind(lex, kind)) {
|
||||
mp_lexer_to_next(lex);
|
||||
return true;
|
||||
return MP_TRUE;
|
||||
}
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
|
||||
bool mp_lexer_opt_str(mp_lexer_t *lex, const char *str) {
|
||||
MP_BOOL mp_lexer_opt_str(mp_lexer_t *lex, const char *str) {
|
||||
if (mp_lexer_is_str(lex, str)) {
|
||||
mp_lexer_to_next(lex);
|
||||
return true;
|
||||
return MP_TRUE;
|
||||
}
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
*/
|
||||
|
||||
bool mp_lexer_show_error(mp_lexer_t *lex, const char *msg) {
|
||||
MP_BOOL mp_lexer_show_error(mp_lexer_t *lex, const char *msg) {
|
||||
return mp_token_show_error(&lex->tok_cur, msg);
|
||||
}
|
||||
|
||||
bool mp_lexer_show_error_pythonic(mp_lexer_t *lex, const char *msg) {
|
||||
MP_BOOL mp_lexer_show_error_pythonic(mp_lexer_t *lex, const char *msg) {
|
||||
printf(" File \"%s\", line %d column %d\n%s\n", lex->tok_cur.src_name, lex->tok_cur.src_line, lex->tok_cur.src_column, msg);
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
|
14
py/lexer.h
14
py/lexer.h
@ -124,20 +124,20 @@ typedef struct _mp_lexer_t mp_lexer_t;
|
||||
|
||||
void mp_token_show(const mp_token_t *tok);
|
||||
void mp_token_show_error_prefix(const mp_token_t *tok);
|
||||
bool mp_token_show_error(const mp_token_t *tok, const char *msg);
|
||||
MP_BOOL mp_token_show_error(const mp_token_t *tok, const char *msg);
|
||||
|
||||
mp_lexer_t *mp_lexer_new(const char *src_name, void *stream_data, mp_lexer_stream_next_char_t stream_next_char, mp_lexer_stream_close_t stream_close);
|
||||
void mp_lexer_free(mp_lexer_t *lex);
|
||||
void mp_lexer_to_next(mp_lexer_t *lex);
|
||||
const mp_token_t *mp_lexer_cur(const mp_lexer_t *lex);
|
||||
bool mp_lexer_is_kind(mp_lexer_t *lex, mp_token_kind_t kind);
|
||||
MP_BOOL mp_lexer_is_kind(mp_lexer_t *lex, mp_token_kind_t kind);
|
||||
/* unused
|
||||
bool mp_lexer_is_str(mp_lexer_t *lex, const char *str);
|
||||
bool mp_lexer_opt_kind(mp_lexer_t *lex, mp_token_kind_t kind);
|
||||
bool mp_lexer_opt_str(mp_lexer_t *lex, const char *str);
|
||||
MP_BOOL mp_lexer_is_str(mp_lexer_t *lex, const char *str);
|
||||
MP_BOOL mp_lexer_opt_kind(mp_lexer_t *lex, mp_token_kind_t kind);
|
||||
MP_BOOL mp_lexer_opt_str(mp_lexer_t *lex, const char *str);
|
||||
*/
|
||||
bool mp_lexer_show_error(mp_lexer_t *lex, const char *msg);
|
||||
bool mp_lexer_show_error_pythonic(mp_lexer_t *lex, const char *msg);
|
||||
MP_BOOL mp_lexer_show_error(mp_lexer_t *lex, const char *msg);
|
||||
MP_BOOL mp_lexer_show_error_pythonic(mp_lexer_t *lex, const char *msg);
|
||||
|
||||
// used to import a module; must be implemented for a specific port
|
||||
mp_lexer_t *mp_import_open_file(qstr mod_name);
|
||||
|
@ -7,7 +7,7 @@
|
||||
#include "lexer.h"
|
||||
|
||||
typedef struct _str_buf_t {
|
||||
bool free; // free src_beg when done
|
||||
MP_BOOL free; // free src_beg when done
|
||||
const char *src_beg; // beginning of source
|
||||
const char *src_cur; // current location in source
|
||||
const char *src_end; // end (exclusive) of source
|
||||
@ -30,7 +30,7 @@ void str_buf_free(str_buf_t *sb) {
|
||||
}
|
||||
}
|
||||
|
||||
mp_lexer_t *mp_lexer_new_from_str_len(const char *src_name, const char *str, uint len, bool free_str) {
|
||||
mp_lexer_t *mp_lexer_new_from_str_len(const char *src_name, const char *str, uint len, MP_BOOL free_str) {
|
||||
str_buf_t *sb = m_new(str_buf_t, 1);
|
||||
sb->free = free_str;
|
||||
sb->src_beg = str;
|
||||
@ -56,7 +56,7 @@ mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return mp_lexer_new_from_str_len(filename, data, size, true);
|
||||
return mp_lexer_new_from_str_len(filename, data, size, MP_TRUE);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
@ -1,4 +1,4 @@
|
||||
mp_lexer_t *mp_lexer_new_from_str_len(const char *src_name, const char *str, uint len, bool free_str);
|
||||
mp_lexer_t *mp_lexer_new_from_str_len(const char *src_name, const char *str, uint len, MP_BOOL free_str);
|
||||
mp_lexer_t *mp_lexer_new_from_file(const char *filename);
|
||||
|
||||
void mp_import_set_directory(const char *dir);
|
||||
|
12
py/map.c
12
py/map.c
@ -38,8 +38,8 @@ mp_map_t *mp_map_new(mp_map_kind_t kind, int n) {
|
||||
return map;
|
||||
}
|
||||
|
||||
mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_not_found) {
|
||||
bool is_map_mp_obj = (map->kind == MP_MAP_OBJ);
|
||||
mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, MP_BOOL add_if_not_found) {
|
||||
MP_BOOL is_map_mp_obj = (map->kind == MP_MAP_OBJ);
|
||||
machine_uint_t hash;
|
||||
if (is_map_mp_obj) {
|
||||
hash = mp_obj_hash(index);
|
||||
@ -61,7 +61,7 @@ mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_n
|
||||
map->table = m_new0(mp_map_elem_t, map->alloc);
|
||||
for (int i = 0; i < old_alloc; i++) {
|
||||
if (old_table[i].key != NULL) {
|
||||
mp_map_lookup_helper(map, old_table[i].key, true)->value = old_table[i].value;
|
||||
mp_map_lookup_helper(map, old_table[i].key, MP_TRUE)->value = old_table[i].value;
|
||||
}
|
||||
}
|
||||
m_del(mp_map_elem_t, old_table, old_alloc);
|
||||
@ -90,7 +90,7 @@ mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_n
|
||||
}
|
||||
}
|
||||
|
||||
mp_map_elem_t* mp_qstr_map_lookup(mp_map_t *map, qstr index, bool add_if_not_found) {
|
||||
mp_map_elem_t* mp_qstr_map_lookup(mp_map_t *map, qstr index, MP_BOOL add_if_not_found) {
|
||||
mp_obj_t o = (mp_obj_t)(machine_uint_t)index;
|
||||
return mp_map_lookup_helper(map, o, add_if_not_found);
|
||||
}
|
||||
@ -104,7 +104,7 @@ void mp_set_init(mp_set_t *set, int n) {
|
||||
set->table = m_new0(mp_obj_t, set->alloc);
|
||||
}
|
||||
|
||||
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, bool add_if_not_found) {
|
||||
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, MP_BOOL add_if_not_found) {
|
||||
int hash = mp_obj_hash(index);
|
||||
int pos = hash % set->alloc;
|
||||
for (;;) {
|
||||
@ -121,7 +121,7 @@ mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, bool add_if_not_found) {
|
||||
set->table = m_new(mp_obj_t, set->alloc);
|
||||
for (int i = 0; i < old_alloc; i++) {
|
||||
if (old_table[i] != NULL) {
|
||||
mp_set_lookup(set, old_table[i], true);
|
||||
mp_set_lookup(set, old_table[i], MP_TRUE);
|
||||
}
|
||||
}
|
||||
m_del(mp_obj_t, old_table, old_alloc);
|
||||
|
6
py/map.h
6
py/map.h
@ -26,8 +26,8 @@ typedef struct _mp_set_t {
|
||||
int get_doubling_prime_greater_or_equal_to(int x);
|
||||
void mp_map_init(mp_map_t *map, mp_map_kind_t kind, int n);
|
||||
mp_map_t *mp_map_new(mp_map_kind_t kind, int n);
|
||||
mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_not_found);
|
||||
mp_map_elem_t* mp_qstr_map_lookup(mp_map_t *map, qstr index, bool add_if_not_found);
|
||||
mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, MP_BOOL add_if_not_found);
|
||||
mp_map_elem_t* mp_qstr_map_lookup(mp_map_t *map, qstr index, MP_BOOL add_if_not_found);
|
||||
|
||||
void mp_set_init(mp_set_t *set, int n);
|
||||
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, bool add_if_not_found);
|
||||
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, MP_BOOL add_if_not_found);
|
||||
|
18
py/misc.h
18
py/misc.h
@ -5,10 +5,10 @@
|
||||
|
||||
/** types *******************************************************/
|
||||
|
||||
typedef int bool;
|
||||
typedef int MP_BOOL;
|
||||
enum {
|
||||
false = 0,
|
||||
true = 1
|
||||
MP_FALSE = 0,
|
||||
MP_TRUE = 1
|
||||
};
|
||||
|
||||
typedef unsigned char byte;
|
||||
@ -42,10 +42,10 @@ typedef int unichar; // TODO
|
||||
unichar utf8_get_char(const char *s);
|
||||
char *utf8_next_char(const char *s);
|
||||
|
||||
bool unichar_isspace(unichar c);
|
||||
bool unichar_isalpha(unichar c);
|
||||
bool unichar_isprint(unichar c);
|
||||
bool unichar_isdigit(unichar c);
|
||||
MP_BOOL unichar_isspace(unichar c);
|
||||
MP_BOOL unichar_isalpha(unichar c);
|
||||
MP_BOOL unichar_isprint(unichar c);
|
||||
MP_BOOL unichar_isdigit(unichar c);
|
||||
|
||||
/** string ******************************************************/
|
||||
|
||||
@ -59,7 +59,7 @@ typedef struct _vstr_t {
|
||||
int alloc;
|
||||
int len;
|
||||
char *buf;
|
||||
bool had_error;
|
||||
MP_BOOL had_error;
|
||||
} vstr_t;
|
||||
|
||||
void vstr_init(vstr_t *vstr);
|
||||
@ -67,7 +67,7 @@ void vstr_clear(vstr_t *vstr);
|
||||
vstr_t *vstr_new(void);
|
||||
void vstr_free(vstr_t *vstr);
|
||||
void vstr_reset(vstr_t *vstr);
|
||||
bool vstr_had_error(vstr_t *vstr);
|
||||
MP_BOOL vstr_had_error(vstr_t *vstr);
|
||||
char *vstr_str(vstr_t *vstr);
|
||||
int vstr_len(vstr_t *vstr);
|
||||
void vstr_hint_size(vstr_t *vstr, int size);
|
||||
|
20
py/obj.c
20
py/obj.c
@ -46,9 +46,9 @@ void mp_obj_print(mp_obj_t o_in) {
|
||||
mp_obj_print_helper(printf_wrapper, NULL, o_in);
|
||||
}
|
||||
|
||||
bool mp_obj_is_callable(mp_obj_t o_in) {
|
||||
MP_BOOL mp_obj_is_callable(mp_obj_t o_in) {
|
||||
if (MP_OBJ_IS_SMALL_INT(o_in)) {
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
} else {
|
||||
mp_obj_base_t *o = o_in;
|
||||
return o->type->call_n != NULL;
|
||||
@ -77,13 +77,13 @@ machine_int_t mp_obj_hash(mp_obj_t o_in) {
|
||||
// "The objects need not have the same type. If both are numbers, they are converted
|
||||
// to a common type. Otherwise, the == and != operators always consider objects of
|
||||
// different types to be unequal."
|
||||
// note also that False==0 and True==1 are true expressions
|
||||
bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
|
||||
// note also that False==0 and True==1 are MP_TRUE expressions
|
||||
MP_BOOL mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
|
||||
if (o1 == o2) {
|
||||
return true;
|
||||
return MP_TRUE;
|
||||
} else if (MP_OBJ_IS_SMALL_INT(o1) || MP_OBJ_IS_SMALL_INT(o2)) {
|
||||
if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
} else {
|
||||
if (MP_OBJ_IS_SMALL_INT(o2)) {
|
||||
mp_obj_t temp = o1; o1 = o2; o2 = temp;
|
||||
@ -95,25 +95,25 @@ bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
|
||||
} else if (o2 == mp_const_true) {
|
||||
return val == 1;
|
||||
} else {
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
}
|
||||
} else if (MP_OBJ_IS_TYPE(o1, &str_type) && MP_OBJ_IS_TYPE(o2, &str_type)) {
|
||||
return mp_obj_str_get(o1) == mp_obj_str_get(o2);
|
||||
} else {
|
||||
assert(0);
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
bool mp_obj_less(mp_obj_t o1, mp_obj_t o2) {
|
||||
MP_BOOL mp_obj_less(mp_obj_t o1, mp_obj_t o2) {
|
||||
if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
|
||||
mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1);
|
||||
mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2);
|
||||
return i1 < i2;
|
||||
} else {
|
||||
assert(0);
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
|
31
py/obj.h
31
py/obj.h
@ -18,12 +18,11 @@ typedef machine_float_t mp_float_t;
|
||||
// Anything that wants to be a Micro Python object must
|
||||
// have 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 {
|
||||
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
|
||||
// It *cannot* be used when an mp_obj_t is expected, except where explicitly allowed
|
||||
@ -43,12 +42,13 @@ struct _mp_obj_base_t {
|
||||
|
||||
#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}, 0, 0, fun_name}
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 1, 1, fun_name}
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 2, 2, fun_name}
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 3, 3, 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}, n_args_min, (~((machine_uint_t)0)), 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}, n_args_min, n_args_max, fun_name}
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, n_args_min, n_args_max, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, n_args_min, n_args_max, (void *)fun_name}
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, 0, 0, (mp_fun_0_t)fun_name)
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, 1, 1, (mp_fun_1_t)fun_name)
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, 2, 2, (mp_fun_2_t)fun_name)
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, 3, 3, (mp_fun_3_t)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, n_args_min, (~((machine_uint_t)0)), (mp_fun_var_t)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, n_args_min, n_args_max, (mp_fun_var_t)fun_name)
|
||||
|
||||
// Type definitions for methods
|
||||
|
||||
@ -83,7 +83,7 @@ struct _mp_obj_type_t {
|
||||
mp_fun_1_t getiter;
|
||||
mp_fun_1_t iternext;
|
||||
|
||||
const mp_method_t methods[];
|
||||
const mp_method_t *methods;
|
||||
|
||||
/*
|
||||
What we might need to add here:
|
||||
@ -107,6 +107,7 @@ struct _mp_obj_type_t {
|
||||
__next__ gen-instance
|
||||
*/
|
||||
};
|
||||
typedef struct _mp_obj_type_t mp_obj_type_t;
|
||||
|
||||
// Constant objects, globally accessible
|
||||
|
||||
@ -125,7 +126,7 @@ struct _mp_map_t;
|
||||
// General API for objects
|
||||
|
||||
mp_obj_t mp_obj_new_none(void);
|
||||
mp_obj_t mp_obj_new_bool(bool value);
|
||||
mp_obj_t mp_obj_new_bool(MP_BOOL value);
|
||||
mp_obj_t mp_obj_new_cell(mp_obj_t obj);
|
||||
mp_obj_t mp_obj_new_int(machine_int_t value);
|
||||
mp_obj_t mp_obj_new_str(qstr qstr);
|
||||
@ -161,10 +162,10 @@ const char *mp_obj_get_type_str(mp_obj_t o_in);
|
||||
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in);
|
||||
void mp_obj_print(mp_obj_t o);
|
||||
|
||||
bool mp_obj_is_callable(mp_obj_t o_in);
|
||||
MP_BOOL mp_obj_is_callable(mp_obj_t o_in);
|
||||
machine_int_t mp_obj_hash(mp_obj_t o_in);
|
||||
bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2);
|
||||
bool mp_obj_less(mp_obj_t o1, mp_obj_t o2);
|
||||
MP_BOOL mp_obj_equal(mp_obj_t o1, mp_obj_t o2);
|
||||
MP_BOOL mp_obj_less(mp_obj_t o1, mp_obj_t o2);
|
||||
|
||||
machine_int_t mp_obj_get_int(mp_obj_t arg);
|
||||
#if MICROPY_ENABLE_FLOAT
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
typedef struct _mp_obj_bool_t {
|
||||
mp_obj_base_t base;
|
||||
bool value;
|
||||
MP_BOOL value;
|
||||
} mp_obj_bool_t;
|
||||
|
||||
static void bool_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
||||
@ -41,11 +41,11 @@ const mp_obj_type_t bool_type = {
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.methods = {{NULL, NULL},},
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
static const mp_obj_bool_t false_obj = {{&bool_type}, false};
|
||||
static const mp_obj_bool_t true_obj = {{&bool_type}, true};
|
||||
static const mp_obj_bool_t false_obj = {{&bool_type}, MP_FALSE};
|
||||
static const mp_obj_bool_t true_obj = {{&bool_type}, MP_TRUE};
|
||||
|
||||
const mp_obj_t mp_const_false = (mp_obj_t)&false_obj;
|
||||
const mp_obj_t mp_const_true = (mp_obj_t)&true_obj;
|
||||
|
@ -43,7 +43,7 @@ const mp_obj_type_t bound_meth_type = {
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.methods = {{NULL, NULL},},
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_bound_meth(mp_obj_t self, mp_obj_t meth) {
|
||||
|
@ -33,7 +33,7 @@ const mp_obj_type_t cell_type = {
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.methods = {{NULL, NULL},},
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_cell(mp_obj_t obj) {
|
||||
|
@ -26,7 +26,7 @@ mp_obj_t class_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
|
||||
mp_obj_t o = mp_obj_new_instance(self_in);
|
||||
|
||||
// look for __init__ function
|
||||
mp_map_elem_t *init_fn = mp_qstr_map_lookup(self->locals, MP_QSTR___init__, false);
|
||||
mp_map_elem_t *init_fn = mp_qstr_map_lookup(self->locals, MP_QSTR___init__, MP_FALSE);
|
||||
|
||||
if (init_fn != NULL) {
|
||||
// call __init__ function
|
||||
@ -70,7 +70,7 @@ const mp_obj_type_t class_type = {
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.methods = {{NULL, NULL},},
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_class(mp_map_t *class_locals) {
|
||||
|
@ -42,7 +42,7 @@ const mp_obj_type_t closure_type = {
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.methods = {{NULL, NULL},},
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_closure(mp_obj_t fun, mp_obj_t closure_tuple) {
|
||||
|
@ -125,7 +125,7 @@ const mp_obj_type_t complex_type = {
|
||||
complex_binary_op, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.methods = { { NULL, NULL }, },
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) {
|
||||
|
10
py/objdict.c
10
py/objdict.c
@ -19,14 +19,14 @@ typedef struct _mp_obj_dict_t {
|
||||
|
||||
static void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
||||
mp_obj_dict_t *self = self_in;
|
||||
bool first = true;
|
||||
MP_BOOL first = MP_TRUE;
|
||||
print(env, "{");
|
||||
for (int i = 0; i < self->map.alloc; i++) {
|
||||
if (self->map.table[i].key != NULL) {
|
||||
if (!first) {
|
||||
print(env, ", ");
|
||||
}
|
||||
first = false;
|
||||
first = MP_FALSE;
|
||||
mp_obj_print_helper(print, env, self->map.table[i].key);
|
||||
print(env, ": ");
|
||||
mp_obj_print_helper(print, env, self->map.table[i].value);
|
||||
@ -47,7 +47,7 @@ static mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
||||
case RT_BINARY_OP_SUBSCR:
|
||||
{
|
||||
// dict load
|
||||
mp_map_elem_t *elem = mp_map_lookup_helper(&o->map, rhs_in, false);
|
||||
mp_map_elem_t *elem = mp_map_lookup_helper(&o->map, rhs_in, MP_FALSE);
|
||||
if (elem == NULL) {
|
||||
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "<value>"));
|
||||
} else {
|
||||
@ -67,7 +67,7 @@ const mp_obj_type_t dict_type = {
|
||||
.make_new = dict_make_new,
|
||||
.binary_op = dict_binary_op,
|
||||
.getiter = NULL,
|
||||
.methods = {{NULL, NULL},},
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_dict(int n_args) {
|
||||
@ -91,6 +91,6 @@ uint mp_obj_dict_len(mp_obj_t self_in) {
|
||||
mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
|
||||
assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
|
||||
mp_obj_dict_t *self = self_in;
|
||||
mp_map_lookup_helper(&self->map, key, true)->value = value;
|
||||
mp_map_lookup_helper(&self->map, key, MP_TRUE)->value = value;
|
||||
return self_in;
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ const mp_obj_type_t exception_type = {
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.methods = {{NULL, NULL},},
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_exception(qstr id) {
|
||||
|
@ -83,7 +83,7 @@ const mp_obj_type_t float_type = {
|
||||
.make_new = float_make_new,
|
||||
.unary_op = float_unary_op,
|
||||
.binary_op = float_binary_op,
|
||||
.methods = { { NULL, NULL }, },
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_float(mp_float_t value) {
|
||||
|
12
py/objfun.c
12
py/objfun.c
@ -79,9 +79,7 @@ const mp_obj_type_t fun_native_type = {
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.methods = {
|
||||
{NULL, NULL}, // end-of-list sentinel
|
||||
},
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
mp_obj_t rt_make_function_0(mp_fun_0_t fun) {
|
||||
@ -181,9 +179,7 @@ const mp_obj_type_t fun_bc_type = {
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.methods = {
|
||||
{NULL, NULL}, // end-of-list sentinel
|
||||
},
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_fun_bc(int n_args, uint n_state, const byte *code) {
|
||||
@ -295,9 +291,7 @@ static const mp_obj_type_t fun_asm_type = {
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.methods = {
|
||||
{NULL, NULL}, // end-of-list sentinel
|
||||
},
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun) {
|
||||
|
@ -46,7 +46,7 @@ const mp_obj_type_t gen_wrap_type = {
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.methods = {{NULL, NULL},},
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_gen_wrap(uint n_locals, uint n_stack, mp_obj_t fun) {
|
||||
@ -78,7 +78,7 @@ mp_obj_t gen_instance_getiter(mp_obj_t self_in) {
|
||||
|
||||
mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
|
||||
mp_obj_gen_instance_t *self = self_in;
|
||||
bool yield = mp_execute_byte_code_2(&self->ip, &self->state[0], &self->sp);
|
||||
MP_BOOL yield = mp_execute_byte_code_2(&self->ip, &self->state[0], &self->sp);
|
||||
if (yield) {
|
||||
return *self->sp;
|
||||
} else {
|
||||
@ -101,7 +101,7 @@ const mp_obj_type_t gen_instance_type = {
|
||||
NULL, // binary_op
|
||||
gen_instance_getiter, // getiter
|
||||
gen_instance_iternext, // iternext
|
||||
.methods = {{NULL, NULL},},
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
// args are in reverse order in the array
|
||||
|
@ -21,7 +21,7 @@ typedef struct _mp_obj_instance_t {
|
||||
type needs to be specified dynamically
|
||||
case O_OBJ:
|
||||
{
|
||||
py_map_elem_t *qn = py_qstr_map_lookup(o->u_obj.class->u_class.locals, qstr_from_str_static("__qualname__"), false); assert(qn != NULL);
|
||||
py_map_elem_t *qn = py_qstr_map_lookup(o->u_obj.class->u_class.locals, qstr_from_str_static("__qualname__"), MP_FALSE); assert(qn != NULL);
|
||||
assert(IS_O(qn->value, O_STR));
|
||||
return qstr_str(((py_obj_base_t*)qn->value)->u_str);
|
||||
}
|
||||
@ -30,12 +30,12 @@ type needs to be specified dynamically
|
||||
mp_obj_t mp_obj_instance_load_attr(mp_obj_t self_in, qstr attr) {
|
||||
// logic: look in obj members then class locals (TODO check this against CPython)
|
||||
mp_obj_instance_t *self = self_in;
|
||||
mp_map_elem_t *elem = mp_qstr_map_lookup(self->members, attr, false);
|
||||
mp_map_elem_t *elem = mp_qstr_map_lookup(self->members, attr, MP_FALSE);
|
||||
if (elem != NULL) {
|
||||
// object member, always treated as a value
|
||||
return elem->value;
|
||||
}
|
||||
elem = mp_qstr_map_lookup(mp_obj_class_get_locals(self->class), attr, false);
|
||||
elem = mp_qstr_map_lookup(mp_obj_class_get_locals(self->class), attr, MP_FALSE);
|
||||
if (elem != NULL) {
|
||||
if (mp_obj_is_callable(elem->value)) {
|
||||
// class member is callable so build a bound method
|
||||
@ -51,14 +51,14 @@ mp_obj_t mp_obj_instance_load_attr(mp_obj_t self_in, qstr attr) {
|
||||
void mp_obj_instance_load_method(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
|
||||
// logic: look in obj members then class locals (TODO check this against CPython)
|
||||
mp_obj_instance_t *self = self_in;
|
||||
mp_map_elem_t *elem = mp_qstr_map_lookup(self->members, attr, false);
|
||||
mp_map_elem_t *elem = mp_qstr_map_lookup(self->members, attr, MP_FALSE);
|
||||
if (elem != NULL) {
|
||||
// object member, always treated as a value
|
||||
dest[1] = elem->value;
|
||||
dest[0] = NULL;
|
||||
return;
|
||||
}
|
||||
elem = mp_qstr_map_lookup(mp_obj_class_get_locals(self->class), attr, false);
|
||||
elem = mp_qstr_map_lookup(mp_obj_class_get_locals(self->class), attr, MP_FALSE);
|
||||
if (elem != NULL) {
|
||||
if (mp_obj_is_callable(elem->value)) {
|
||||
// class member is callable so build a bound method
|
||||
@ -81,11 +81,11 @@ void mp_obj_instance_load_method(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
|
||||
void mp_obj_instance_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
|
||||
// logic: look in class locals (no add) then obj members (add) (TODO check this against CPython)
|
||||
mp_obj_instance_t *self = self_in;
|
||||
mp_map_elem_t *elem = mp_qstr_map_lookup(mp_obj_class_get_locals(self->class), attr, false);
|
||||
mp_map_elem_t *elem = mp_qstr_map_lookup(mp_obj_class_get_locals(self->class), attr, MP_FALSE);
|
||||
if (elem != NULL) {
|
||||
elem->value = value;
|
||||
} else {
|
||||
mp_qstr_map_lookup(self->members, attr, true)->value = value;
|
||||
mp_qstr_map_lookup(self->members, attr, MP_TRUE)->value = value;
|
||||
}
|
||||
}
|
||||
|
||||
@ -99,7 +99,7 @@ const mp_obj_type_t instance_type = {
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.methods = {{NULL, NULL},},
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_instance(mp_obj_t class) {
|
||||
|
@ -34,7 +34,7 @@ const mp_obj_type_t int_type = {
|
||||
{ &mp_const_type },
|
||||
"int",
|
||||
.make_new = int_make_new,
|
||||
.methods = { { NULL, NULL }, },
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_int(machine_int_t value) {
|
||||
|
28
py/objlist.c
28
py/objlist.c
@ -260,6 +260,18 @@ 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_2(list_sort_obj, list_sort);
|
||||
|
||||
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 },
|
||||
{ "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 = {
|
||||
{ &mp_const_type },
|
||||
"list",
|
||||
@ -268,19 +280,7 @@ const mp_obj_type_t list_type = {
|
||||
.unary_op = NULL,
|
||||
.binary_op = list_binary_op,
|
||||
.getiter = list_getiter,
|
||||
.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
|
||||
},
|
||||
.methods = list_type_methods,
|
||||
};
|
||||
|
||||
static mp_obj_list_t *list_new(uint n) {
|
||||
@ -344,7 +344,7 @@ static const mp_obj_type_t list_it_type = {
|
||||
{ &mp_const_type },
|
||||
"list_iterator",
|
||||
.iternext = list_it_iternext,
|
||||
.methods = { { NULL, NULL }, },
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) {
|
||||
|
@ -31,7 +31,7 @@ const mp_obj_type_t module_type = {
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.methods = {{NULL, NULL},},
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_module(qstr module_name) {
|
||||
|
@ -18,7 +18,7 @@ const mp_obj_type_t none_type = {
|
||||
{ &mp_const_type },
|
||||
"NoneType",
|
||||
.print = none_print,
|
||||
.methods = {{NULL, NULL},},
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
static const mp_obj_none_t none_obj = {{&none_type}};
|
||||
|
@ -32,7 +32,7 @@ static const mp_obj_type_t range_type = {
|
||||
NULL, // binary_op
|
||||
range_getiter,
|
||||
NULL, // iternext
|
||||
.methods = {{NULL, NULL},},
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
// range is a class and instances are immutable sequence objects
|
||||
@ -77,7 +77,7 @@ static const mp_obj_type_t range_it_type = {
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
range_it_iternext,
|
||||
.methods = {{NULL, NULL},},
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_range_iterator(int cur, int stop, int step) {
|
||||
|
10
py/objset.c
10
py/objset.c
@ -17,14 +17,14 @@ typedef struct _mp_obj_set_t {
|
||||
|
||||
void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
||||
mp_obj_set_t *self = self_in;
|
||||
bool first = true;
|
||||
MP_BOOL first = MP_TRUE;
|
||||
print(env, "{");
|
||||
for (int i = 0; i < self->set.alloc; i++) {
|
||||
if (self->set.table[i] != MP_OBJ_NULL) {
|
||||
if (!first) {
|
||||
print(env, ", ");
|
||||
}
|
||||
first = false;
|
||||
first = MP_FALSE;
|
||||
mp_obj_print_helper(print, env, self->set.table[i]);
|
||||
}
|
||||
}
|
||||
@ -64,7 +64,7 @@ const mp_obj_type_t set_type = {
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.methods = { { NULL, NULL }, },
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {
|
||||
@ -72,7 +72,7 @@ mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {
|
||||
o->base.type = &set_type;
|
||||
mp_set_init(&o->set, n_args);
|
||||
for (int i = 0; i < n_args; i++) {
|
||||
mp_set_lookup(&o->set, items[i], true);
|
||||
mp_set_lookup(&o->set, items[i], MP_TRUE);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
@ -80,5 +80,5 @@ mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {
|
||||
void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
|
||||
assert(MP_OBJ_IS_TYPE(self_in, &set_type));
|
||||
mp_obj_set_t *self = self_in;
|
||||
mp_set_lookup(&self->set, item, true);
|
||||
mp_set_lookup(&self->set, item, MP_TRUE);
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ const mp_obj_type_t ellipsis_type = {
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
.methods = {{NULL, NULL},},
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
static const mp_obj_ellipsis_t ellipsis_obj = {{&ellipsis_type}};
|
||||
@ -58,7 +58,7 @@ const mp_obj_type_t slice_type = {
|
||||
{ &mp_const_type },
|
||||
"slice",
|
||||
.print = slice_print,
|
||||
.methods = { { NULL, NULL }, },
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
// TODO: Make sure to handle "empty" values, which are signified by None in CPython
|
||||
|
13
py/objstr.c
13
py/objstr.c
@ -184,17 +184,18 @@ 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_VAR(str_format_obj, 1, str_format);
|
||||
|
||||
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 = {
|
||||
{ &mp_const_type },
|
||||
"str",
|
||||
.print = str_print,
|
||||
.binary_op = str_binary_op,
|
||||
.getiter = str_getiter,
|
||||
.methods = {
|
||||
{ "join", &str_join_obj },
|
||||
{ "format", &str_format_obj },
|
||||
{ NULL, NULL }, // end-of-list sentinel
|
||||
},
|
||||
.methods = str_type_methods,
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_str(qstr qstr) {
|
||||
@ -235,7 +236,7 @@ static const mp_obj_type_t str_it_type = {
|
||||
{ &mp_const_type },
|
||||
"str_iterator",
|
||||
.iternext = str_it_iternext,
|
||||
.methods = { { NULL, NULL }, },
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_str_iterator(mp_obj_str_t *str, int cur) {
|
||||
|
@ -103,7 +103,7 @@ const mp_obj_type_t tuple_type = {
|
||||
.make_new = tuple_make_new,
|
||||
.binary_op = tuple_binary_op,
|
||||
.getiter = tuple_getiter,
|
||||
.methods = {{NULL, NULL},},
|
||||
.methods = NULL,
|
||||
};
|
||||
|
||||
// the zero-length tuple
|
||||
@ -165,8 +165,14 @@ static mp_obj_t tuple_it_iternext(mp_obj_t self_in) {
|
||||
static const mp_obj_type_t tuple_it_type = {
|
||||
{ &mp_const_type },
|
||||
"tuple_iterator",
|
||||
.iternext = tuple_it_iternext,
|
||||
.methods = {{NULL, NULL},},
|
||||
NULL, // print
|
||||
NULL, // make_new
|
||||
NULL, // call_n
|
||||
NULL, // unary_op
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
tuple_it_iternext,
|
||||
NULL, // method list
|
||||
};
|
||||
|
||||
static mp_obj_t mp_obj_new_tuple_iterator(mp_obj_tuple_t *tuple, int cur) {
|
||||
|
@ -27,5 +27,5 @@ const mp_obj_type_t mp_const_type = {
|
||||
"type",
|
||||
.print = type_print,
|
||||
.call_n = type_call_n,
|
||||
.methods = {{NULL, NULL},},
|
||||
.methods = NULL,
|
||||
};
|
||||
|
44
py/parse.c
44
py/parse.c
@ -189,8 +189,8 @@ static void push_result_token(parser_t *parser, const mp_lexer_t *lex) {
|
||||
if (tok->kind == MP_TOKEN_NAME) {
|
||||
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_ID, qstr_from_strn_copy(tok->str, tok->len));
|
||||
} else if (tok->kind == MP_TOKEN_NUMBER) {
|
||||
bool dec = false;
|
||||
bool small_int = true;
|
||||
MP_BOOL dec = MP_FALSE;
|
||||
MP_BOOL small_int = MP_TRUE;
|
||||
int int_val = 0;
|
||||
int len = tok->len;
|
||||
const char *str = tok->str;
|
||||
@ -219,10 +219,10 @@ static void push_result_token(parser_t *parser, const mp_lexer_t *lex) {
|
||||
} else if (base == 16 && 'A' <= str[i] && str[i] <= 'F') {
|
||||
int_val = base * int_val + str[i] - 'A' + 10;
|
||||
} else if (str[i] == '.' || str[i] == 'e' || str[i] == 'E' || str[i] == 'j' || str[i] == 'J') {
|
||||
dec = true;
|
||||
dec = MP_TRUE;
|
||||
break;
|
||||
} else {
|
||||
small_int = false;
|
||||
small_int = MP_FALSE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -269,11 +269,11 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
|
||||
push_rule(parser, rules[top_level_rule], 0);
|
||||
|
||||
uint n, i;
|
||||
bool backtrack = false;
|
||||
MP_BOOL backtrack = MP_FALSE;
|
||||
const rule_t *rule;
|
||||
mp_token_kind_t tok_kind;
|
||||
bool emit_rule;
|
||||
bool had_trailing_sep;
|
||||
MP_BOOL emit_rule;
|
||||
MP_BOOL had_trailing_sep;
|
||||
|
||||
for (;;) {
|
||||
next_rule:
|
||||
@ -298,7 +298,7 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
|
||||
if (i > 0 && !backtrack) {
|
||||
goto next_rule;
|
||||
} else {
|
||||
backtrack = false;
|
||||
backtrack = MP_FALSE;
|
||||
}
|
||||
for (; i < n - 1; ++i) {
|
||||
switch (rule->arg[i] & RULE_ARG_KIND_MASK) {
|
||||
@ -322,7 +322,7 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
|
||||
push_result_token(parser, lex);
|
||||
mp_lexer_to_next(lex);
|
||||
} else {
|
||||
backtrack = true;
|
||||
backtrack = MP_TRUE;
|
||||
goto next_rule;
|
||||
}
|
||||
} else {
|
||||
@ -338,7 +338,7 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
|
||||
if ((rule->arg[i - 1] & RULE_ARG_KIND_MASK) == RULE_ARG_OPT_RULE) {
|
||||
// an optional rule that failed, so continue with next arg
|
||||
push_result_node(parser, MP_PARSE_NODE_NULL);
|
||||
backtrack = false;
|
||||
backtrack = MP_FALSE;
|
||||
} else {
|
||||
// a mandatory rule that failed, so propagate backtrack
|
||||
if (i > 1) {
|
||||
@ -369,7 +369,7 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
|
||||
goto syntax_error;
|
||||
} else {
|
||||
// this rule failed, so backtrack
|
||||
backtrack = true;
|
||||
backtrack = MP_TRUE;
|
||||
goto next_rule;
|
||||
}
|
||||
}
|
||||
@ -395,12 +395,12 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
|
||||
|
||||
// count number of arguments for the parse_node
|
||||
i = 0;
|
||||
emit_rule = false;
|
||||
emit_rule = MP_FALSE;
|
||||
for (int x = 0; x < n; ++x) {
|
||||
if ((rule->arg[x] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
|
||||
tok_kind = rule->arg[x] & RULE_ARG_ARG_MASK;
|
||||
if (tok_kind >= MP_TOKEN_NAME) {
|
||||
emit_rule = true;
|
||||
emit_rule = MP_TRUE;
|
||||
}
|
||||
if (tok_kind == MP_TOKEN_NAME) {
|
||||
// only tokens which were names are pushed to stack
|
||||
@ -414,19 +414,19 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
|
||||
|
||||
// always emit these rules, even if they have only 1 argument
|
||||
if (rule->rule_id == RULE_expr_stmt || rule->rule_id == RULE_yield_stmt) {
|
||||
emit_rule = true;
|
||||
emit_rule = MP_TRUE;
|
||||
}
|
||||
|
||||
// never emit these rules if they have only 1 argument
|
||||
// NOTE: can't put atom_paren here because we need it to distinguisg, for example, [a,b] from [(a,b)]
|
||||
// TODO possibly put varargslist_name, varargslist_equal here as well
|
||||
if (rule->rule_id == RULE_else_stmt || rule->rule_id == RULE_testlist_comp_3b || rule->rule_id == RULE_import_as_names_paren || rule->rule_id == RULE_typedargslist_name || rule->rule_id == RULE_typedargslist_colon || rule->rule_id == RULE_typedargslist_equal || rule->rule_id == RULE_dictorsetmaker_colon || rule->rule_id == RULE_classdef_2 || rule->rule_id == RULE_with_item_as || rule->rule_id == RULE_assert_stmt_extra || rule->rule_id == RULE_as_name || rule->rule_id == RULE_raise_stmt_from || rule->rule_id == RULE_vfpdef) {
|
||||
emit_rule = false;
|
||||
emit_rule = MP_FALSE;
|
||||
}
|
||||
|
||||
// always emit these rules, and add an extra blank node at the end (to be used by the compiler to store data)
|
||||
if (rule->rule_id == RULE_funcdef || rule->rule_id == RULE_classdef || rule->rule_id == RULE_comp_for || rule->rule_id == RULE_lambdef || rule->rule_id == RULE_lambdef_nocond) {
|
||||
emit_rule = true;
|
||||
emit_rule = MP_TRUE;
|
||||
push_result_node(parser, MP_PARSE_NODE_NULL);
|
||||
i += 1;
|
||||
}
|
||||
@ -465,14 +465,14 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
|
||||
// n=3 is: item (sep item)* [sep]
|
||||
if (backtrack) {
|
||||
list_backtrack:
|
||||
had_trailing_sep = false;
|
||||
had_trailing_sep = MP_FALSE;
|
||||
if (n == 2) {
|
||||
if (i == 1) {
|
||||
// fail on item, first time round; propagate backtrack
|
||||
goto next_rule;
|
||||
} else {
|
||||
// fail on item, in later rounds; finish with this rule
|
||||
backtrack = false;
|
||||
backtrack = MP_FALSE;
|
||||
}
|
||||
} else {
|
||||
if (i == 1) {
|
||||
@ -482,15 +482,15 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
|
||||
// fail on item, in later rounds; have eaten tokens so can't backtrack
|
||||
if (n == 3) {
|
||||
// list allows trailing separator; finish parsing list
|
||||
had_trailing_sep = true;
|
||||
backtrack = false;
|
||||
had_trailing_sep = MP_TRUE;
|
||||
backtrack = MP_FALSE;
|
||||
} else {
|
||||
// list doesn't allowing trailing separator; fail
|
||||
goto syntax_error;
|
||||
}
|
||||
} else {
|
||||
// fail on separator; finish parsing list
|
||||
backtrack = false;
|
||||
backtrack = MP_FALSE;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@ -510,7 +510,7 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
|
||||
} else {
|
||||
// couldn't get element of list
|
||||
i += 1;
|
||||
backtrack = true;
|
||||
backtrack = MP_TRUE;
|
||||
goto list_backtrack;
|
||||
}
|
||||
break;
|
||||
|
@ -1,17 +1,17 @@
|
||||
#include "misc.h"
|
||||
#include "repl.h"
|
||||
|
||||
bool str_startswith_word(const char *str, const char *head) {
|
||||
MP_BOOL str_startswith_word(const char *str, const char *head) {
|
||||
int i;
|
||||
for (i = 0; str[i] && head[i]; i++) {
|
||||
if (str[i] != head[i]) {
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
}
|
||||
return head[i] == '\0' && (str[i] == '\0' || !unichar_isalpha(str[i]));
|
||||
}
|
||||
|
||||
bool mp_repl_is_compound_stmt(const char *line) {
|
||||
MP_BOOL mp_repl_is_compound_stmt(const char *line) {
|
||||
// compound if line starts with a certain keyword
|
||||
if (
|
||||
str_startswith_word(line, "if")
|
||||
@ -23,7 +23,7 @@ bool mp_repl_is_compound_stmt(const char *line) {
|
||||
|| str_startswith_word(line, "class")
|
||||
|| str_startswith_word(line, "@")
|
||||
) {
|
||||
return true;
|
||||
return MP_TRUE;
|
||||
}
|
||||
|
||||
// also "compound" if unmatched open bracket
|
||||
|
@ -1 +1 @@
|
||||
bool mp_repl_is_compound_stmt(const char *line);
|
||||
MP_BOOL mp_repl_is_compound_stmt(const char *line);
|
||||
|
142
py/runtime.c
142
py/runtime.c
@ -45,7 +45,7 @@ typedef struct _mp_code_t {
|
||||
int n_args;
|
||||
int n_locals;
|
||||
int n_stack;
|
||||
bool is_generator;
|
||||
MP_BOOL is_generator;
|
||||
union {
|
||||
struct {
|
||||
byte *code;
|
||||
@ -70,60 +70,60 @@ FILE *fp_write_code = NULL;
|
||||
void rt_init(void) {
|
||||
// locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
|
||||
map_locals = map_globals = mp_map_new(MP_MAP_QSTR, 1);
|
||||
mp_qstr_map_lookup(map_globals, MP_QSTR___name__, true)->value = mp_obj_new_str(MP_QSTR___main__);
|
||||
mp_qstr_map_lookup(map_globals, MP_QSTR___name__, MP_TRUE)->value = mp_obj_new_str(MP_QSTR___main__);
|
||||
|
||||
// init built-in hash table
|
||||
mp_map_init(&map_builtins, MP_MAP_QSTR, 3);
|
||||
|
||||
// built-in exceptions (TODO, make these proper classes)
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_AttributeError, true)->value = mp_obj_new_exception(MP_QSTR_AttributeError);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_IndexError, true)->value = mp_obj_new_exception(MP_QSTR_IndexError);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_KeyError, true)->value = mp_obj_new_exception(MP_QSTR_KeyError);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_NameError, true)->value = mp_obj_new_exception(MP_QSTR_NameError);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_TypeError, true)->value = mp_obj_new_exception(MP_QSTR_TypeError);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_SyntaxError, true)->value = mp_obj_new_exception(MP_QSTR_SyntaxError);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_ValueError, true)->value = mp_obj_new_exception(MP_QSTR_ValueError);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_AttributeError, MP_TRUE)->value = mp_obj_new_exception(MP_QSTR_AttributeError);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_IndexError, MP_TRUE)->value = mp_obj_new_exception(MP_QSTR_IndexError);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_KeyError, MP_TRUE)->value = mp_obj_new_exception(MP_QSTR_KeyError);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_NameError, MP_TRUE)->value = mp_obj_new_exception(MP_QSTR_NameError);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_TypeError, MP_TRUE)->value = mp_obj_new_exception(MP_QSTR_TypeError);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_SyntaxError, MP_TRUE)->value = mp_obj_new_exception(MP_QSTR_SyntaxError);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_ValueError, MP_TRUE)->value = mp_obj_new_exception(MP_QSTR_ValueError);
|
||||
|
||||
// built-in objects
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_Ellipsis, true)->value = mp_const_ellipsis;
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_Ellipsis, MP_TRUE)->value = mp_const_ellipsis;
|
||||
|
||||
// built-in core functions
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR___build_class__, true)->value = rt_make_function_2(mp_builtin___build_class__);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR___repl_print__, true)->value = rt_make_function_1(mp_builtin___repl_print__);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR___build_class__, MP_TRUE)->value = rt_make_function_2(mp_builtin___build_class__);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR___repl_print__, MP_TRUE)->value = rt_make_function_1(mp_builtin___repl_print__);
|
||||
|
||||
// built-in types
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_bool, true)->value = (mp_obj_t)&bool_type;
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_bool, MP_TRUE)->value = (mp_obj_t)&bool_type;
|
||||
#if MICROPY_ENABLE_FLOAT
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_complex, true)->value = (mp_obj_t)&complex_type;
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_complex, MP_TRUE)->value = (mp_obj_t)&complex_type;
|
||||
#endif
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_dict, true)->value = (mp_obj_t)&dict_type;
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_dict, MP_TRUE)->value = (mp_obj_t)&dict_type;
|
||||
#if MICROPY_ENABLE_FLOAT
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_float, true)->value = (mp_obj_t)&float_type;
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_float, MP_TRUE)->value = (mp_obj_t)&float_type;
|
||||
#endif
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_int, true)->value = (mp_obj_t)&int_type;
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_list, true)->value = (mp_obj_t)&list_type;
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_set, true)->value = (mp_obj_t)&set_type;
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_tuple, true)->value = (mp_obj_t)&tuple_type;
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_type, true)->value = (mp_obj_t)&mp_builtin_type_obj; // TODO
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_int, MP_TRUE)->value = (mp_obj_t)&int_type;
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_list, MP_TRUE)->value = (mp_obj_t)&list_type;
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_set, MP_TRUE)->value = (mp_obj_t)&set_type;
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_tuple, MP_TRUE)->value = (mp_obj_t)&tuple_type;
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_type, MP_TRUE)->value = (mp_obj_t)&mp_builtin_type_obj; // TODO
|
||||
|
||||
// built-in user functions; TODO covert all to &mp_builtin_xxx's
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_abs, true)->value = rt_make_function_1(mp_builtin_abs);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_all, true)->value = rt_make_function_1(mp_builtin_all);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_any, true)->value = rt_make_function_1(mp_builtin_any);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_callable, true)->value = rt_make_function_1(mp_builtin_callable);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_chr, true)->value = rt_make_function_1(mp_builtin_chr);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_divmod, true)->value = rt_make_function_2(mp_builtin_divmod);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_hash, true)->value = (mp_obj_t)&mp_builtin_hash_obj;
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_iter, true)->value = (mp_obj_t)&mp_builtin_iter_obj;
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_len, true)->value = rt_make_function_1(mp_builtin_len);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_max, true)->value = rt_make_function_var(1, mp_builtin_max);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_min, true)->value = rt_make_function_var(1, mp_builtin_min);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_next, true)->value = (mp_obj_t)&mp_builtin_next_obj;
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_ord, true)->value = rt_make_function_1(mp_builtin_ord);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_pow, true)->value = rt_make_function_var(2, mp_builtin_pow);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_print, true)->value = rt_make_function_var(0, mp_builtin_print);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_range, true)->value = rt_make_function_var(1, mp_builtin_range);
|
||||
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_abs, MP_TRUE)->value = rt_make_function_1(mp_builtin_abs);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_all, MP_TRUE)->value = rt_make_function_1(mp_builtin_all);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_any, MP_TRUE)->value = rt_make_function_1(mp_builtin_any);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_callable, MP_TRUE)->value = rt_make_function_1(mp_builtin_callable);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_chr, MP_TRUE)->value = rt_make_function_1(mp_builtin_chr);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_divmod, MP_TRUE)->value = rt_make_function_2(mp_builtin_divmod);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_hash, MP_TRUE)->value = (mp_obj_t)&mp_builtin_hash_obj;
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_iter, MP_TRUE)->value = (mp_obj_t)&mp_builtin_iter_obj;
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_len, MP_TRUE)->value = rt_make_function_1(mp_builtin_len);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_max, MP_TRUE)->value = rt_make_function_var(1, mp_builtin_max);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_min, MP_TRUE)->value = rt_make_function_var(1, mp_builtin_min);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_next, MP_TRUE)->value = (mp_obj_t)&mp_builtin_next_obj;
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_ord, MP_TRUE)->value = rt_make_function_1(mp_builtin_ord);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_pow, MP_TRUE)->value = rt_make_function_var(2, mp_builtin_pow);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_print, MP_TRUE)->value = rt_make_function_var(0, mp_builtin_print);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_range, MP_TRUE)->value = rt_make_function_var(1, mp_builtin_range);
|
||||
mp_qstr_map_lookup(&map_builtins, MP_QSTR_sum, MP_TRUE)->value = rt_make_function_var(1, mp_builtin_sum);
|
||||
|
||||
next_unique_code_id = 1; // 0 indicates "no code"
|
||||
unique_codes = NULL;
|
||||
@ -154,7 +154,7 @@ static void alloc_unique_codes(void) {
|
||||
}
|
||||
}
|
||||
|
||||
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, MP_BOOL is_generator) {
|
||||
alloc_unique_codes();
|
||||
|
||||
assert(1 <= unique_code_id && unique_code_id < next_unique_code_id);
|
||||
@ -197,7 +197,7 @@ void rt_assign_native_code(int unique_code_id, void *fun, uint len, int 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_stack = 0;
|
||||
unique_codes[unique_code_id].is_generator = false;
|
||||
unique_codes[unique_code_id].is_generator = MP_FALSE;
|
||||
unique_codes[unique_code_id].u_native.fun = fun;
|
||||
|
||||
//printf("native code: %d bytes\n", len);
|
||||
@ -230,7 +230,7 @@ void rt_assign_inline_asm_code(int unique_code_id, void *fun, uint len, int n_ar
|
||||
unique_codes[unique_code_id].n_args = n_args;
|
||||
unique_codes[unique_code_id].n_locals = 0;
|
||||
unique_codes[unique_code_id].n_stack = 0;
|
||||
unique_codes[unique_code_id].is_generator = false;
|
||||
unique_codes[unique_code_id].is_generator = MP_FALSE;
|
||||
unique_codes[unique_code_id].u_inline_asm.fun = fun;
|
||||
|
||||
#ifdef DEBUG_PRINT
|
||||
@ -252,8 +252,8 @@ void rt_assign_inline_asm_code(int unique_code_id, void *fun, uint len, int n_ar
|
||||
#endif
|
||||
}
|
||||
|
||||
static bool fit_small_int(mp_small_int_t o) {
|
||||
return true;
|
||||
static MP_BOOL fit_small_int(mp_small_int_t o) {
|
||||
return MP_TRUE;
|
||||
}
|
||||
|
||||
int rt_is_true(mp_obj_t arg) {
|
||||
@ -290,10 +290,10 @@ mp_obj_t rt_load_const_dec(qstr qstr) {
|
||||
const char *s = qstr_str(qstr);
|
||||
int in = PARSE_DEC_IN_INTG;
|
||||
mp_float_t dec_val = 0;
|
||||
bool exp_neg = false;
|
||||
MP_BOOL exp_neg = MP_FALSE;
|
||||
int exp_val = 0;
|
||||
int exp_extra = 0;
|
||||
bool imag = false;
|
||||
MP_BOOL imag = MP_FALSE;
|
||||
for (; *s; s++) {
|
||||
int dig = *s;
|
||||
if ('0' <= dig && dig <= '9') {
|
||||
@ -314,11 +314,11 @@ mp_obj_t rt_load_const_dec(qstr qstr) {
|
||||
s++;
|
||||
} else if (s[1] == '-') {
|
||||
s++;
|
||||
exp_neg = true;
|
||||
exp_neg = MP_TRUE;
|
||||
}
|
||||
} else if (dig == 'J' || dig == 'j') {
|
||||
s++;
|
||||
imag = true;
|
||||
imag = MP_TRUE;
|
||||
break;
|
||||
} else {
|
||||
// unknown character
|
||||
@ -356,11 +356,11 @@ mp_obj_t rt_load_const_str(qstr qstr) {
|
||||
mp_obj_t rt_load_name(qstr qstr) {
|
||||
// logic: search locals, globals, builtins
|
||||
DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
|
||||
mp_map_elem_t *elem = mp_qstr_map_lookup(map_locals, qstr, false);
|
||||
mp_map_elem_t *elem = mp_qstr_map_lookup(map_locals, qstr, MP_FALSE);
|
||||
if (elem == NULL) {
|
||||
elem = mp_qstr_map_lookup(map_globals, qstr, false);
|
||||
elem = mp_qstr_map_lookup(map_globals, qstr, MP_FALSE);
|
||||
if (elem == NULL) {
|
||||
elem = mp_qstr_map_lookup(&map_builtins, qstr, false);
|
||||
elem = mp_qstr_map_lookup(&map_builtins, qstr, MP_FALSE);
|
||||
if (elem == NULL) {
|
||||
nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_NameError, "name '%s' is not defined", qstr_str(qstr)));
|
||||
}
|
||||
@ -372,9 +372,9 @@ mp_obj_t rt_load_name(qstr qstr) {
|
||||
mp_obj_t rt_load_global(qstr qstr) {
|
||||
// logic: search globals, builtins
|
||||
DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
|
||||
mp_map_elem_t *elem = mp_qstr_map_lookup(map_globals, qstr, false);
|
||||
mp_map_elem_t *elem = mp_qstr_map_lookup(map_globals, qstr, MP_FALSE);
|
||||
if (elem == NULL) {
|
||||
elem = mp_qstr_map_lookup(&map_builtins, qstr, false);
|
||||
elem = mp_qstr_map_lookup(&map_builtins, qstr, MP_FALSE);
|
||||
if (elem == NULL) {
|
||||
nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_NameError, "name '%s' is not defined", qstr_str(qstr)));
|
||||
}
|
||||
@ -384,7 +384,7 @@ mp_obj_t rt_load_global(qstr qstr) {
|
||||
|
||||
mp_obj_t rt_load_build_class(void) {
|
||||
DEBUG_OP_printf("load_build_class\n");
|
||||
mp_map_elem_t *elem = mp_qstr_map_lookup(&map_builtins, MP_QSTR___build_class__, false);
|
||||
mp_map_elem_t *elem = mp_qstr_map_lookup(&map_builtins, MP_QSTR___build_class__, MP_FALSE);
|
||||
if (elem == NULL) {
|
||||
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_NameError, "name '__build_class__' is not defined"));
|
||||
}
|
||||
@ -401,12 +401,12 @@ void rt_set_cell(mp_obj_t cell, mp_obj_t val) {
|
||||
|
||||
void rt_store_name(qstr qstr, mp_obj_t obj) {
|
||||
DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
|
||||
mp_qstr_map_lookup(map_locals, qstr, true)->value = obj;
|
||||
mp_qstr_map_lookup(map_locals, qstr, MP_TRUE)->value = obj;
|
||||
}
|
||||
|
||||
void rt_store_global(qstr qstr, mp_obj_t obj) {
|
||||
DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
|
||||
mp_qstr_map_lookup(map_globals, qstr, true)->value = obj;
|
||||
mp_qstr_map_lookup(map_globals, qstr, MP_TRUE)->value = obj;
|
||||
}
|
||||
|
||||
mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
|
||||
@ -750,7 +750,7 @@ mp_obj_t rt_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value) {
|
||||
mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
|
||||
DEBUG_OP_printf("load attr %s\n", qstr_str(attr));
|
||||
if (MP_OBJ_IS_TYPE(base, &class_type)) {
|
||||
mp_map_elem_t *elem = mp_qstr_map_lookup(mp_obj_class_get_locals(base), attr, false);
|
||||
mp_map_elem_t *elem = mp_qstr_map_lookup(mp_obj_class_get_locals(base), attr, MP_FALSE);
|
||||
if (elem == NULL) {
|
||||
// TODO what about generic method lookup?
|
||||
goto no_attr;
|
||||
@ -760,7 +760,7 @@ mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
|
||||
return mp_obj_instance_load_attr(base, attr);
|
||||
} else if (MP_OBJ_IS_TYPE(base, &module_type)) {
|
||||
DEBUG_OP_printf("lookup module map %p\n", mp_obj_module_get_globals(base));
|
||||
mp_map_elem_t *elem = mp_qstr_map_lookup(mp_obj_module_get_globals(base), attr, false);
|
||||
mp_map_elem_t *elem = mp_qstr_map_lookup(mp_obj_module_get_globals(base), attr, MP_FALSE);
|
||||
if (elem == NULL) {
|
||||
// TODO what about generic method lookup?
|
||||
goto no_attr;
|
||||
@ -769,10 +769,12 @@ mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
|
||||
} else if (MP_OBJ_IS_OBJ(base)) {
|
||||
// generic method lookup
|
||||
mp_obj_base_t *o = base;
|
||||
const mp_method_t *meth = &o->type->methods[0];
|
||||
for (; meth->name != NULL; meth++) {
|
||||
if (strcmp(meth->name, qstr_str(attr)) == 0) {
|
||||
return mp_obj_new_bound_meth(base, (mp_obj_t)meth->fun);
|
||||
const mp_method_t *meth = o->type->methods;
|
||||
if (meth != NULL) {
|
||||
for (; meth->name != NULL; meth++) {
|
||||
if (strcmp(meth->name, qstr_str(attr)) == 0) {
|
||||
return mp_obj_new_bound_meth(base, (mp_obj_t)meth->fun);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -793,12 +795,14 @@ void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
|
||||
} else if (MP_OBJ_IS_OBJ(base)) {
|
||||
// generic method lookup
|
||||
mp_obj_base_t *o = base;
|
||||
const mp_method_t *meth = &o->type->methods[0];
|
||||
for (; meth->name != NULL; meth++) {
|
||||
if (strcmp(meth->name, qstr_str(attr)) == 0) {
|
||||
dest[1] = (mp_obj_t)meth->fun;
|
||||
dest[0] = base;
|
||||
return;
|
||||
const mp_method_t *meth = o->type->methods;
|
||||
if (meth != NULL) {
|
||||
for (; meth->name != NULL; meth++) {
|
||||
if (strcmp(meth->name, qstr_str(attr)) == 0) {
|
||||
dest[1] = (mp_obj_t)meth->fun;
|
||||
dest[0] = base;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -813,13 +817,13 @@ void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
|
||||
if (MP_OBJ_IS_TYPE(base, &class_type)) {
|
||||
// TODO CPython allows STORE_ATTR to a class, but is this the correct implementation?
|
||||
mp_map_t *locals = mp_obj_class_get_locals(base);
|
||||
mp_qstr_map_lookup(locals, attr, true)->value = value;
|
||||
mp_qstr_map_lookup(locals, attr, MP_TRUE)->value = value;
|
||||
} else if (MP_OBJ_IS_TYPE(base, &instance_type)) {
|
||||
mp_obj_instance_store_attr(base, attr, value);
|
||||
} else if (MP_OBJ_IS_TYPE(base, &module_type)) {
|
||||
// TODO CPython allows STORE_ATTR to a module, but is this the correct implementation?
|
||||
mp_map_t *globals = mp_obj_module_get_globals(base);
|
||||
mp_qstr_map_lookup(globals, attr, true)->value = value;
|
||||
mp_qstr_map_lookup(globals, attr, MP_TRUE)->value = value;
|
||||
} else {
|
||||
nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
|
||||
}
|
||||
|
@ -82,6 +82,6 @@ extern void *const rt_fun_table[RT_F_NUMBER_OF];
|
||||
void rt_init(void);
|
||||
void rt_deinit(void);
|
||||
int rt_get_unique_code_id(void);
|
||||
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, MP_BOOL is_generator);
|
||||
void rt_assign_native_code(int unique_code_id, void *f, uint len, int n_args);
|
||||
void rt_assign_inline_asm_code(int unique_code_id, void *f, uint len, int n_args);
|
||||
|
14
py/scope.c
14
py/scope.c
@ -58,10 +58,10 @@ scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, uint unique_code_id, u
|
||||
return scope;
|
||||
}
|
||||
|
||||
id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added) {
|
||||
id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, MP_BOOL *added) {
|
||||
for (int i = 0; i < scope->id_info_len; i++) {
|
||||
if (scope->id_info[i].qstr == qstr) {
|
||||
*added = false;
|
||||
*added = MP_FALSE;
|
||||
return &scope->id_info[i];
|
||||
}
|
||||
}
|
||||
@ -100,11 +100,11 @@ id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added) {
|
||||
id_info = &scope->id_info[scope->id_info_len++];
|
||||
}
|
||||
|
||||
id_info->param = false;
|
||||
id_info->param = MP_FALSE;
|
||||
id_info->kind = 0;
|
||||
id_info->qstr = qstr;
|
||||
id_info->local_num = 0;
|
||||
*added = true;
|
||||
*added = MP_TRUE;
|
||||
return id_info;
|
||||
}
|
||||
|
||||
@ -155,7 +155,7 @@ void scope_close_over_in_parents(scope_t *scope, qstr qstr) {
|
||||
}
|
||||
if (id == NULL) {
|
||||
// variable not declared in this scope, so declare it as free and keep searching parents
|
||||
bool added;
|
||||
MP_BOOL added;
|
||||
id = scope_find_or_add_id(s, qstr, &added);
|
||||
assert(added);
|
||||
id->kind = ID_INFO_KIND_FREE;
|
||||
@ -178,7 +178,7 @@ void scope_declare_global(scope_t *scope, qstr qstr) {
|
||||
printf("SyntaxError?: can't declare global in outer code\n");
|
||||
return;
|
||||
}
|
||||
bool added;
|
||||
MP_BOOL added;
|
||||
id_info_t *id_info = scope_find_or_add_id(scope, qstr, &added);
|
||||
if (!added) {
|
||||
printf("SyntaxError?: identifier already declared something\n");
|
||||
@ -198,7 +198,7 @@ void scope_declare_nonlocal(scope_t *scope, qstr qstr) {
|
||||
printf("SyntaxError?: can't declare nonlocal in outer code\n");
|
||||
return;
|
||||
}
|
||||
bool added;
|
||||
MP_BOOL added;
|
||||
id_info_t *id_info = scope_find_or_add_id(scope, qstr, &added);
|
||||
if (!added) {
|
||||
printf("SyntaxError?: identifier already declared something\n");
|
||||
|
@ -8,7 +8,7 @@ enum {
|
||||
|
||||
typedef struct _id_info_t {
|
||||
// TODO compress this info to make structure smaller in memory
|
||||
bool param;
|
||||
MP_BOOL param;
|
||||
int kind;
|
||||
qstr qstr;
|
||||
|
||||
@ -55,7 +55,7 @@ typedef struct _scope_t {
|
||||
} scope_t;
|
||||
|
||||
scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, uint unique_code_id, uint emit_options);
|
||||
id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added);
|
||||
id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, MP_BOOL *added);
|
||||
id_info_t *scope_find(scope_t *scope, qstr qstr);
|
||||
id_info_t *scope_find_global(scope_t *scope, qstr qstr);
|
||||
id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qstr);
|
||||
|
14
py/unicode.c
14
py/unicode.c
@ -46,32 +46,32 @@ char *utf8_next_char(const char *s) {
|
||||
return (char*)(s + 1);
|
||||
}
|
||||
|
||||
bool unichar_isspace(unichar c) {
|
||||
MP_BOOL unichar_isspace(unichar c) {
|
||||
return c < 128 && (attr[c] & FL_SPACE) != 0;
|
||||
}
|
||||
|
||||
bool unichar_isalpha(unichar c) {
|
||||
MP_BOOL unichar_isalpha(unichar c) {
|
||||
return c < 128 && (attr[c] & FL_ALPHA) != 0;
|
||||
}
|
||||
|
||||
bool unichar_isprint(unichar c) {
|
||||
MP_BOOL unichar_isprint(unichar c) {
|
||||
return c < 128 && (attr[c] & FL_PRINT) != 0;
|
||||
}
|
||||
|
||||
bool unichar_isdigit(unichar c) {
|
||||
MP_BOOL unichar_isdigit(unichar c) {
|
||||
return c < 128 && (attr[c] & FL_DIGIT) != 0;
|
||||
}
|
||||
|
||||
/*
|
||||
bool char_is_alpha_or_digit(unichar c) {
|
||||
MP_BOOL char_is_alpha_or_digit(unichar c) {
|
||||
return c < 128 && (attr[c] & (FL_ALPHA | FL_DIGIT)) != 0;
|
||||
}
|
||||
|
||||
bool char_is_upper(unichar c) {
|
||||
MP_BOOL char_is_upper(unichar c) {
|
||||
return c < 128 && (attr[c] & FL_UPPER) != 0;
|
||||
}
|
||||
|
||||
bool char_is_lower(unichar c) {
|
||||
MP_BOOL char_is_lower(unichar c) {
|
||||
return c < 128 && (attr[c] & FL_LOWER) != 0;
|
||||
}
|
||||
*/
|
||||
|
8
py/vm.c
8
py/vm.c
@ -65,7 +65,7 @@ mp_obj_t mp_execute_byte_code(const byte *code, const mp_obj_t *args, uint n_arg
|
||||
|
||||
// fastn has items in normal order
|
||||
// sp points to top of stack which grows down
|
||||
bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **sp_in_out) {
|
||||
MP_BOOL mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **sp_in_out) {
|
||||
// careful: be sure to declare volatile any variables read in the exception handler (written is ok, I think)
|
||||
|
||||
const byte *ip = *ip_in_out;
|
||||
@ -472,7 +472,7 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **
|
||||
nlr_pop();
|
||||
*sp_in_out = sp;
|
||||
assert(exc_sp == &exc_stack[0] - 1);
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
|
||||
case MP_BC_YIELD_VALUE:
|
||||
nlr_pop();
|
||||
@ -481,7 +481,7 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **
|
||||
fastn[1] = fast1;
|
||||
fastn[2] = fast2;
|
||||
*sp_in_out = sp;
|
||||
return true;
|
||||
return MP_TRUE;
|
||||
|
||||
case MP_BC_IMPORT_NAME:
|
||||
DECODE_QSTR;
|
||||
@ -499,7 +499,7 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **
|
||||
printf("code %p, byte code 0x%02x not implemented\n", ip, op);
|
||||
assert(0);
|
||||
nlr_pop();
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
|
18
py/vstr.c
18
py/vstr.c
@ -11,11 +11,11 @@ void vstr_init(vstr_t *vstr) {
|
||||
vstr->len = 0;
|
||||
vstr->buf = m_new(char, vstr->alloc);
|
||||
if (vstr->buf == NULL) {
|
||||
vstr->had_error = true;
|
||||
vstr->had_error = MP_TRUE;
|
||||
return;
|
||||
}
|
||||
vstr->buf[0] = 0;
|
||||
vstr->had_error = false;
|
||||
vstr->had_error = MP_FALSE;
|
||||
}
|
||||
|
||||
void vstr_clear(vstr_t *vstr) {
|
||||
@ -42,10 +42,10 @@ void vstr_free(vstr_t *vstr) {
|
||||
void vstr_reset(vstr_t *vstr) {
|
||||
vstr->len = 0;
|
||||
vstr->buf[0] = 0;
|
||||
vstr->had_error = false;
|
||||
vstr->had_error = MP_FALSE;
|
||||
}
|
||||
|
||||
bool vstr_had_error(vstr_t *vstr) {
|
||||
MP_BOOL vstr_had_error(vstr_t *vstr) {
|
||||
return vstr->had_error;
|
||||
}
|
||||
|
||||
@ -63,23 +63,23 @@ int vstr_len(vstr_t *vstr) {
|
||||
return vstr->len;
|
||||
}
|
||||
|
||||
bool vstr_ensure_extra(vstr_t *vstr, int size) {
|
||||
MP_BOOL vstr_ensure_extra(vstr_t *vstr, int size) {
|
||||
if (vstr->len + size + 1 > vstr->alloc) {
|
||||
int new_alloc = ROUND_ALLOC((vstr->len + size + 1) * 2);
|
||||
char *new_buf = m_renew(char, vstr->buf, vstr->alloc, new_alloc);
|
||||
if (new_buf == NULL) {
|
||||
vstr->had_error = true;
|
||||
return false;
|
||||
vstr->had_error = MP_TRUE;
|
||||
return MP_FALSE;
|
||||
}
|
||||
vstr->alloc = new_alloc;
|
||||
vstr->buf = new_buf;
|
||||
}
|
||||
return true;
|
||||
return MP_TRUE;
|
||||
}
|
||||
|
||||
void vstr_hint_size(vstr_t *vstr, int size) {
|
||||
// it's not an error if we fail to allocate for the size hint
|
||||
bool er = vstr->had_error;
|
||||
MP_BOOL er = vstr->had_error;
|
||||
vstr_ensure_extra(vstr, size);
|
||||
vstr->had_error = er;
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ int sample_buf_in;
|
||||
int sample_buf_out;
|
||||
byte sample_buf[SAMPLE_BUF_SIZE];
|
||||
|
||||
bool audio_is_full(void) {
|
||||
MP_BOOL audio_is_full(void) {
|
||||
return ((sample_buf_in + 1) % SAMPLE_BUF_SIZE) == sample_buf_out;
|
||||
}
|
||||
|
||||
|
@ -165,11 +165,11 @@ void pyb_cc3000_spi_init(void) {
|
||||
|
||||
/*
|
||||
// WLAN CS, EN and WALN IRQ Configuration
|
||||
jshSetPinStateIsManual(WLAN_CS_PIN, false);
|
||||
jshSetPinStateIsManual(WLAN_CS_PIN, MP_FALSE);
|
||||
jshPinOutput(WLAN_CS_PIN, 1); // de-assert CS
|
||||
jshSetPinStateIsManual(WLAN_EN_PIN, false);
|
||||
jshSetPinStateIsManual(WLAN_EN_PIN, MP_FALSE);
|
||||
jshPinOutput(WLAN_EN_PIN, 0); // disable WLAN
|
||||
jshSetPinStateIsManual(WLAN_IRQ_PIN, true);
|
||||
jshSetPinStateIsManual(WLAN_IRQ_PIN, MP_TRUE);
|
||||
jshPinSetState(WLAN_IRQ_PIN, JSHPINSTATE_GPIO_IN_PULLUP); // flip into read mode with pullup
|
||||
*/
|
||||
// configure wlan CS and EN pins
|
||||
|
83
stm/i2c.c
83
stm/i2c.c
@ -20,9 +20,9 @@ typedef enum {
|
||||
I2C_STATE_READ = 2,
|
||||
} i2c_state_t;
|
||||
|
||||
// set to true if the port has already been initialized
|
||||
bool i2c1_port_initialized = false;
|
||||
bool i2c2_port_initialized = false;
|
||||
// set to MP_TRUE if the port has already been initialized
|
||||
MP_BOOL i2c1_port_initialized = MP_FALSE;
|
||||
MP_BOOL i2c2_port_initialized = MP_FALSE;
|
||||
|
||||
static I2C_TypeDef * _i2c_port_addr(pyb_i2c_t i2c_port) {
|
||||
if (i2c_port == PYB_I2C_1) {
|
||||
@ -37,17 +37,17 @@ static I2C_TypeDef * _i2c_port_addr(pyb_i2c_t i2c_port) {
|
||||
// todo - perhaps there should be some global resource management for gpio
|
||||
// this function would fail if the i2c pins have already been defined for
|
||||
// use by another python object
|
||||
// as it is, this always returns true (unless i2c_port is invalid)
|
||||
static bool _i2c_init(pyb_i2c_t i2c_port) {
|
||||
// as it is, this always returns MP_TRUE (unless i2c_port is invalid)
|
||||
static MP_BOOL _i2c_init(pyb_i2c_t i2c_port) {
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
|
||||
I2C_TypeDef *i2c = _i2c_port_addr(i2c_port);
|
||||
if (i2c == NULL)
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
|
||||
if (i2c_port == PYB_I2C_1) {
|
||||
if (i2c1_port_initialized == true) {
|
||||
return true;
|
||||
if (i2c1_port_initialized == MP_TRUE) {
|
||||
return MP_TRUE;
|
||||
}
|
||||
RCC->APB1ENR |= RCC_APB1ENR_I2C1EN; // enable I2C1
|
||||
|
||||
@ -64,12 +64,12 @@ static bool _i2c_init(pyb_i2c_t i2c_port) {
|
||||
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1);
|
||||
|
||||
|
||||
i2c1_port_initialized = true;
|
||||
i2c1_port_initialized = MP_TRUE;
|
||||
}
|
||||
|
||||
if (i2c_port == PYB_I2C_2) {
|
||||
if (i2c2_port_initialized == true) {
|
||||
return true;
|
||||
if (i2c2_port_initialized == MP_TRUE) {
|
||||
return MP_TRUE;
|
||||
}
|
||||
RCC->APB1ENR |= RCC_APB1ENR_I2C2EN; // enable I2C2
|
||||
|
||||
@ -85,7 +85,7 @@ static bool _i2c_init(pyb_i2c_t i2c_port) {
|
||||
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_I2C2);
|
||||
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_I2C2);
|
||||
|
||||
i2c2_port_initialized = true;
|
||||
i2c2_port_initialized = MP_TRUE;
|
||||
}
|
||||
|
||||
// get clock speeds
|
||||
@ -108,7 +108,7 @@ static bool _i2c_init(pyb_i2c_t i2c_port) {
|
||||
// enable the I2C peripheral
|
||||
i2c->CR1 |= I2C_CR1_PE;
|
||||
|
||||
return true;
|
||||
return MP_TRUE;
|
||||
}
|
||||
|
||||
static uint32_t _i2c_get_sr(pyb_i2c_t i2c_port) {
|
||||
@ -121,9 +121,9 @@ static uint32_t _i2c_get_sr(pyb_i2c_t i2c_port) {
|
||||
return (sr2 << 16) | sr1;
|
||||
}
|
||||
|
||||
static bool _i2c_restart(pyb_i2c_t i2c_port, uint8_t addr, int write) {
|
||||
static MP_BOOL _i2c_restart(pyb_i2c_t i2c_port, uint8_t addr, int write) {
|
||||
I2C_TypeDef *i2c = _i2c_port_addr(i2c_port);
|
||||
if (i2c == NULL) return false;
|
||||
if (i2c == NULL) return MP_FALSE;
|
||||
|
||||
// send start condition
|
||||
i2c->CR1 |= I2C_CR1_START;
|
||||
@ -133,7 +133,7 @@ static bool _i2c_restart(pyb_i2c_t i2c_port, uint8_t addr, int write) {
|
||||
while ((_i2c_get_sr(i2c_port) & 0x00030001) != 0x00030001) {
|
||||
if (--timeout == 0) {
|
||||
//printf("timeout in _i2c_restart\n");
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
@ -145,7 +145,7 @@ static bool _i2c_restart(pyb_i2c_t i2c_port, uint8_t addr, int write) {
|
||||
while ((_i2c_get_sr(i2c_port) & 0x00070082) != 0x00070082) {
|
||||
if (--timeout == 0) {
|
||||
//printf("timeout in _i2c_restart write\n");
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@ -156,16 +156,16 @@ static bool _i2c_restart(pyb_i2c_t i2c_port, uint8_t addr, int write) {
|
||||
while ((_i2c_get_sr(i2c_port) & 0x00030002) != 0x00030002) {
|
||||
if (--timeout == 0) {
|
||||
//printf("timeout in _i2c_restart read\n");
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return MP_TRUE;
|
||||
}
|
||||
|
||||
static bool _i2c_send_byte(pyb_i2c_t i2c_port, uint8_t data) {
|
||||
static MP_BOOL _i2c_send_byte(pyb_i2c_t i2c_port, uint8_t data) {
|
||||
I2C_TypeDef *i2c = _i2c_port_addr(i2c_port);
|
||||
if (i2c == NULL) return false;
|
||||
if (i2c == NULL) return MP_FALSE;
|
||||
|
||||
// send byte
|
||||
i2c->DR = data;
|
||||
@ -174,10 +174,10 @@ static bool _i2c_send_byte(pyb_i2c_t i2c_port, uint8_t data) {
|
||||
while ((_i2c_get_sr(i2c_port) & 0x00070084) != 0x00070084) {
|
||||
if (--timeout == 0) {
|
||||
//printf("timeout in _i2c_send_byte\n");
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return MP_TRUE;
|
||||
}
|
||||
|
||||
static uint8_t _i2c_read_ack(pyb_i2c_t i2c_port) {
|
||||
@ -220,18 +220,18 @@ static uint8_t _i2c_read_nack(pyb_i2c_t i2c_port) {
|
||||
return data;
|
||||
}
|
||||
|
||||
static bool _i2c_start(pyb_i2c_t i2c_port) {
|
||||
static MP_BOOL _i2c_start(pyb_i2c_t i2c_port) {
|
||||
I2C_TypeDef *i2c = _i2c_port_addr(i2c_port);
|
||||
if (i2c == NULL) return false;
|
||||
if (i2c == NULL) return MP_FALSE;
|
||||
|
||||
// wait until I2C is not busy
|
||||
uint32_t timeout = 1000000;
|
||||
while (i2c->SR2 & I2C_SR2_BUSY) {
|
||||
if (--timeout == 0) {
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return MP_TRUE;
|
||||
}
|
||||
|
||||
static void _i2c_stop(pyb_i2c_t i2c_port) {
|
||||
@ -264,7 +264,7 @@ mp_obj_t i2c_obj_start(mp_obj_t self_in) {
|
||||
_i2c_stop(self->i2c_port);
|
||||
self->i2c_state = I2C_STATE_IDLE;
|
||||
}
|
||||
if (_i2c_start(self->i2c_port) == true)
|
||||
if (_i2c_start(self->i2c_port) == MP_TRUE)
|
||||
return mp_const_true;
|
||||
return mp_const_false;
|
||||
}
|
||||
@ -272,7 +272,7 @@ mp_obj_t i2c_obj_start(mp_obj_t self_in) {
|
||||
mp_obj_t i2c_obj_write(mp_obj_t self_in, mp_obj_t data_in) {
|
||||
pyb_i2c_obj_t *self = self_in;
|
||||
if (self->i2c_state != I2C_STATE_WRITE) {
|
||||
if (_i2c_restart(self->i2c_port, self->i2c_addr, 1) == false) {
|
||||
if (_i2c_restart(self->i2c_port, self->i2c_addr, 1) == MP_FALSE) {
|
||||
_i2c_stop(self->i2c_port);
|
||||
self->i2c_state = I2C_STATE_IDLE;
|
||||
return mp_const_false;
|
||||
@ -280,7 +280,7 @@ mp_obj_t i2c_obj_write(mp_obj_t self_in, mp_obj_t data_in) {
|
||||
self->i2c_state = I2C_STATE_WRITE;
|
||||
}
|
||||
uint8_t data = mp_obj_get_int(data_in);
|
||||
if (_i2c_send_byte(self->i2c_port, data) == false)
|
||||
if (_i2c_send_byte(self->i2c_port, data) == MP_FALSE)
|
||||
return mp_const_false;
|
||||
return mp_const_true;
|
||||
}
|
||||
@ -288,7 +288,7 @@ mp_obj_t i2c_obj_write(mp_obj_t self_in, mp_obj_t data_in) {
|
||||
mp_obj_t i2c_obj_read(mp_obj_t self_in) {
|
||||
pyb_i2c_obj_t *self = self_in;
|
||||
if (self->i2c_state != I2C_STATE_READ) {
|
||||
if (_i2c_restart(self->i2c_port, self->i2c_addr, 0) == false) {
|
||||
if (_i2c_restart(self->i2c_port, self->i2c_addr, 0) == MP_FALSE) {
|
||||
_i2c_stop(self->i2c_port);
|
||||
self->i2c_state = I2C_STATE_IDLE;
|
||||
return mp_const_false;
|
||||
@ -302,7 +302,7 @@ mp_obj_t i2c_obj_read(mp_obj_t self_in) {
|
||||
mp_obj_t i2c_obj_readAndStop(mp_obj_t self_in) {
|
||||
pyb_i2c_obj_t *self = self_in;
|
||||
if (self->i2c_state != I2C_STATE_READ) {
|
||||
if (_i2c_restart(self->i2c_port, self->i2c_addr, 0) == false) {
|
||||
if (_i2c_restart(self->i2c_port, self->i2c_addr, 0) == MP_FALSE) {
|
||||
_i2c_stop(self->i2c_port);
|
||||
self->i2c_state = I2C_STATE_IDLE;
|
||||
return mp_const_false;
|
||||
@ -326,18 +326,19 @@ 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_stop_obj, i2c_obj_stop);
|
||||
|
||||
static const mp_method_t i2c_obj_type_type_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 = {
|
||||
{ &mp_const_type },
|
||||
"I2C",
|
||||
.print = i2c_obj_print,
|
||||
.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 },
|
||||
}
|
||||
.methods = i2c_obj_type_type_methods,
|
||||
};
|
||||
|
||||
// create the I2C object
|
||||
@ -350,7 +351,7 @@ mp_obj_t pyb_I2C(mp_obj_t i2c_id, mp_obj_t i2c_addr) {
|
||||
case 1: i2c_port = PYB_I2C_2; break;
|
||||
default: return mp_const_none;
|
||||
}
|
||||
if (_i2c_init(i2c_port) == false) {
|
||||
if (_i2c_init(i2c_port) == MP_FALSE) {
|
||||
return mp_const_none;
|
||||
}
|
||||
pyb_i2c_obj_t *o = m_new_obj(pyb_i2c_obj_t);
|
||||
|
11
stm/led.c
11
stm/led.c
@ -176,15 +176,16 @@ 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_off_obj, led_obj_off);
|
||||
|
||||
static const mp_method_t led_obj_type_methods[] = {
|
||||
{ "on", &led_obj_on_obj },
|
||||
{ "off", &led_obj_off_obj },
|
||||
{ NULL, NULL },
|
||||
};
|
||||
static const mp_obj_type_t led_obj_type = {
|
||||
{ &mp_const_type },
|
||||
"Led",
|
||||
.print = led_obj_print,
|
||||
.methods = {
|
||||
{ "on", &led_obj_on_obj },
|
||||
{ "off", &led_obj_off_obj },
|
||||
{ NULL, NULL },
|
||||
}
|
||||
.methods = led_obj_type_methods,
|
||||
};
|
||||
|
||||
mp_obj_t pyb_Led(mp_obj_t led_id) {
|
||||
|
@ -21,7 +21,7 @@ void str_buf_free(mp_lexer_str_buf_t *sb) {
|
||||
}
|
||||
}
|
||||
|
||||
mp_lexer_t *mp_lexer_new_from_str_len(const char *src_name, const char *str, uint len, bool free_str, mp_lexer_str_buf_t *sb) {
|
||||
mp_lexer_t *mp_lexer_new_from_str_len(const char *src_name, const char *str, uint len, MP_BOOL free_str, mp_lexer_str_buf_t *sb) {
|
||||
sb->free = free_str;
|
||||
sb->src_beg = str;
|
||||
sb->src_cur = str;
|
||||
|
@ -1,5 +1,5 @@
|
||||
typedef struct _py_lexer_str_buf_t {
|
||||
bool free; // free src_beg when done
|
||||
MP_BOOL free; // free src_beg when done
|
||||
const char *src_beg; // beginning of source
|
||||
const char *src_cur; // current location in source
|
||||
const char *src_end; // end (exclusive) of source
|
||||
@ -12,5 +12,5 @@ typedef struct _py_lexer_file_buf_t {
|
||||
uint16_t pos;
|
||||
} mp_lexer_file_buf_t;
|
||||
|
||||
mp_lexer_t *mp_lexer_new_from_str_len(const char *src_name, const char *str, uint len, bool free_str, mp_lexer_str_buf_t *sb);
|
||||
mp_lexer_t *mp_lexer_new_from_str_len(const char *src_name, const char *str, uint len, MP_BOOL free_str, mp_lexer_str_buf_t *sb);
|
||||
mp_lexer_t *mp_lexer_new_from_file(const char *filename, mp_lexer_file_buf_t *fb);
|
||||
|
47
stm/main.c
47
stm/main.c
@ -359,7 +359,7 @@ int readline(vstr_t *line, const char *prompt) {
|
||||
readline_hist[0] = strdup(vstr_str(line));
|
||||
return 1;
|
||||
} else if (c == 27) {
|
||||
escape = true;
|
||||
escape = MP_TRUE;
|
||||
} else if (c == 127) {
|
||||
if (vstr_len(line) > len) {
|
||||
vstr_cut_tail(line, 1);
|
||||
@ -432,12 +432,12 @@ void do_repl(void) {
|
||||
}
|
||||
|
||||
mp_lexer_str_buf_t sb;
|
||||
mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", vstr_str(&line), vstr_len(&line), false, &sb);
|
||||
mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", vstr_str(&line), vstr_len(&line), MP_FALSE, &sb);
|
||||
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT);
|
||||
mp_lexer_free(lex);
|
||||
|
||||
if (pn != MP_PARSE_NODE_NULL) {
|
||||
mp_obj_t module_fun = mp_compile(pn, true);
|
||||
mp_obj_t module_fun = mp_compile(pn, MP_TRUE);
|
||||
if (module_fun != mp_const_none) {
|
||||
nlr_buf_t nlr;
|
||||
uint32_t start = sys_tick_counter;
|
||||
@ -461,37 +461,37 @@ void do_repl(void) {
|
||||
stdout_tx_str("\r\n");
|
||||
}
|
||||
|
||||
bool do_file(const char *filename) {
|
||||
MP_BOOL do_file(const char *filename) {
|
||||
mp_lexer_file_buf_t fb;
|
||||
mp_lexer_t *lex = mp_lexer_new_from_file(filename, &fb);
|
||||
|
||||
if (lex == NULL) {
|
||||
printf("could not open file '%s' for reading\n", filename);
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
|
||||
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT);
|
||||
mp_lexer_free(lex);
|
||||
|
||||
if (pn == MP_PARSE_NODE_NULL) {
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
|
||||
mp_obj_t module_fun = mp_compile(pn, false);
|
||||
mp_obj_t module_fun = mp_compile(pn, MP_FALSE);
|
||||
if (module_fun == mp_const_none) {
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
|
||||
nlr_buf_t nlr;
|
||||
if (nlr_push(&nlr) == 0) {
|
||||
rt_call_function_0(module_fun);
|
||||
nlr_pop();
|
||||
return true;
|
||||
return MP_TRUE;
|
||||
} else {
|
||||
// uncaught exception
|
||||
mp_obj_print((mp_obj_t)nlr.ret_val);
|
||||
printf("\n");
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
@ -689,6 +689,12 @@ 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
|
||||
|
||||
static const mp_method_t file_obj_type_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 = {
|
||||
{ &mp_const_type },
|
||||
"File",
|
||||
@ -699,12 +705,7 @@ static const mp_obj_type_t file_obj_type = {
|
||||
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},
|
||||
}
|
||||
.methods = file_obj_type_methods,
|
||||
};
|
||||
|
||||
mp_obj_t pyb_io_open(mp_obj_t o_filename, mp_obj_t o_mode) {
|
||||
@ -778,7 +779,7 @@ int main(void) {
|
||||
|
||||
//usart_init(); disabled while wi-fi is enabled
|
||||
|
||||
int first_soft_reset = true;
|
||||
int first_soft_reset = MP_TRUE;
|
||||
|
||||
soft_reset:
|
||||
|
||||
@ -847,12 +848,12 @@ soft_reset:
|
||||
lcd_print_str(" micro py board\n");
|
||||
|
||||
// check if user switch held (initiates reset of filesystem)
|
||||
bool reset_filesystem = false;
|
||||
MP_BOOL reset_filesystem = MP_FALSE;
|
||||
if (switch_get()) {
|
||||
reset_filesystem = true;
|
||||
reset_filesystem = MP_TRUE;
|
||||
for (int i = 0; i < 50; i++) {
|
||||
if (!switch_get()) {
|
||||
reset_filesystem = false;
|
||||
reset_filesystem = MP_FALSE;
|
||||
break;
|
||||
}
|
||||
sys_tick_delay_ms(10);
|
||||
@ -1063,7 +1064,7 @@ soft_reset:
|
||||
"f()\n";
|
||||
|
||||
mp_lexer_str_buf_t mp_lexer_str_buf;
|
||||
mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", pysrc, strlen(pysrc), false, &mp_lexer_str_buf);
|
||||
mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", pysrc, strlen(pysrc), MP_FALSE, &mp_lexer_str_buf);
|
||||
|
||||
// nalloc=1740;6340;6836 -> 140;4600;496 bytes for lexer, parser, compiler
|
||||
printf("lex; al=%u\n", m_get_total_bytes_allocated());
|
||||
@ -1074,7 +1075,7 @@ soft_reset:
|
||||
printf("pars;al=%u\n", m_get_total_bytes_allocated());
|
||||
sys_tick_delay_ms(1000);
|
||||
//parse_node_show(pn, 0);
|
||||
mp_obj_t module_fun = mp_compile(pn, false);
|
||||
mp_obj_t module_fun = mp_compile(pn, MP_FALSE);
|
||||
printf("comp;al=%u\n", m_get_total_bytes_allocated());
|
||||
sys_tick_delay_ms(1000);
|
||||
|
||||
@ -1171,7 +1172,7 @@ soft_reset:
|
||||
|
||||
printf("PYB: soft reboot\n");
|
||||
|
||||
first_soft_reset = false;
|
||||
first_soft_reset = MP_FALSE;
|
||||
goto soft_reset;
|
||||
}
|
||||
|
||||
|
10
stm/printf.c
10
stm/printf.c
@ -152,10 +152,10 @@ int pfenv_printf(const pfenv_t *pfenv, const char *fmt, va_list args) {
|
||||
}
|
||||
|
||||
// parse long specifiers (current not used)
|
||||
//bool long_arg = false;
|
||||
//MP_BOOL long_arg = MP_FALSE;
|
||||
if (*fmt == 'l') {
|
||||
++fmt;
|
||||
//long_arg = true;
|
||||
//long_arg = MP_TRUE;
|
||||
}
|
||||
|
||||
if (*fmt == '\0') {
|
||||
@ -215,14 +215,14 @@ int pfenv_printf(const pfenv_t *pfenv, const char *fmt, va_list args) {
|
||||
|
||||
void stdout_print_strn(void *data, const char *str, unsigned int len) {
|
||||
// send stdout to USART, USB CDC VCP, and LCD if nothing else
|
||||
bool any = false;
|
||||
MP_BOOL any = MP_FALSE;
|
||||
if (usart_is_enabled()) {
|
||||
usart_tx_strn_cooked(str, len);
|
||||
any = true;
|
||||
any = MP_TRUE;
|
||||
}
|
||||
if (usb_vcp_is_enabled()) {
|
||||
usb_vcp_send_strn_cooked(str, len);
|
||||
any = true;
|
||||
any = MP_TRUE;
|
||||
}
|
||||
if (!any) {
|
||||
lcd_print_strn(str, len);
|
||||
|
@ -336,7 +336,7 @@ void CC3000_UsynchCallback(long lEventType, char * data, unsigned char length)
|
||||
socketnum = data[0];
|
||||
//PRINT_F("TCP Close wait #"); printDec(socketnum);
|
||||
if (socketnum < MAX_SOCKETS)
|
||||
closed_sockets[socketnum] = true;
|
||||
closed_sockets[socketnum] = MP_TRUE;
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
@ -137,14 +137,15 @@ 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 const mp_method_t servo_obj_type_methods[] = {
|
||||
{ "angle", &servo_obj_angle_obj },
|
||||
{ NULL, NULL },
|
||||
};
|
||||
static const mp_obj_type_t servo_obj_type = {
|
||||
{ &mp_const_type },
|
||||
"Servo",
|
||||
.print = servo_obj_print,
|
||||
.methods = {
|
||||
{ "angle", &servo_obj_angle_obj },
|
||||
{ NULL, NULL },
|
||||
}
|
||||
.methods = servo_obj_type_methods,
|
||||
};
|
||||
|
||||
mp_obj_t pyb_Servo(mp_obj_t servo_id) {
|
||||
|
@ -15,18 +15,18 @@
|
||||
#define FLASH_PART1_NUM_BLOCKS (224) // 16k+16k+16k+64k=112k
|
||||
#define FLASH_MEM_START_ADDR (0x08004000) // sector 1, 16k
|
||||
|
||||
static bool is_initialised = false;
|
||||
static MP_BOOL is_initialised = MP_FALSE;
|
||||
static uint32_t cache_flash_sector_id;
|
||||
static uint32_t cache_flash_sector_start;
|
||||
static uint32_t cache_flash_sector_size;
|
||||
static bool cache_dirty;
|
||||
static MP_BOOL cache_dirty;
|
||||
static uint32_t sys_tick_counter_last_write;
|
||||
|
||||
static void cache_flush(void) {
|
||||
if (cache_dirty) {
|
||||
// sync the cache RAM buffer by writing it to the flash page
|
||||
flash_write(cache_flash_sector_start, (const uint32_t*)CACHE_MEM_START_ADDR, cache_flash_sector_size / 4);
|
||||
cache_dirty = false;
|
||||
cache_dirty = MP_FALSE;
|
||||
// indicate a clean cache with LED off
|
||||
led_state(PYB_LED_R1, 0);
|
||||
}
|
||||
@ -43,7 +43,7 @@ static uint8_t *cache_get_addr_for_write(uint32_t flash_addr) {
|
||||
cache_flash_sector_start = flash_sector_start;
|
||||
cache_flash_sector_size = flash_sector_size;
|
||||
}
|
||||
cache_dirty = true;
|
||||
cache_dirty = MP_TRUE;
|
||||
// indicate a dirty cache with LED on
|
||||
led_state(PYB_LED_R1, 1);
|
||||
return (uint8_t*)CACHE_MEM_START_ADDR + flash_addr - flash_sector_start;
|
||||
@ -64,8 +64,8 @@ static uint8_t *cache_get_addr_for_read(uint32_t flash_addr) {
|
||||
void storage_init(void) {
|
||||
if (!is_initialised) {
|
||||
cache_flash_sector_id = 0;
|
||||
cache_dirty = false;
|
||||
is_initialised = true;
|
||||
cache_dirty = MP_FALSE;
|
||||
is_initialised = MP_TRUE;
|
||||
sys_tick_counter_last_write = 0;
|
||||
}
|
||||
}
|
||||
@ -78,7 +78,7 @@ uint32_t storage_get_block_count(void) {
|
||||
return FLASH_PART1_START_BLOCK + FLASH_PART1_NUM_BLOCKS;
|
||||
}
|
||||
|
||||
bool storage_needs_flush(void) {
|
||||
MP_BOOL storage_needs_flush(void) {
|
||||
// wait 2 seconds after last write to flush
|
||||
return cache_dirty && sys_tick_has_passed(sys_tick_counter_last_write, 2000);
|
||||
}
|
||||
@ -123,7 +123,7 @@ static void build_partition(uint8_t *buf, int boot, int type, uint32_t start_blo
|
||||
buf[15] = num_blocks >> 24;
|
||||
}
|
||||
|
||||
bool storage_read_block(uint8_t *dest, uint32_t block) {
|
||||
MP_BOOL storage_read_block(uint8_t *dest, uint32_t block) {
|
||||
//printf("RD %u\n", block);
|
||||
if (block == 0) {
|
||||
// fake the MBR so we can decide on our own partition table
|
||||
@ -140,26 +140,26 @@ bool storage_read_block(uint8_t *dest, uint32_t block) {
|
||||
dest[510] = 0x55;
|
||||
dest[511] = 0xaa;
|
||||
|
||||
return true;
|
||||
return MP_TRUE;
|
||||
|
||||
} else if (FLASH_PART1_START_BLOCK <= block && block < FLASH_PART1_START_BLOCK + FLASH_PART1_NUM_BLOCKS) {
|
||||
// non-MBR block, get data from flash memory, possibly via cache
|
||||
uint32_t flash_addr = FLASH_MEM_START_ADDR + (block - FLASH_PART1_START_BLOCK) * BLOCK_SIZE;
|
||||
uint8_t *src = cache_get_addr_for_read(flash_addr);
|
||||
memcpy(dest, src, BLOCK_SIZE);
|
||||
return true;
|
||||
return MP_TRUE;
|
||||
|
||||
} else {
|
||||
// bad block number
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
bool storage_write_block(const uint8_t *src, uint32_t block) {
|
||||
MP_BOOL storage_write_block(const uint8_t *src, uint32_t block) {
|
||||
//printf("WR %u\n", block);
|
||||
if (block == 0) {
|
||||
// can't write MBR, but pretend we did
|
||||
return true;
|
||||
return MP_TRUE;
|
||||
|
||||
} else if (FLASH_PART1_START_BLOCK <= block && block < FLASH_PART1_START_BLOCK + FLASH_PART1_NUM_BLOCKS) {
|
||||
// non-MBR block, copy to cache
|
||||
@ -167,10 +167,10 @@ bool storage_write_block(const uint8_t *src, uint32_t block) {
|
||||
uint8_t *dest = cache_get_addr_for_write(flash_addr);
|
||||
memcpy(dest, src, BLOCK_SIZE);
|
||||
sys_tick_counter_last_write = sys_tick_counter;
|
||||
return true;
|
||||
return MP_TRUE;
|
||||
|
||||
} else {
|
||||
// bad block number
|
||||
return false;
|
||||
return MP_FALSE;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
void storage_init(void);
|
||||
uint32_t storage_get_block_size(void);
|
||||
uint32_t storage_get_block_count(void);
|
||||
bool storage_needs_flush(void);
|
||||
MP_BOOL storage_needs_flush(void);
|
||||
void storage_flush(void);
|
||||
bool storage_read_block(uint8_t *dest, uint32_t block);
|
||||
bool storage_write_block(const uint8_t *src, uint32_t block);
|
||||
MP_BOOL storage_read_block(uint8_t *dest, uint32_t block);
|
||||
MP_BOOL storage_write_block(const uint8_t *src, uint32_t block);
|
||||
|
@ -43,7 +43,7 @@ void sys_tick_wait_at_least(uint32_t stc, uint32_t delay_ms) {
|
||||
}
|
||||
}
|
||||
|
||||
bool sys_tick_has_passed(uint32_t stc, uint32_t delay_ms) {
|
||||
MP_BOOL sys_tick_has_passed(uint32_t stc, uint32_t delay_ms) {
|
||||
// stc_wait is the value of sys_tick_counter that we wait for
|
||||
uint32_t stc_wait = stc + delay_ms;
|
||||
if (stc_wait < stc) {
|
||||
|
@ -4,4 +4,4 @@ void sys_tick_init(void);
|
||||
void SysTick_Handler(void);
|
||||
void sys_tick_delay_ms(uint32_t delay_ms);
|
||||
void sys_tick_wait_at_least(uint32_t stc, uint32_t delay_ms);
|
||||
bool sys_tick_has_passed(uint32_t stc, uint32_t delay_ms);
|
||||
MP_BOOL sys_tick_has_passed(uint32_t stc, uint32_t delay_ms);
|
||||
|
@ -5,7 +5,7 @@
|
||||
#include "misc.h"
|
||||
#include "usart.h"
|
||||
|
||||
static bool is_enabled;
|
||||
static MP_BOOL is_enabled;
|
||||
|
||||
// USART6 on PC6 (TX), PC7 (RX)
|
||||
void usart_init(void) {
|
||||
@ -33,14 +33,14 @@ void usart_init(void) {
|
||||
|
||||
USART_Cmd(USART6, ENABLE);
|
||||
|
||||
is_enabled = true;
|
||||
is_enabled = MP_TRUE;
|
||||
}
|
||||
|
||||
bool usart_is_enabled(void) {
|
||||
MP_BOOL usart_is_enabled(void) {
|
||||
return is_enabled;
|
||||
}
|
||||
|
||||
bool usart_rx_any(void) {
|
||||
MP_BOOL usart_rx_any(void) {
|
||||
return USART_GetFlagStatus(USART6, USART_FLAG_RXNE) == SET;
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
void usart_init(void);
|
||||
bool usart_is_enabled(void);
|
||||
bool usart_rx_any(void);
|
||||
MP_BOOL usart_is_enabled(void);
|
||||
MP_BOOL usart_rx_any(void);
|
||||
int usart_rx_char(void);
|
||||
void usart_tx_char(int c);
|
||||
void usart_tx_str(const char *str);
|
||||
|
@ -30,7 +30,7 @@ void usb_init(void) {
|
||||
is_enabled = 1;
|
||||
}
|
||||
|
||||
bool usb_vcp_is_enabled(void) {
|
||||
MP_BOOL usb_vcp_is_enabled(void) {
|
||||
return is_enabled;
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
void usb_init(void);
|
||||
bool usb_vcp_is_enabled(void);
|
||||
MP_BOOL usb_vcp_is_enabled(void);
|
||||
int usb_vcp_rx_any(void);
|
||||
char usb_vcp_rx_get(void);
|
||||
void usb_vcp_send_str(const char* str);
|
||||
|
@ -37,7 +37,7 @@ void do_file(const char *file) {
|
||||
//printf("----------------\n");
|
||||
//parse_node_show(pn, 0);
|
||||
//printf("----------------\n");
|
||||
mp_obj_t module_fun = mp_compile(pn, false);
|
||||
mp_obj_t module_fun = mp_compile(pn, MP_FALSE);
|
||||
//printf("----------------\n");
|
||||
|
||||
if (module_fun == mp_const_none) {
|
||||
|
@ -79,13 +79,13 @@ 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), MP_FALSE);
|
||||
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT);
|
||||
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);
|
||||
mp_obj_t module_fun = mp_compile(pn, MP_TRUE);
|
||||
if (module_fun != mp_const_none) {
|
||||
nlr_buf_t nlr;
|
||||
if (nlr_push(&nlr) == 0) {
|
||||
@ -139,7 +139,7 @@ void do_file(const char *file) {
|
||||
//printf("----------------\n");
|
||||
//parse_node_show(pn, 0);
|
||||
//printf("----------------\n");
|
||||
mp_obj_t module_fun = mp_compile(pn, false);
|
||||
mp_obj_t module_fun = mp_compile(pn, MP_FALSE);
|
||||
//printf("----------------\n");
|
||||
|
||||
#if MICROPY_EMIT_CPYTHON
|
||||
@ -166,7 +166,7 @@ void do_file(const char *file) {
|
||||
|
||||
typedef struct _test_obj_t {
|
||||
mp_obj_base_t base;
|
||||
bool value;
|
||||
MP_BOOL value;
|
||||
} test_obj_t;
|
||||
|
||||
static void test_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
||||
|
Loading…
Reference in New Issue
Block a user