micropython/unix/main.c

179 wiersze
4.6 KiB
C
Czysty Zwykły widok Historia

2013-10-04 18:53:11 +00:00
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "nlr.h"
2013-10-04 18:53:11 +00:00
#include "misc.h"
#include "mpyconfig.h"
2013-10-04 18:53:11 +00:00
#include "lexer.h"
#include "lexerunix.h"
2013-10-04 18:53:11 +00:00
#include "parse.h"
#include "compile.h"
#include "runtime.h"
#include "repl.h"
2013-10-04 18:53:11 +00:00
2013-10-18 18:58:12 +00:00
#include <readline/readline.h>
2013-10-04 18:53:11 +00:00
2013-10-18 18:58:12 +00:00
char *str_join(const char *s1, int sep_char, const char *s2) {
int l1 = strlen(s1);
int l2 = strlen(s2);
char *s = m_new(char, l1 + l2 + 2);
memcpy(s, s1, l1);
if (sep_char != 0) {
s[l1] = sep_char;
l1 += 1;
}
memcpy(s + l1, s2, l2);
s[l1 + l2] = 0;
2013-10-18 18:58:12 +00:00
return s;
}
void do_repl(void) {
2013-10-18 18:58:12 +00:00
for (;;) {
char *line = readline(">>> ");
if (line == NULL) {
// EOF
return;
}
if (py_repl_is_compound_stmt(line)) {
2013-10-18 18:58:12 +00:00
for (;;) {
char *line2 = readline("... ");
if (line2 == NULL || strlen(line2) == 0) {
break;
}
char *line3 = str_join(line, '\n', line2);
m_free(line);
m_free(line2);
line = line3;
}
}
py_lexer_t *lex = py_lexer_new_from_str_len("<stdin>", line, strlen(line), false);
2013-10-18 18:58:12 +00:00
py_parse_node_t pn = py_parse(lex, PY_PARSE_SINGLE_INPUT);
py_lexer_free(lex);
2013-10-18 18:58:12 +00:00
if (pn != PY_PARSE_NODE_NULL) {
//py_parse_node_show(pn, 0);
bool comp_ok = py_compile(pn, true);
if (comp_ok) {
py_obj_t module_fun = rt_make_function_from_id(1);
if (module_fun != py_const_none) {
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
rt_call_function_0(module_fun);
nlr_pop();
} else {
// uncaught exception
py_obj_print((py_obj_t)nlr.ret_val);
printf("\n");
}
}
}
}
2013-10-04 18:53:11 +00:00
}
2013-10-18 18:58:12 +00:00
}
void do_file(const char *file) {
py_lexer_t *lex = py_lexer_new_from_file(file);
2013-10-04 18:53:11 +00:00
//const char *pysrc = "def f():\n x=x+1\n print(42)\n";
//py_lexer_t *lex = py_lexer_from_str_len("<>", pysrc, strlen(pysrc), false);
if (lex == NULL) {
2013-10-18 18:58:12 +00:00
return;
2013-10-04 18:53:11 +00:00
}
if (0) {
2013-10-18 18:58:12 +00:00
// just tokenise
2013-10-04 18:53:11 +00:00
while (!py_lexer_is_kind(lex, PY_TOKEN_END)) {
py_token_show(py_lexer_cur(lex));
py_lexer_to_next(lex);
}
2013-10-18 18:58:12 +00:00
py_lexer_free(lex);
2013-10-04 18:53:11 +00:00
} else {
2013-10-18 18:58:12 +00:00
// compile
py_parse_node_t pn = py_parse(lex, PY_PARSE_FILE_INPUT);
py_lexer_free(lex);
if (pn != PY_PARSE_NODE_NULL) {
//printf("----------------\n");
//parse_node_show(pn, 0);
//printf("----------------\n");
2013-10-18 18:58:12 +00:00
bool comp_ok = py_compile(pn, false);
//printf("----------------\n");
2013-10-04 18:53:11 +00:00
#if MICROPY_EMIT_CPYTHON
if (!comp_ok) {
printf("compile error\n");
}
#else
2013-10-18 18:58:12 +00:00
if (1 && comp_ok) {
// execute it
py_obj_t module_fun = rt_make_function_from_id(1);
if (module_fun != py_const_none) {
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
py_obj_t ret = rt_call_function_0(module_fun);
printf("done! got: ");
py_obj_print(ret);
printf("\n");
nlr_pop();
} else {
// uncaught exception
printf("exception: ");
py_obj_print((py_obj_t)nlr.ret_val);
printf("\n");
}
}
}
2013-10-18 18:58:12 +00:00
#endif
2013-10-04 18:53:11 +00:00
}
}
2013-10-18 18:58:12 +00:00
}
2013-10-04 18:53:11 +00:00
2013-11-02 23:58:38 +00:00
void test_print(py_obj_t o_in) {
printf("<test>");
}
py_obj_t test_get(py_obj_t o_in) {
py_obj_t d1;
py_obj_t d2;
py_user_get_data(o_in, &d1, &d2);
return d1;
}
py_obj_t test_set(py_obj_t o_in, py_obj_t arg) {
py_user_set_data(o_in, (machine_uint_t)arg, (machine_uint_t)arg);
return py_const_none;
}
const py_user_info_t test_obj_info = {
"Test",
test_print,
{
{ "get", 0, test_get },
{ "set", 1, test_set },
{ NULL, 0, NULL },
}
};
2013-10-18 18:58:12 +00:00
int main(int argc, char **argv) {
qstr_init();
rt_init();
2013-11-02 23:58:38 +00:00
rt_store_name(qstr_from_str_static("test"), py_obj_new_user(&test_obj_info, py_obj_new_int(42), 0));
2013-10-18 18:58:12 +00:00
if (argc == 1) {
do_repl();
} else if (argc == 2) {
do_file(argv[1]);
} else {
printf("usage: py [<file>]\n");
return 1;
}
2013-10-04 18:53:11 +00:00
rt_deinit();
//printf("total bytes = %d\n", m_get_total_bytes_allocated());
return 0;
}