blob: 16012e71811a5c55071d11ce9f8988211626a528 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <stdint.h>
2#include <stdio.h>
3#include <string.h>
4
Damience89a212013-10-15 22:25:17 +01005#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +01006#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00007#include "mpconfig.h"
Damien429d7192013-10-04 19:53:11 +01008#include "lexer.h"
Damiena5185f42013-10-20 14:41:27 +01009#include "lexerunix.h"
Damien429d7192013-10-04 19:53:11 +010010#include "parse.h"
11#include "compile.h"
Damien0f082672013-12-17 18:33:53 +000012#include "obj.h"
Damiend99b0522013-12-21 18:17:45 +000013#include "runtime0.h"
14#include "runtime.h"
Damien92c06562013-10-22 22:32:27 +010015#include "repl.h"
Damien429d7192013-10-04 19:53:11 +010016
Damien5ac1b2e2013-10-18 19:58:12 +010017#include <readline/readline.h>
Damien429d7192013-10-04 19:53:11 +010018
Damiend99b0522013-12-21 18:17:45 +000019static char *str_join(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +010020 int l1 = strlen(s1);
21 int l2 = strlen(s2);
22 char *s = m_new(char, l1 + l2 + 2);
23 memcpy(s, s1, l1);
24 if (sep_char != 0) {
25 s[l1] = sep_char;
26 l1 += 1;
27 }
28 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +010029 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +010030 return s;
31}
32
Damiend99b0522013-12-21 18:17:45 +000033static void do_repl(void) {
Damien5ac1b2e2013-10-18 19:58:12 +010034 for (;;) {
35 char *line = readline(">>> ");
36 if (line == NULL) {
37 // EOF
38 return;
39 }
Damiend99b0522013-12-21 18:17:45 +000040 if (mp_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +010041 for (;;) {
42 char *line2 = readline("... ");
43 if (line2 == NULL || strlen(line2) == 0) {
44 break;
45 }
46 char *line3 = str_join(line, '\n', line2);
47 m_free(line);
48 m_free(line2);
49 line = line3;
50 }
51 }
Damienfa2162b2013-10-20 17:42:00 +010052
Damiend99b0522013-12-21 18:17:45 +000053 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", line, strlen(line), false);
54 mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT);
55 mp_lexer_free(lex);
Damienfa2162b2013-10-20 17:42:00 +010056
Damiend99b0522013-12-21 18:17:45 +000057 if (pn != MP_PARSE_NODE_NULL) {
58 //mp_parse_node_show(pn, 0);
59 bool comp_ok = mp_compile(pn, true);
Damien5ac1b2e2013-10-18 19:58:12 +010060 if (comp_ok) {
Damiend99b0522013-12-21 18:17:45 +000061 mp_obj_t module_fun = rt_make_function_from_id(1);
62 if (module_fun != mp_const_none) {
Damien5ac1b2e2013-10-18 19:58:12 +010063 nlr_buf_t nlr;
64 if (nlr_push(&nlr) == 0) {
65 rt_call_function_0(module_fun);
66 nlr_pop();
67 } else {
68 // uncaught exception
Damiend99b0522013-12-21 18:17:45 +000069 mp_obj_print((mp_obj_t)nlr.ret_val);
Damien5ac1b2e2013-10-18 19:58:12 +010070 printf("\n");
71 }
72 }
73 }
74 }
75 }
76}
77
78void do_file(const char *file) {
Damiend99b0522013-12-21 18:17:45 +000079 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien429d7192013-10-04 19:53:11 +010080 //const char *pysrc = "def f():\n x=x+1\n print(42)\n";
Damiend99b0522013-12-21 18:17:45 +000081 //mp_lexer_t *lex = mp_lexer_from_str_len("<>", pysrc, strlen(pysrc), false);
Damien429d7192013-10-04 19:53:11 +010082 if (lex == NULL) {
Damien5ac1b2e2013-10-18 19:58:12 +010083 return;
Damien429d7192013-10-04 19:53:11 +010084 }
85
86 if (0) {
Damien5ac1b2e2013-10-18 19:58:12 +010087 // just tokenise
Damiend99b0522013-12-21 18:17:45 +000088 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
89 mp_token_show(mp_lexer_cur(lex));
90 mp_lexer_to_next(lex);
Damien429d7192013-10-04 19:53:11 +010091 }
Damiend99b0522013-12-21 18:17:45 +000092 mp_lexer_free(lex);
Damien5ac1b2e2013-10-18 19:58:12 +010093
Damien429d7192013-10-04 19:53:11 +010094 } else {
Damien5ac1b2e2013-10-18 19:58:12 +010095 // compile
96
Damiend99b0522013-12-21 18:17:45 +000097 mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT);
98 mp_lexer_free(lex);
Damienfa2162b2013-10-20 17:42:00 +010099
Damiend99b0522013-12-21 18:17:45 +0000100 if (pn != MP_PARSE_NODE_NULL) {
Damien91d387d2013-10-09 15:09:52 +0100101 //printf("----------------\n");
Damiena3977762013-10-09 23:10:10 +0100102 //parse_node_show(pn, 0);
Damien91d387d2013-10-09 15:09:52 +0100103 //printf("----------------\n");
Damiend99b0522013-12-21 18:17:45 +0000104 bool comp_ok = mp_compile(pn, false);
Damien91d387d2013-10-09 15:09:52 +0100105 //printf("----------------\n");
Damien429d7192013-10-04 19:53:11 +0100106
Damiena5185f42013-10-20 14:41:27 +0100107#if MICROPY_EMIT_CPYTHON
108 if (!comp_ok) {
109 printf("compile error\n");
110 }
111#else
Damien5ac1b2e2013-10-18 19:58:12 +0100112 if (1 && comp_ok) {
113 // execute it
Damiend99b0522013-12-21 18:17:45 +0000114 mp_obj_t module_fun = rt_make_function_from_id(1);
115 if (module_fun != mp_const_none) {
Damien5ac1b2e2013-10-18 19:58:12 +0100116 nlr_buf_t nlr;
117 if (nlr_push(&nlr) == 0) {
Damienb86e3f92013-12-29 17:17:43 +0000118 rt_call_function_0(module_fun);
Damien5ac1b2e2013-10-18 19:58:12 +0100119 nlr_pop();
120 } else {
121 // uncaught exception
Damiend99b0522013-12-21 18:17:45 +0000122 mp_obj_print((mp_obj_t)nlr.ret_val);
Damien5ac1b2e2013-10-18 19:58:12 +0100123 printf("\n");
124 }
125 }
Damience89a212013-10-15 22:25:17 +0100126 }
Damien5ac1b2e2013-10-18 19:58:12 +0100127#endif
Damien429d7192013-10-04 19:53:11 +0100128 }
129 }
Damien5ac1b2e2013-10-18 19:58:12 +0100130}
Damien429d7192013-10-04 19:53:11 +0100131
Damiend99b0522013-12-21 18:17:45 +0000132typedef struct _test_obj_t {
133 mp_obj_base_t base;
134 bool value;
135} test_obj_t;
136
137static void test_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
138 test_obj_t *self = self_in;
139 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000140}
141
Damiend99b0522013-12-21 18:17:45 +0000142static mp_obj_t test_get(mp_obj_t self_in) {
143 test_obj_t *self = self_in;
144 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000145}
146
Damiend99b0522013-12-21 18:17:45 +0000147static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
148 test_obj_t *self = self_in;
149 self->value = mp_obj_get_int(arg);
150 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000151}
152
Damiend99b0522013-12-21 18:17:45 +0000153static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
154static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
155
156static const mp_obj_type_t test_type = {
157 { &mp_const_type },
Damiena53f6942013-11-02 23:58:38 +0000158 "Test",
Damiend99b0522013-12-21 18:17:45 +0000159 test_print, // print
160 NULL, // call_n
161 NULL, // unary_op
162 NULL, // binary_op
163 NULL, // getiter
164 NULL, // iternext
165 { // method list
166 { "get", &test_get_obj },
167 { "set", &test_set_obj },
168 { NULL, NULL },
Damiena53f6942013-11-02 23:58:38 +0000169 }
170};
171
Damiend99b0522013-12-21 18:17:45 +0000172mp_obj_t test_obj_new(int value) {
173 test_obj_t *o = m_new_obj(test_obj_t);
174 o->base.type = &test_type;
175 o->value = value;
176 return o;
177}
178
Damien5ac1b2e2013-10-18 19:58:12 +0100179int main(int argc, char **argv) {
180 qstr_init();
181 rt_init();
182
Damiend99b0522013-12-21 18:17:45 +0000183 rt_store_name(qstr_from_str_static("test"), test_obj_new(42));
Damiena53f6942013-11-02 23:58:38 +0000184
Damien5ac1b2e2013-10-18 19:58:12 +0100185 if (argc == 1) {
186 do_repl();
187 } else if (argc == 2) {
188 do_file(argv[1]);
189 } else {
190 printf("usage: py [<file>]\n");
191 return 1;
192 }
Damien429d7192013-10-04 19:53:11 +0100193 rt_deinit();
194
195 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
196 return 0;
197}
Damien087d2182013-11-09 20:14:30 +0000198
199// for sqrt
200#include <math.h>
201machine_float_t machine_sqrt(machine_float_t x) {
202 return sqrt(x);
203}