blob: 0a928199f04dbc599d154d29317ddd773023b0a7 [file] [log] [blame]
Damien Georged02c6d82014-01-15 22:14:03 +00001#include <stdint.h>
Damien Georged02c6d82014-01-15 22:14:03 +00002
3#include "nlr.h"
4#include "misc.h"
5#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00006#include "qstr.h"
Damien Georged02c6d82014-01-15 22:14:03 +00007#include "lexer.h"
8#include "lexerunix.h"
9#include "parse.h"
10#include "obj.h"
Damien Georgec5966122014-02-15 16:10:44 +000011#include "parsehelper.h"
Damien Georged02c6d82014-01-15 22:14:03 +000012#include "compile.h"
13#include "runtime0.h"
14#include "runtime.h"
Damien Georged02c6d82014-01-15 22:14:03 +000015#include "builtin.h"
16
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020017STATIC 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 +000018 uint str_len;
Damien George698ec212014-02-08 18:17:23 +000019 const char *str = mp_obj_str_get_data(o_in, &str_len);
Damien Georged02c6d82014-01-15 22:14:03 +000020
21 // create the lexer
Damien George698ec212014-02-08 18:17:23 +000022 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 +000023 qstr source_name = mp_lexer_source_name(lex);
Damien Georged02c6d82014-01-15 22:14:03 +000024
25 // parse the string
Damien Georgec5966122014-02-15 16:10:44 +000026 mp_parse_error_kind_t parse_error_kind;
27 mp_parse_node_t pn = mp_parse(lex, parse_input_kind, &parse_error_kind);
Damien Georged02c6d82014-01-15 22:14:03 +000028 mp_lexer_free(lex);
29
30 if (pn == MP_PARSE_NODE_NULL) {
31 // parse error; raise exception
Damien Georgeea13f402014-04-05 18:32:08 +010032 nlr_raise(mp_parse_make_exception(parse_error_kind));
Damien Georged02c6d82014-01-15 22:14:03 +000033 }
34
35 // compile the string
Damien George08335002014-01-18 23:24:36 +000036 mp_obj_t module_fun = mp_compile(pn, source_name, false);
Damien Georgeb829b5c2014-01-25 13:51:19 +000037 mp_parse_node_free(pn);
Damien Georged02c6d82014-01-15 22:14:03 +000038
39 if (module_fun == mp_const_none) {
40 // TODO handle compile error correctly
41 return mp_const_none;
42 }
43
44 // complied successfully, execute it
Damien Georged17926d2014-03-30 13:35:08 +010045 return mp_call_function_0(module_fun);
Damien Georged02c6d82014-01-15 22:14:03 +000046}
47
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020048STATIC mp_obj_t mp_builtin_eval(mp_obj_t o_in) {
Damien Georgeca476792014-02-03 22:44:10 +000049 return parse_compile_execute(o_in, MP_PARSE_EVAL_INPUT);
50}
51
Damien Georged02c6d82014-01-15 22:14:03 +000052MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_eval_obj, mp_builtin_eval);
Damien Georgeca476792014-02-03 22:44:10 +000053
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +020054STATIC mp_obj_t mp_builtin_exec(uint n_args, const mp_obj_t *args) {
55 // Unconditional getting/setting assumes that these operations
56 // are cheap, which is the case when this comment was written.
Damien George7efc5b32014-04-05 22:36:42 +010057 mp_obj_dict_t *old_globals = mp_globals_get();
58 mp_obj_dict_t *old_locals = mp_locals_get();
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +020059 if (n_args > 1) {
60 mp_obj_t globals = args[1];
61 mp_obj_t locals;
62 if (n_args > 2) {
63 locals = args[2];
64 } else {
65 locals = globals;
66 }
Damien George7efc5b32014-04-05 22:36:42 +010067 mp_globals_set(globals);
68 mp_locals_set(locals);
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +020069 }
70 mp_obj_t res = parse_compile_execute(args[0], MP_PARSE_FILE_INPUT);
Damien Georgec5966122014-02-15 16:10:44 +000071 // TODO if the above call throws an exception, then we never get to reset the globals/locals
Damien Georged17926d2014-03-30 13:35:08 +010072 mp_globals_set(old_globals);
73 mp_locals_set(old_locals);
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +020074 return res;
Damien Georgeca476792014-02-03 22:44:10 +000075}
76
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +020077MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec);