blob: 3b7fb16f871ab6e3287fb9b1842bf8e6bc8b25e5 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
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 Georged02c6d82014-01-15 22:14:03 +000027#include <stdint.h>
Damien Georged02c6d82014-01-15 22:14:03 +000028
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030029#include "mpconfig.h"
Damien Georged02c6d82014-01-15 22:14:03 +000030#include "nlr.h"
31#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000032#include "qstr.h"
Damien Georged02c6d82014-01-15 22:14:03 +000033#include "lexer.h"
34#include "lexerunix.h"
35#include "parse.h"
36#include "obj.h"
Damien Georgec5966122014-02-15 16:10:44 +000037#include "parsehelper.h"
Damien Georged02c6d82014-01-15 22:14:03 +000038#include "compile.h"
39#include "runtime0.h"
40#include "runtime.h"
Damien Georged02c6d82014-01-15 22:14:03 +000041#include "builtin.h"
42
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020043STATIC mp_obj_t parse_compile_execute(mp_obj_t o_in, mp_parse_input_kind_t parse_input_kind) {
Damien George55baff42014-01-21 21:40:13 +000044 uint str_len;
Damien George698ec212014-02-08 18:17:23 +000045 const char *str = mp_obj_str_get_data(o_in, &str_len);
Damien Georged02c6d82014-01-15 22:14:03 +000046
47 // create the lexer
Damien George698ec212014-02-08 18:17:23 +000048 mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, str, str_len, 0);
Damien Georgeb829b5c2014-01-25 13:51:19 +000049 qstr source_name = mp_lexer_source_name(lex);
Damien Georged02c6d82014-01-15 22:14:03 +000050
51 // parse the string
Damien Georgec5966122014-02-15 16:10:44 +000052 mp_parse_error_kind_t parse_error_kind;
53 mp_parse_node_t pn = mp_parse(lex, parse_input_kind, &parse_error_kind);
Damien Georged02c6d82014-01-15 22:14:03 +000054
55 if (pn == MP_PARSE_NODE_NULL) {
56 // parse error; raise exception
Damien George4b01de42014-04-13 11:56:02 +010057 mp_obj_t exc = mp_parse_make_exception(lex, parse_error_kind);
58 mp_lexer_free(lex);
59 nlr_raise(exc);
Damien Georged02c6d82014-01-15 22:14:03 +000060 }
61
Damien George4b01de42014-04-13 11:56:02 +010062 mp_lexer_free(lex);
63
Damien Georged02c6d82014-01-15 22:14:03 +000064 // compile the string
Damien George65cad122014-04-06 11:48:15 +010065 mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, false);
Damien Georgeb829b5c2014-01-25 13:51:19 +000066 mp_parse_node_free(pn);
Damien Georged02c6d82014-01-15 22:14:03 +000067
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 Georged17926d2014-03-30 13:35:08 +010074 return mp_call_function_0(module_fun);
Damien Georged02c6d82014-01-15 22:14:03 +000075}
76
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020077STATIC mp_obj_t mp_builtin_eval(mp_obj_t o_in) {
Damien Georgeca476792014-02-03 22:44:10 +000078 return parse_compile_execute(o_in, MP_PARSE_EVAL_INPUT);
79}
80
Damien Georged02c6d82014-01-15 22:14:03 +000081MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_eval_obj, mp_builtin_eval);
Damien Georgeca476792014-02-03 22:44:10 +000082
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +020083STATIC 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 George7efc5b32014-04-05 22:36:42 +010086 mp_obj_dict_t *old_globals = mp_globals_get();
87 mp_obj_dict_t *old_locals = mp_locals_get();
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +020088 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 George7efc5b32014-04-05 22:36:42 +010096 mp_globals_set(globals);
97 mp_locals_set(locals);
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +020098 }
99 mp_obj_t res = parse_compile_execute(args[0], MP_PARSE_FILE_INPUT);
Damien Georgec5966122014-02-15 16:10:44 +0000100 // TODO if the above call throws an exception, then we never get to reset the globals/locals
Damien Georged17926d2014-03-30 13:35:08 +0100101 mp_globals_set(old_globals);
102 mp_locals_set(old_locals);
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +0200103 return res;
Damien Georgeca476792014-02-03 22:44:10 +0000104}
105
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +0200106MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec);