builtinevex.c 2.32 KB
Newer Older
Damien George's avatar
Damien George committed
1
2
3
4
5
#include <stdint.h>

#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
6
#include "qstr.h"
Damien George's avatar
Damien George committed
7
8
9
10
#include "lexer.h"
#include "lexerunix.h"
#include "parse.h"
#include "obj.h"
11
#include "parsehelper.h"
Damien George's avatar
Damien George committed
12
13
14
15
16
#include "compile.h"
#include "runtime0.h"
#include "runtime.h"
#include "builtin.h"

17
STATIC mp_obj_t parse_compile_execute(mp_obj_t o_in, mp_parse_input_kind_t parse_input_kind) {
18
    uint str_len;
19
    const char *str = mp_obj_str_get_data(o_in, &str_len);
Damien George's avatar
Damien George committed
20
21

    // create the lexer
22
    mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, str, str_len, 0);
23
    qstr source_name = mp_lexer_source_name(lex);
Damien George's avatar
Damien George committed
24
25

    // parse the string
26
27
    mp_parse_error_kind_t parse_error_kind;
    mp_parse_node_t pn = mp_parse(lex, parse_input_kind, &parse_error_kind);
Damien George's avatar
Damien George committed
28
29
30
31
    mp_lexer_free(lex);

    if (pn == MP_PARSE_NODE_NULL) {
        // parse error; raise exception
32
        nlr_raise(mp_parse_make_exception(parse_error_kind));
Damien George's avatar
Damien George committed
33
34
35
    }

    // compile the string
36
    mp_obj_t module_fun = mp_compile(pn, source_name, false);
37
    mp_parse_node_free(pn);
Damien George's avatar
Damien George committed
38
39
40
41
42
43
44

    if (module_fun == mp_const_none) {
        // TODO handle compile error correctly
        return mp_const_none;
    }

    // complied successfully, execute it
Damien George's avatar
Damien George committed
45
    return mp_call_function_0(module_fun);
Damien George's avatar
Damien George committed
46
47
}

48
STATIC mp_obj_t mp_builtin_eval(mp_obj_t o_in) {
Damien George's avatar
Damien George committed
49
50
51
    return parse_compile_execute(o_in, MP_PARSE_EVAL_INPUT);
}

Damien George's avatar
Damien George committed
52
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_eval_obj, mp_builtin_eval);
Damien George's avatar
Damien George committed
53

54
55
56
STATIC mp_obj_t mp_builtin_exec(uint n_args, const mp_obj_t *args) {
    // Unconditional getting/setting assumes that these operations
    // are cheap, which is the case when this comment was written.
Damien George's avatar
Damien George committed
57
58
    mp_map_t *old_globals = mp_globals_get();
    mp_map_t *old_locals = mp_locals_get();
59
60
61
62
63
64
65
66
    if (n_args > 1) {
        mp_obj_t globals = args[1];
        mp_obj_t locals;
        if (n_args > 2) {
            locals = args[2];
        } else {
            locals = globals;
        }
Damien George's avatar
Damien George committed
67
68
        mp_globals_set(mp_obj_dict_get_map(globals));
        mp_locals_set(mp_obj_dict_get_map(locals));
69
70
    }
    mp_obj_t res = parse_compile_execute(args[0], MP_PARSE_FILE_INPUT);
71
    // TODO if the above call throws an exception, then we never get to reset the globals/locals
Damien George's avatar
Damien George committed
72
73
    mp_globals_set(old_globals);
    mp_locals_set(old_locals);
74
    return res;
Damien George's avatar
Damien George committed
75
76
}

77
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec);