2014-01-15 17:14:03 -05:00
|
|
|
#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"
|
2014-01-21 16:40:13 -05:00
|
|
|
#include "qstr.h"
|
2014-01-15 17:14:03 -05:00
|
|
|
#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) {
|
2014-01-21 16:40:13 -05:00
|
|
|
uint str_len;
|
2014-01-22 09:35:10 -05:00
|
|
|
const byte *str = mp_obj_str_get_data(o_in, &str_len);
|
2014-01-15 17:14:03 -05:00
|
|
|
|
|
|
|
// create the lexer
|
2014-01-25 08:51:19 -05:00
|
|
|
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, (const char*)str, str_len, 0);
|
|
|
|
qstr source_name = mp_lexer_source_name(lex);
|
2014-01-15 17:14:03 -05:00
|
|
|
|
|
|
|
// 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
|
2014-01-18 18:24:36 -05:00
|
|
|
mp_obj_t module_fun = mp_compile(pn, source_name, false);
|
2014-01-25 08:51:19 -05:00
|
|
|
mp_parse_node_free(pn);
|
2014-01-15 17:14:03 -05:00
|
|
|
|
|
|
|
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);
|