blob: 6e920e85f075ad247f315e22f9e6d15c225bc216 [file] [log] [blame]
Damien Georged02c6d82014-01-15 22:14:03 +00001#include <stdint.h>
2#include <stdlib.h>
3#include <stdio.h>
4#include <stdarg.h>
5#include <string.h>
6#include <assert.h>
7
8#include "nlr.h"
9#include "misc.h"
10#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000011#include "qstr.h"
Damien Georged02c6d82014-01-15 22:14:03 +000012#include "lexer.h"
13#include "lexerunix.h"
14#include "parse.h"
15#include "obj.h"
Damien Georgec5966122014-02-15 16:10:44 +000016#include "parsehelper.h"
Damien Georged02c6d82014-01-15 22:14:03 +000017#include "compile.h"
18#include "runtime0.h"
19#include "runtime.h"
20#include "map.h"
21#include "builtin.h"
22
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020023STATIC 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 +000024 uint str_len;
Damien George698ec212014-02-08 18:17:23 +000025 const char *str = mp_obj_str_get_data(o_in, &str_len);
Damien Georged02c6d82014-01-15 22:14:03 +000026
27 // create the lexer
Damien George698ec212014-02-08 18:17:23 +000028 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 +000029 qstr source_name = mp_lexer_source_name(lex);
Damien Georged02c6d82014-01-15 22:14:03 +000030
31 // parse the string
Damien Georgec5966122014-02-15 16:10:44 +000032 mp_parse_error_kind_t parse_error_kind;
33 mp_parse_node_t pn = mp_parse(lex, parse_input_kind, &parse_error_kind);
Damien Georged02c6d82014-01-15 22:14:03 +000034 mp_lexer_free(lex);
35
36 if (pn == MP_PARSE_NODE_NULL) {
37 // parse error; raise exception
Damien Georgec5966122014-02-15 16:10:44 +000038 nlr_jump(mp_parse_make_exception(parse_error_kind));
Damien Georged02c6d82014-01-15 22:14:03 +000039 }
40
41 // compile the string
Damien George08335002014-01-18 23:24:36 +000042 mp_obj_t module_fun = mp_compile(pn, source_name, false);
Damien Georgeb829b5c2014-01-25 13:51:19 +000043 mp_parse_node_free(pn);
Damien Georged02c6d82014-01-15 22:14:03 +000044
45 if (module_fun == mp_const_none) {
46 // TODO handle compile error correctly
47 return mp_const_none;
48 }
49
50 // complied successfully, execute it
51 return rt_call_function_0(module_fun);
52}
53
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020054STATIC mp_obj_t mp_builtin_eval(mp_obj_t o_in) {
Damien Georgeca476792014-02-03 22:44:10 +000055 return parse_compile_execute(o_in, MP_PARSE_EVAL_INPUT);
56}
57
Damien Georged02c6d82014-01-15 22:14:03 +000058MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_eval_obj, mp_builtin_eval);
Damien Georgeca476792014-02-03 22:44:10 +000059
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +020060STATIC mp_obj_t mp_builtin_exec(uint n_args, const mp_obj_t *args) {
61 // Unconditional getting/setting assumes that these operations
62 // are cheap, which is the case when this comment was written.
63 mp_map_t *old_globals = rt_globals_get();
64 mp_map_t *old_locals = rt_locals_get();
65 if (n_args > 1) {
66 mp_obj_t globals = args[1];
67 mp_obj_t locals;
68 if (n_args > 2) {
69 locals = args[2];
70 } else {
71 locals = globals;
72 }
73 rt_globals_set(mp_obj_dict_get_map(globals));
74 rt_locals_set(mp_obj_dict_get_map(locals));
75 }
76 mp_obj_t res = parse_compile_execute(args[0], MP_PARSE_FILE_INPUT);
Damien Georgec5966122014-02-15 16:10:44 +000077 // TODO if the above call throws an exception, then we never get to reset the globals/locals
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +020078 rt_globals_set(old_globals);
79 rt_locals_set(old_locals);
80 return res;
Damien Georgeca476792014-02-03 22:44:10 +000081}
82
Paul Sokolovsky2aa217b2014-02-13 00:36:54 +020083MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec);