blob: c33a4393769c27acf8e07f93c981b2470a5b9125 [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;
24
Damien George136f6752014-01-07 14:54:15 +000025static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
26 if (lex == NULL) {
27 return;
28 }
29
30 if (0) {
31 // just tokenise
32 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
33 mp_token_show(mp_lexer_cur(lex));
34 mp_lexer_to_next(lex);
35 }
36 mp_lexer_free(lex);
37 return;
38 }
39
Damien George9528cd62014-01-15 21:23:31 +000040 qstr parse_exc_id;
41 const char *parse_exc_msg;
42 mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_exc_id, &parse_exc_msg);
Damien George136f6752014-01-07 14:54:15 +000043
44 if (pn == MP_PARSE_NODE_NULL) {
45 // parse error
Damien George9528cd62014-01-15 21:23:31 +000046 mp_lexer_show_error_pythonic_prefix(lex);
47 printf("%s: %s\n", qstr_str(parse_exc_id), parse_exc_msg);
48 mp_lexer_free(lex);
Damien George136f6752014-01-07 14:54:15 +000049 return;
50 }
51
Damien George08335002014-01-18 23:24:36 +000052 qstr source_name = mp_lexer_source_name(lex);
Damien George9528cd62014-01-15 21:23:31 +000053 mp_lexer_free(lex);
54
Damien George136f6752014-01-07 14:54:15 +000055 //printf("----------------\n");
Damien George08335002014-01-18 23:24:36 +000056 //mp_parse_node_show(pn, 0);
Damien George136f6752014-01-07 14:54:15 +000057 //printf("----------------\n");
58
Damien George08335002014-01-18 23:24:36 +000059 mp_obj_t module_fun = mp_compile(pn, source_name, is_repl);
Damien George136f6752014-01-07 14:54:15 +000060
61 if (module_fun == mp_const_none) {
62 // compile error
63 return;
64 }
65
66 // execute it
67 nlr_buf_t nlr;
68 if (nlr_push(&nlr) == 0) {
69 rt_call_function_0(module_fun);
70 nlr_pop();
71 } else {
72 // uncaught exception
Damien George08335002014-01-18 23:24:36 +000073 mp_obj_t exc = (mp_obj_t)nlr.ret_val;
74 if (MP_OBJ_IS_TYPE(exc, &exception_type)) {
75 qstr file;
76 machine_uint_t line;
77 mp_obj_exception_get_source_info(exc, &file, &line);
78 printf("File \"%s\", line %d\n", qstr_str(file), (int)line);
79 }
80 mp_obj_print(exc, PRINT_REPR);
Damien George136f6752014-01-07 14:54:15 +000081 printf("\n");
82 }
83}
84
Damiend99b0522013-12-21 18:17:45 +000085static char *str_join(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +010086 int l1 = strlen(s1);
87 int l2 = strlen(s2);
88 char *s = m_new(char, l1 + l2 + 2);
89 memcpy(s, s1, l1);
90 if (sep_char != 0) {
91 s[l1] = sep_char;
92 l1 += 1;
93 }
94 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +010095 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +010096 return s;
97}
98
Paul Sokolovskyfa027672014-01-01 18:28:01 +020099static char *prompt(char *p) {
Paul Sokolovskyd674bd52014-01-04 19:38:19 +0200100#if MICROPY_USE_READLINE
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200101 char *line = readline(p);
102 if (line) {
103 add_history(line);
104 }
105#else
106 static char buf[256];
107 fputs(p, stdout);
108 char *s = fgets(buf, sizeof(buf), stdin);
109 if (!s) {
110 return NULL;
111 }
112 int l = strlen(buf);
113 if (buf[l - 1] == '\n') {
114 buf[l - 1] = 0;
115 } else {
116 l++;
117 }
118 char *line = m_new(char, l);
119 memcpy(line, buf, l);
120#endif
121 return line;
122}
123
Damiend99b0522013-12-21 18:17:45 +0000124static void do_repl(void) {
Damien5ac1b2e2013-10-18 19:58:12 +0100125 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200126 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100127 if (line == NULL) {
128 // EOF
129 return;
130 }
Damiend99b0522013-12-21 18:17:45 +0000131 if (mp_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +0100132 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200133 char *line2 = prompt("... ");
Damien5ac1b2e2013-10-18 19:58:12 +0100134 if (line2 == NULL || strlen(line2) == 0) {
135 break;
136 }
137 char *line3 = str_join(line, '\n', line2);
Damien732407f2013-12-29 19:33:23 +0000138 free(line);
139 free(line2);
Damien5ac1b2e2013-10-18 19:58:12 +0100140 line = line3;
141 }
142 }
Damienfa2162b2013-10-20 17:42:00 +0100143
Damiend99b0522013-12-21 18:17:45 +0000144 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", line, strlen(line), false);
Damien George136f6752014-01-07 14:54:15 +0000145 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Damien5ac1b2e2013-10-18 19:58:12 +0100146 }
147}
148
Damien George136f6752014-01-07 14:54:15 +0000149static void do_file(const char *file) {
Damien George66028ab2014-01-03 14:03:48 +0000150 // hack: set dir for import based on where this file is
151 {
152 const char * s = strrchr(file, '/');
153 if (s != NULL) {
154 int len = s - file;
155 char *dir = m_new(char, len + 1);
156 memcpy(dir, file, len);
157 dir[len] = '\0';
158 mp_import_set_directory(dir);
159 }
160 }
161
Damiend99b0522013-12-21 18:17:45 +0000162 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000163 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
164}
Damien429d7192013-10-04 19:53:11 +0100165
Damien George136f6752014-01-07 14:54:15 +0000166static void do_str(const char *str) {
167 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", str, strlen(str), false);
168 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100169}
Damien429d7192013-10-04 19:53:11 +0100170
Damiend99b0522013-12-21 18:17:45 +0000171typedef struct _test_obj_t {
172 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000173 int value;
Damiend99b0522013-12-21 18:17:45 +0000174} test_obj_t;
175
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200176static 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 +0000177 test_obj_t *self = self_in;
178 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000179}
180
Damiend99b0522013-12-21 18:17:45 +0000181static mp_obj_t test_get(mp_obj_t self_in) {
182 test_obj_t *self = self_in;
183 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000184}
185
Damiend99b0522013-12-21 18:17:45 +0000186static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
187 test_obj_t *self = self_in;
188 self->value = mp_obj_get_int(arg);
189 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000190}
191
Damiend99b0522013-12-21 18:17:45 +0000192static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
193static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
194
Damien George97209d32014-01-07 15:58:30 +0000195static const mp_method_t test_methods[] = {
196 { "get", &test_get_obj },
197 { "set", &test_set_obj },
198 { NULL, NULL },
199};
200
Damiend99b0522013-12-21 18:17:45 +0000201static const mp_obj_type_t test_type = {
202 { &mp_const_type },
Damiena53f6942013-11-02 23:58:38 +0000203 "Test",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200204 .print = test_print,
Damien George97209d32014-01-07 15:58:30 +0000205 .methods = test_methods,
Damiena53f6942013-11-02 23:58:38 +0000206};
207
Damiend99b0522013-12-21 18:17:45 +0000208mp_obj_t test_obj_new(int value) {
209 test_obj_t *o = m_new_obj(test_obj_t);
210 o->base.type = &test_type;
211 o->value = value;
212 return o;
213}
214
Damien George136f6752014-01-07 14:54:15 +0000215int usage(void) {
216 printf("usage: py [-c <command>] [<filename>]\n");
217 return 1;
218}
219
Damien5ac1b2e2013-10-18 19:58:12 +0100220int main(int argc, char **argv) {
221 qstr_init();
222 rt_init();
223
Damiend99b0522013-12-21 18:17:45 +0000224 rt_store_name(qstr_from_str_static("test"), test_obj_new(42));
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +0200225 rt_store_name(qstr_from_str_static("open"), (mp_obj_t)&mp_builtin_open_obj);
Damiena53f6942013-11-02 23:58:38 +0000226
Damien George062478e2014-01-09 20:57:50 +0000227 // Here is some example code to create a class and instance of that class.
228 // First is the Python, then the C code.
229 //
230 // class TestClass:
231 // pass
232 // test_obj = TestClass()
233 // test_obj.attr = 42
234 mp_obj_t test_class_type, test_class_instance;
Damien George004cdce2014-01-09 21:43:51 +0000235 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 +0000236 rt_store_name(qstr_from_str_static("test_obj"), test_class_instance = rt_call_function_0(test_class_type));
237 rt_store_attr(test_class_instance, qstr_from_str_static("attr"), mp_obj_new_int(42));
238
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000239 /*
240 printf("bytes:\n");
241 printf(" total %d\n", m_get_total_bytes_allocated());
242 printf(" cur %d\n", m_get_current_bytes_allocated());
243 printf(" peak %d\n", m_get_peak_bytes_allocated());
244 */
245
Damien5ac1b2e2013-10-18 19:58:12 +0100246 if (argc == 1) {
247 do_repl();
Damien5ac1b2e2013-10-18 19:58:12 +0100248 } else {
Damien George136f6752014-01-07 14:54:15 +0000249 for (int a = 1; a < argc; a++) {
250 if (argv[a][0] == '-') {
251 if (strcmp(argv[a], "-c") == 0) {
252 if (a + 1 >= argc) {
253 return usage();
254 }
255 do_str(argv[a + 1]);
256 a += 1;
257 } else {
258 return usage();
259 }
260 } else {
261 do_file(argv[a]);
262 }
263 }
Damien5ac1b2e2013-10-18 19:58:12 +0100264 }
Damien George136f6752014-01-07 14:54:15 +0000265
Damien429d7192013-10-04 19:53:11 +0100266 rt_deinit();
267
268 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
269 return 0;
270}
Damien087d2182013-11-09 20:14:30 +0000271
272// for sqrt
273#include <math.h>
274machine_float_t machine_sqrt(machine_float_t x) {
275 return sqrt(x);
276}