blob: e73f69ee24144ec79d93d6d5bd83a48352bb668c [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 Sokolovskyfa027672014-01-01 18:28:01 +020018#ifdef 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
Damiend99b0522013-12-21 18:17:45 +000023static char *str_join(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +010024 int l1 = strlen(s1);
25 int l2 = strlen(s2);
26 char *s = m_new(char, l1 + l2 + 2);
27 memcpy(s, s1, l1);
28 if (sep_char != 0) {
29 s[l1] = sep_char;
30 l1 += 1;
31 }
32 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +010033 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +010034 return s;
35}
36
Paul Sokolovskyfa027672014-01-01 18:28:01 +020037static char *prompt(char *p) {
38#ifdef USE_READLINE
39 char *line = readline(p);
40 if (line) {
41 add_history(line);
42 }
43#else
44 static char buf[256];
45 fputs(p, stdout);
46 char *s = fgets(buf, sizeof(buf), stdin);
47 if (!s) {
48 return NULL;
49 }
50 int l = strlen(buf);
51 if (buf[l - 1] == '\n') {
52 buf[l - 1] = 0;
53 } else {
54 l++;
55 }
56 char *line = m_new(char, l);
57 memcpy(line, buf, l);
58#endif
59 return line;
60}
61
Damiend99b0522013-12-21 18:17:45 +000062static void do_repl(void) {
Damien5ac1b2e2013-10-18 19:58:12 +010063 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +020064 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +010065 if (line == NULL) {
66 // EOF
67 return;
68 }
Damiend99b0522013-12-21 18:17:45 +000069 if (mp_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +010070 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +020071 char *line2 = prompt("... ");
Damien5ac1b2e2013-10-18 19:58:12 +010072 if (line2 == NULL || strlen(line2) == 0) {
73 break;
74 }
75 char *line3 = str_join(line, '\n', line2);
Damien732407f2013-12-29 19:33:23 +000076 free(line);
77 free(line2);
Damien5ac1b2e2013-10-18 19:58:12 +010078 line = line3;
79 }
80 }
Damienfa2162b2013-10-20 17:42:00 +010081
Damiend99b0522013-12-21 18:17:45 +000082 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", line, strlen(line), false);
83 mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT);
84 mp_lexer_free(lex);
Damienfa2162b2013-10-20 17:42:00 +010085
Damiend99b0522013-12-21 18:17:45 +000086 if (pn != MP_PARSE_NODE_NULL) {
87 //mp_parse_node_show(pn, 0);
Damien George1fb03172014-01-03 14:22:03 +000088 mp_obj_t module_fun = mp_compile(pn, true);
89 if (module_fun != mp_const_none) {
90 nlr_buf_t nlr;
91 if (nlr_push(&nlr) == 0) {
92 rt_call_function_0(module_fun);
93 nlr_pop();
94 } else {
95 // uncaught exception
96 mp_obj_print((mp_obj_t)nlr.ret_val);
97 printf("\n");
Damien5ac1b2e2013-10-18 19:58:12 +010098 }
99 }
100 }
101 }
102}
103
104void do_file(const char *file) {
Damien George66028ab2014-01-03 14:03:48 +0000105 // hack: set dir for import based on where this file is
106 {
107 const char * s = strrchr(file, '/');
108 if (s != NULL) {
109 int len = s - file;
110 char *dir = m_new(char, len + 1);
111 memcpy(dir, file, len);
112 dir[len] = '\0';
113 mp_import_set_directory(dir);
114 }
115 }
116
Damiend99b0522013-12-21 18:17:45 +0000117 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien429d7192013-10-04 19:53:11 +0100118 //const char *pysrc = "def f():\n x=x+1\n print(42)\n";
Damiend99b0522013-12-21 18:17:45 +0000119 //mp_lexer_t *lex = mp_lexer_from_str_len("<>", pysrc, strlen(pysrc), false);
Damien429d7192013-10-04 19:53:11 +0100120 if (lex == NULL) {
Damien5ac1b2e2013-10-18 19:58:12 +0100121 return;
Damien429d7192013-10-04 19:53:11 +0100122 }
123
124 if (0) {
Damien5ac1b2e2013-10-18 19:58:12 +0100125 // just tokenise
Damiend99b0522013-12-21 18:17:45 +0000126 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
127 mp_token_show(mp_lexer_cur(lex));
128 mp_lexer_to_next(lex);
Damien429d7192013-10-04 19:53:11 +0100129 }
Damiend99b0522013-12-21 18:17:45 +0000130 mp_lexer_free(lex);
Damien5ac1b2e2013-10-18 19:58:12 +0100131
Damien429d7192013-10-04 19:53:11 +0100132 } else {
Damien5ac1b2e2013-10-18 19:58:12 +0100133 // compile
134
Damiend99b0522013-12-21 18:17:45 +0000135 mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT);
136 mp_lexer_free(lex);
Damienfa2162b2013-10-20 17:42:00 +0100137
Damiend99b0522013-12-21 18:17:45 +0000138 if (pn != MP_PARSE_NODE_NULL) {
Damien91d387d2013-10-09 15:09:52 +0100139 //printf("----------------\n");
Damiena3977762013-10-09 23:10:10 +0100140 //parse_node_show(pn, 0);
Damien91d387d2013-10-09 15:09:52 +0100141 //printf("----------------\n");
Damien George1fb03172014-01-03 14:22:03 +0000142 mp_obj_t module_fun = mp_compile(pn, false);
Damien91d387d2013-10-09 15:09:52 +0100143 //printf("----------------\n");
Damien429d7192013-10-04 19:53:11 +0100144
Damiena5185f42013-10-20 14:41:27 +0100145#if MICROPY_EMIT_CPYTHON
146 if (!comp_ok) {
147 printf("compile error\n");
148 }
149#else
Damien George1fb03172014-01-03 14:22:03 +0000150 if (1 && module_fun != mp_const_none) {
Damien5ac1b2e2013-10-18 19:58:12 +0100151 // execute it
Damien George1fb03172014-01-03 14:22:03 +0000152 nlr_buf_t nlr;
153 if (nlr_push(&nlr) == 0) {
154 rt_call_function_0(module_fun);
155 nlr_pop();
156 } else {
157 // uncaught exception
158 mp_obj_print((mp_obj_t)nlr.ret_val);
159 printf("\n");
Damien5ac1b2e2013-10-18 19:58:12 +0100160 }
Damience89a212013-10-15 22:25:17 +0100161 }
Damien5ac1b2e2013-10-18 19:58:12 +0100162#endif
Damien429d7192013-10-04 19:53:11 +0100163 }
164 }
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;
169 bool value;
170} test_obj_t;
171
172static void test_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
173 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
191static const mp_obj_type_t test_type = {
192 { &mp_const_type },
Damiena53f6942013-11-02 23:58:38 +0000193 "Test",
Damiend99b0522013-12-21 18:17:45 +0000194 test_print, // print
195 NULL, // call_n
196 NULL, // unary_op
197 NULL, // binary_op
198 NULL, // getiter
199 NULL, // iternext
200 { // method list
201 { "get", &test_get_obj },
202 { "set", &test_set_obj },
203 { NULL, NULL },
Damiena53f6942013-11-02 23:58:38 +0000204 }
205};
206
Damiend99b0522013-12-21 18:17:45 +0000207mp_obj_t test_obj_new(int value) {
208 test_obj_t *o = m_new_obj(test_obj_t);
209 o->base.type = &test_type;
210 o->value = value;
211 return o;
212}
213
Damien5ac1b2e2013-10-18 19:58:12 +0100214int main(int argc, char **argv) {
215 qstr_init();
216 rt_init();
217
Damiend99b0522013-12-21 18:17:45 +0000218 rt_store_name(qstr_from_str_static("test"), test_obj_new(42));
Damiena53f6942013-11-02 23:58:38 +0000219
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000220 /*
221 printf("bytes:\n");
222 printf(" total %d\n", m_get_total_bytes_allocated());
223 printf(" cur %d\n", m_get_current_bytes_allocated());
224 printf(" peak %d\n", m_get_peak_bytes_allocated());
225 */
226
Damien5ac1b2e2013-10-18 19:58:12 +0100227 if (argc == 1) {
228 do_repl();
229 } else if (argc == 2) {
230 do_file(argv[1]);
231 } else {
232 printf("usage: py [<file>]\n");
233 return 1;
234 }
Damien429d7192013-10-04 19:53:11 +0100235 rt_deinit();
236
237 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
238 return 0;
239}
Damien087d2182013-11-09 20:14:30 +0000240
241// for sqrt
242#include <math.h>
243machine_float_t machine_sqrt(machine_float_t x) {
244 return sqrt(x);
245}