summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/builtin.h1
-rw-r--r--py/builtineval.c50
-rw-r--r--py/compile.c8
-rw-r--r--py/grammar.h2
-rw-r--r--py/mpqstrraw.h1
-rw-r--r--py/parse.c2
-rw-r--r--py/py.mk1
-rw-r--r--py/runtime.c1
8 files changed, 63 insertions, 3 deletions
diff --git a/py/builtin.h b/py/builtin.h
index cb8836c48b..7d001341ae 100644
--- a/py/builtin.h
+++ b/py/builtin.h
@@ -8,6 +8,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_any_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_callable_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_chr_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_divmod_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_eval_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hash_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_isinstance_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_issubclass_obj);
diff --git a/py/builtineval.c b/py/builtineval.c
new file mode 100644
index 0000000000..a8a7a476a3
--- /dev/null
+++ b/py/builtineval.c
@@ -0,0 +1,50 @@
+#include <stdint.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <string.h>
+#include <assert.h>
+
+#include "nlr.h"
+#include "misc.h"
+#include "mpconfig.h"
+#include "lexer.h"
+#include "lexerunix.h"
+#include "parse.h"
+#include "obj.h"
+#include "compile.h"
+#include "runtime0.h"
+#include "runtime.h"
+#include "map.h"
+#include "builtin.h"
+
+static mp_obj_t mp_builtin_eval(mp_obj_t o_in) {
+ const char *str = qstr_str(mp_obj_get_qstr(o_in));
+
+ // create the lexer
+ mp_lexer_t *lex = mp_lexer_new_from_str_len("<string>", str, strlen(str), 0);
+
+ // parse the string
+ qstr parse_exc_id;
+ const char *parse_exc_msg;
+ mp_parse_node_t pn = mp_parse(lex, MP_PARSE_EVAL_INPUT, &parse_exc_id, &parse_exc_msg);
+ mp_lexer_free(lex);
+
+ if (pn == MP_PARSE_NODE_NULL) {
+ // parse error; raise exception
+ nlr_jump(mp_obj_new_exception_msg(parse_exc_id, parse_exc_msg));
+ }
+
+ // compile the string
+ mp_obj_t module_fun = mp_compile(pn, false);
+
+ if (module_fun == mp_const_none) {
+ // TODO handle compile error correctly
+ return mp_const_none;
+ }
+
+ // complied successfully, execute it
+ return rt_call_function_0(module_fun);
+}
+
+MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_eval_obj, mp_builtin_eval);
diff --git a/py/compile.c b/py/compile.c
index b948f7aa47..b24e94a8dc 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -2708,7 +2708,12 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
#endif
// compile
- if (scope->kind == SCOPE_MODULE) {
+ if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
+ assert(scope->kind == SCOPE_MODULE);
+ mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
+ compile_node(comp, pns->nodes[0]); // compile the expression
+ EMIT(return_value);
+ } else if (scope->kind == SCOPE_MODULE) {
if (!comp->is_repl) {
check_for_doc_string(comp, scope->pn);
}
@@ -2833,7 +2838,6 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
}
EMIT(end_pass);
-
}
void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
diff --git a/py/grammar.h b/py/grammar.h
index 4d53bd3fc4..32be6c66ca 100644
--- a/py/grammar.h
+++ b/py/grammar.h
@@ -15,6 +15,8 @@ DEF_RULE(single_input, nc, or(3), tok(NEWLINE), rule(simple_stmt), rule(compound
DEF_RULE(file_input, nc, and(1), opt_rule(file_input_2))
DEF_RULE(file_input_2, c(generic_all_nodes), one_or_more, rule(file_input_3))
DEF_RULE(file_input_3, nc, or(2), tok(NEWLINE), rule(stmt))
+DEF_RULE(eval_input, nc, and(2), rule(testlist), opt_rule(eval_input_2))
+DEF_RULE(eval_input_2, nc, and(1), tok(NEWLINE))
// decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
// decorators: decorator+
diff --git a/py/mpqstrraw.h b/py/mpqstrraw.h
index 3ee569aec6..06f83ddf09 100644
--- a/py/mpqstrraw.h
+++ b/py/mpqstrraw.h
@@ -42,6 +42,7 @@ Q(chr)
Q(complex)
Q(dict)
Q(divmod)
+Q(eval)
Q(float)
Q(hash)
Q(int)
diff --git a/py/parse.c b/py/parse.c
index e26e235aa3..49b42e5d77 100644
--- a/py/parse.c
+++ b/py/parse.c
@@ -284,7 +284,7 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, qstr
int top_level_rule;
switch (input_kind) {
case MP_PARSE_SINGLE_INPUT: top_level_rule = RULE_single_input; break;
- //case MP_PARSE_EVAL_INPUT: top_level_rule = RULE_eval_input; break;
+ case MP_PARSE_EVAL_INPUT: top_level_rule = RULE_eval_input; break;
default: top_level_rule = RULE_file_input;
}
push_rule(parser, rules[top_level_rule], 0);
diff --git a/py/py.mk b/py/py.mk
index 95f9c07671..16e0640195 100644
--- a/py/py.mk
+++ b/py/py.mk
@@ -94,6 +94,7 @@ PY_O_BASENAME = \
stream.o \
builtin.o \
builtinimport.o \
+ builtineval.o \
vm.o \
showbc.o \
repl.o \
diff --git a/py/runtime.c b/py/runtime.c
index cbdee01dde..594f79d847 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -122,6 +122,7 @@ void rt_init(void) {
mp_map_add_qstr(&map_builtins, MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj);
+ mp_map_add_qstr(&map_builtins, MP_QSTR_eval, (mp_obj_t)&mp_builtin_eval_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj);