blob: c23a8e54c4d6ad45efa5153656931d0ac3e4eeba [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"
12#include "compile.h"
Damien0f082672013-12-17 18:33:53 +000013#include "obj.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);
88 bool comp_ok = mp_compile(pn, true);
Damien5ac1b2e2013-10-18 19:58:12 +010089 if (comp_ok) {
Damiend99b0522013-12-21 18:17:45 +000090 mp_obj_t module_fun = rt_make_function_from_id(1);
91 if (module_fun != mp_const_none) {
Damien5ac1b2e2013-10-18 19:58:12 +010092 nlr_buf_t nlr;
93 if (nlr_push(&nlr) == 0) {
94 rt_call_function_0(module_fun);
95 nlr_pop();
96 } else {
97 // uncaught exception
Damiend99b0522013-12-21 18:17:45 +000098 mp_obj_print((mp_obj_t)nlr.ret_val);
Damien5ac1b2e2013-10-18 19:58:12 +010099 printf("\n");
100 }
101 }
102 }
103 }
104 }
105}
106
107void do_file(const char *file) {
Damien George66028ab2014-01-03 14:03:48 +0000108 // hack: set dir for import based on where this file is
109 {
110 const char * s = strrchr(file, '/');
111 if (s != NULL) {
112 int len = s - file;
113 char *dir = m_new(char, len + 1);
114 memcpy(dir, file, len);
115 dir[len] = '\0';
116 mp_import_set_directory(dir);
117 }
118 }
119
Damiend99b0522013-12-21 18:17:45 +0000120 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien429d7192013-10-04 19:53:11 +0100121 //const char *pysrc = "def f():\n x=x+1\n print(42)\n";
Damiend99b0522013-12-21 18:17:45 +0000122 //mp_lexer_t *lex = mp_lexer_from_str_len("<>", pysrc, strlen(pysrc), false);
Damien429d7192013-10-04 19:53:11 +0100123 if (lex == NULL) {
Damien5ac1b2e2013-10-18 19:58:12 +0100124 return;
Damien429d7192013-10-04 19:53:11 +0100125 }
126
127 if (0) {
Damien5ac1b2e2013-10-18 19:58:12 +0100128 // just tokenise
Damiend99b0522013-12-21 18:17:45 +0000129 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
130 mp_token_show(mp_lexer_cur(lex));
131 mp_lexer_to_next(lex);
Damien429d7192013-10-04 19:53:11 +0100132 }
Damiend99b0522013-12-21 18:17:45 +0000133 mp_lexer_free(lex);
Damien5ac1b2e2013-10-18 19:58:12 +0100134
Damien429d7192013-10-04 19:53:11 +0100135 } else {
Damien5ac1b2e2013-10-18 19:58:12 +0100136 // compile
137
Damiend99b0522013-12-21 18:17:45 +0000138 mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT);
139 mp_lexer_free(lex);
Damienfa2162b2013-10-20 17:42:00 +0100140
Damiend99b0522013-12-21 18:17:45 +0000141 if (pn != MP_PARSE_NODE_NULL) {
Damien91d387d2013-10-09 15:09:52 +0100142 //printf("----------------\n");
Damiena3977762013-10-09 23:10:10 +0100143 //parse_node_show(pn, 0);
Damien91d387d2013-10-09 15:09:52 +0100144 //printf("----------------\n");
Damiend99b0522013-12-21 18:17:45 +0000145 bool comp_ok = mp_compile(pn, false);
Damien91d387d2013-10-09 15:09:52 +0100146 //printf("----------------\n");
Damien429d7192013-10-04 19:53:11 +0100147
Damiena5185f42013-10-20 14:41:27 +0100148#if MICROPY_EMIT_CPYTHON
149 if (!comp_ok) {
150 printf("compile error\n");
151 }
152#else
Damien5ac1b2e2013-10-18 19:58:12 +0100153 if (1 && comp_ok) {
154 // execute it
Damiend99b0522013-12-21 18:17:45 +0000155 mp_obj_t module_fun = rt_make_function_from_id(1);
156 if (module_fun != mp_const_none) {
Damien5ac1b2e2013-10-18 19:58:12 +0100157 nlr_buf_t nlr;
158 if (nlr_push(&nlr) == 0) {
Damienb86e3f92013-12-29 17:17:43 +0000159 rt_call_function_0(module_fun);
Damien5ac1b2e2013-10-18 19:58:12 +0100160 nlr_pop();
161 } else {
162 // uncaught exception
Damiend99b0522013-12-21 18:17:45 +0000163 mp_obj_print((mp_obj_t)nlr.ret_val);
Damien5ac1b2e2013-10-18 19:58:12 +0100164 printf("\n");
165 }
166 }
Damience89a212013-10-15 22:25:17 +0100167 }
Damien5ac1b2e2013-10-18 19:58:12 +0100168#endif
Damien429d7192013-10-04 19:53:11 +0100169 }
170 }
Damien5ac1b2e2013-10-18 19:58:12 +0100171}
Damien429d7192013-10-04 19:53:11 +0100172
Damiend99b0522013-12-21 18:17:45 +0000173typedef struct _test_obj_t {
174 mp_obj_base_t base;
175 bool value;
176} test_obj_t;
177
178static void test_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
179 test_obj_t *self = self_in;
180 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000181}
182
Damiend99b0522013-12-21 18:17:45 +0000183static mp_obj_t test_get(mp_obj_t self_in) {
184 test_obj_t *self = self_in;
185 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000186}
187
Damiend99b0522013-12-21 18:17:45 +0000188static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
189 test_obj_t *self = self_in;
190 self->value = mp_obj_get_int(arg);
191 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000192}
193
Damiend99b0522013-12-21 18:17:45 +0000194static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
195static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
196
197static const mp_obj_type_t test_type = {
198 { &mp_const_type },
Damiena53f6942013-11-02 23:58:38 +0000199 "Test",
Damiend99b0522013-12-21 18:17:45 +0000200 test_print, // print
201 NULL, // call_n
202 NULL, // unary_op
203 NULL, // binary_op
204 NULL, // getiter
205 NULL, // iternext
206 { // method list
207 { "get", &test_get_obj },
208 { "set", &test_set_obj },
209 { NULL, NULL },
Damiena53f6942013-11-02 23:58:38 +0000210 }
211};
212
Damiend99b0522013-12-21 18:17:45 +0000213mp_obj_t test_obj_new(int value) {
214 test_obj_t *o = m_new_obj(test_obj_t);
215 o->base.type = &test_type;
216 o->value = value;
217 return o;
218}
219
Damien5ac1b2e2013-10-18 19:58:12 +0100220int main(int argc, char **argv) {
221 qstr_init();
222 rt_init();
223
Damiend99b0522013-12-21 18:17:45 +0000224 rt_store_name(qstr_from_str_static("test"), test_obj_new(42));
Damiena53f6942013-11-02 23:58:38 +0000225
Damien5ac1b2e2013-10-18 19:58:12 +0100226 if (argc == 1) {
227 do_repl();
228 } else if (argc == 2) {
229 do_file(argv[1]);
230 } else {
231 printf("usage: py [<file>]\n");
232 return 1;
233 }
Damien429d7192013-10-04 19:53:11 +0100234 rt_deinit();
235
236 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
237 return 0;
238}
Damien087d2182013-11-09 20:14:30 +0000239
240// for sqrt
241#include <math.h>
242machine_float_t machine_sqrt(machine_float_t x) {
243 return sqrt(x);
244}