Skip to content

Commit d02c6d8

Browse files
committed
Implement eval.
1 parent e2fb2ba commit d02c6d8

9 files changed

Lines changed: 76 additions & 3 deletions

File tree

py/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_any_obj);
88
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_callable_obj);
99
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_chr_obj);
1010
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_divmod_obj);
11+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_eval_obj);
1112
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hash_obj);
1213
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_isinstance_obj);
1314
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_issubclass_obj);

py/builtineval.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <stdint.h>
2+
#include <stdlib.h>
3+
#include <stdio.h>
4+
#include <stdarg.h>
5+
#include <string.h>
6+
#include <assert.h>
7+
8+
#include "nlr.h"
9+
#include "misc.h"
10+
#include "mpconfig.h"
11+
#include "lexer.h"
12+
#include "lexerunix.h"
13+
#include "parse.h"
14+
#include "obj.h"
15+
#include "compile.h"
16+
#include "runtime0.h"
17+
#include "runtime.h"
18+
#include "map.h"
19+
#include "builtin.h"
20+
21+
static mp_obj_t mp_builtin_eval(mp_obj_t o_in) {
22+
const char *str = qstr_str(mp_obj_get_qstr(o_in));
23+
24+
// create the lexer
25+
mp_lexer_t *lex = mp_lexer_new_from_str_len("<string>", str, strlen(str), 0);
26+
27+
// parse the string
28+
qstr parse_exc_id;
29+
const char *parse_exc_msg;
30+
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_EVAL_INPUT, &parse_exc_id, &parse_exc_msg);
31+
mp_lexer_free(lex);
32+
33+
if (pn == MP_PARSE_NODE_NULL) {
34+
// parse error; raise exception
35+
nlr_jump(mp_obj_new_exception_msg(parse_exc_id, parse_exc_msg));
36+
}
37+
38+
// compile the string
39+
mp_obj_t module_fun = mp_compile(pn, false);
40+
41+
if (module_fun == mp_const_none) {
42+
// TODO handle compile error correctly
43+
return mp_const_none;
44+
}
45+
46+
// complied successfully, execute it
47+
return rt_call_function_0(module_fun);
48+
}
49+
50+
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_eval_obj, mp_builtin_eval);

py/compile.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2708,7 +2708,12 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
27082708
#endif
27092709

27102710
// compile
2711-
if (scope->kind == SCOPE_MODULE) {
2711+
if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2712+
assert(scope->kind == SCOPE_MODULE);
2713+
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2714+
compile_node(comp, pns->nodes[0]); // compile the expression
2715+
EMIT(return_value);
2716+
} else if (scope->kind == SCOPE_MODULE) {
27122717
if (!comp->is_repl) {
27132718
check_for_doc_string(comp, scope->pn);
27142719
}
@@ -2833,7 +2838,6 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
28332838
}
28342839

28352840
EMIT(end_pass);
2836-
28372841
}
28382842

28392843
void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {

py/grammar.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ DEF_RULE(single_input, nc, or(3), tok(NEWLINE), rule(simple_stmt), rule(compound
1515
DEF_RULE(file_input, nc, and(1), opt_rule(file_input_2))
1616
DEF_RULE(file_input_2, c(generic_all_nodes), one_or_more, rule(file_input_3))
1717
DEF_RULE(file_input_3, nc, or(2), tok(NEWLINE), rule(stmt))
18+
DEF_RULE(eval_input, nc, and(2), rule(testlist), opt_rule(eval_input_2))
19+
DEF_RULE(eval_input_2, nc, and(1), tok(NEWLINE))
1820

1921
// decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
2022
// decorators: decorator+

py/mpqstrraw.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Q(chr)
4242
Q(complex)
4343
Q(dict)
4444
Q(divmod)
45+
Q(eval)
4546
Q(float)
4647
Q(hash)
4748
Q(int)

py/parse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, qstr
284284
int top_level_rule;
285285
switch (input_kind) {
286286
case MP_PARSE_SINGLE_INPUT: top_level_rule = RULE_single_input; break;
287-
//case MP_PARSE_EVAL_INPUT: top_level_rule = RULE_eval_input; break;
287+
case MP_PARSE_EVAL_INPUT: top_level_rule = RULE_eval_input; break;
288288
default: top_level_rule = RULE_file_input;
289289
}
290290
push_rule(parser, rules[top_level_rule], 0);

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ PY_O_BASENAME = \
9494
stream.o \
9595
builtin.o \
9696
builtinimport.o \
97+
builtineval.o \
9798
vm.o \
9899
showbc.o \
99100
repl.o \

py/runtime.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ void rt_init(void) {
122122
mp_map_add_qstr(&map_builtins, MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj);
123123
mp_map_add_qstr(&map_builtins, MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj);
124124
mp_map_add_qstr(&map_builtins, MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj);
125+
mp_map_add_qstr(&map_builtins, MP_QSTR_eval, (mp_obj_t)&mp_builtin_eval_obj);
125126
mp_map_add_qstr(&map_builtins, MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj);
126127
mp_map_add_qstr(&map_builtins, MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj);
127128
mp_map_add_qstr(&map_builtins, MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj);

tests/basics/tests/eval1.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# builtin eval
2+
3+
eval('1 + 2')
4+
eval('1 + 2\n')
5+
eval('1 + 2\n\n#comment\n')
6+
7+
x = 4
8+
eval('x')
9+
10+
eval('lambda x: x + 10')(-5)
11+
12+
y = 6
13+
eval('lambda: y * 2')()

0 commit comments

Comments
 (0)