main.c 4.69 KB
Newer Older
Damien's avatar
Damien committed
1
2
3
4
#include <stdint.h>
#include <stdio.h>
#include <string.h>

5
#include "nlr.h"
Damien's avatar
Damien committed
6
#include "misc.h"
7
#include "mpyconfig.h"
Damien's avatar
Damien committed
8
#include "lexer.h"
9
#include "lexerunix.h"
Damien's avatar
Damien committed
10
11
12
#include "parse.h"
#include "compile.h"
#include "runtime.h"
13
#include "repl.h"
Damien's avatar
Damien committed
14

Damien's avatar
Damien committed
15
#include <readline/readline.h>
Damien's avatar
Damien committed
16

Damien's avatar
Damien committed
17
18
19
20
21
22
23
24
25
26
char *str_join(const char *s1, int sep_char, const char *s2) {
    int l1 = strlen(s1);
    int l2 = strlen(s2);
    char *s = m_new(char, l1 + l2 + 2);
    memcpy(s, s1, l1);
    if (sep_char != 0) {
        s[l1] = sep_char;
        l1 += 1;
    }
    memcpy(s + l1, s2, l2);
27
    s[l1 + l2] = 0;
Damien's avatar
Damien committed
28
29
30
    return s;
}

31
void do_repl(void) {
Damien's avatar
Damien committed
32
33
34
35
36
37
    for (;;) {
        char *line = readline(">>> ");
        if (line == NULL) {
            // EOF
            return;
        }
38
        if (py_repl_is_compound_stmt(line)) {
Damien's avatar
Damien committed
39
40
41
42
43
44
45
46
47
48
49
            for (;;) {
                char *line2 = readline("... ");
                if (line2 == NULL || strlen(line2) == 0) {
                    break;
                }
                char *line3 = str_join(line, '\n', line2);
                m_free(line);
                m_free(line2);
                line = line3;
            }
        }
50

51
        py_lexer_t *lex = py_lexer_new_from_str_len("<stdin>", line, strlen(line), false);
Damien's avatar
Damien committed
52
        py_parse_node_t pn = py_parse(lex, PY_PARSE_SINGLE_INPUT);
53
54
        py_lexer_free(lex);

Damien's avatar
Damien committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
        if (pn != PY_PARSE_NODE_NULL) {
            //py_parse_node_show(pn, 0);
            bool comp_ok = py_compile(pn, true);
            if (comp_ok) {
                py_obj_t module_fun = rt_make_function_from_id(1);
                if (module_fun != py_const_none) {
                    nlr_buf_t nlr;
                    if (nlr_push(&nlr) == 0) {
                        rt_call_function_0(module_fun);
                        nlr_pop();
                    } else {
                        // uncaught exception
                        py_obj_print((py_obj_t)nlr.ret_val);
                        printf("\n");
                    }
                }
            }
        }
Damien's avatar
Damien committed
73
    }
Damien's avatar
Damien committed
74
75
76
}

void do_file(const char *file) {
77
    py_lexer_t *lex = py_lexer_new_from_file(file);
Damien's avatar
Damien committed
78
79
80
    //const char *pysrc = "def f():\n  x=x+1\n  print(42)\n";
    //py_lexer_t *lex = py_lexer_from_str_len("<>", pysrc, strlen(pysrc), false);
    if (lex == NULL) {
Damien's avatar
Damien committed
81
        return;
Damien's avatar
Damien committed
82
83
84
    }

    if (0) {
Damien's avatar
Damien committed
85
        // just tokenise
Damien's avatar
Damien committed
86
87
88
89
        while (!py_lexer_is_kind(lex, PY_TOKEN_END)) {
            py_token_show(py_lexer_cur(lex));
            py_lexer_to_next(lex);
        }
Damien's avatar
Damien committed
90
91
        py_lexer_free(lex);

Damien's avatar
Damien committed
92
    } else {
Damien's avatar
Damien committed
93
94
95
        // compile

        py_parse_node_t pn = py_parse(lex, PY_PARSE_FILE_INPUT);
96
97
        py_lexer_free(lex);

98
99
        if (pn != PY_PARSE_NODE_NULL) {
            //printf("----------------\n");
100
            //parse_node_show(pn, 0);
101
            //printf("----------------\n");
Damien's avatar
Damien committed
102
            bool comp_ok = py_compile(pn, false);
103
            //printf("----------------\n");
Damien's avatar
Damien committed
104

105
106
107
108
109
#if MICROPY_EMIT_CPYTHON
            if (!comp_ok) {
                printf("compile error\n");
            }
#else
Damien's avatar
Damien committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
            if (1 && comp_ok) {
                // execute it
                py_obj_t module_fun = rt_make_function_from_id(1);
                if (module_fun != py_const_none) {
                    nlr_buf_t nlr;
                    if (nlr_push(&nlr) == 0) {
                        py_obj_t ret = rt_call_function_0(module_fun);
                        printf("done! got: ");
                        py_obj_print(ret);
                        printf("\n");
                        nlr_pop();
                    } else {
                        // uncaught exception
                        printf("exception: ");
                        py_obj_print((py_obj_t)nlr.ret_val);
                        printf("\n");
                    }
                }
128
            }
Damien's avatar
Damien committed
129
#endif
Damien's avatar
Damien committed
130
131
        }
    }
Damien's avatar
Damien committed
132
}
Damien's avatar
Damien committed
133

Damien's avatar
Damien committed
134
135
136
137
138
139
140
void test_print(py_obj_t o_in) {
    printf("<test>");
}

py_obj_t test_get(py_obj_t o_in) {
    py_obj_t d1;
    py_obj_t d2;
Damien's avatar
Damien committed
141
    py_user_get_data(o_in, (machine_uint_t*)&d1, (machine_uint_t*)&d2);
Damien's avatar
Damien committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    return d1;
}

py_obj_t test_set(py_obj_t o_in, py_obj_t arg) {
    py_user_set_data(o_in, (machine_uint_t)arg, (machine_uint_t)arg);
    return py_const_none;
}

const py_user_info_t test_obj_info = {
    "Test",
    test_print,
    {
        { "get", 0, test_get },
        { "set", 1, test_set },
        { NULL, 0, NULL },
    }
};

Damien's avatar
Damien committed
160
161
162
163
int main(int argc, char **argv) {
    qstr_init();
    rt_init();

Damien's avatar
Damien committed
164
    rt_store_name(qstr_from_str_static("test"), py_obj_new_user(&test_obj_info, (machine_uint_t)py_obj_new_int(42), 0));
Damien's avatar
Damien committed
165

Damien's avatar
Damien committed
166
167
168
169
170
171
172
173
    if (argc == 1) {
        do_repl();
    } else if (argc == 2) {
        do_file(argv[1]);
    } else {
        printf("usage: py [<file>]\n");
        return 1;
    }
Damien's avatar
Damien committed
174
175
176
177
178
    rt_deinit();

    //printf("total bytes = %d\n", m_get_total_bytes_allocated());
    return 0;
}