blob: 08567c04dcf4b26ef0f4802b75abf675742f87a4 [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 George136b1492014-01-19 12:38:49 +000076 mp_obj_print_exception((mp_obj_t)nlr.ret_val);
Damien George136f6752014-01-07 14:54:15 +000077 }
78}
79
Damiend99b0522013-12-21 18:17:45 +000080static char *str_join(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +010081 int l1 = strlen(s1);
82 int l2 = strlen(s2);
83 char *s = m_new(char, l1 + l2 + 2);
84 memcpy(s, s1, l1);
85 if (sep_char != 0) {
86 s[l1] = sep_char;
87 l1 += 1;
88 }
89 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +010090 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +010091 return s;
92}
93
Paul Sokolovskyfa027672014-01-01 18:28:01 +020094static char *prompt(char *p) {
Paul Sokolovskyd674bd52014-01-04 19:38:19 +020095#if MICROPY_USE_READLINE
Paul Sokolovskyfa027672014-01-01 18:28:01 +020096 char *line = readline(p);
97 if (line) {
98 add_history(line);
99 }
100#else
101 static char buf[256];
102 fputs(p, stdout);
103 char *s = fgets(buf, sizeof(buf), stdin);
104 if (!s) {
105 return NULL;
106 }
107 int l = strlen(buf);
108 if (buf[l - 1] == '\n') {
109 buf[l - 1] = 0;
110 } else {
111 l++;
112 }
113 char *line = m_new(char, l);
114 memcpy(line, buf, l);
115#endif
116 return line;
117}
118
Damiend99b0522013-12-21 18:17:45 +0000119static void do_repl(void) {
Damien5ac1b2e2013-10-18 19:58:12 +0100120 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200121 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100122 if (line == NULL) {
123 // EOF
124 return;
125 }
Damiend99b0522013-12-21 18:17:45 +0000126 if (mp_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +0100127 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200128 char *line2 = prompt("... ");
Damien5ac1b2e2013-10-18 19:58:12 +0100129 if (line2 == NULL || strlen(line2) == 0) {
130 break;
131 }
132 char *line3 = str_join(line, '\n', line2);
Damien732407f2013-12-29 19:33:23 +0000133 free(line);
134 free(line2);
Damien5ac1b2e2013-10-18 19:58:12 +0100135 line = line3;
136 }
137 }
Damienfa2162b2013-10-20 17:42:00 +0100138
Damiend99b0522013-12-21 18:17:45 +0000139 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", line, strlen(line), false);
Damien George136f6752014-01-07 14:54:15 +0000140 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Damien5ac1b2e2013-10-18 19:58:12 +0100141 }
142}
143
Damien George136f6752014-01-07 14:54:15 +0000144static void do_file(const char *file) {
Damien George66028ab2014-01-03 14:03:48 +0000145 // hack: set dir for import based on where this file is
146 {
147 const char * s = strrchr(file, '/');
148 if (s != NULL) {
149 int len = s - file;
150 char *dir = m_new(char, len + 1);
151 memcpy(dir, file, len);
152 dir[len] = '\0';
153 mp_import_set_directory(dir);
154 }
155 }
156
Damiend99b0522013-12-21 18:17:45 +0000157 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000158 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
159}
Damien429d7192013-10-04 19:53:11 +0100160
Damien George136f6752014-01-07 14:54:15 +0000161static void do_str(const char *str) {
162 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", str, strlen(str), false);
163 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100164}
Damien429d7192013-10-04 19:53:11 +0100165
Damiend99b0522013-12-21 18:17:45 +0000166typedef struct _test_obj_t {
167 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000168 int value;
Damiend99b0522013-12-21 18:17:45 +0000169} test_obj_t;
170
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200171static 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 +0000172 test_obj_t *self = self_in;
173 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000174}
175
Damiend99b0522013-12-21 18:17:45 +0000176static mp_obj_t test_get(mp_obj_t self_in) {
177 test_obj_t *self = self_in;
178 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000179}
180
Damiend99b0522013-12-21 18:17:45 +0000181static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
182 test_obj_t *self = self_in;
183 self->value = mp_obj_get_int(arg);
184 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000185}
186
Damiend99b0522013-12-21 18:17:45 +0000187static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
188static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
189
Damien George97209d32014-01-07 15:58:30 +0000190static const mp_method_t test_methods[] = {
191 { "get", &test_get_obj },
192 { "set", &test_set_obj },
193 { NULL, NULL },
194};
195
Damiend99b0522013-12-21 18:17:45 +0000196static const mp_obj_type_t test_type = {
197 { &mp_const_type },
Damiena53f6942013-11-02 23:58:38 +0000198 "Test",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200199 .print = test_print,
Damien George97209d32014-01-07 15:58:30 +0000200 .methods = test_methods,
Damiena53f6942013-11-02 23:58:38 +0000201};
202
Damiend99b0522013-12-21 18:17:45 +0000203mp_obj_t test_obj_new(int value) {
204 test_obj_t *o = m_new_obj(test_obj_t);
205 o->base.type = &test_type;
206 o->value = value;
207 return o;
208}
209
Damien George136f6752014-01-07 14:54:15 +0000210int usage(void) {
211 printf("usage: py [-c <command>] [<filename>]\n");
212 return 1;
213}
214
Damien5ac1b2e2013-10-18 19:58:12 +0100215int main(int argc, char **argv) {
216 qstr_init();
217 rt_init();
218
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200219 mp_obj_t m_sys = mp_obj_new_module(qstr_from_str_static("sys"));
220 mp_obj_t py_argv = mp_obj_new_list(0, NULL);
221 rt_store_attr(m_sys, qstr_from_str_static("argv"), py_argv);
222
Damiend99b0522013-12-21 18:17:45 +0000223 rt_store_name(qstr_from_str_static("test"), test_obj_new(42));
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +0200224 rt_store_name(qstr_from_str_static("open"), (mp_obj_t)&mp_builtin_open_obj);
Paul Sokolovskyfc926082014-01-18 23:47:44 +0200225 rawsocket_init();
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 {
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200261 for (int i = a; i < argc; i++) {
262 rt_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_strn_copy(argv[i], strlen(argv[i]))));
263 }
Damien George136f6752014-01-07 14:54:15 +0000264 do_file(argv[a]);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200265 break;
Damien George136f6752014-01-07 14:54:15 +0000266 }
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}