blob: 5ca81153687972429a07cfe4881306c5950007fd [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <stdint.h>
2#include <stdio.h>
3#include <string.h>
Edd Barrett8146aea2014-01-01 23:14:36 +00004#include <stdlib.h>
Damien429d7192013-10-04 19:53:11 +01005
Damience89a212013-10-15 22:25:17 +01006#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +01007#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00008#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00009#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010010#include "lexer.h"
Damiena5185f42013-10-20 14:41:27 +010011#include "lexerunix.h"
Damien429d7192013-10-04 19:53:11 +010012#include "parse.h"
Damien0f082672013-12-17 18:33:53 +000013#include "obj.h"
Damien George1fb03172014-01-03 14:22:03 +000014#include "compile.h"
Damiend99b0522013-12-21 18:17:45 +000015#include "runtime0.h"
16#include "runtime.h"
Damien92c06562013-10-22 22:32:27 +010017#include "repl.h"
Damien429d7192013-10-04 19:53:11 +010018
Paul Sokolovskyd674bd52014-01-04 19:38:19 +020019#if MICROPY_USE_READLINE
Damien5ac1b2e2013-10-18 19:58:12 +010020#include <readline/readline.h>
Paul Sokolovsky903b24f2014-01-01 14:54:39 +020021#include <readline/history.h>
Paul Sokolovskyfa027672014-01-01 18:28:01 +020022#endif
Damien429d7192013-10-04 19:53:11 +010023
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +020024extern const mp_obj_fun_native_t mp_builtin_open_obj;
Paul Sokolovsky51ee44a2014-01-20 23:49:56 +020025void file_init();
Paul Sokolovskyfc926082014-01-18 23:47:44 +020026void rawsocket_init();
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +020027void ffi_init();
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +020028
Damien George136f6752014-01-07 14:54:15 +000029static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
30 if (lex == NULL) {
31 return;
32 }
33
34 if (0) {
35 // just tokenise
36 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
37 mp_token_show(mp_lexer_cur(lex));
38 mp_lexer_to_next(lex);
39 }
40 mp_lexer_free(lex);
41 return;
42 }
43
Damien George9528cd62014-01-15 21:23:31 +000044 qstr parse_exc_id;
45 const char *parse_exc_msg;
46 mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_exc_id, &parse_exc_msg);
Damien George136f6752014-01-07 14:54:15 +000047
48 if (pn == MP_PARSE_NODE_NULL) {
49 // parse error
Damien George9528cd62014-01-15 21:23:31 +000050 mp_lexer_show_error_pythonic_prefix(lex);
51 printf("%s: %s\n", qstr_str(parse_exc_id), parse_exc_msg);
52 mp_lexer_free(lex);
Damien George136f6752014-01-07 14:54:15 +000053 return;
54 }
55
Damien George08335002014-01-18 23:24:36 +000056 qstr source_name = mp_lexer_source_name(lex);
Damien George9528cd62014-01-15 21:23:31 +000057 mp_lexer_free(lex);
58
Damien Georgecbd2f742014-01-19 11:48:48 +000059 /*
60 printf("----------------\n");
61 mp_parse_node_print(pn, 0);
62 printf("----------------\n");
63 */
Damien George136f6752014-01-07 14:54:15 +000064
Damien George08335002014-01-18 23:24:36 +000065 mp_obj_t module_fun = mp_compile(pn, source_name, is_repl);
Damien George136f6752014-01-07 14:54:15 +000066
67 if (module_fun == mp_const_none) {
68 // compile error
69 return;
70 }
71
72 // execute it
73 nlr_buf_t nlr;
74 if (nlr_push(&nlr) == 0) {
75 rt_call_function_0(module_fun);
76 nlr_pop();
77 } else {
78 // uncaught exception
Damien George136b1492014-01-19 12:38:49 +000079 mp_obj_print_exception((mp_obj_t)nlr.ret_val);
Damien George136f6752014-01-07 14:54:15 +000080 }
81}
82
Damiend99b0522013-12-21 18:17:45 +000083static char *str_join(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +010084 int l1 = strlen(s1);
85 int l2 = strlen(s2);
86 char *s = m_new(char, l1 + l2 + 2);
87 memcpy(s, s1, l1);
88 if (sep_char != 0) {
89 s[l1] = sep_char;
90 l1 += 1;
91 }
92 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +010093 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +010094 return s;
95}
96
Paul Sokolovskyfa027672014-01-01 18:28:01 +020097static char *prompt(char *p) {
Paul Sokolovskyd674bd52014-01-04 19:38:19 +020098#if MICROPY_USE_READLINE
Paul Sokolovskyfa027672014-01-01 18:28:01 +020099 char *line = readline(p);
100 if (line) {
101 add_history(line);
102 }
103#else
104 static char buf[256];
105 fputs(p, stdout);
106 char *s = fgets(buf, sizeof(buf), stdin);
107 if (!s) {
108 return NULL;
109 }
110 int l = strlen(buf);
111 if (buf[l - 1] == '\n') {
112 buf[l - 1] = 0;
113 } else {
114 l++;
115 }
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200116 char *line = malloc(l);
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200117 memcpy(line, buf, l);
118#endif
119 return line;
120}
121
Damiend99b0522013-12-21 18:17:45 +0000122static void do_repl(void) {
Damien5ac1b2e2013-10-18 19:58:12 +0100123 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200124 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100125 if (line == NULL) {
126 // EOF
127 return;
128 }
Damiend99b0522013-12-21 18:17:45 +0000129 if (mp_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +0100130 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200131 char *line2 = prompt("... ");
Damien5ac1b2e2013-10-18 19:58:12 +0100132 if (line2 == NULL || strlen(line2) == 0) {
133 break;
134 }
135 char *line3 = str_join(line, '\n', line2);
Damien732407f2013-12-29 19:33:23 +0000136 free(line);
137 free(line2);
Damien5ac1b2e2013-10-18 19:58:12 +0100138 line = line3;
139 }
140 }
Damienfa2162b2013-10-20 17:42:00 +0100141
Damien Georgeb829b5c2014-01-25 13:51:19 +0000142 mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line, strlen(line), false);
Damien George136f6752014-01-07 14:54:15 +0000143 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200144 free(line);
Damien5ac1b2e2013-10-18 19:58:12 +0100145 }
146}
147
Damien George136f6752014-01-07 14:54:15 +0000148static void do_file(const char *file) {
Damien George66028ab2014-01-03 14:03:48 +0000149 // hack: set dir for import based on where this file is
150 {
151 const char * s = strrchr(file, '/');
152 if (s != NULL) {
153 int len = s - file;
154 char *dir = m_new(char, len + 1);
155 memcpy(dir, file, len);
156 dir[len] = '\0';
157 mp_import_set_directory(dir);
158 }
159 }
160
Damiend99b0522013-12-21 18:17:45 +0000161 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000162 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
163}
Damien429d7192013-10-04 19:53:11 +0100164
Damien George136f6752014-01-07 14:54:15 +0000165static void do_str(const char *str) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000166 mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, str, strlen(str), false);
Damien George136f6752014-01-07 14:54:15 +0000167 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100168}
Damien429d7192013-10-04 19:53:11 +0100169
Damiend99b0522013-12-21 18:17:45 +0000170typedef struct _test_obj_t {
171 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000172 int value;
Damiend99b0522013-12-21 18:17:45 +0000173} test_obj_t;
174
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200175static void test_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +0000176 test_obj_t *self = self_in;
177 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000178}
179
Damiend99b0522013-12-21 18:17:45 +0000180static mp_obj_t test_get(mp_obj_t self_in) {
181 test_obj_t *self = self_in;
182 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000183}
184
Damiend99b0522013-12-21 18:17:45 +0000185static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
186 test_obj_t *self = self_in;
187 self->value = mp_obj_get_int(arg);
188 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000189}
190
Damiend99b0522013-12-21 18:17:45 +0000191static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
192static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
193
Damien George97209d32014-01-07 15:58:30 +0000194static const mp_method_t test_methods[] = {
195 { "get", &test_get_obj },
196 { "set", &test_set_obj },
197 { NULL, NULL },
198};
199
Damiend99b0522013-12-21 18:17:45 +0000200static const mp_obj_type_t test_type = {
201 { &mp_const_type },
Damiena53f6942013-11-02 23:58:38 +0000202 "Test",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200203 .print = test_print,
Damien George97209d32014-01-07 15:58:30 +0000204 .methods = test_methods,
Damiena53f6942013-11-02 23:58:38 +0000205};
206
Damiend99b0522013-12-21 18:17:45 +0000207mp_obj_t test_obj_new(int value) {
208 test_obj_t *o = m_new_obj(test_obj_t);
209 o->base.type = &test_type;
210 o->value = value;
211 return o;
212}
213
Damien George136f6752014-01-07 14:54:15 +0000214int usage(void) {
215 printf("usage: py [-c <command>] [<filename>]\n");
216 return 1;
217}
218
Damien George4d5b28c2014-01-29 18:56:46 +0000219mp_obj_t mem_info(void) {
220 printf("mem: total=%d, current=%d, peak=%d\n", m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
221 return mp_const_none;
222}
223
224mp_obj_t qstr_info(void) {
225 uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
226 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
227 printf("qstr pool: n_pool=%u, n_qstr=%u, n_str_data_bytes=%u, n_total_bytes=%u\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
228 return mp_const_none;
229}
230
Damien5ac1b2e2013-10-18 19:58:12 +0100231int main(int argc, char **argv) {
232 qstr_init();
233 rt_init();
234
Damien George55baff42014-01-21 21:40:13 +0000235 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200236 mp_obj_t py_argv = mp_obj_new_list(0, NULL);
Damien George55baff42014-01-21 21:40:13 +0000237 rt_store_attr(m_sys, MP_QSTR_argv, py_argv);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200238
Damien George55baff42014-01-21 21:40:13 +0000239 rt_store_name(qstr_from_str("test"), test_obj_new(42));
Damien George4d5b28c2014-01-29 18:56:46 +0000240 rt_store_name(qstr_from_str("mem_info"), rt_make_function_n(0, mem_info));
241 rt_store_name(qstr_from_str("qstr_info"), rt_make_function_n(0, qstr_info));
Damien George12eacca2014-01-21 21:54:15 +0000242
Paul Sokolovsky51ee44a2014-01-20 23:49:56 +0200243 file_init();
Paul Sokolovskyfc926082014-01-18 23:47:44 +0200244 rawsocket_init();
Paul Sokolovskyed1239f2014-02-01 20:06:55 +0200245#if MICROPY_MOD_FFI
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +0200246 ffi_init();
Paul Sokolovskyed1239f2014-02-01 20:06:55 +0200247#endif
Damiena53f6942013-11-02 23:58:38 +0000248
Damien George062478e2014-01-09 20:57:50 +0000249 // Here is some example code to create a class and instance of that class.
250 // First is the Python, then the C code.
251 //
252 // class TestClass:
253 // pass
254 // test_obj = TestClass()
255 // test_obj.attr = 42
256 mp_obj_t test_class_type, test_class_instance;
Damien George5fa93b62014-01-22 14:35:10 +0000257 test_class_type = mp_obj_new_type("TestClass", mp_const_empty_tuple, mp_obj_new_dict(0));
Damien George55baff42014-01-21 21:40:13 +0000258 rt_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = rt_call_function_0(test_class_type));
259 rt_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
Damien George062478e2014-01-09 20:57:50 +0000260
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000261 /*
262 printf("bytes:\n");
263 printf(" total %d\n", m_get_total_bytes_allocated());
264 printf(" cur %d\n", m_get_current_bytes_allocated());
265 printf(" peak %d\n", m_get_peak_bytes_allocated());
266 */
267
Damien5ac1b2e2013-10-18 19:58:12 +0100268 if (argc == 1) {
269 do_repl();
Damien5ac1b2e2013-10-18 19:58:12 +0100270 } else {
Damien George136f6752014-01-07 14:54:15 +0000271 for (int a = 1; a < argc; a++) {
272 if (argv[a][0] == '-') {
273 if (strcmp(argv[a], "-c") == 0) {
274 if (a + 1 >= argc) {
275 return usage();
276 }
277 do_str(argv[a + 1]);
278 a += 1;
279 } else {
280 return usage();
281 }
282 } else {
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200283 for (int i = a; i < argc; i++) {
Damien George55baff42014-01-21 21:40:13 +0000284 rt_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200285 }
Damien George136f6752014-01-07 14:54:15 +0000286 do_file(argv[a]);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200287 break;
Damien George136f6752014-01-07 14:54:15 +0000288 }
289 }
Damien5ac1b2e2013-10-18 19:58:12 +0100290 }
Damien George136f6752014-01-07 14:54:15 +0000291
Damien429d7192013-10-04 19:53:11 +0100292 rt_deinit();
293
294 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
295 return 0;
296}
Damien087d2182013-11-09 20:14:30 +0000297
298// for sqrt
299#include <math.h>
300machine_float_t machine_sqrt(machine_float_t x) {
301 return sqrt(x);
302}