2014-05-03 18:27:38 -04: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 17:14:03 -05:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2014-05-02 10:47:01 -04:00
|
|
|
#include "mpconfig.h"
|
2014-01-15 17:14:03 -05:00
|
|
|
#include "nlr.h"
|
|
|
|
#include "misc.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"
|
2014-02-15 11:10:44 -05:00
|
|
|
#include "parsehelper.h"
|
2014-01-15 17:14:03 -05:00
|
|
|
#include "compile.h"
|
|
|
|
#include "runtime0.h"
|
|
|
|
#include "runtime.h"
|
|
|
|
#include "builtin.h"
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC mp_obj_t parse_compile_execute(mp_obj_t o_in, mp_parse_input_kind_t parse_input_kind) {
|
2014-08-30 09:19:41 -04:00
|
|
|
mp_uint_t str_len;
|
2014-02-08 13:17:23 -05:00
|
|
|
const char *str = mp_obj_str_get_data(o_in, &str_len);
|
2014-01-15 17:14:03 -05:00
|
|
|
|
|
|
|
// create the lexer
|
2014-02-08 13:17:23 -05:00
|
|
|
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, str, str_len, 0);
|
2014-01-25 08:51:19 -05:00
|
|
|
qstr source_name = mp_lexer_source_name(lex);
|
2014-01-15 17:14:03 -05:00
|
|
|
|
|
|
|
// parse the string
|
2014-02-15 11:10:44 -05: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 17:14:03 -05:00
|
|
|
|
|
|
|
if (pn == MP_PARSE_NODE_NULL) {
|
|
|
|
// parse error; raise exception
|
2014-04-13 06:56:02 -04:00
|
|
|
mp_obj_t exc = mp_parse_make_exception(lex, parse_error_kind);
|
|
|
|
mp_lexer_free(lex);
|
|
|
|
nlr_raise(exc);
|
2014-01-15 17:14:03 -05:00
|
|
|
}
|
|
|
|
|
2014-04-13 06:56:02 -04:00
|
|
|
mp_lexer_free(lex);
|
|
|
|
|
2014-01-15 17:14:03 -05:00
|
|
|
// compile the string
|
2014-04-06 06:48:15 -04:00
|
|
|
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, false);
|
2014-01-15 17:14:03 -05:00
|
|
|
|
2014-10-05 14:01:34 -04:00
|
|
|
// check if there was a compile error
|
|
|
|
if (mp_obj_is_exception_instance(module_fun)) {
|
|
|
|
nlr_raise(module_fun);
|
2014-01-15 17:14:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// complied successfully, execute it
|
2014-03-30 08:35:08 -04:00
|
|
|
return mp_call_function_0(module_fun);
|
2014-01-15 17:14:03 -05:00
|
|
|
}
|
|
|
|
|
2014-02-12 11:31:30 -05:00
|
|
|
STATIC mp_obj_t mp_builtin_eval(mp_obj_t o_in) {
|
2014-02-03 17:44:10 -05:00
|
|
|
return parse_compile_execute(o_in, MP_PARSE_EVAL_INPUT);
|
|
|
|
}
|
|
|
|
|
2014-01-15 17:14:03 -05:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_eval_obj, mp_builtin_eval);
|
2014-02-03 17:44:10 -05:00
|
|
|
|
2014-02-12 17:36:54 -05: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.
|
2014-04-05 17:36:42 -04:00
|
|
|
mp_obj_dict_t *old_globals = mp_globals_get();
|
|
|
|
mp_obj_dict_t *old_locals = mp_locals_get();
|
2014-02-12 17:36:54 -05:00
|
|
|
if (n_args > 1) {
|
|
|
|
mp_obj_t globals = args[1];
|
|
|
|
mp_obj_t locals;
|
|
|
|
if (n_args > 2) {
|
|
|
|
locals = args[2];
|
|
|
|
} else {
|
|
|
|
locals = globals;
|
|
|
|
}
|
2014-04-05 17:36:42 -04:00
|
|
|
mp_globals_set(globals);
|
|
|
|
mp_locals_set(locals);
|
2014-02-12 17:36:54 -05:00
|
|
|
}
|
|
|
|
mp_obj_t res = parse_compile_execute(args[0], MP_PARSE_FILE_INPUT);
|
2014-02-15 11:10:44 -05:00
|
|
|
// TODO if the above call throws an exception, then we never get to reset the globals/locals
|
2014-03-30 08:35:08 -04:00
|
|
|
mp_globals_set(old_globals);
|
|
|
|
mp_locals_set(old_locals);
|
2014-02-12 17:36:54 -05:00
|
|
|
return res;
|
2014-02-03 17:44:10 -05:00
|
|
|
}
|
|
|
|
|
2014-02-12 17:36:54 -05:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec);
|