blob: 10003c992973c168f330917aac962d68d36c924f [file] [log] [blame]
Damiende690d12013-12-29 18:01:01 +00001#include <stdint.h>
2#include <stdio.h>
3#include <string.h>
4
5#include "nlr.h"
6#include "misc.h"
7#include "mpconfig.h"
Damien George5fa93b62014-01-22 14:35:10 +00008#include "qstr.h"
Damiende690d12013-12-29 18:01:01 +00009#include "lexer.h"
10#include "lexerunix.h"
11#include "parse.h"
Damiende690d12013-12-29 18:01:01 +000012#include "obj.h"
Damien George1fb03172014-01-03 14:22:03 +000013#include "compile.h"
Damiende690d12013-12-29 18:01:01 +000014#include "runtime0.h"
Damiende690d12013-12-29 18:01:01 +000015
16void do_file(const char *file) {
17 mp_lexer_t *lex = mp_lexer_new_from_file(file);
18 if (lex == NULL) {
19 return;
20 }
21
22 if (0) {
23 // just tokenise
24 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
25 mp_token_show(mp_lexer_cur(lex));
26 mp_lexer_to_next(lex);
27 }
28 mp_lexer_free(lex);
29
30 } else {
Damien George9528cd62014-01-15 21:23:31 +000031 // parse
32 qstr parse_exc_id;
33 const char *parse_exc_msg;
34 mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT, &parse_exc_id, &parse_exc_msg);
Damiende690d12013-12-29 18:01:01 +000035
Damien George9528cd62014-01-15 21:23:31 +000036 if (pn == MP_PARSE_NODE_NULL) {
37 // parse error
38 mp_lexer_show_error_pythonic_prefix(lex);
39 printf("%s: %s\n", qstr_str(parse_exc_id), parse_exc_msg);
40 mp_lexer_free(lex);
41 return;
42 }
43
Damiende690d12013-12-29 18:01:01 +000044 mp_lexer_free(lex);
45
46 if (pn != MP_PARSE_NODE_NULL) {
47 //printf("----------------\n");
Damien Georgecbd2f742014-01-19 11:48:48 +000048 //mp_parse_node_print(pn, 0);
Damiende690d12013-12-29 18:01:01 +000049 //printf("----------------\n");
Damien George9528cd62014-01-15 21:23:31 +000050
51 // compile
Damien George08335002014-01-18 23:24:36 +000052 mp_obj_t module_fun = mp_compile(pn, 0, false);
Damien George9528cd62014-01-15 21:23:31 +000053
Damiende690d12013-12-29 18:01:01 +000054 //printf("----------------\n");
55
Damien George1fb03172014-01-03 14:22:03 +000056 if (module_fun == mp_const_none) {
Damiende690d12013-12-29 18:01:01 +000057 printf("compile error\n");
58 }
59 }
60 }
61}
62
63int main(int argc, char **argv) {
64 qstr_init();
65 rt_init();
66
67 if (argc == 2) {
68 do_file(argv[1]);
69 } else {
70 printf("usage: py [<file>]\n");
71 return 1;
72 }
73 rt_deinit();
74
75 return 0;
76}
77
78// for sqrt
79#include <math.h>
80machine_float_t machine_sqrt(machine_float_t x) {
81 return sqrt(x);
82}
Damien George4a081b12014-02-06 09:52:53 +000083
84mp_import_stat_t mp_import_stat(const char *path) {
85 return MP_IMPORT_STAT_NO_EXIST;
86}