blob: 35fca2059a590a1c0e2bdb1b2c7e50030d5788ae [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"
Damien429d7192013-10-04 19:53:11 +01009#include "lexer.h"
Damiena5185f42013-10-20 14:41:27 +010010#include "lexerunix.h"
Damien429d7192013-10-04 19:53:11 +010011#include "parse.h"
Damien0f082672013-12-17 18:33:53 +000012#include "obj.h"
Damien George1fb03172014-01-03 14:22:03 +000013#include "compile.h"
Damiend99b0522013-12-21 18:17:45 +000014#include "runtime0.h"
15#include "runtime.h"
Damien92c06562013-10-22 22:32:27 +010016#include "repl.h"
Damien429d7192013-10-04 19:53:11 +010017
Paul Sokolovskyd674bd52014-01-04 19:38:19 +020018#if MICROPY_USE_READLINE
Damien5ac1b2e2013-10-18 19:58:12 +010019#include <readline/readline.h>
Paul Sokolovsky903b24f2014-01-01 14:54:39 +020020#include <readline/history.h>
Paul Sokolovskyfa027672014-01-01 18:28:01 +020021#endif
Damien429d7192013-10-04 19:53:11 +010022
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +020023extern const mp_obj_fun_native_t mp_builtin_open_obj;
Paul Sokolovskyfc926082014-01-18 23:47:44 +020024void rawsocket_init();
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +020025
Damien George136f6752014-01-07 14:54:15 +000026static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
27 if (lex == NULL) {
28 return;
29 }
30
31 if (0) {
32 // just tokenise
33 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
34 mp_token_show(mp_lexer_cur(lex));
35 mp_lexer_to_next(lex);
36 }
37 mp_lexer_free(lex);
38 return;
39 }
40
Damien George9528cd62014-01-15 21:23:31 +000041 qstr parse_exc_id;
42 const char *parse_exc_msg;
43 mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_exc_id, &parse_exc_msg);
Damien George136f6752014-01-07 14:54:15 +000044
45 if (pn == MP_PARSE_NODE_NULL) {
46 // parse error
Damien George9528cd62014-01-15 21:23:31 +000047 mp_lexer_show_error_pythonic_prefix(lex);
48 printf("%s: %s\n", qstr_str(parse_exc_id), parse_exc_msg);
49 mp_lexer_free(lex);
Damien George136f6752014-01-07 14:54:15 +000050 return;
51 }
52
Damien George08335002014-01-18 23:24:36 +000053 qstr source_name = mp_lexer_source_name(lex);
Damien George9528cd62014-01-15 21:23:31 +000054 mp_lexer_free(lex);
55
Damien Georgecbd2f742014-01-19 11:48:48 +000056 /*
57 printf("----------------\n");
58 mp_parse_node_print(pn, 0);
59 printf("----------------\n");
60 */
Damien George136f6752014-01-07 14:54:15 +000061
Damien George08335002014-01-18 23:24:36 +000062 mp_obj_t module_fun = mp_compile(pn, source_name, is_repl);
Damien George136f6752014-01-07 14:54:15 +000063
64 if (module_fun == mp_const_none) {
65 // compile error
66 return;
67 }
68
69 // execute it
70 nlr_buf_t nlr;
71 if (nlr_push(&nlr) == 0) {
72 rt_call_function_0(module_fun);
73 nlr_pop();
74 } else {
75 // uncaught exception
Damien George08335002014-01-18 23:24:36 +000076 mp_obj_t exc = (mp_obj_t)nlr.ret_val;
77 if (MP_OBJ_IS_TYPE(exc, &exception_type)) {
Damien Georgecbd2f742014-01-19 11:48:48 +000078 qstr file, block;
Damien George08335002014-01-18 23:24:36 +000079 machine_uint_t line;
Damien Georgecbd2f742014-01-19 11:48:48 +000080 mp_obj_exception_get_source_info(exc, &file, &line, &block);
81 printf("File \"%s\", line %d, in %s\n", qstr_str(file), (int)line, qstr_str(block));
Damien George08335002014-01-18 23:24:36 +000082 }
83 mp_obj_print(exc, PRINT_REPR);
Damien George136f6752014-01-07 14:54:15 +000084 printf("\n");
85 }
86}
87
Damiend99b0522013-12-21 18:17:45 +000088static char *str_join(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +010089 int l1 = strlen(s1);
90 int l2 = strlen(s2);
91 char *s = m_new(char, l1 + l2 + 2);
92 memcpy(s, s1, l1);
93 if (sep_char != 0) {
94 s[l1] = sep_char;
95 l1 += 1;
96 }
97 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +010098 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +010099 return s;
100}
101
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200102static char *prompt(char *p) {
Paul Sokolovskyd674bd52014-01-04 19:38:19 +0200103#if MICROPY_USE_READLINE
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200104 char *line = readline(p);
105 if (line) {
106 add_history(line);
107 }
108#else
109 static char buf[256];
110 fputs(p, stdout);
111 char *s = fgets(buf, sizeof(buf), stdin);
112 if (!s) {
113 return NULL;
114 }
115 int l = strlen(buf);
116 if (buf[l - 1] == '\n') {
117 buf[l - 1] = 0;
118 } else {
119 l++;
120 }
121 char *line = m_new(char, l);
122 memcpy(line, buf, l);
123#endif
124 return line;
125}
126
Damiend99b0522013-12-21 18:17:45 +0000127static void do_repl(void) {
Damien5ac1b2e2013-10-18 19:58:12 +0100128 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200129 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100130 if (line == NULL) {
131 // EOF
132 return;
133 }
Damiend99b0522013-12-21 18:17:45 +0000134 if (mp_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +0100135 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200136 char *line2 = prompt("... ");
Damien5ac1b2e2013-10-18 19:58:12 +0100137 if (line2 == NULL || strlen(line2) == 0) {
138 break;
139 }
140 char *line3 = str_join(line, '\n', line2);
Damien732407f2013-12-29 19:33:23 +0000141 free(line);
142 free(line2);
Damien5ac1b2e2013-10-18 19:58:12 +0100143 line = line3;
144 }
145 }
Damienfa2162b2013-10-20 17:42:00 +0100146
Damiend99b0522013-12-21 18:17:45 +0000147 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", line, strlen(line), false);
Damien George136f6752014-01-07 14:54:15 +0000148 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Damien5ac1b2e2013-10-18 19:58:12 +0100149 }
150}
151
Damien George136f6752014-01-07 14:54:15 +0000152static void do_file(const char *file) {
Damien George66028ab2014-01-03 14:03:48 +0000153 // hack: set dir for import based on where this file is
154 {
155 const char * s = strrchr(file, '/');
156 if (s != NULL) {
157 int len = s - file;
158 char *dir = m_new(char, len + 1);
159 memcpy(dir, file, len);
160 dir[len] = '\0';
161 mp_import_set_directory(dir);
162 }
163 }
164
Damiend99b0522013-12-21 18:17:45 +0000165 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000166 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
167}
Damien429d7192013-10-04 19:53:11 +0100168
Damien George136f6752014-01-07 14:54:15 +0000169static void do_str(const char *str) {
170 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", str, strlen(str), false);
171 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100172}
Damien429d7192013-10-04 19:53:11 +0100173
Damiend99b0522013-12-21 18:17:45 +0000174typedef struct _test_obj_t {
175 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000176 int value;
Damiend99b0522013-12-21 18:17:45 +0000177} test_obj_t;
178
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200179static 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 +0000180 test_obj_t *self = self_in;
181 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000182}
183
Damiend99b0522013-12-21 18:17:45 +0000184static mp_obj_t test_get(mp_obj_t self_in) {
185 test_obj_t *self = self_in;
186 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000187}
188
Damiend99b0522013-12-21 18:17:45 +0000189static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
190 test_obj_t *self = self_in;
191 self->value = mp_obj_get_int(arg);
192 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000193}
194
Damiend99b0522013-12-21 18:17:45 +0000195static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
196static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
197
Damien George97209d32014-01-07 15:58:30 +0000198static const mp_method_t test_methods[] = {
199 { "get", &test_get_obj },
200 { "set", &test_set_obj },
201 { NULL, NULL },
202};
203
Damiend99b0522013-12-21 18:17:45 +0000204static const mp_obj_type_t test_type = {
205 { &mp_const_type },
Damiena53f6942013-11-02 23:58:38 +0000206 "Test",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200207 .print = test_print,
Damien George97209d32014-01-07 15:58:30 +0000208 .methods = test_methods,
Damiena53f6942013-11-02 23:58:38 +0000209};
210
Damiend99b0522013-12-21 18:17:45 +0000211mp_obj_t test_obj_new(int value) {
212 test_obj_t *o = m_new_obj(test_obj_t);
213 o->base.type = &test_type;
214 o->value = value;
215 return o;
216}
217
Damien George136f6752014-01-07 14:54:15 +0000218int usage(void) {
219 printf("usage: py [-c <command>] [<filename>]\n");
220 return 1;
221}
222
Damien5ac1b2e2013-10-18 19:58:12 +0100223int main(int argc, char **argv) {
224 qstr_init();
225 rt_init();
226
Damiend99b0522013-12-21 18:17:45 +0000227 rt_store_name(qstr_from_str_static("test"), test_obj_new(42));
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +0200228 rt_store_name(qstr_from_str_static("open"), (mp_obj_t)&mp_builtin_open_obj);
Paul Sokolovskyfc926082014-01-18 23:47:44 +0200229 rawsocket_init();
Damiena53f6942013-11-02 23:58:38 +0000230
Damien George062478e2014-01-09 20:57:50 +0000231 // Here is some example code to create a class and instance of that class.
232 // First is the Python, then the C code.
233 //
234 // class TestClass:
235 // pass
236 // test_obj = TestClass()
237 // test_obj.attr = 42
238 mp_obj_t test_class_type, test_class_instance;
Damien George004cdce2014-01-09 21:43:51 +0000239 test_class_type = mp_obj_new_type(qstr_from_str_static("TestClass"), mp_const_empty_tuple, mp_obj_new_dict(0));
Damien George062478e2014-01-09 20:57:50 +0000240 rt_store_name(qstr_from_str_static("test_obj"), test_class_instance = rt_call_function_0(test_class_type));
241 rt_store_attr(test_class_instance, qstr_from_str_static("attr"), mp_obj_new_int(42));
242
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000243 /*
244 printf("bytes:\n");
245 printf(" total %d\n", m_get_total_bytes_allocated());
246 printf(" cur %d\n", m_get_current_bytes_allocated());
247 printf(" peak %d\n", m_get_peak_bytes_allocated());
248 */
249
Damien5ac1b2e2013-10-18 19:58:12 +0100250 if (argc == 1) {
251 do_repl();
Damien5ac1b2e2013-10-18 19:58:12 +0100252 } else {
Damien George136f6752014-01-07 14:54:15 +0000253 for (int a = 1; a < argc; a++) {
254 if (argv[a][0] == '-') {
255 if (strcmp(argv[a], "-c") == 0) {
256 if (a + 1 >= argc) {
257 return usage();
258 }
259 do_str(argv[a + 1]);
260 a += 1;
261 } else {
262 return usage();
263 }
264 } else {
265 do_file(argv[a]);
266 }
267 }
Damien5ac1b2e2013-10-18 19:58:12 +0100268 }
Damien George136f6752014-01-07 14:54:15 +0000269
Damien429d7192013-10-04 19:53:11 +0100270 rt_deinit();
271
272 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
273 return 0;
274}
Damien087d2182013-11-09 20:14:30 +0000275
276// for sqrt
277#include <math.h>
278machine_float_t machine_sqrt(machine_float_t x) {
279 return sqrt(x);
280}