Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
d02c6d89
Commit
d02c6d89
authored
Jan 15, 2014
by
Damien George
Browse files
Implement eval.
parent
e2fb2baa
Changes
9
Hide whitespace changes
Inline
Side-by-side
py/builtin.h
View file @
d02c6d89
...
...
@@ -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
);
...
...
py/builtineval.c
0 → 100644
View file @
d02c6d89
#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
);
py/compile.c
View file @
d02c6d89
...
...
@@ -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
)
{
...
...
py/grammar.h
View file @
d02c6d89
...
...
@@ -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+
...
...
py/mpqstrraw.h
View file @
d02c6d89
...
...
@@ -42,6 +42,7 @@ Q(chr)
Q
(
complex
)
Q
(
dict
)
Q
(
divmod
)
Q
(
eval
)
Q
(
float
)
Q
(
hash
)
Q
(
int
)
...
...
py/parse.c
View file @
d02c6d89
...
...
@@ -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
);
...
...
py/py.mk
View file @
d02c6d89
...
...
@@ -94,6 +94,7 @@ PY_O_BASENAME = \
stream.o
\
builtin.o
\
builtinimport.o
\
builtineval.o
\
vm.o
\
showbc.o
\
repl.o
\
...
...
py/runtime.c
View file @
d02c6d89
...
...
@@ -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
);
...
...
tests/basics/tests/eval1.py
0 → 100644
View file @
d02c6d89
# 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'
)()
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment