summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/builtin.c52
-rw-r--r--py/builtin.h2
-rw-r--r--py/lexer.h2
-rw-r--r--py/lexerstr.c35
-rw-r--r--py/lexerunix.c36
-rw-r--r--py/lexerunix.h1
-rw-r--r--py/obj.c9
-rw-r--r--py/obj.h1
-rw-r--r--py/py.mk1
-rw-r--r--py/runtime.c2
10 files changed, 94 insertions, 47 deletions
diff --git a/py/builtin.c b/py/builtin.c
index 6babc7669..4ee5c57c6 100644
--- a/py/builtin.c
+++ b/py/builtin.c
@@ -15,22 +15,59 @@
#include "map.h"
#include "builtin.h"
-mp_obj_t mp_builtin___build_class__(mp_obj_t o_class_fun, mp_obj_t o_class_name) {
+// args[0] is function from class body
+// args[1] is class name
+// args[2:] are base objects
+mp_obj_t mp_builtin___build_class__(int n_args, const mp_obj_t *args) {
+ assert(2 <= n_args);
+
// we differ from CPython: we set the new __locals__ object here
mp_map_t *old_locals = rt_locals_get();
mp_map_t *class_locals = mp_map_new(MP_MAP_QSTR, 0);
rt_locals_set(class_locals);
// call the class code
- rt_call_function_1(o_class_fun, (mp_obj_t)0xdeadbeef);
+ mp_obj_t cell = rt_call_function_1(args[0], (mp_obj_t)0xdeadbeef);
// restore old __locals__ object
rt_locals_set(old_locals);
- // create and return the new class
- return mp_obj_new_class(class_locals);
+ /*
+ // get the class type (meta object) from the base objects
+ mp_obj_t meta;
+ if (n_args == 2) {
+ // no explicit bases, so use 'type'
+ meta = (mp_obj_t)&mp_const_type;
+ } else {
+ // use type of first base object
+ meta = mp_obj_get_type(args[2]);
+ }
+ */
+
+ // TODO do proper metaclass resolution for multiple base objects
+
+ /*
+ // create the new class using a call to the meta object
+ // (arguments must be backwards in the array)
+ mp_obj_t meta_args[3];
+ meta_args[2] = args[1]; // class name
+ meta_args[1] = mp_obj_new_tuple(n_args - 2, args + 2); // tuple of bases
+ meta_args[0] = class_locals; // dict of members TODO, currently is a map
+ mp_obj_t new_class = rt_call_function_n(meta, 3, meta_args);
+ */
+ // create the new class
+ mp_obj_t new_class = mp_obj_new_class(class_locals);
+
+ // store into cell if neede
+ if (cell != mp_const_none) {
+ mp_obj_cell_set(cell, new_class);
+ }
+
+ return new_class;
}
+MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__);
+
mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
if (o != mp_const_none) {
mp_obj_print(o);
@@ -281,12 +318,7 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
static mp_obj_t mp_builtin_type(mp_obj_t o_in) {
// TODO implement the 3 argument version of type()
- if (MP_OBJ_IS_SMALL_INT(o_in)) {
- return (mp_obj_t)&int_type;
- } else {
- mp_obj_base_t *o = o_in;
- return (mp_obj_t)o->type;
- }
+ return mp_obj_get_type(o_in);
}
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_type_obj, mp_builtin_type);
diff --git a/py/builtin.h b/py/builtin.h
index 980662420..2cd0ef15f 100644
--- a/py/builtin.h
+++ b/py/builtin.h
@@ -1,6 +1,6 @@
// TODO convert all these to objects using MP_DECLARE and MP_DEFINE
-mp_obj_t mp_builtin___build_class__(mp_obj_t o_class_fun, mp_obj_t o_class_name);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin___build_class___obj);
mp_obj_t mp_builtin___import__(int n, mp_obj_t *args);
mp_obj_t mp_builtin___repl_print__(mp_obj_t o);
mp_obj_t mp_builtin_abs(mp_obj_t o_in);
diff --git a/py/lexer.h b/py/lexer.h
index 3cb48ce9e..9dfcb128c 100644
--- a/py/lexer.h
+++ b/py/lexer.h
@@ -127,6 +127,8 @@ 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_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);
+mp_lexer_t *mp_lexer_new_from_str_len(const char *src_name, const char *str, uint len, uint free_len);
+
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);
diff --git a/py/lexerstr.c b/py/lexerstr.c
new file mode 100644
index 000000000..b8594f420
--- /dev/null
+++ b/py/lexerstr.c
@@ -0,0 +1,35 @@
+#include <stdint.h>
+#include <stdio.h>
+
+#include "misc.h"
+#include "lexer.h"
+
+typedef struct _mp_lexer_str_buf_t {
+ uint free_len; // if > 0, src_beg will be freed when done by: m_free(src_beg, free_len)
+ const char *src_beg; // beginning of source
+ const char *src_cur; // current location in source
+ const char *src_end; // end (exclusive) of source
+} mp_lexer_str_buf_t;
+
+static unichar str_buf_next_char(mp_lexer_str_buf_t *sb) {
+ if (sb->src_cur < sb->src_end) {
+ return *sb->src_cur++;
+ } else {
+ return MP_LEXER_CHAR_EOF;
+ }
+}
+
+static void str_buf_free(mp_lexer_str_buf_t *sb) {
+ if (sb->free_len > 0) {
+ m_free((char*)sb->src_beg, sb->free_len);
+ }
+}
+
+mp_lexer_t *mp_lexer_new_from_str_len(const char *src_name, const char *str, uint len, uint free_len) {
+ mp_lexer_str_buf_t *sb = m_new_obj(mp_lexer_str_buf_t);
+ sb->free_len = free_len;
+ sb->src_beg = str;
+ sb->src_cur = str;
+ sb->src_end = str + len;
+ return mp_lexer_new(src_name, sb, (mp_lexer_stream_next_char_t)str_buf_next_char, (mp_lexer_stream_close_t)str_buf_free);
+}
diff --git a/py/lexerunix.c b/py/lexerunix.c
index 5336610ba..225ed20a7 100644
--- a/py/lexerunix.c
+++ b/py/lexerunix.c
@@ -6,42 +6,10 @@
#include "misc.h"
#include "mpconfig.h"
#include "lexer.h"
+#include "lexerunix.h"
#if MICROPY_ENABLE_LEXER_UNIX
-typedef struct _str_buf_t {
- 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
-} str_buf_t;
-
-unichar str_buf_next_char(str_buf_t *sb) {
- if (sb->src_cur < sb->src_end) {
- return *sb->src_cur++;
- } else {
- return MP_LEXER_CHAR_EOF;
- }
-}
-
-void str_buf_free(str_buf_t *sb) {
- if (sb) {
- if (sb->free) {
- m_del(char, (char*)sb->src_beg, 0 /* unknown size of src_beg */);
- }
- m_del_obj(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) {
- str_buf_t *sb = m_new(str_buf_t, 1);
- sb->free = free_str;
- sb->src_beg = str;
- sb->src_cur = str;
- sb->src_end = str + len;
- return mp_lexer_new(src_name, sb, (mp_lexer_stream_next_char_t)str_buf_next_char, (mp_lexer_stream_close_t)str_buf_free);
-}
-
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
int fd = open(filename, O_RDONLY);
if (fd < 0) {
@@ -59,7 +27,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, size);
}
/******************************************************************************/
diff --git a/py/lexerunix.h b/py/lexerunix.h
index b422a4306..3451c1c1c 100644
--- a/py/lexerunix.h
+++ b/py/lexerunix.h
@@ -1,4 +1,3 @@
-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_file(const char *filename);
void mp_import_set_directory(const char *dir);
diff --git a/py/obj.c b/py/obj.c
index 77580e1fe..7d0122c8f 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -13,6 +13,15 @@
#include "runtime.h"
#include "map.h"
+mp_obj_t mp_obj_get_type(mp_obj_t o_in) {
+ if (MP_OBJ_IS_SMALL_INT(o_in)) {
+ return (mp_obj_t)&int_type;
+ } else {
+ mp_obj_base_t *o = o_in;
+ return (mp_obj_t)o->type;
+ }
+}
+
const char *mp_obj_get_type_str(mp_obj_t o_in) {
if (MP_OBJ_IS_SMALL_INT(o_in)) {
return "int";
diff --git a/py/obj.h b/py/obj.h
index 71b43cb8d..208b23442 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -161,6 +161,7 @@ mp_obj_t mp_obj_new_class(struct _mp_map_t *class_locals);
mp_obj_t mp_obj_new_instance(mp_obj_t clas);
mp_obj_t mp_obj_new_module(qstr module_name);
+mp_obj_t mp_obj_get_type(mp_obj_t o_in);
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);
diff --git a/py/py.mk b/py/py.mk
index 3ed8a3e3d..0502dc0ff 100644
--- a/py/py.mk
+++ b/py/py.mk
@@ -29,6 +29,7 @@ PY_O_BASENAME = \
vstr.o \
unicode.o \
lexer.o \
+ lexerstr.o \
lexerunix.o \
parse.o \
scope.o \
diff --git a/py/runtime.c b/py/runtime.c
index 5c476d9ec..c0ed3b1fa 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -90,7 +90,7 @@ void rt_init(void) {
mp_qstr_map_lookup(&map_builtins, MP_QSTR_Ellipsis, 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___build_class__, true)->value = (mp_obj_t)&mp_builtin___build_class___obj;
mp_qstr_map_lookup(&map_builtins, MP_QSTR___repl_print__, true)->value = rt_make_function_1(mp_builtin___repl_print__);
// built-in types