|
| 1 | +#include <stdint.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +#include "py/nlr.h" |
| 6 | +#include "py/parsehelper.h" |
| 7 | +#include "py/compile.h" |
| 8 | +#include "py/runtime.h" |
| 9 | +#include "py/repl.h" |
| 10 | +#include "py/pfenv.h" |
| 11 | + |
| 12 | +void do_str(const char *src) { |
| 13 | + mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0); |
| 14 | + if (lex == NULL) { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + mp_parse_error_kind_t parse_error_kind; |
| 19 | + mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT, &parse_error_kind); |
| 20 | + |
| 21 | + if (pn == MP_PARSE_NODE_NULL) { |
| 22 | + // parse error |
| 23 | + mp_parse_show_exception(lex, parse_error_kind); |
| 24 | + mp_lexer_free(lex); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + // parse okay |
| 29 | + qstr source_name = lex->source_name; |
| 30 | + mp_lexer_free(lex); |
| 31 | + mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true); |
| 32 | + |
| 33 | + if (mp_obj_is_exception_instance(module_fun)) { |
| 34 | + // compile error |
| 35 | + mp_obj_print_exception(printf_wrapper, NULL, module_fun); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + nlr_buf_t nlr; |
| 40 | + if (nlr_push(&nlr) == 0) { |
| 41 | + mp_call_function_0(module_fun); |
| 42 | + nlr_pop(); |
| 43 | + } else { |
| 44 | + // uncaught exception |
| 45 | + mp_obj_print_exception(printf_wrapper, NULL, (mp_obj_t)nlr.ret_val); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +int main(int argc, char **argv) { |
| 50 | + mp_init(); |
| 51 | + do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\n')"); |
| 52 | + mp_deinit(); |
| 53 | + return 0; |
| 54 | +} |
| 55 | + |
| 56 | +void gc_collect(void) { |
| 57 | +} |
| 58 | + |
| 59 | +mp_lexer_t *mp_lexer_new_from_file(const char *filename) { |
| 60 | + return NULL; |
| 61 | +} |
| 62 | + |
| 63 | +mp_import_stat_t mp_import_stat(const char *path) { |
| 64 | + return MP_IMPORT_STAT_NO_EXIST; |
| 65 | +} |
| 66 | + |
| 67 | +mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) { |
| 68 | + return mp_const_none; |
| 69 | +} |
| 70 | +MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open); |
| 71 | + |
| 72 | +void nlr_jump_fail(void *val) { |
| 73 | +} |
| 74 | + |
| 75 | +void NORETURN __fatal_error(const char *msg) { |
| 76 | + while (1); |
| 77 | +} |
| 78 | + |
| 79 | +#ifndef NDEBUG |
| 80 | +void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) { |
| 81 | + printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line); |
| 82 | + __fatal_error("Assertion failed"); |
| 83 | +} |
| 84 | +#endif |
| 85 | + |
| 86 | +/* |
| 87 | +int _lseek() {return 0;} |
| 88 | +int _read() {return 0;} |
| 89 | +int _write() {return 0;} |
| 90 | +int _close() {return 0;} |
| 91 | +void _exit(int x) {for(;;){}} |
| 92 | +int _sbrk() {return 0;} |
| 93 | +int _kill() {return 0;} |
| 94 | +int _getpid() {return 0;} |
| 95 | +int _fstat() {return 0;} |
| 96 | +int _isatty() {return 0;} |
| 97 | +*/ |
| 98 | + |
| 99 | +void *malloc(size_t n) {return NULL;} |
| 100 | +void *calloc(size_t nmemb, size_t size) {return NULL;} |
| 101 | +void *realloc(void *ptr, size_t size) {return NULL;} |
| 102 | +void free(void *p) {} |
| 103 | +int printf(const char *m, ...) {return 0;} |
| 104 | +void *memcpy(void *dest, const void *src, size_t n) {return NULL;} |
| 105 | +int memcmp(const void *s1, const void *s2, size_t n) {return 0;} |
| 106 | +void *memmove(void *dest, const void *src, size_t n) {return NULL;} |
| 107 | +void *memset(void *s, int c, size_t n) {return NULL;} |
| 108 | +int strcmp(const char *s1, const char* s2) {return 0;} |
| 109 | +int strncmp(const char *s1, const char* s2, size_t n) {return 0;} |
| 110 | +size_t strlen(const char *s) {return 0;} |
| 111 | +char *strcat(char *dest, const char *src) {return NULL;} |
| 112 | +char *strchr(const char *dest, int c) {return NULL;} |
| 113 | +#include <stdarg.h> |
| 114 | +int vprintf(const char *format, va_list ap) {return 0;} |
| 115 | +int vsnprintf(char *str, size_t size, const char *format, va_list ap) {return 0;} |
| 116 | + |
| 117 | +#undef putchar |
| 118 | +int putchar(int c) {return 0;} |
| 119 | +int puts(const char *s) {return 0;} |
| 120 | + |
| 121 | +void _start(void) {main(0, NULL);} |
0 commit comments