blob: 39a772d1eb06337c11b2e0cbb58b59afe7e3ee01 [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 George136f6752014-01-07 14:54:15 +000056 //printf("----------------\n");
Damien George08335002014-01-18 23:24:36 +000057 //mp_parse_node_show(pn, 0);
Damien George136f6752014-01-07 14:54:15 +000058 //printf("----------------\n");
59
Damien George08335002014-01-18 23:24:36 +000060 mp_obj_t module_fun = mp_compile(pn, source_name, is_repl);
Damien George136f6752014-01-07 14:54:15 +000061
62 if (module_fun == mp_const_none) {
63 // compile error
64 return;
65 }
66
67 // execute it
68 nlr_buf_t nlr;
69 if (nlr_push(&nlr) == 0) {
70 rt_call_function_0(module_fun);
71 nlr_pop();
72 } else {
73 // uncaught exception
Damien George08335002014-01-18 23:24:36 +000074 mp_obj_t exc = (mp_obj_t)nlr.ret_val;
75 if (MP_OBJ_IS_TYPE(exc, &exception_type)) {
76 qstr file;
77 machine_uint_t line;
78 mp_obj_exception_get_source_info(exc, &file, &line);
79 printf("File \"%s\", line %d\n", qstr_str(file), (int)line);
80 }
81 mp_obj_print(exc, PRINT_REPR);
Damien George136f6752014-01-07 14:54:15 +000082 printf("\n");
83 }
84}
85
Damiend99b0522013-12-21 18:17:45 +000086static char *str_join(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +010087 int l1 = strlen(s1);
88 int l2 = strlen(s2);
89 char *s = m_new(char, l1 + l2 + 2);
90 memcpy(s, s1, l1);
91 if (sep_char != 0) {
92 s[l1] = sep_char;
93 l1 += 1;
94 }
95 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +010096 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +010097 return s;
98}
99
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200100static char *prompt(char *p) {
Paul Sokolovskyd674bd52014-01-04 19:38:19 +0200101#if MICROPY_USE_READLINE
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200102 char *line = readline(p);
103 if (line) {
104 add_history(line);
105 }
106#else
107 static char buf[256];
108 fputs(p, stdout);
109 char *s = fgets(buf, sizeof(buf), stdin);
110 if (!s) {
111 return NULL;
112 }
113 int l = strlen(buf);
114 if (buf[l - 1] == '\n') {
115 buf[l - 1] = 0;
116 } else {
117 l++;
118 }
119 char *line = m_new(char, l);
120 memcpy(line, buf, l);
121#endif
122 return line;
123}
124
Damiend99b0522013-12-21 18:17:45 +0000125static void do_repl(void) {
Damien5ac1b2e2013-10-18 19:58:12 +0100126 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200127 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100128 if (line == NULL) {
129 // EOF
130 return;
131 }
Damiend99b0522013-12-21 18:17:45 +0000132 if (mp_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +0100133 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200134 char *line2 = prompt("... ");
Damien5ac1b2e2013-10-18 19:58:12 +0100135 if (line2 == NULL || strlen(line2) == 0) {
136 break;
137 }
138 char *line3 = str_join(line, '\n', line2);
Damien732407f2013-12-29 19:33:23 +0000139 free(line);
140 free(line2);
Damien5ac1b2e2013-10-18 19:58:12 +0100141 line = line3;
142 }
143 }
Damienfa2162b2013-10-20 17:42:00 +0100144
Damiend99b0522013-12-21 18:17:45 +0000145 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", line, strlen(line), false);
Damien George136f6752014-01-07 14:54:15 +0000146 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Damien5ac1b2e2013-10-18 19:58:12 +0100147 }
148}
149
Damien George136f6752014-01-07 14:54:15 +0000150static void do_file(const char *file) {
Damien George66028ab2014-01-03 14:03:48 +0000151 // hack: set dir for import based on where this file is
152 {
153 const char * s = strrchr(file, '/');
154 if (s != NULL) {
155 int len = s - file;
156 char *dir = m_new(char, len + 1);
157 memcpy(dir, file, len);
158 dir[len] = '\0';
159 mp_import_set_directory(dir);
160 }
161 }
162
Damiend99b0522013-12-21 18:17:45 +0000163 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000164 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
165}
Damien429d7192013-10-04 19:53:11 +0100166
Damien George136f6752014-01-07 14:54:15 +0000167static void do_str(const char *str) {
168 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", str, strlen(str), false);
169 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100170}
Damien429d7192013-10-04 19:53:11 +0100171
Damiend99b0522013-12-21 18:17:45 +0000172typedef struct _test_obj_t {
173 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000174 int value;
Damiend99b0522013-12-21 18:17:45 +0000175} test_obj_t;
176
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200177static 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 +0000178 test_obj_t *self = self_in;
179 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000180}
181
Damiend99b0522013-12-21 18:17:45 +0000182static mp_obj_t test_get(mp_obj_t self_in) {
183 test_obj_t *self = self_in;
184 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000185}
186
Damiend99b0522013-12-21 18:17:45 +0000187static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
188 test_obj_t *self = self_in;
189 self->value = mp_obj_get_int(arg);
190 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000191}
192
Damiend99b0522013-12-21 18:17:45 +0000193static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
194static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
195
Damien George97209d32014-01-07 15:58:30 +0000196static const mp_method_t test_methods[] = {
197 { "get", &test_get_obj },
198 { "set", &test_set_obj },
199 { NULL, NULL },
200};
201
Damiend99b0522013-12-21 18:17:45 +0000202static const mp_obj_type_t test_type = {
203 { &mp_const_type },
Damiena53f6942013-11-02 23:58:38 +0000204 "Test",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200205 .print = test_print,
Damien George97209d32014-01-07 15:58:30 +0000206 .methods = test_methods,
Damiena53f6942013-11-02 23:58:38 +0000207};
208
Damiend99b0522013-12-21 18:17:45 +0000209mp_obj_t test_obj_new(int value) {
210 test_obj_t *o = m_new_obj(test_obj_t);
211 o->base.type = &test_type;
212 o->value = value;
213 return o;
214}
215
Damien George136f6752014-01-07 14:54:15 +0000216int usage(void) {
217 printf("usage: py [-c <command>] [<filename>]\n");
218 return 1;
219}
220
Damien5ac1b2e2013-10-18 19:58:12 +0100221int main(int argc, char **argv) {
222 qstr_init();
223 rt_init();
224
Damiend99b0522013-12-21 18:17:45 +0000225 rt_store_name(qstr_from_str_static("test"), test_obj_new(42));
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +0200226 rt_store_name(qstr_from_str_static("open"), (mp_obj_t)&mp_builtin_open_obj);
Paul Sokolovskyfc926082014-01-18 23:47:44 +0200227 rawsocket_init();
Damiena53f6942013-11-02 23:58:38 +0000228
Damien George062478e2014-01-09 20:57:50 +0000229 // Here is some example code to create a class and instance of that class.
230 // First is the Python, then the C code.
231 //
232 // class TestClass:
233 // pass
234 // test_obj = TestClass()
235 // test_obj.attr = 42
236 mp_obj_t test_class_type, test_class_instance;
Damien George004cdce2014-01-09 21:43:51 +0000237 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 +0000238 rt_store_name(qstr_from_str_static("test_obj"), test_class_instance = rt_call_function_0(test_class_type));
239 rt_store_attr(test_class_instance, qstr_from_str_static("attr"), mp_obj_new_int(42));
240
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000241 /*
242 printf("bytes:\n");
243 printf(" total %d\n", m_get_total_bytes_allocated());
244 printf(" cur %d\n", m_get_current_bytes_allocated());
245 printf(" peak %d\n", m_get_peak_bytes_allocated());
246 */
247
Damien5ac1b2e2013-10-18 19:58:12 +0100248 if (argc == 1) {
249 do_repl();
Damien5ac1b2e2013-10-18 19:58:12 +0100250 } else {
Damien George136f6752014-01-07 14:54:15 +0000251 for (int a = 1; a < argc; a++) {
252 if (argv[a][0] == '-') {
253 if (strcmp(argv[a], "-c") == 0) {
254 if (a + 1 >= argc) {
255 return usage();
256 }
257 do_str(argv[a + 1]);
258 a += 1;
259 } else {
260 return usage();
261 }
262 } else {
263 do_file(argv[a]);
264 }
265 }
Damien5ac1b2e2013-10-18 19:58:12 +0100266 }
Damien George136f6752014-01-07 14:54:15 +0000267
Damien429d7192013-10-04 19:53:11 +0100268 rt_deinit();
269
270 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
271 return 0;
272}
Damien087d2182013-11-09 20:14:30 +0000273
274// for sqrt
275#include <math.h>
276machine_float_t machine_sqrt(machine_float_t x) {
277 return sqrt(x);
278}