blob: c93610670656d3da543a98062bef695f5e429f1f [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"
Damien George55baff42014-01-21 21:40:13 +00009#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010010#include "lexer.h"
Damiena5185f42013-10-20 14:41:27 +010011#include "lexerunix.h"
Damien429d7192013-10-04 19:53:11 +010012#include "parse.h"
Damien0f082672013-12-17 18:33:53 +000013#include "obj.h"
Damien George1fb03172014-01-03 14:22:03 +000014#include "compile.h"
Damiend99b0522013-12-21 18:17:45 +000015#include "runtime0.h"
16#include "runtime.h"
Damien92c06562013-10-22 22:32:27 +010017#include "repl.h"
Damien429d7192013-10-04 19:53:11 +010018
Paul Sokolovskyd674bd52014-01-04 19:38:19 +020019#if MICROPY_USE_READLINE
Damien5ac1b2e2013-10-18 19:58:12 +010020#include <readline/readline.h>
Paul Sokolovsky903b24f2014-01-01 14:54:39 +020021#include <readline/history.h>
Paul Sokolovskyfa027672014-01-01 18:28:01 +020022#endif
Damien429d7192013-10-04 19:53:11 +010023
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +020024extern const mp_obj_fun_native_t mp_builtin_open_obj;
Paul Sokolovsky51ee44a2014-01-20 23:49:56 +020025void file_init();
Paul Sokolovskyfc926082014-01-18 23:47:44 +020026void rawsocket_init();
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +020027
Damien George136f6752014-01-07 14:54:15 +000028static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
29 if (lex == NULL) {
30 return;
31 }
32
33 if (0) {
34 // just tokenise
35 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
36 mp_token_show(mp_lexer_cur(lex));
37 mp_lexer_to_next(lex);
38 }
39 mp_lexer_free(lex);
40 return;
41 }
42
Damien George9528cd62014-01-15 21:23:31 +000043 qstr parse_exc_id;
44 const char *parse_exc_msg;
45 mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_exc_id, &parse_exc_msg);
Damien George136f6752014-01-07 14:54:15 +000046
47 if (pn == MP_PARSE_NODE_NULL) {
48 // parse error
Damien George9528cd62014-01-15 21:23:31 +000049 mp_lexer_show_error_pythonic_prefix(lex);
50 printf("%s: %s\n", qstr_str(parse_exc_id), parse_exc_msg);
51 mp_lexer_free(lex);
Damien George136f6752014-01-07 14:54:15 +000052 return;
53 }
54
Damien George08335002014-01-18 23:24:36 +000055 qstr source_name = mp_lexer_source_name(lex);
Damien George9528cd62014-01-15 21:23:31 +000056 mp_lexer_free(lex);
57
Damien Georgecbd2f742014-01-19 11:48:48 +000058 /*
59 printf("----------------\n");
60 mp_parse_node_print(pn, 0);
61 printf("----------------\n");
62 */
Damien George136f6752014-01-07 14:54:15 +000063
Damien George08335002014-01-18 23:24:36 +000064 mp_obj_t module_fun = mp_compile(pn, source_name, is_repl);
Damien George136f6752014-01-07 14:54:15 +000065
66 if (module_fun == mp_const_none) {
67 // compile error
68 return;
69 }
70
71 // execute it
72 nlr_buf_t nlr;
73 if (nlr_push(&nlr) == 0) {
74 rt_call_function_0(module_fun);
75 nlr_pop();
76 } else {
77 // uncaught exception
Damien George136b1492014-01-19 12:38:49 +000078 mp_obj_print_exception((mp_obj_t)nlr.ret_val);
Damien George136f6752014-01-07 14:54:15 +000079 }
80}
81
Damiend99b0522013-12-21 18:17:45 +000082static char *str_join(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +010083 int l1 = strlen(s1);
84 int l2 = strlen(s2);
85 char *s = m_new(char, l1 + l2 + 2);
86 memcpy(s, s1, l1);
87 if (sep_char != 0) {
88 s[l1] = sep_char;
89 l1 += 1;
90 }
91 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +010092 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +010093 return s;
94}
95
Paul Sokolovskyfa027672014-01-01 18:28:01 +020096static char *prompt(char *p) {
Paul Sokolovskyd674bd52014-01-04 19:38:19 +020097#if MICROPY_USE_READLINE
Paul Sokolovskyfa027672014-01-01 18:28:01 +020098 char *line = readline(p);
99 if (line) {
100 add_history(line);
101 }
102#else
103 static char buf[256];
104 fputs(p, stdout);
105 char *s = fgets(buf, sizeof(buf), stdin);
106 if (!s) {
107 return NULL;
108 }
109 int l = strlen(buf);
110 if (buf[l - 1] == '\n') {
111 buf[l - 1] = 0;
112 } else {
113 l++;
114 }
115 char *line = m_new(char, l);
116 memcpy(line, buf, l);
117#endif
118 return line;
119}
120
Damiend99b0522013-12-21 18:17:45 +0000121static void do_repl(void) {
Damien5ac1b2e2013-10-18 19:58:12 +0100122 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200123 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100124 if (line == NULL) {
125 // EOF
126 return;
127 }
Damiend99b0522013-12-21 18:17:45 +0000128 if (mp_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +0100129 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200130 char *line2 = prompt("... ");
Damien5ac1b2e2013-10-18 19:58:12 +0100131 if (line2 == NULL || strlen(line2) == 0) {
132 break;
133 }
134 char *line3 = str_join(line, '\n', line2);
Damien732407f2013-12-29 19:33:23 +0000135 free(line);
136 free(line2);
Damien5ac1b2e2013-10-18 19:58:12 +0100137 line = line3;
138 }
139 }
Damienfa2162b2013-10-20 17:42:00 +0100140
Damiend99b0522013-12-21 18:17:45 +0000141 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", line, strlen(line), false);
Damien George136f6752014-01-07 14:54:15 +0000142 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Damien5ac1b2e2013-10-18 19:58:12 +0100143 }
144}
145
Damien George136f6752014-01-07 14:54:15 +0000146static void do_file(const char *file) {
Damien George66028ab2014-01-03 14:03:48 +0000147 // hack: set dir for import based on where this file is
148 {
149 const char * s = strrchr(file, '/');
150 if (s != NULL) {
151 int len = s - file;
152 char *dir = m_new(char, len + 1);
153 memcpy(dir, file, len);
154 dir[len] = '\0';
155 mp_import_set_directory(dir);
156 }
157 }
158
Damiend99b0522013-12-21 18:17:45 +0000159 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000160 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
161}
Damien429d7192013-10-04 19:53:11 +0100162
Damien George136f6752014-01-07 14:54:15 +0000163static void do_str(const char *str) {
164 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", str, strlen(str), false);
165 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100166}
Damien429d7192013-10-04 19:53:11 +0100167
Damiend99b0522013-12-21 18:17:45 +0000168typedef struct _test_obj_t {
169 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000170 int value;
Damiend99b0522013-12-21 18:17:45 +0000171} test_obj_t;
172
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200173static 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 +0000174 test_obj_t *self = self_in;
175 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000176}
177
Damiend99b0522013-12-21 18:17:45 +0000178static mp_obj_t test_get(mp_obj_t self_in) {
179 test_obj_t *self = self_in;
180 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000181}
182
Damiend99b0522013-12-21 18:17:45 +0000183static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
184 test_obj_t *self = self_in;
185 self->value = mp_obj_get_int(arg);
186 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000187}
188
Damiend99b0522013-12-21 18:17:45 +0000189static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
190static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
191
Damien George97209d32014-01-07 15:58:30 +0000192static const mp_method_t test_methods[] = {
193 { "get", &test_get_obj },
194 { "set", &test_set_obj },
195 { NULL, NULL },
196};
197
Damiend99b0522013-12-21 18:17:45 +0000198static const mp_obj_type_t test_type = {
199 { &mp_const_type },
Damiena53f6942013-11-02 23:58:38 +0000200 "Test",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200201 .print = test_print,
Damien George97209d32014-01-07 15:58:30 +0000202 .methods = test_methods,
Damiena53f6942013-11-02 23:58:38 +0000203};
204
Damiend99b0522013-12-21 18:17:45 +0000205mp_obj_t test_obj_new(int value) {
206 test_obj_t *o = m_new_obj(test_obj_t);
207 o->base.type = &test_type;
208 o->value = value;
209 return o;
210}
211
Damien George136f6752014-01-07 14:54:15 +0000212int usage(void) {
213 printf("usage: py [-c <command>] [<filename>]\n");
214 return 1;
215}
216
Damien5ac1b2e2013-10-18 19:58:12 +0100217int main(int argc, char **argv) {
218 qstr_init();
219 rt_init();
220
Damien George55baff42014-01-21 21:40:13 +0000221 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200222 mp_obj_t py_argv = mp_obj_new_list(0, NULL);
Damien George55baff42014-01-21 21:40:13 +0000223 rt_store_attr(m_sys, MP_QSTR_argv, py_argv);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200224
Damien George55baff42014-01-21 21:40:13 +0000225 rt_store_name(qstr_from_str("test"), test_obj_new(42));
Damien George12eacca2014-01-21 21:54:15 +0000226
Paul Sokolovsky51ee44a2014-01-20 23:49:56 +0200227 file_init();
Paul Sokolovskyfc926082014-01-18 23:47:44 +0200228 rawsocket_init();
Damiena53f6942013-11-02 23:58:38 +0000229
Damien George062478e2014-01-09 20:57:50 +0000230 // Here is some example code to create a class and instance of that class.
231 // First is the Python, then the C code.
232 //
233 // class TestClass:
234 // pass
235 // test_obj = TestClass()
236 // test_obj.attr = 42
237 mp_obj_t test_class_type, test_class_instance;
Damien George5fa93b62014-01-22 14:35:10 +0000238 test_class_type = mp_obj_new_type("TestClass", mp_const_empty_tuple, mp_obj_new_dict(0));
Damien George55baff42014-01-21 21:40:13 +0000239 rt_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = rt_call_function_0(test_class_type));
240 rt_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
Damien George062478e2014-01-09 20:57:50 +0000241
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000242 /*
243 printf("bytes:\n");
244 printf(" total %d\n", m_get_total_bytes_allocated());
245 printf(" cur %d\n", m_get_current_bytes_allocated());
246 printf(" peak %d\n", m_get_peak_bytes_allocated());
247 */
248
Damien5ac1b2e2013-10-18 19:58:12 +0100249 if (argc == 1) {
250 do_repl();
Damien5ac1b2e2013-10-18 19:58:12 +0100251 } else {
Damien George136f6752014-01-07 14:54:15 +0000252 for (int a = 1; a < argc; a++) {
253 if (argv[a][0] == '-') {
254 if (strcmp(argv[a], "-c") == 0) {
255 if (a + 1 >= argc) {
256 return usage();
257 }
258 do_str(argv[a + 1]);
259 a += 1;
260 } else {
261 return usage();
262 }
263 } else {
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200264 for (int i = a; i < argc; i++) {
Damien George55baff42014-01-21 21:40:13 +0000265 rt_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200266 }
Damien George136f6752014-01-07 14:54:15 +0000267 do_file(argv[a]);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200268 break;
Damien George136f6752014-01-07 14:54:15 +0000269 }
270 }
Damien5ac1b2e2013-10-18 19:58:12 +0100271 }
Damien George136f6752014-01-07 14:54:15 +0000272
Damien429d7192013-10-04 19:53:11 +0100273 rt_deinit();
274
275 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
276 return 0;
277}
Damien087d2182013-11-09 20:14:30 +0000278
279// for sqrt
280#include <math.h>
281machine_float_t machine_sqrt(machine_float_t x) {
282 return sqrt(x);
283}