blob: bc3adb625626aa50d031b5b23e72b16e25d7a9cc [file] [log] [blame]
Damien Georged02c6d82014-01-15 22:14:03 +00001#include <stdint.h>
Damien Georged02c6d82014-01-15 22:14:03 +00002
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +03003#include "mpconfig.h"
Damien Georged02c6d82014-01-15 22:14:03 +00004#include "nlr.h"
5#include "misc.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
29 if (pn == MP_PARSE_NODE_NULL) {
30 // parse error; raise exception
Damien George4b01de42014-04-13 11:56:02 +010031 mp_obj_t exc = mp_parse_make_exception(lex, parse_error_kind);
32 mp_lexer_free(lex);
33 nlr_raise(exc);
Damien Georged02c6d82014-01-15 22:14:03 +000034 }
35
Damien George4b01de42014-04-13 11:56:02 +010036 mp_lexer_free(lex);
37
Damien Georged02c6d82014-01-15 22:14:03 +000038 // compile the string
Damien George65cad122014-04-06 11:48:15 +010039 mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, false);
Damien Georgeb829b5c2014-01-25 13:51:19 +000040 mp_parse_node_free(pn);
Damien Georged02c6d82014-01-15 22:14:03 +000041
42 if (module_fun == mp_const_none) {
43 // TODO handle compile error correctly
44 return mp_const_none;
45 }
46
47 // complied successfully, execute it
Damien Georged17926d2014-03-30 13:35:08 +010048 return mp_call_function_0(module_fun);
Damien Georged02c6d82014-01-15 22:14:03 +000049}
50
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020051STATIC mp_obj_t mp_builtin_eval(mp_obj_t o_in) {
Damien Georgeca476792014-02-03 22:44:10 +000052 return parse_compile_execute(o_in, MP_PARSE_EVAL_INPUT);
53}
54
Damien Georged02c6d82014-01-15 22:14:03 +000055MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_eval_obj, mp_builtin_eval);
Damien Georgeca476792014-02-03 22:44:10 +000056
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +020057STATIC mp_obj_t mp_builtin_exec(uint n_args, const mp_obj_t *args) {
58 // Unconditional getting/setting assumes that these operations
59 // are cheap, which is the case when this comment was written.
Damien George7efc5b32014-04-05 22:36:42 +010060 mp_obj_dict_t *old_globals = mp_globals_get();
61 mp_obj_dict_t *old_locals = mp_locals_get();
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +020062 if (n_args > 1) {
63 mp_obj_t globals = args[1];
64 mp_obj_t locals;
65 if (n_args > 2) {
66 locals = args[2];
67 } else {
68 locals = globals;
69 }
Damien George7efc5b32014-04-05 22:36:42 +010070 mp_globals_set(globals);
71 mp_locals_set(locals);
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +020072 }
73 mp_obj_t res = parse_compile_execute(args[0], MP_PARSE_FILE_INPUT);
Damien Georgec5966122014-02-15 16:10:44 +000074 // TODO if the above call throws an exception, then we never get to reset the globals/locals
Damien Georged17926d2014-03-30 13:35:08 +010075 mp_globals_set(old_globals);
76 mp_locals_set(old_locals);
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +020077 return res;
Damien Georgeca476792014-02-03 22:44:10 +000078}
79
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +020080MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec);