Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 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" |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 11 | #include "qstr.h" |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 12 | #include "lexer.h" |
| 13 | #include "lexerunix.h" |
| 14 | #include "parse.h" |
| 15 | #include "obj.h" |
| 16 | #include "compile.h" |
| 17 | #include "runtime0.h" |
| 18 | #include "runtime.h" |
| 19 | #include "map.h" |
| 20 | #include "builtin.h" |
| 21 | |
| 22 | static mp_obj_t mp_builtin_eval(mp_obj_t o_in) { |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 23 | uint str_len; |
Damien George | 5fa93b6 | 2014-01-22 14:35:10 +0000 | [diff] [blame] | 24 | const byte *str = mp_obj_str_get_data(o_in, &str_len); |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 25 | |
| 26 | // create the lexer |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 27 | mp_lexer_t *lex = mp_lexer_new_from_str_len("<string>", (const char*)str, str_len, 0); |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 28 | |
| 29 | // parse the string |
| 30 | qstr parse_exc_id; |
| 31 | const char *parse_exc_msg; |
| 32 | mp_parse_node_t pn = mp_parse(lex, MP_PARSE_EVAL_INPUT, &parse_exc_id, &parse_exc_msg); |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 33 | qstr source_name = mp_lexer_source_name(lex); |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 34 | mp_lexer_free(lex); |
| 35 | |
| 36 | if (pn == MP_PARSE_NODE_NULL) { |
| 37 | // parse error; raise exception |
| 38 | nlr_jump(mp_obj_new_exception_msg(parse_exc_id, parse_exc_msg)); |
| 39 | } |
| 40 | |
| 41 | // compile the string |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 42 | mp_obj_t module_fun = mp_compile(pn, source_name, false); |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 43 | |
| 44 | if (module_fun == mp_const_none) { |
| 45 | // TODO handle compile error correctly |
| 46 | return mp_const_none; |
| 47 | } |
| 48 | |
| 49 | // complied successfully, execute it |
| 50 | return rt_call_function_0(module_fun); |
| 51 | } |
| 52 | |
| 53 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_eval_obj, mp_builtin_eval); |