main.c 3.02 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

27
#include <stdlib.h>
28
29
30
31
#include <stdint.h>
#include <stdio.h>
#include <string.h>

32
#include "mpconfig.h"
33
34
#include "nlr.h"
#include "misc.h"
35
#include "qstr.h"
36
37
38
39
#include "lexer.h"
#include "lexerunix.h"
#include "parse.h"
#include "obj.h"
40
#include "parsehelper.h"
41
#include "compile.h"
42
#include "runtime.h"
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

void do_file(const char *file) {
    mp_lexer_t *lex = mp_lexer_new_from_file(file);
    if (lex == NULL) {
        return;
    }

    if (0) {
        // just tokenise
        while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
            mp_token_show(mp_lexer_cur(lex));
            mp_lexer_to_next(lex);
        }
        mp_lexer_free(lex);

    } else {
59
        // parse
60
61
        mp_parse_error_kind_t parse_error_kind;
        mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT, &parse_error_kind);
62
63
64

        if (pn == MP_PARSE_NODE_NULL) {
            // parse error
65
            mp_parse_show_exception(lex, parse_error_kind);
66
67
68
            mp_lexer_free(lex);
            return;
        }
69
70
71
72
73

        mp_lexer_free(lex);

        if (pn != MP_PARSE_NODE_NULL) {
            //printf("----------------\n");
74
            //mp_parse_node_print(pn, 0);
75
            //printf("----------------\n");
76
77

            // compile
78
            mp_obj_t module_fun = mp_compile(pn, 0, MP_EMIT_OPT_NONE, false);
79

80
81
            //printf("----------------\n");

82
            if (module_fun == mp_const_none) {
83
84
85
86
87
88
89
                printf("compile error\n");
            }
        }
    }
}

int main(int argc, char **argv) {
Damien George's avatar
Damien George committed
90
    mp_init();
91
92
93
94
95
96
97

    if (argc == 2) {
        do_file(argv[1]);
    } else {
        printf("usage: py [<file>]\n");
        return 1;
    }
Damien George's avatar
Damien George committed
98
    mp_deinit();
99
100
101
102

    return 0;
}

103
104
105
mp_import_stat_t mp_import_stat(const char *path) {
    return MP_IMPORT_STAT_NO_EXIST;
}
106
107
108
109
110

void nlr_jump_fail(void *val) {
    printf("FATAL: uncaught NLR %p\n", val);
    exit(1);
}