blob: 52d63273cee5942355d9f8231ea0e9e7dded91b5 [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 Georgec5966122014-02-15 16:10:44 +000013#include "parsehelper.h"
Damien George1fb03172014-01-03 14:22:03 +000014#include "compile.h"
Damiende690d12013-12-29 18:01:01 +000015#include "runtime0.h"
Damiende690d12013-12-29 18:01:01 +000016
17void do_file(const char *file) {
18 mp_lexer_t *lex = mp_lexer_new_from_file(file);
19 if (lex == NULL) {
20 return;
21 }
22
23 if (0) {
24 // just tokenise
25 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
26 mp_token_show(mp_lexer_cur(lex));
27 mp_lexer_to_next(lex);
28 }
29 mp_lexer_free(lex);
30
31 } else {
Damien George9528cd62014-01-15 21:23:31 +000032 // parse
Damien Georgec5966122014-02-15 16:10:44 +000033 mp_parse_error_kind_t parse_error_kind;
34 mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT, &parse_error_kind);
Damiende690d12013-12-29 18:01:01 +000035
Damien George9528cd62014-01-15 21:23:31 +000036 if (pn == MP_PARSE_NODE_NULL) {
37 // parse error
Damien Georgec5966122014-02-15 16:10:44 +000038 mp_parse_show_exception(lex, parse_error_kind);
Damien George9528cd62014-01-15 21:23:31 +000039 mp_lexer_free(lex);
40 return;
41 }
42
Damiende690d12013-12-29 18:01:01 +000043 mp_lexer_free(lex);
44
45 if (pn != MP_PARSE_NODE_NULL) {
46 //printf("----------------\n");
Damien Georgecbd2f742014-01-19 11:48:48 +000047 //mp_parse_node_print(pn, 0);
Damiende690d12013-12-29 18:01:01 +000048 //printf("----------------\n");
Damien George9528cd62014-01-15 21:23:31 +000049
50 // compile
Damien George08335002014-01-18 23:24:36 +000051 mp_obj_t module_fun = mp_compile(pn, 0, false);
Damien George9528cd62014-01-15 21:23:31 +000052
Damiende690d12013-12-29 18:01:01 +000053 //printf("----------------\n");
54
Damien George1fb03172014-01-03 14:22:03 +000055 if (module_fun == mp_const_none) {
Damiende690d12013-12-29 18:01:01 +000056 printf("compile error\n");
57 }
58 }
59 }
60}
61
62int main(int argc, char **argv) {
63 qstr_init();
64 rt_init();
65
66 if (argc == 2) {
67 do_file(argv[1]);
68 } else {
69 printf("usage: py [<file>]\n");
70 return 1;
71 }
72 rt_deinit();
73
74 return 0;
75}
76
77// for sqrt
78#include <math.h>
79machine_float_t machine_sqrt(machine_float_t x) {
80 return sqrt(x);
81}
Damien George4a081b12014-02-06 09:52:53 +000082
83mp_import_stat_t mp_import_stat(const char *path) {
84 return MP_IMPORT_STAT_NO_EXIST;
85}