blob: 74e903c2efba2bc2b8297dc65c6e66015ae3d386 [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 Sokolovskyfc926082014-01-18 23:47:44 +020025void rawsocket_init();
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +020026
Damien George136f6752014-01-07 14:54:15 +000027static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
28 if (lex == NULL) {
29 return;
30 }
31
32 if (0) {
33 // just tokenise
34 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
35 mp_token_show(mp_lexer_cur(lex));
36 mp_lexer_to_next(lex);
37 }
38 mp_lexer_free(lex);
39 return;
40 }
41
Damien George9528cd62014-01-15 21:23:31 +000042 qstr parse_exc_id;
43 const char *parse_exc_msg;
44 mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_exc_id, &parse_exc_msg);
Damien George136f6752014-01-07 14:54:15 +000045
46 if (pn == MP_PARSE_NODE_NULL) {
47 // parse error
Damien George9528cd62014-01-15 21:23:31 +000048 mp_lexer_show_error_pythonic_prefix(lex);
49 printf("%s: %s\n", qstr_str(parse_exc_id), parse_exc_msg);
50 mp_lexer_free(lex);
Damien George136f6752014-01-07 14:54:15 +000051 return;
52 }
53
Damien George08335002014-01-18 23:24:36 +000054 qstr source_name = mp_lexer_source_name(lex);
Damien George9528cd62014-01-15 21:23:31 +000055 mp_lexer_free(lex);
56
Damien Georgecbd2f742014-01-19 11:48:48 +000057 /*
58 printf("----------------\n");
59 mp_parse_node_print(pn, 0);
60 printf("----------------\n");
61 */
Damien George136f6752014-01-07 14:54:15 +000062
Damien George08335002014-01-18 23:24:36 +000063 mp_obj_t module_fun = mp_compile(pn, source_name, is_repl);
Damien George136f6752014-01-07 14:54:15 +000064
65 if (module_fun == mp_const_none) {
66 // compile error
67 return;
68 }
69
70 // execute it
71 nlr_buf_t nlr;
72 if (nlr_push(&nlr) == 0) {
73 rt_call_function_0(module_fun);
74 nlr_pop();
75 } else {
76 // uncaught exception
Damien George136b1492014-01-19 12:38:49 +000077 mp_obj_print_exception((mp_obj_t)nlr.ret_val);
Damien George136f6752014-01-07 14:54:15 +000078 }
79}
80
Damiend99b0522013-12-21 18:17:45 +000081static char *str_join(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +010082 int l1 = strlen(s1);
83 int l2 = strlen(s2);
84 char *s = m_new(char, l1 + l2 + 2);
85 memcpy(s, s1, l1);
86 if (sep_char != 0) {
87 s[l1] = sep_char;
88 l1 += 1;
89 }
90 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +010091 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +010092 return s;
93}
94
Paul Sokolovskyfa027672014-01-01 18:28:01 +020095static char *prompt(char *p) {
Paul Sokolovskyd674bd52014-01-04 19:38:19 +020096#if MICROPY_USE_READLINE
Paul Sokolovskyfa027672014-01-01 18:28:01 +020097 char *line = readline(p);
98 if (line) {
99 add_history(line);
100 }
101#else
102 static char buf[256];
103 fputs(p, stdout);
104 char *s = fgets(buf, sizeof(buf), stdin);
105 if (!s) {
106 return NULL;
107 }
108 int l = strlen(buf);
109 if (buf[l - 1] == '\n') {
110 buf[l - 1] = 0;
111 } else {
112 l++;
113 }
114 char *line = m_new(char, l);
115 memcpy(line, buf, l);
116#endif
117 return line;
118}
119
Damiend99b0522013-12-21 18:17:45 +0000120static void do_repl(void) {
Damien5ac1b2e2013-10-18 19:58:12 +0100121 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200122 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100123 if (line == NULL) {
124 // EOF
125 return;
126 }
Damiend99b0522013-12-21 18:17:45 +0000127 if (mp_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +0100128 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200129 char *line2 = prompt("... ");
Damien5ac1b2e2013-10-18 19:58:12 +0100130 if (line2 == NULL || strlen(line2) == 0) {
131 break;
132 }
133 char *line3 = str_join(line, '\n', line2);
Damien732407f2013-12-29 19:33:23 +0000134 free(line);
135 free(line2);
Damien5ac1b2e2013-10-18 19:58:12 +0100136 line = line3;
137 }
138 }
Damienfa2162b2013-10-20 17:42:00 +0100139
Damiend99b0522013-12-21 18:17:45 +0000140 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", line, strlen(line), false);
Damien George136f6752014-01-07 14:54:15 +0000141 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Damien5ac1b2e2013-10-18 19:58:12 +0100142 }
143}
144
Damien George136f6752014-01-07 14:54:15 +0000145static void do_file(const char *file) {
Damien George66028ab2014-01-03 14:03:48 +0000146 // hack: set dir for import based on where this file is
147 {
148 const char * s = strrchr(file, '/');
149 if (s != NULL) {
150 int len = s - file;
151 char *dir = m_new(char, len + 1);
152 memcpy(dir, file, len);
153 dir[len] = '\0';
154 mp_import_set_directory(dir);
155 }
156 }
157
Damiend99b0522013-12-21 18:17:45 +0000158 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000159 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
160}
Damien429d7192013-10-04 19:53:11 +0100161
Damien George136f6752014-01-07 14:54:15 +0000162static void do_str(const char *str) {
163 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", str, strlen(str), false);
164 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100165}
Damien429d7192013-10-04 19:53:11 +0100166
Damiend99b0522013-12-21 18:17:45 +0000167typedef struct _test_obj_t {
168 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000169 int value;
Damiend99b0522013-12-21 18:17:45 +0000170} test_obj_t;
171
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200172static 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 +0000173 test_obj_t *self = self_in;
174 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000175}
176
Damiend99b0522013-12-21 18:17:45 +0000177static mp_obj_t test_get(mp_obj_t self_in) {
178 test_obj_t *self = self_in;
179 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000180}
181
Damiend99b0522013-12-21 18:17:45 +0000182static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
183 test_obj_t *self = self_in;
184 self->value = mp_obj_get_int(arg);
185 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000186}
187
Damiend99b0522013-12-21 18:17:45 +0000188static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
189static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
190
Damien George97209d32014-01-07 15:58:30 +0000191static const mp_method_t test_methods[] = {
192 { "get", &test_get_obj },
193 { "set", &test_set_obj },
194 { NULL, NULL },
195};
196
Damiend99b0522013-12-21 18:17:45 +0000197static const mp_obj_type_t test_type = {
198 { &mp_const_type },
Damiena53f6942013-11-02 23:58:38 +0000199 "Test",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200200 .print = test_print,
Damien George97209d32014-01-07 15:58:30 +0000201 .methods = test_methods,
Damiena53f6942013-11-02 23:58:38 +0000202};
203
Damiend99b0522013-12-21 18:17:45 +0000204mp_obj_t test_obj_new(int value) {
205 test_obj_t *o = m_new_obj(test_obj_t);
206 o->base.type = &test_type;
207 o->value = value;
208 return o;
209}
210
Damien George136f6752014-01-07 14:54:15 +0000211int usage(void) {
212 printf("usage: py [-c <command>] [<filename>]\n");
213 return 1;
214}
215
Damien5ac1b2e2013-10-18 19:58:12 +0100216int main(int argc, char **argv) {
217 qstr_init();
218 rt_init();
219
Damien George55baff42014-01-21 21:40:13 +0000220 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200221 mp_obj_t py_argv = mp_obj_new_list(0, NULL);
Damien George55baff42014-01-21 21:40:13 +0000222 rt_store_attr(m_sys, MP_QSTR_argv, py_argv);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200223
Damien George55baff42014-01-21 21:40:13 +0000224 rt_store_name(qstr_from_str("test"), test_obj_new(42));
225 rt_store_name(MP_QSTR_open, (mp_obj_t)&mp_builtin_open_obj);
Paul Sokolovskyfc926082014-01-18 23:47:44 +0200226 rawsocket_init();
Damiena53f6942013-11-02 23:58:38 +0000227
Damien George062478e2014-01-09 20:57:50 +0000228 // Here is some example code to create a class and instance of that class.
229 // First is the Python, then the C code.
230 //
231 // class TestClass:
232 // pass
233 // test_obj = TestClass()
234 // test_obj.attr = 42
235 mp_obj_t test_class_type, test_class_instance;
Damien George55baff42014-01-21 21:40:13 +0000236 test_class_type = mp_obj_new_type(QSTR_FROM_STR_STATIC("TestClass"), mp_const_empty_tuple, mp_obj_new_dict(0));
237 rt_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = rt_call_function_0(test_class_type));
238 rt_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
Damien George062478e2014-01-09 20:57:50 +0000239
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000240 /*
241 printf("bytes:\n");
242 printf(" total %d\n", m_get_total_bytes_allocated());
243 printf(" cur %d\n", m_get_current_bytes_allocated());
244 printf(" peak %d\n", m_get_peak_bytes_allocated());
245 */
246
Damien5ac1b2e2013-10-18 19:58:12 +0100247 if (argc == 1) {
248 do_repl();
Damien5ac1b2e2013-10-18 19:58:12 +0100249 } else {
Damien George136f6752014-01-07 14:54:15 +0000250 for (int a = 1; a < argc; a++) {
251 if (argv[a][0] == '-') {
252 if (strcmp(argv[a], "-c") == 0) {
253 if (a + 1 >= argc) {
254 return usage();
255 }
256 do_str(argv[a + 1]);
257 a += 1;
258 } else {
259 return usage();
260 }
261 } else {
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200262 for (int i = a; i < argc; i++) {
Damien George55baff42014-01-21 21:40:13 +0000263 rt_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200264 }
Damien George136f6752014-01-07 14:54:15 +0000265 do_file(argv[a]);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200266 break;
Damien George136f6752014-01-07 14:54:15 +0000267 }
268 }
Damien5ac1b2e2013-10-18 19:58:12 +0100269 }
Damien George136f6752014-01-07 14:54:15 +0000270
Damien429d7192013-10-04 19:53:11 +0100271 rt_deinit();
272
273 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
274 return 0;
275}
Damien087d2182013-11-09 20:14:30 +0000276
277// for sqrt
278#include <math.h>
279machine_float_t machine_sqrt(machine_float_t x) {
280 return sqrt(x);
281}