blob: ba0e09d242f4ebcc209eff6a44ac1f6f1f52877d [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 George9528cd62014-01-15 21:23:31 +000053 mp_lexer_free(lex);
54
Damien George136f6752014-01-07 14:54:15 +000055 //printf("----------------\n");
56 //parse_node_show(pn, 0);
57 //printf("----------------\n");
58
59 mp_obj_t module_fun = mp_compile(pn, is_repl);
60
61 if (module_fun == mp_const_none) {
62 // compile error
63 return;
64 }
65
66 // execute it
67 nlr_buf_t nlr;
68 if (nlr_push(&nlr) == 0) {
69 rt_call_function_0(module_fun);
70 nlr_pop();
71 } else {
72 // uncaught exception
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020073 mp_obj_print((mp_obj_t)nlr.ret_val, PRINT_REPR);
Damien George136f6752014-01-07 14:54:15 +000074 printf("\n");
75 }
76}
77
Damiend99b0522013-12-21 18:17:45 +000078static char *str_join(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +010079 int l1 = strlen(s1);
80 int l2 = strlen(s2);
81 char *s = m_new(char, l1 + l2 + 2);
82 memcpy(s, s1, l1);
83 if (sep_char != 0) {
84 s[l1] = sep_char;
85 l1 += 1;
86 }
87 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +010088 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +010089 return s;
90}
91
Paul Sokolovskyfa027672014-01-01 18:28:01 +020092static char *prompt(char *p) {
Paul Sokolovskyd674bd52014-01-04 19:38:19 +020093#if MICROPY_USE_READLINE
Paul Sokolovskyfa027672014-01-01 18:28:01 +020094 char *line = readline(p);
95 if (line) {
96 add_history(line);
97 }
98#else
99 static char buf[256];
100 fputs(p, stdout);
101 char *s = fgets(buf, sizeof(buf), stdin);
102 if (!s) {
103 return NULL;
104 }
105 int l = strlen(buf);
106 if (buf[l - 1] == '\n') {
107 buf[l - 1] = 0;
108 } else {
109 l++;
110 }
111 char *line = m_new(char, l);
112 memcpy(line, buf, l);
113#endif
114 return line;
115}
116
Damiend99b0522013-12-21 18:17:45 +0000117static void do_repl(void) {
Damien5ac1b2e2013-10-18 19:58:12 +0100118 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200119 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100120 if (line == NULL) {
121 // EOF
122 return;
123 }
Damiend99b0522013-12-21 18:17:45 +0000124 if (mp_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +0100125 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200126 char *line2 = prompt("... ");
Damien5ac1b2e2013-10-18 19:58:12 +0100127 if (line2 == NULL || strlen(line2) == 0) {
128 break;
129 }
130 char *line3 = str_join(line, '\n', line2);
Damien732407f2013-12-29 19:33:23 +0000131 free(line);
132 free(line2);
Damien5ac1b2e2013-10-18 19:58:12 +0100133 line = line3;
134 }
135 }
Damienfa2162b2013-10-20 17:42:00 +0100136
Damiend99b0522013-12-21 18:17:45 +0000137 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", line, strlen(line), false);
Damien George136f6752014-01-07 14:54:15 +0000138 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Damien5ac1b2e2013-10-18 19:58:12 +0100139 }
140}
141
Damien George136f6752014-01-07 14:54:15 +0000142static void do_file(const char *file) {
Damien George66028ab2014-01-03 14:03:48 +0000143 // hack: set dir for import based on where this file is
144 {
145 const char * s = strrchr(file, '/');
146 if (s != NULL) {
147 int len = s - file;
148 char *dir = m_new(char, len + 1);
149 memcpy(dir, file, len);
150 dir[len] = '\0';
151 mp_import_set_directory(dir);
152 }
153 }
154
Damiend99b0522013-12-21 18:17:45 +0000155 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000156 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
157}
Damien429d7192013-10-04 19:53:11 +0100158
Damien George136f6752014-01-07 14:54:15 +0000159static void do_str(const char *str) {
160 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", str, strlen(str), false);
161 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100162}
Damien429d7192013-10-04 19:53:11 +0100163
Damiend99b0522013-12-21 18:17:45 +0000164typedef struct _test_obj_t {
165 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000166 int value;
Damiend99b0522013-12-21 18:17:45 +0000167} test_obj_t;
168
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200169static 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 +0000170 test_obj_t *self = self_in;
171 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000172}
173
Damiend99b0522013-12-21 18:17:45 +0000174static mp_obj_t test_get(mp_obj_t self_in) {
175 test_obj_t *self = self_in;
176 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000177}
178
Damiend99b0522013-12-21 18:17:45 +0000179static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
180 test_obj_t *self = self_in;
181 self->value = mp_obj_get_int(arg);
182 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000183}
184
Damiend99b0522013-12-21 18:17:45 +0000185static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
186static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
187
Damien George97209d32014-01-07 15:58:30 +0000188static const mp_method_t test_methods[] = {
189 { "get", &test_get_obj },
190 { "set", &test_set_obj },
191 { NULL, NULL },
192};
193
Damiend99b0522013-12-21 18:17:45 +0000194static const mp_obj_type_t test_type = {
195 { &mp_const_type },
Damiena53f6942013-11-02 23:58:38 +0000196 "Test",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200197 .print = test_print,
Damien George97209d32014-01-07 15:58:30 +0000198 .methods = test_methods,
Damiena53f6942013-11-02 23:58:38 +0000199};
200
Damiend99b0522013-12-21 18:17:45 +0000201mp_obj_t test_obj_new(int value) {
202 test_obj_t *o = m_new_obj(test_obj_t);
203 o->base.type = &test_type;
204 o->value = value;
205 return o;
206}
207
Damien George136f6752014-01-07 14:54:15 +0000208int usage(void) {
209 printf("usage: py [-c <command>] [<filename>]\n");
210 return 1;
211}
212
Damien5ac1b2e2013-10-18 19:58:12 +0100213int main(int argc, char **argv) {
214 qstr_init();
215 rt_init();
216
Damiend99b0522013-12-21 18:17:45 +0000217 rt_store_name(qstr_from_str_static("test"), test_obj_new(42));
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +0200218 rt_store_name(qstr_from_str_static("open"), (mp_obj_t)&mp_builtin_open_obj);
Paul Sokolovskyfc926082014-01-18 23:47:44 +0200219 rawsocket_init();
Damiena53f6942013-11-02 23:58:38 +0000220
Damien George062478e2014-01-09 20:57:50 +0000221 // Here is some example code to create a class and instance of that class.
222 // First is the Python, then the C code.
223 //
224 // class TestClass:
225 // pass
226 // test_obj = TestClass()
227 // test_obj.attr = 42
228 mp_obj_t test_class_type, test_class_instance;
Damien George004cdce2014-01-09 21:43:51 +0000229 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 +0000230 rt_store_name(qstr_from_str_static("test_obj"), test_class_instance = rt_call_function_0(test_class_type));
231 rt_store_attr(test_class_instance, qstr_from_str_static("attr"), mp_obj_new_int(42));
232
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000233 /*
234 printf("bytes:\n");
235 printf(" total %d\n", m_get_total_bytes_allocated());
236 printf(" cur %d\n", m_get_current_bytes_allocated());
237 printf(" peak %d\n", m_get_peak_bytes_allocated());
238 */
239
Damien5ac1b2e2013-10-18 19:58:12 +0100240 if (argc == 1) {
241 do_repl();
Damien5ac1b2e2013-10-18 19:58:12 +0100242 } else {
Damien George136f6752014-01-07 14:54:15 +0000243 for (int a = 1; a < argc; a++) {
244 if (argv[a][0] == '-') {
245 if (strcmp(argv[a], "-c") == 0) {
246 if (a + 1 >= argc) {
247 return usage();
248 }
249 do_str(argv[a + 1]);
250 a += 1;
251 } else {
252 return usage();
253 }
254 } else {
255 do_file(argv[a]);
256 }
257 }
Damien5ac1b2e2013-10-18 19:58:12 +0100258 }
Damien George136f6752014-01-07 14:54:15 +0000259
Damien429d7192013-10-04 19:53:11 +0100260 rt_deinit();
261
262 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
263 return 0;
264}
Damien087d2182013-11-09 20:14:30 +0000265
266// for sqrt
267#include <math.h>
268machine_float_t machine_sqrt(machine_float_t x) {
269 return sqrt(x);
270}