blob: 7d56ceaf343ef57e354884f63ab61822aff1decc [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"
8#include "lexer.h"
9#include "lexerunix.h"
10#include "parse.h"
Damiende690d12013-12-29 18:01:01 +000011#include "obj.h"
Damien George1fb03172014-01-03 14:22:03 +000012#include "compile.h"
Damiende690d12013-12-29 18:01:01 +000013#include "runtime0.h"
Damiende690d12013-12-29 18:01:01 +000014
15void do_file(const char *file) {
16 mp_lexer_t *lex = mp_lexer_new_from_file(file);
17 if (lex == NULL) {
18 return;
19 }
20
21 if (0) {
22 // just tokenise
23 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
24 mp_token_show(mp_lexer_cur(lex));
25 mp_lexer_to_next(lex);
26 }
27 mp_lexer_free(lex);
28
29 } else {
Damien George9528cd62014-01-15 21:23:31 +000030 // parse
31 qstr parse_exc_id;
32 const char *parse_exc_msg;
33 mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT, &parse_exc_id, &parse_exc_msg);
Damiende690d12013-12-29 18:01:01 +000034
Damien George9528cd62014-01-15 21:23:31 +000035 if (pn == MP_PARSE_NODE_NULL) {
36 // parse error
37 mp_lexer_show_error_pythonic_prefix(lex);
38 printf("%s: %s\n", qstr_str(parse_exc_id), parse_exc_msg);
39 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");
47 //parse_node_show(pn, 0);
48 //printf("----------------\n");
Damien George9528cd62014-01-15 21:23:31 +000049
50 // compile
Damien George1fb03172014-01-03 14:22:03 +000051 mp_obj_t module_fun = mp_compile(pn, 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}