main.c 1.79 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
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "lexer.h"
#include "parse.h"
#include "compile.h"
#include "runtime.h"

int main(int argc, char **argv) {
    qstr_init();
    rt_init();

    if (argc != 2) {
        printf("usage: py <file>\n");
        return 1;
    }
    py_lexer_t *lex = py_lexer_from_file(argv[1]);
    //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) {
        return 1;
    }

    if (0) {
        while (!py_lexer_is_kind(lex, PY_TOKEN_END)) {
            py_token_show(py_lexer_cur(lex));
            py_lexer_to_next(lex);
        }
    } else {
        py_parse_node_t pn = py_parse(lex, 0);
35
36
        if (pn != PY_PARSE_NODE_NULL) {
            //printf("----------------\n");
37
            //parse_node_show(pn, 0);
38
39
40
41
            //printf("----------------\n");
            py_compile(pn);
            //printf("----------------\n");
        }
Damien's avatar
Damien committed
42
43
44
45
    }

    py_lexer_free(lex);

46
#if !MICROPY_EMIT_CPYTHON
47
    if (1) {
Damien's avatar
Damien committed
48
49
50
        // execute it
        py_obj_t module_fun = rt_make_function_from_id(1);
        if (module_fun != py_const_none) {
51
52
53
54
55
56
57
58
59
60
61
62
63
            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");
            }
Damien's avatar
Damien committed
64
65
        }
    }
66
#endif
Damien's avatar
Damien committed
67
68
69
70
71
72

    rt_deinit();

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