2014-05-03 23:27:38 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-01-15 22:14:03 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2014-05-02 15:47:01 +01:00
|
|
|
#include "mpconfig.h"
|
2014-01-15 22:14:03 +00:00
|
|
|
#include "nlr.h"
|
|
|
|
#include "misc.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 "builtin.h"
|
|
|
|
|
2014-10-05 20:13:34 +01:00
|
|
|
STATIC mp_obj_t eval_exec_helper(mp_uint_t n_args, const mp_obj_t *args, mp_parse_input_kind_t parse_input_kind) {
|
2014-08-30 14:19:41 +01:00
|
|
|
mp_uint_t str_len;
|
2014-10-05 20:13:34 +01:00
|
|
|
const char *str = mp_obj_str_get_data(args[0], &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-15 22:14:03 +00:00
|
|
|
|
2014-10-05 20:13:34 +01:00
|
|
|
// work out the context
|
|
|
|
mp_obj_dict_t *globals = mp_globals_get();
|
|
|
|
mp_obj_dict_t *locals = mp_locals_get();
|
2014-02-12 22:36:54 +00:00
|
|
|
if (n_args > 1) {
|
2014-10-05 20:13:34 +01:00
|
|
|
globals = args[1];
|
2014-02-12 22:36:54 +00:00
|
|
|
if (n_args > 2) {
|
|
|
|
locals = args[2];
|
|
|
|
} else {
|
|
|
|
locals = globals;
|
|
|
|
}
|
|
|
|
}
|
2014-10-05 20:13:34 +01:00
|
|
|
|
|
|
|
return mp_parse_compile_execute(lex, parse_input_kind, globals, locals);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t mp_builtin_eval(mp_uint_t n_args, const mp_obj_t *args) {
|
|
|
|
return eval_exec_helper(n_args, args, MP_PARSE_EVAL_INPUT);
|
2014-02-03 22:44:10 +00:00
|
|
|
}
|
2014-10-05 20:13:34 +01:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_eval_obj, 1, 3, mp_builtin_eval);
|
2014-02-03 22:44:10 +00:00
|
|
|
|
2014-10-05 20:13:34 +01:00
|
|
|
STATIC mp_obj_t mp_builtin_exec(mp_uint_t n_args, const mp_obj_t *args) {
|
|
|
|
return eval_exec_helper(n_args, args, MP_PARSE_FILE_INPUT);
|
|
|
|
}
|
2014-02-12 22:36:54 +00:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec);
|