Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 27 | #include <stdint.h> |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 28 | |
Paul Sokolovsky | f54bcbf | 2014-05-02 17:47:01 +0300 | [diff] [blame] | 29 | #include "mpconfig.h" |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 30 | #include "nlr.h" |
| 31 | #include "misc.h" |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 32 | #include "qstr.h" |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 33 | #include "lexer.h" |
| 34 | #include "lexerunix.h" |
| 35 | #include "parse.h" |
| 36 | #include "obj.h" |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 37 | #include "parsehelper.h" |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 38 | #include "compile.h" |
| 39 | #include "runtime0.h" |
| 40 | #include "runtime.h" |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 41 | #include "builtin.h" |
| 42 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 43 | STATIC mp_obj_t parse_compile_execute(mp_obj_t o_in, mp_parse_input_kind_t parse_input_kind) { |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 44 | uint str_len; |
Damien George | 698ec21 | 2014-02-08 18:17:23 +0000 | [diff] [blame] | 45 | const char *str = mp_obj_str_get_data(o_in, &str_len); |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 46 | |
| 47 | // create the lexer |
Damien George | 698ec21 | 2014-02-08 18:17:23 +0000 | [diff] [blame] | 48 | mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, str, str_len, 0); |
Damien George | b829b5c | 2014-01-25 13:51:19 +0000 | [diff] [blame] | 49 | qstr source_name = mp_lexer_source_name(lex); |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 50 | |
| 51 | // parse the string |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 52 | mp_parse_error_kind_t parse_error_kind; |
| 53 | mp_parse_node_t pn = mp_parse(lex, parse_input_kind, &parse_error_kind); |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 54 | |
| 55 | if (pn == MP_PARSE_NODE_NULL) { |
| 56 | // parse error; raise exception |
Damien George | 4b01de4 | 2014-04-13 11:56:02 +0100 | [diff] [blame] | 57 | mp_obj_t exc = mp_parse_make_exception(lex, parse_error_kind); |
| 58 | mp_lexer_free(lex); |
| 59 | nlr_raise(exc); |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Damien George | 4b01de4 | 2014-04-13 11:56:02 +0100 | [diff] [blame] | 62 | mp_lexer_free(lex); |
| 63 | |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 64 | // compile the string |
Damien George | 65cad12 | 2014-04-06 11:48:15 +0100 | [diff] [blame] | 65 | mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, false); |
Damien George | b829b5c | 2014-01-25 13:51:19 +0000 | [diff] [blame] | 66 | mp_parse_node_free(pn); |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 67 | |
| 68 | if (module_fun == mp_const_none) { |
| 69 | // TODO handle compile error correctly |
| 70 | return mp_const_none; |
| 71 | } |
| 72 | |
| 73 | // complied successfully, execute it |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 74 | return mp_call_function_0(module_fun); |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 77 | STATIC mp_obj_t mp_builtin_eval(mp_obj_t o_in) { |
Damien George | ca47679 | 2014-02-03 22:44:10 +0000 | [diff] [blame] | 78 | return parse_compile_execute(o_in, MP_PARSE_EVAL_INPUT); |
| 79 | } |
| 80 | |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 81 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_eval_obj, mp_builtin_eval); |
Damien George | ca47679 | 2014-02-03 22:44:10 +0000 | [diff] [blame] | 82 | |
Paul Sokolovsky | 2aa217b | 2014-02-13 00:36:54 +0200 | [diff] [blame] | 83 | STATIC mp_obj_t mp_builtin_exec(uint n_args, const mp_obj_t *args) { |
| 84 | // Unconditional getting/setting assumes that these operations |
| 85 | // are cheap, which is the case when this comment was written. |
Damien George | 7efc5b3 | 2014-04-05 22:36:42 +0100 | [diff] [blame] | 86 | mp_obj_dict_t *old_globals = mp_globals_get(); |
| 87 | mp_obj_dict_t *old_locals = mp_locals_get(); |
Paul Sokolovsky | 2aa217b | 2014-02-13 00:36:54 +0200 | [diff] [blame] | 88 | if (n_args > 1) { |
| 89 | mp_obj_t globals = args[1]; |
| 90 | mp_obj_t locals; |
| 91 | if (n_args > 2) { |
| 92 | locals = args[2]; |
| 93 | } else { |
| 94 | locals = globals; |
| 95 | } |
Damien George | 7efc5b3 | 2014-04-05 22:36:42 +0100 | [diff] [blame] | 96 | mp_globals_set(globals); |
| 97 | mp_locals_set(locals); |
Paul Sokolovsky | 2aa217b | 2014-02-13 00:36:54 +0200 | [diff] [blame] | 98 | } |
| 99 | mp_obj_t res = parse_compile_execute(args[0], MP_PARSE_FILE_INPUT); |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 100 | // TODO if the above call throws an exception, then we never get to reset the globals/locals |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 101 | mp_globals_set(old_globals); |
| 102 | mp_locals_set(old_locals); |
Paul Sokolovsky | 2aa217b | 2014-02-13 00:36:54 +0200 | [diff] [blame] | 103 | return res; |
Damien George | ca47679 | 2014-02-03 22:44:10 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Paul Sokolovsky | 2aa217b | 2014-02-13 00:36:54 +0200 | [diff] [blame] | 106 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec); |