blob: 376dbc0c045d3ab21d0ed93a9e4f52a37053d178 [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) {
Damiend99b0522013-12-21 18:17:45 +0000108 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien429d7192013-10-04 19:53:11 +0100109 //const char *pysrc = "def f():\n x=x+1\n print(42)\n";
Damiend99b0522013-12-21 18:17:45 +0000110 //mp_lexer_t *lex = mp_lexer_from_str_len("<>", pysrc, strlen(pysrc), false);
Damien429d7192013-10-04 19:53:11 +0100111 if (lex == NULL) {
Damien5ac1b2e2013-10-18 19:58:12 +0100112 return;
Damien429d7192013-10-04 19:53:11 +0100113 }
114
115 if (0) {
Damien5ac1b2e2013-10-18 19:58:12 +0100116 // just tokenise
Damiend99b0522013-12-21 18:17:45 +0000117 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
118 mp_token_show(mp_lexer_cur(lex));
119 mp_lexer_to_next(lex);
Damien429d7192013-10-04 19:53:11 +0100120 }
Damiend99b0522013-12-21 18:17:45 +0000121 mp_lexer_free(lex);
Damien5ac1b2e2013-10-18 19:58:12 +0100122
Damien429d7192013-10-04 19:53:11 +0100123 } else {
Damien5ac1b2e2013-10-18 19:58:12 +0100124 // compile
125
Damiend99b0522013-12-21 18:17:45 +0000126 mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT);
127 mp_lexer_free(lex);
Damienfa2162b2013-10-20 17:42:00 +0100128
Damiend99b0522013-12-21 18:17:45 +0000129 if (pn != MP_PARSE_NODE_NULL) {
Damien91d387d2013-10-09 15:09:52 +0100130 //printf("----------------\n");
Damiena3977762013-10-09 23:10:10 +0100131 //parse_node_show(pn, 0);
Damien91d387d2013-10-09 15:09:52 +0100132 //printf("----------------\n");
Damiend99b0522013-12-21 18:17:45 +0000133 bool comp_ok = mp_compile(pn, false);
Damien91d387d2013-10-09 15:09:52 +0100134 //printf("----------------\n");
Damien429d7192013-10-04 19:53:11 +0100135
Damiena5185f42013-10-20 14:41:27 +0100136#if MICROPY_EMIT_CPYTHON
137 if (!comp_ok) {
138 printf("compile error\n");
139 }
140#else
Damien5ac1b2e2013-10-18 19:58:12 +0100141 if (1 && comp_ok) {
142 // execute it
Damiend99b0522013-12-21 18:17:45 +0000143 mp_obj_t module_fun = rt_make_function_from_id(1);
144 if (module_fun != mp_const_none) {
Damien5ac1b2e2013-10-18 19:58:12 +0100145 nlr_buf_t nlr;
146 if (nlr_push(&nlr) == 0) {
Damienb86e3f92013-12-29 17:17:43 +0000147 rt_call_function_0(module_fun);
Damien5ac1b2e2013-10-18 19:58:12 +0100148 nlr_pop();
149 } else {
150 // uncaught exception
Damiend99b0522013-12-21 18:17:45 +0000151 mp_obj_print((mp_obj_t)nlr.ret_val);
Damien5ac1b2e2013-10-18 19:58:12 +0100152 printf("\n");
153 }
154 }
Damience89a212013-10-15 22:25:17 +0100155 }
Damien5ac1b2e2013-10-18 19:58:12 +0100156#endif
Damien429d7192013-10-04 19:53:11 +0100157 }
158 }
Damien5ac1b2e2013-10-18 19:58:12 +0100159}
Damien429d7192013-10-04 19:53:11 +0100160
Damiend99b0522013-12-21 18:17:45 +0000161typedef struct _test_obj_t {
162 mp_obj_base_t base;
163 bool value;
164} test_obj_t;
165
166static void test_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
167 test_obj_t *self = self_in;
168 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000169}
170
Damiend99b0522013-12-21 18:17:45 +0000171static mp_obj_t test_get(mp_obj_t self_in) {
172 test_obj_t *self = self_in;
173 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000174}
175
Damiend99b0522013-12-21 18:17:45 +0000176static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
177 test_obj_t *self = self_in;
178 self->value = mp_obj_get_int(arg);
179 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000180}
181
Damiend99b0522013-12-21 18:17:45 +0000182static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
183static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
184
185static const mp_obj_type_t test_type = {
186 { &mp_const_type },
Damiena53f6942013-11-02 23:58:38 +0000187 "Test",
Damiend99b0522013-12-21 18:17:45 +0000188 test_print, // print
189 NULL, // call_n
190 NULL, // unary_op
191 NULL, // binary_op
192 NULL, // getiter
193 NULL, // iternext
194 { // method list
195 { "get", &test_get_obj },
196 { "set", &test_set_obj },
197 { NULL, NULL },
Damiena53f6942013-11-02 23:58:38 +0000198 }
199};
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
Damien5ac1b2e2013-10-18 19:58:12 +0100208int main(int argc, char **argv) {
209 qstr_init();
210 rt_init();
211
Damiend99b0522013-12-21 18:17:45 +0000212 rt_store_name(qstr_from_str_static("test"), test_obj_new(42));
Damiena53f6942013-11-02 23:58:38 +0000213
Damien5ac1b2e2013-10-18 19:58:12 +0100214 if (argc == 1) {
215 do_repl();
216 } else if (argc == 2) {
217 do_file(argv[1]);
218 } else {
219 printf("usage: py [<file>]\n");
220 return 1;
221 }
Damien429d7192013-10-04 19:53:11 +0100222 rt_deinit();
223
224 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
225 return 0;
226}
Damien087d2182013-11-09 20:14:30 +0000227
228// for sqrt
229#include <math.h>
230machine_float_t machine_sqrt(machine_float_t x) {
231 return sqrt(x);
232}