blob: 03db8627389a3c878023a29069aadbb669b152cc [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"
16#include "compile.h"
17#include "runtime0.h"
18#include "runtime.h"
19#include "map.h"
20#include "builtin.h"
21
Damien Georgeca476792014-02-03 22:44:10 +000022static 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 +000023 uint str_len;
Damien George5fa93b62014-01-22 14:35:10 +000024 const byte *str = mp_obj_str_get_data(o_in, &str_len);
Damien Georged02c6d82014-01-15 22:14:03 +000025
26 // create the lexer
Damien Georgeb829b5c2014-01-25 13:51:19 +000027 mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, (const char*)str, str_len, 0);
28 qstr source_name = mp_lexer_source_name(lex);
Damien Georged02c6d82014-01-15 22:14:03 +000029
30 // parse the string
31 qstr parse_exc_id;
32 const char *parse_exc_msg;
Damien Georgeca476792014-02-03 22:44:10 +000033 mp_parse_node_t pn = mp_parse(lex, parse_input_kind, &parse_exc_id, &parse_exc_msg);
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
38 nlr_jump(mp_obj_new_exception_msg(parse_exc_id, parse_exc_msg));
39 }
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
Damien Georgeca476792014-02-03 22:44:10 +000054static mp_obj_t mp_builtin_eval(mp_obj_t o_in) {
55 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
60static mp_obj_t mp_builtin_exec(mp_obj_t o_in) {
61 return parse_compile_execute(o_in, MP_PARSE_FILE_INPUT);
62}
63
64MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_exec_obj, mp_builtin_exec);