2014-01-15 22:14:03 +00: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 21:40:13 +00:00
|
|
|
#include "qstr.h"
|
2014-01-15 22:14:03 +00:00
|
|
|
#include "lexer.h"
|
|
|
|
#include "lexerunix.h"
|
|
|
|
#include "parse.h"
|
|
|
|
#include "obj.h"
|
2014-02-15 16:10:44 +00:00
|
|
|
#include "parsehelper.h"
|
2014-01-15 22:14:03 +00:00
|
|
|
#include "compile.h"
|
|
|
|
#include "runtime0.h"
|
|
|
|
#include "runtime.h"
|
|
|
|
#include "map.h"
|
|
|
|
#include "builtin.h"
|
|
|
|
|
2014-02-12 16:31:30 +00:00
|
|
|
STATIC mp_obj_t parse_compile_execute(mp_obj_t o_in, mp_parse_input_kind_t parse_input_kind) {
|
2014-01-21 21:40:13 +00:00
|
|
|
uint str_len;
|
2014-02-08 18:17:23 +00:00
|
|
|
const char *str = mp_obj_str_get_data(o_in, &str_len);
|
2014-01-15 22:14:03 +00:00
|
|
|
|
|
|
|
// create the lexer
|
2014-02-08 18:17:23 +00:00
|
|
|
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, str, str_len, 0);
|
2014-01-25 13:51:19 +00:00
|
|
|
qstr source_name = mp_lexer_source_name(lex);
|
2014-01-15 22:14:03 +00:00
|
|
|
|
|
|
|
// parse the string
|
2014-02-15 16:10:44 +00:00
|
|
|
mp_parse_error_kind_t parse_error_kind;
|
|
|
|
mp_parse_node_t pn = mp_parse(lex, parse_input_kind, &parse_error_kind);
|
2014-01-15 22:14:03 +00:00
|
|
|
mp_lexer_free(lex);
|
|
|
|
|
|
|
|
if (pn == MP_PARSE_NODE_NULL) {
|
|
|
|
// parse error; raise exception
|
2014-02-15 16:10:44 +00:00
|
|
|
nlr_jump(mp_parse_make_exception(parse_error_kind));
|
2014-01-15 22:14:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// compile the string
|
2014-01-18 23:24:36 +00:00
|
|
|
mp_obj_t module_fun = mp_compile(pn, source_name, false);
|
2014-01-25 13:51:19 +00:00
|
|
|
mp_parse_node_free(pn);
|
2014-01-15 22:14:03 +00: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);
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:31:30 +00:00
|
|
|
STATIC mp_obj_t mp_builtin_eval(mp_obj_t o_in) {
|
2014-02-03 22:44:10 +00:00
|
|
|
return parse_compile_execute(o_in, MP_PARSE_EVAL_INPUT);
|
|
|
|
}
|
|
|
|
|
2014-01-15 22:14:03 +00:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_eval_obj, mp_builtin_eval);
|
2014-02-03 22:44:10 +00:00
|
|
|
|
2014-02-12 22:36:54 +00:00
|
|
|
STATIC mp_obj_t mp_builtin_exec(uint n_args, const mp_obj_t *args) {
|
|
|
|
// Unconditional getting/setting assumes that these operations
|
|
|
|
// are cheap, which is the case when this comment was written.
|
|
|
|
mp_map_t *old_globals = rt_globals_get();
|
|
|
|
mp_map_t *old_locals = rt_locals_get();
|
|
|
|
if (n_args > 1) {
|
|
|
|
mp_obj_t globals = args[1];
|
|
|
|
mp_obj_t locals;
|
|
|
|
if (n_args > 2) {
|
|
|
|
locals = args[2];
|
|
|
|
} else {
|
|
|
|
locals = globals;
|
|
|
|
}
|
|
|
|
rt_globals_set(mp_obj_dict_get_map(globals));
|
|
|
|
rt_locals_set(mp_obj_dict_get_map(locals));
|
|
|
|
}
|
|
|
|
mp_obj_t res = parse_compile_execute(args[0], MP_PARSE_FILE_INPUT);
|
2014-02-15 16:10:44 +00:00
|
|
|
// TODO if the above call throws an exception, then we never get to reset the globals/locals
|
2014-02-12 22:36:54 +00:00
|
|
|
rt_globals_set(old_globals);
|
|
|
|
rt_locals_set(old_locals);
|
|
|
|
return res;
|
2014-02-03 22:44:10 +00:00
|
|
|
}
|
|
|
|
|
2014-02-12 22:36:54 +00:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec);
|