blob: d89f39da70e23728f7a0c978f8feaf44aa3268db [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 George9528cd62014-01-15 21:23:31 +000052 mp_lexer_free(lex);
53
Damien George136f6752014-01-07 14:54:15 +000054 //printf("----------------\n");
55 //parse_node_show(pn, 0);
56 //printf("----------------\n");
57
58 mp_obj_t module_fun = mp_compile(pn, is_repl);
59
60 if (module_fun == mp_const_none) {
61 // compile error
62 return;
63 }
64
65 // execute it
66 nlr_buf_t nlr;
67 if (nlr_push(&nlr) == 0) {
68 rt_call_function_0(module_fun);
69 nlr_pop();
70 } else {
71 // uncaught exception
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020072 mp_obj_print((mp_obj_t)nlr.ret_val, PRINT_REPR);
Damien George136f6752014-01-07 14:54:15 +000073 printf("\n");
74 }
75}
76
Damiend99b0522013-12-21 18:17:45 +000077static char *str_join(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +010078 int l1 = strlen(s1);
79 int l2 = strlen(s2);
80 char *s = m_new(char, l1 + l2 + 2);
81 memcpy(s, s1, l1);
82 if (sep_char != 0) {
83 s[l1] = sep_char;
84 l1 += 1;
85 }
86 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +010087 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +010088 return s;
89}
90
Paul Sokolovskyfa027672014-01-01 18:28:01 +020091static char *prompt(char *p) {
Paul Sokolovskyd674bd52014-01-04 19:38:19 +020092#if MICROPY_USE_READLINE
Paul Sokolovskyfa027672014-01-01 18:28:01 +020093 char *line = readline(p);
94 if (line) {
95 add_history(line);
96 }
97#else
98 static char buf[256];
99 fputs(p, stdout);
100 char *s = fgets(buf, sizeof(buf), stdin);
101 if (!s) {
102 return NULL;
103 }
104 int l = strlen(buf);
105 if (buf[l - 1] == '\n') {
106 buf[l - 1] = 0;
107 } else {
108 l++;
109 }
110 char *line = m_new(char, l);
111 memcpy(line, buf, l);
112#endif
113 return line;
114}
115
Damiend99b0522013-12-21 18:17:45 +0000116static void do_repl(void) {
Damien5ac1b2e2013-10-18 19:58:12 +0100117 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200118 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100119 if (line == NULL) {
120 // EOF
121 return;
122 }
Damiend99b0522013-12-21 18:17:45 +0000123 if (mp_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +0100124 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200125 char *line2 = prompt("... ");
Damien5ac1b2e2013-10-18 19:58:12 +0100126 if (line2 == NULL || strlen(line2) == 0) {
127 break;
128 }
129 char *line3 = str_join(line, '\n', line2);
Damien732407f2013-12-29 19:33:23 +0000130 free(line);
131 free(line2);
Damien5ac1b2e2013-10-18 19:58:12 +0100132 line = line3;
133 }
134 }
Damienfa2162b2013-10-20 17:42:00 +0100135
Damiend99b0522013-12-21 18:17:45 +0000136 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", line, strlen(line), false);
Damien George136f6752014-01-07 14:54:15 +0000137 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Damien5ac1b2e2013-10-18 19:58:12 +0100138 }
139}
140
Damien George136f6752014-01-07 14:54:15 +0000141static void do_file(const char *file) {
Damien George66028ab2014-01-03 14:03:48 +0000142 // hack: set dir for import based on where this file is
143 {
144 const char * s = strrchr(file, '/');
145 if (s != NULL) {
146 int len = s - file;
147 char *dir = m_new(char, len + 1);
148 memcpy(dir, file, len);
149 dir[len] = '\0';
150 mp_import_set_directory(dir);
151 }
152 }
153
Damiend99b0522013-12-21 18:17:45 +0000154 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000155 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
156}
Damien429d7192013-10-04 19:53:11 +0100157
Damien George136f6752014-01-07 14:54:15 +0000158static void do_str(const char *str) {
159 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", str, strlen(str), false);
160 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100161}
Damien429d7192013-10-04 19:53:11 +0100162
Damiend99b0522013-12-21 18:17:45 +0000163typedef struct _test_obj_t {
164 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000165 int value;
Damiend99b0522013-12-21 18:17:45 +0000166} test_obj_t;
167
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200168static 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 +0000169 test_obj_t *self = self_in;
170 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000171}
172
Damiend99b0522013-12-21 18:17:45 +0000173static mp_obj_t test_get(mp_obj_t self_in) {
174 test_obj_t *self = self_in;
175 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000176}
177
Damiend99b0522013-12-21 18:17:45 +0000178static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
179 test_obj_t *self = self_in;
180 self->value = mp_obj_get_int(arg);
181 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000182}
183
Damiend99b0522013-12-21 18:17:45 +0000184static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
185static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
186
Damien George97209d32014-01-07 15:58:30 +0000187static const mp_method_t test_methods[] = {
188 { "get", &test_get_obj },
189 { "set", &test_set_obj },
190 { NULL, NULL },
191};
192
Damiend99b0522013-12-21 18:17:45 +0000193static const mp_obj_type_t test_type = {
194 { &mp_const_type },
Damiena53f6942013-11-02 23:58:38 +0000195 "Test",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200196 .print = test_print,
Damien George97209d32014-01-07 15:58:30 +0000197 .methods = test_methods,
Damiena53f6942013-11-02 23:58:38 +0000198};
199
Damiend99b0522013-12-21 18:17:45 +0000200mp_obj_t test_obj_new(int value) {
201 test_obj_t *o = m_new_obj(test_obj_t);
202 o->base.type = &test_type;
203 o->value = value;
204 return o;
205}
206
Damien George136f6752014-01-07 14:54:15 +0000207int usage(void) {
208 printf("usage: py [-c <command>] [<filename>]\n");
209 return 1;
210}
211
Damien5ac1b2e2013-10-18 19:58:12 +0100212int main(int argc, char **argv) {
213 qstr_init();
214 rt_init();
215
Damiend99b0522013-12-21 18:17:45 +0000216 rt_store_name(qstr_from_str_static("test"), test_obj_new(42));
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +0200217 rt_store_name(qstr_from_str_static("open"), (mp_obj_t)&mp_builtin_open_obj);
Damiena53f6942013-11-02 23:58:38 +0000218
Damien George062478e2014-01-09 20:57:50 +0000219 // Here is some example code to create a class and instance of that class.
220 // First is the Python, then the C code.
221 //
222 // class TestClass:
223 // pass
224 // test_obj = TestClass()
225 // test_obj.attr = 42
226 mp_obj_t test_class_type, test_class_instance;
Damien George004cdce2014-01-09 21:43:51 +0000227 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 +0000228 rt_store_name(qstr_from_str_static("test_obj"), test_class_instance = rt_call_function_0(test_class_type));
229 rt_store_attr(test_class_instance, qstr_from_str_static("attr"), mp_obj_new_int(42));
230
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000231 /*
232 printf("bytes:\n");
233 printf(" total %d\n", m_get_total_bytes_allocated());
234 printf(" cur %d\n", m_get_current_bytes_allocated());
235 printf(" peak %d\n", m_get_peak_bytes_allocated());
236 */
237
Damien5ac1b2e2013-10-18 19:58:12 +0100238 if (argc == 1) {
239 do_repl();
Damien5ac1b2e2013-10-18 19:58:12 +0100240 } else {
Damien George136f6752014-01-07 14:54:15 +0000241 for (int a = 1; a < argc; a++) {
242 if (argv[a][0] == '-') {
243 if (strcmp(argv[a], "-c") == 0) {
244 if (a + 1 >= argc) {
245 return usage();
246 }
247 do_str(argv[a + 1]);
248 a += 1;
249 } else {
250 return usage();
251 }
252 } else {
253 do_file(argv[a]);
254 }
255 }
Damien5ac1b2e2013-10-18 19:58:12 +0100256 }
Damien George136f6752014-01-07 14:54:15 +0000257
Damien429d7192013-10-04 19:53:11 +0100258 rt_deinit();
259
260 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
261 return 0;
262}
Damien087d2182013-11-09 20:14:30 +0000263
264// for sqrt
265#include <math.h>
266machine_float_t machine_sqrt(machine_float_t x) {
267 return sqrt(x);
268}