Implement eval.

pull/180/head
Damien George 2014-01-15 22:14:03 +00:00
rodzic e2fb2baaa4
commit d02c6d8962
9 zmienionych plików z 76 dodań i 3 usunięć

Wyświetl plik

@ -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);

50
py/builtineval.c 100644
Wyświetl plik

@ -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);

Wyświetl plik

@ -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) {

Wyświetl plik

@ -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+

Wyświetl plik

@ -42,6 +42,7 @@ Q(chr)
Q(complex)
Q(dict)
Q(divmod)
Q(eval)
Q(float)
Q(hash)
Q(int)

Wyświetl plik

@ -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);

Wyświetl plik

@ -94,6 +94,7 @@ PY_O_BASENAME = \
stream.o \
builtin.o \
builtinimport.o \
builtineval.o \
vm.o \
showbc.o \
repl.o \

Wyświetl plik

@ -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);

Wyświetl plik

@ -0,0 +1,13 @@
# builtin eval
eval('1 + 2')
eval('1 + 2\n')
eval('1 + 2\n\n#comment\n')
x = 4
eval('x')
eval('lambda x: x + 10')(-5)
y = 6
eval('lambda: y * 2')()