blob: b29a99f4fa5f5c2ae92d2b25f071c2bb9c27ac3d [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;
24
Damien George136f6752014-01-07 14:54:15 +000025static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
26 if (lex == NULL) {
27 return;
28 }
29
30 if (0) {
31 // just tokenise
32 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
33 mp_token_show(mp_lexer_cur(lex));
34 mp_lexer_to_next(lex);
35 }
36 mp_lexer_free(lex);
37 return;
38 }
39
40 mp_parse_node_t pn = mp_parse(lex, input_kind);
41 mp_lexer_free(lex);
42
43 if (pn == MP_PARSE_NODE_NULL) {
44 // parse error
45 return;
46 }
47
48 //printf("----------------\n");
49 //parse_node_show(pn, 0);
50 //printf("----------------\n");
51
52 mp_obj_t module_fun = mp_compile(pn, is_repl);
53
54 if (module_fun == mp_const_none) {
55 // compile error
56 return;
57 }
58
59 // execute it
60 nlr_buf_t nlr;
61 if (nlr_push(&nlr) == 0) {
62 rt_call_function_0(module_fun);
63 nlr_pop();
64 } else {
65 // uncaught exception
66 mp_obj_print((mp_obj_t)nlr.ret_val);
67 printf("\n");
68 }
69}
70
Damiend99b0522013-12-21 18:17:45 +000071static char *str_join(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +010072 int l1 = strlen(s1);
73 int l2 = strlen(s2);
74 char *s = m_new(char, l1 + l2 + 2);
75 memcpy(s, s1, l1);
76 if (sep_char != 0) {
77 s[l1] = sep_char;
78 l1 += 1;
79 }
80 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +010081 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +010082 return s;
83}
84
Paul Sokolovskyfa027672014-01-01 18:28:01 +020085static char *prompt(char *p) {
Paul Sokolovskyd674bd52014-01-04 19:38:19 +020086#if MICROPY_USE_READLINE
Paul Sokolovskyfa027672014-01-01 18:28:01 +020087 char *line = readline(p);
88 if (line) {
89 add_history(line);
90 }
91#else
92 static char buf[256];
93 fputs(p, stdout);
94 char *s = fgets(buf, sizeof(buf), stdin);
95 if (!s) {
96 return NULL;
97 }
98 int l = strlen(buf);
99 if (buf[l - 1] == '\n') {
100 buf[l - 1] = 0;
101 } else {
102 l++;
103 }
104 char *line = m_new(char, l);
105 memcpy(line, buf, l);
106#endif
107 return line;
108}
109
Damiend99b0522013-12-21 18:17:45 +0000110static void do_repl(void) {
Damien5ac1b2e2013-10-18 19:58:12 +0100111 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200112 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100113 if (line == NULL) {
114 // EOF
115 return;
116 }
Damiend99b0522013-12-21 18:17:45 +0000117 if (mp_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +0100118 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200119 char *line2 = prompt("... ");
Damien5ac1b2e2013-10-18 19:58:12 +0100120 if (line2 == NULL || strlen(line2) == 0) {
121 break;
122 }
123 char *line3 = str_join(line, '\n', line2);
Damien732407f2013-12-29 19:33:23 +0000124 free(line);
125 free(line2);
Damien5ac1b2e2013-10-18 19:58:12 +0100126 line = line3;
127 }
128 }
Damienfa2162b2013-10-20 17:42:00 +0100129
Damiend99b0522013-12-21 18:17:45 +0000130 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", line, strlen(line), false);
Damien George136f6752014-01-07 14:54:15 +0000131 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Damien5ac1b2e2013-10-18 19:58:12 +0100132 }
133}
134
Damien George136f6752014-01-07 14:54:15 +0000135static void do_file(const char *file) {
Damien George66028ab2014-01-03 14:03:48 +0000136 // hack: set dir for import based on where this file is
137 {
138 const char * s = strrchr(file, '/');
139 if (s != NULL) {
140 int len = s - file;
141 char *dir = m_new(char, len + 1);
142 memcpy(dir, file, len);
143 dir[len] = '\0';
144 mp_import_set_directory(dir);
145 }
146 }
147
Damiend99b0522013-12-21 18:17:45 +0000148 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000149 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
150}
Damien429d7192013-10-04 19:53:11 +0100151
Damien George136f6752014-01-07 14:54:15 +0000152static void do_str(const char *str) {
153 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", str, strlen(str), false);
154 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100155}
Damien429d7192013-10-04 19:53:11 +0100156
Damiend99b0522013-12-21 18:17:45 +0000157typedef struct _test_obj_t {
158 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000159 int value;
Damiend99b0522013-12-21 18:17:45 +0000160} test_obj_t;
161
162static void test_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
163 test_obj_t *self = self_in;
164 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000165}
166
Damiend99b0522013-12-21 18:17:45 +0000167static mp_obj_t test_get(mp_obj_t self_in) {
168 test_obj_t *self = self_in;
169 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000170}
171
Damiend99b0522013-12-21 18:17:45 +0000172static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
173 test_obj_t *self = self_in;
174 self->value = mp_obj_get_int(arg);
175 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000176}
177
Damiend99b0522013-12-21 18:17:45 +0000178static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
179static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
180
Damien George97209d32014-01-07 15:58:30 +0000181static const mp_method_t test_methods[] = {
182 { "get", &test_get_obj },
183 { "set", &test_set_obj },
184 { NULL, NULL },
185};
186
Damiend99b0522013-12-21 18:17:45 +0000187static const mp_obj_type_t test_type = {
188 { &mp_const_type },
Damiena53f6942013-11-02 23:58:38 +0000189 "Test",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200190 .print = test_print,
Damien George97209d32014-01-07 15:58:30 +0000191 .methods = test_methods,
Damiena53f6942013-11-02 23:58:38 +0000192};
193
Damiend99b0522013-12-21 18:17:45 +0000194mp_obj_t test_obj_new(int value) {
195 test_obj_t *o = m_new_obj(test_obj_t);
196 o->base.type = &test_type;
197 o->value = value;
198 return o;
199}
200
Damien George136f6752014-01-07 14:54:15 +0000201int usage(void) {
202 printf("usage: py [-c <command>] [<filename>]\n");
203 return 1;
204}
205
Damien5ac1b2e2013-10-18 19:58:12 +0100206int main(int argc, char **argv) {
207 qstr_init();
208 rt_init();
209
Damiend99b0522013-12-21 18:17:45 +0000210 rt_store_name(qstr_from_str_static("test"), test_obj_new(42));
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +0200211 rt_store_name(qstr_from_str_static("open"), (mp_obj_t)&mp_builtin_open_obj);
Damiena53f6942013-11-02 23:58:38 +0000212
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000213 /*
214 printf("bytes:\n");
215 printf(" total %d\n", m_get_total_bytes_allocated());
216 printf(" cur %d\n", m_get_current_bytes_allocated());
217 printf(" peak %d\n", m_get_peak_bytes_allocated());
218 */
219
Damien5ac1b2e2013-10-18 19:58:12 +0100220 if (argc == 1) {
221 do_repl();
Damien5ac1b2e2013-10-18 19:58:12 +0100222 } else {
Damien George136f6752014-01-07 14:54:15 +0000223 for (int a = 1; a < argc; a++) {
224 if (argv[a][0] == '-') {
225 if (strcmp(argv[a], "-c") == 0) {
226 if (a + 1 >= argc) {
227 return usage();
228 }
229 do_str(argv[a + 1]);
230 a += 1;
231 } else {
232 return usage();
233 }
234 } else {
235 do_file(argv[a]);
236 }
237 }
Damien5ac1b2e2013-10-18 19:58:12 +0100238 }
Damien George136f6752014-01-07 14:54:15 +0000239
Damien429d7192013-10-04 19:53:11 +0100240 rt_deinit();
241
242 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
243 return 0;
244}
Damien087d2182013-11-09 20:14:30 +0000245
246// for sqrt
247#include <math.h>
248machine_float_t machine_sqrt(machine_float_t x) {
249 return sqrt(x);
250}