Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 1 | #include <stdint.h> |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 2 | |
| 3 | #include "nlr.h" |
| 4 | #include "misc.h" |
| 5 | #include "mpconfig.h" |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 6 | #include "qstr.h" |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 7 | #include "lexer.h" |
| 8 | #include "lexerunix.h" |
| 9 | #include "parse.h" |
| 10 | #include "obj.h" |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 11 | #include "parsehelper.h" |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 12 | #include "compile.h" |
| 13 | #include "runtime0.h" |
| 14 | #include "runtime.h" |
| 15 | #include "map.h" |
| 16 | #include "builtin.h" |
| 17 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 18 | STATIC mp_obj_t parse_compile_execute(mp_obj_t o_in, mp_parse_input_kind_t parse_input_kind) { |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 19 | uint str_len; |
Damien George | 698ec21 | 2014-02-08 18:17:23 +0000 | [diff] [blame] | 20 | const char *str = mp_obj_str_get_data(o_in, &str_len); |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 21 | |
| 22 | // create the lexer |
Damien George | 698ec21 | 2014-02-08 18:17:23 +0000 | [diff] [blame] | 23 | mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, str, str_len, 0); |
Damien George | b829b5c | 2014-01-25 13:51:19 +0000 | [diff] [blame] | 24 | qstr source_name = mp_lexer_source_name(lex); |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 25 | |
| 26 | // parse the string |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 27 | mp_parse_error_kind_t parse_error_kind; |
| 28 | mp_parse_node_t pn = mp_parse(lex, parse_input_kind, &parse_error_kind); |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 29 | mp_lexer_free(lex); |
| 30 | |
| 31 | if (pn == MP_PARSE_NODE_NULL) { |
| 32 | // parse error; raise exception |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 33 | nlr_jump(mp_parse_make_exception(parse_error_kind)); |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // compile the string |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 37 | mp_obj_t module_fun = mp_compile(pn, source_name, false); |
Damien George | b829b5c | 2014-01-25 13:51:19 +0000 | [diff] [blame] | 38 | mp_parse_node_free(pn); |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 39 | |
| 40 | if (module_fun == mp_const_none) { |
| 41 | // TODO handle compile error correctly |
| 42 | return mp_const_none; |
| 43 | } |
| 44 | |
| 45 | // complied successfully, execute it |
| 46 | return rt_call_function_0(module_fun); |
| 47 | } |
| 48 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 49 | STATIC mp_obj_t mp_builtin_eval(mp_obj_t o_in) { |
Damien George | ca47679 | 2014-02-03 22:44:10 +0000 | [diff] [blame] | 50 | return parse_compile_execute(o_in, MP_PARSE_EVAL_INPUT); |
| 51 | } |
| 52 | |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 53 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_eval_obj, mp_builtin_eval); |
Damien George | ca47679 | 2014-02-03 22:44:10 +0000 | [diff] [blame] | 54 | |
Paul Sokolovsky | 2aa217b | 2014-02-13 00:36:54 +0200 | [diff] [blame] | 55 | STATIC mp_obj_t mp_builtin_exec(uint n_args, const mp_obj_t *args) { |
| 56 | // Unconditional getting/setting assumes that these operations |
| 57 | // are cheap, which is the case when this comment was written. |
| 58 | mp_map_t *old_globals = rt_globals_get(); |
| 59 | mp_map_t *old_locals = rt_locals_get(); |
| 60 | if (n_args > 1) { |
| 61 | mp_obj_t globals = args[1]; |
| 62 | mp_obj_t locals; |
| 63 | if (n_args > 2) { |
| 64 | locals = args[2]; |
| 65 | } else { |
| 66 | locals = globals; |
| 67 | } |
| 68 | rt_globals_set(mp_obj_dict_get_map(globals)); |
| 69 | rt_locals_set(mp_obj_dict_get_map(locals)); |
| 70 | } |
| 71 | mp_obj_t res = parse_compile_execute(args[0], MP_PARSE_FILE_INPUT); |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 72 | // TODO if the above call throws an exception, then we never get to reset the globals/locals |
Paul Sokolovsky | 2aa217b | 2014-02-13 00:36:54 +0200 | [diff] [blame] | 73 | rt_globals_set(old_globals); |
| 74 | rt_locals_set(old_locals); |
| 75 | return res; |
Damien George | ca47679 | 2014-02-03 22:44:10 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Paul Sokolovsky | 2aa217b | 2014-02-13 00:36:54 +0200 | [diff] [blame] | 78 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec); |