blob: 920aed3444adfaf10c7872bdbf262f78c579f756 [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
Damien George136f6752014-01-07 14:54:15 +000023static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
24 if (lex == NULL) {
25 return;
26 }
27
28 if (0) {
29 // just tokenise
30 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
31 mp_token_show(mp_lexer_cur(lex));
32 mp_lexer_to_next(lex);
33 }
34 mp_lexer_free(lex);
35 return;
36 }
37
38 mp_parse_node_t pn = mp_parse(lex, input_kind);
39 mp_lexer_free(lex);
40
41 if (pn == MP_PARSE_NODE_NULL) {
42 // parse error
43 return;
44 }
45
46 //printf("----------------\n");
47 //parse_node_show(pn, 0);
48 //printf("----------------\n");
49
50 mp_obj_t module_fun = mp_compile(pn, is_repl);
51
52 if (module_fun == mp_const_none) {
53 // compile error
54 return;
55 }
56
57 // execute it
58 nlr_buf_t nlr;
59 if (nlr_push(&nlr) == 0) {
60 rt_call_function_0(module_fun);
61 nlr_pop();
62 } else {
63 // uncaught exception
64 mp_obj_print((mp_obj_t)nlr.ret_val);
65 printf("\n");
66 }
67}
68
Damiend99b0522013-12-21 18:17:45 +000069static char *str_join(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +010070 int l1 = strlen(s1);
71 int l2 = strlen(s2);
72 char *s = m_new(char, l1 + l2 + 2);
73 memcpy(s, s1, l1);
74 if (sep_char != 0) {
75 s[l1] = sep_char;
76 l1 += 1;
77 }
78 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +010079 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +010080 return s;
81}
82
Paul Sokolovskyfa027672014-01-01 18:28:01 +020083static char *prompt(char *p) {
Paul Sokolovskyd674bd52014-01-04 19:38:19 +020084#if MICROPY_USE_READLINE
Paul Sokolovskyfa027672014-01-01 18:28:01 +020085 char *line = readline(p);
86 if (line) {
87 add_history(line);
88 }
89#else
90 static char buf[256];
91 fputs(p, stdout);
92 char *s = fgets(buf, sizeof(buf), stdin);
93 if (!s) {
94 return NULL;
95 }
96 int l = strlen(buf);
97 if (buf[l - 1] == '\n') {
98 buf[l - 1] = 0;
99 } else {
100 l++;
101 }
102 char *line = m_new(char, l);
103 memcpy(line, buf, l);
104#endif
105 return line;
106}
107
Damiend99b0522013-12-21 18:17:45 +0000108static void do_repl(void) {
Damien5ac1b2e2013-10-18 19:58:12 +0100109 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200110 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100111 if (line == NULL) {
112 // EOF
113 return;
114 }
Damiend99b0522013-12-21 18:17:45 +0000115 if (mp_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +0100116 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200117 char *line2 = prompt("... ");
Damien5ac1b2e2013-10-18 19:58:12 +0100118 if (line2 == NULL || strlen(line2) == 0) {
119 break;
120 }
121 char *line3 = str_join(line, '\n', line2);
Damien732407f2013-12-29 19:33:23 +0000122 free(line);
123 free(line2);
Damien5ac1b2e2013-10-18 19:58:12 +0100124 line = line3;
125 }
126 }
Damienfa2162b2013-10-20 17:42:00 +0100127
Damiend99b0522013-12-21 18:17:45 +0000128 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", line, strlen(line), false);
Damien George136f6752014-01-07 14:54:15 +0000129 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Damien5ac1b2e2013-10-18 19:58:12 +0100130 }
131}
132
Damien George136f6752014-01-07 14:54:15 +0000133static void do_file(const char *file) {
Damien George66028ab2014-01-03 14:03:48 +0000134 // hack: set dir for import based on where this file is
135 {
136 const char * s = strrchr(file, '/');
137 if (s != NULL) {
138 int len = s - file;
139 char *dir = m_new(char, len + 1);
140 memcpy(dir, file, len);
141 dir[len] = '\0';
142 mp_import_set_directory(dir);
143 }
144 }
145
Damiend99b0522013-12-21 18:17:45 +0000146 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000147 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
148}
Damien429d7192013-10-04 19:53:11 +0100149
Damien George136f6752014-01-07 14:54:15 +0000150static void do_str(const char *str) {
151 mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", str, strlen(str), false);
152 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100153}
Damien429d7192013-10-04 19:53:11 +0100154
Damiend99b0522013-12-21 18:17:45 +0000155typedef struct _test_obj_t {
156 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000157 int value;
Damiend99b0522013-12-21 18:17:45 +0000158} test_obj_t;
159
160static void test_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
161 test_obj_t *self = self_in;
162 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000163}
164
Damiend99b0522013-12-21 18:17:45 +0000165static mp_obj_t test_get(mp_obj_t self_in) {
166 test_obj_t *self = self_in;
167 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000168}
169
Damiend99b0522013-12-21 18:17:45 +0000170static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
171 test_obj_t *self = self_in;
172 self->value = mp_obj_get_int(arg);
173 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000174}
175
Damiend99b0522013-12-21 18:17:45 +0000176static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
177static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
178
Damien George97209d32014-01-07 15:58:30 +0000179static const mp_method_t test_methods[] = {
180 { "get", &test_get_obj },
181 { "set", &test_set_obj },
182 { NULL, NULL },
183};
184
Damiend99b0522013-12-21 18:17:45 +0000185static const mp_obj_type_t test_type = {
186 { &mp_const_type },
Damiena53f6942013-11-02 23:58:38 +0000187 "Test",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200188 .print = test_print,
Damien George97209d32014-01-07 15:58:30 +0000189 .methods = test_methods,
Damiena53f6942013-11-02 23:58:38 +0000190};
191
Damiend99b0522013-12-21 18:17:45 +0000192mp_obj_t test_obj_new(int value) {
193 test_obj_t *o = m_new_obj(test_obj_t);
194 o->base.type = &test_type;
195 o->value = value;
196 return o;
197}
198
Damien George136f6752014-01-07 14:54:15 +0000199int usage(void) {
200 printf("usage: py [-c <command>] [<filename>]\n");
201 return 1;
202}
203
Damien5ac1b2e2013-10-18 19:58:12 +0100204int main(int argc, char **argv) {
205 qstr_init();
206 rt_init();
207
Damiend99b0522013-12-21 18:17:45 +0000208 rt_store_name(qstr_from_str_static("test"), test_obj_new(42));
Damiena53f6942013-11-02 23:58:38 +0000209
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000210 /*
211 printf("bytes:\n");
212 printf(" total %d\n", m_get_total_bytes_allocated());
213 printf(" cur %d\n", m_get_current_bytes_allocated());
214 printf(" peak %d\n", m_get_peak_bytes_allocated());
215 */
216
Damien5ac1b2e2013-10-18 19:58:12 +0100217 if (argc == 1) {
218 do_repl();
Damien5ac1b2e2013-10-18 19:58:12 +0100219 } else {
Damien George136f6752014-01-07 14:54:15 +0000220 for (int a = 1; a < argc; a++) {
221 if (argv[a][0] == '-') {
222 if (strcmp(argv[a], "-c") == 0) {
223 if (a + 1 >= argc) {
224 return usage();
225 }
226 do_str(argv[a + 1]);
227 a += 1;
228 } else {
229 return usage();
230 }
231 } else {
232 do_file(argv[a]);
233 }
234 }
Damien5ac1b2e2013-10-18 19:58:12 +0100235 }
Damien George136f6752014-01-07 14:54:15 +0000236
Damien429d7192013-10-04 19:53:11 +0100237 rt_deinit();
238
239 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
240 return 0;
241}
Damien087d2182013-11-09 20:14:30 +0000242
243// for sqrt
244#include <math.h>
245machine_float_t machine_sqrt(machine_float_t x) {
246 return sqrt(x);
247}