Skip to content

Commit c4b592d

Browse files
committed
bare-arm, minimal, qemu-arm: Make do_str() take parse-input-kind as arg.
The do_str() function is provided essentially as documentation to show how to compile and execute a string. This patch makes do_str take an extra arg to specify how the string should be interpreted: either as a single line (ie from a REPL) or as multiple lines (ie from a file).
1 parent fe99ea9 commit c4b592d

3 files changed

Lines changed: 11 additions & 9 deletions

File tree

bare-arm/main.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "py/runtime.h"
88
#include "py/repl.h"
99

10-
void do_str(const char *src) {
10+
void do_str(const char *src, mp_parse_input_kind_t input_kind) {
1111
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
1212
if (lex == NULL) {
1313
return;
@@ -16,7 +16,7 @@ void do_str(const char *src) {
1616
nlr_buf_t nlr;
1717
if (nlr_push(&nlr) == 0) {
1818
qstr source_name = lex->source_name;
19-
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT);
19+
mp_parse_node_t pn = mp_parse(lex, input_kind);
2020
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true);
2121
mp_call_function_0(module_fun);
2222
nlr_pop();
@@ -28,7 +28,8 @@ void do_str(const char *src) {
2828

2929
int main(int argc, char **argv) {
3030
mp_init();
31-
do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\n')");
31+
do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\\n')", MP_PARSE_SINGLE_INPUT);
32+
do_str("for i in range(10):\n print(i)", MP_PARSE_FILE_INPUT);
3233
mp_deinit();
3334
return 0;
3435
}

minimal/main.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "py/gc.h"
1010
#include "pyexec.h"
1111

12-
void do_str(const char *src) {
12+
void do_str(const char *src, mp_parse_input_kind_t input_kind) {
1313
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
1414
if (lex == NULL) {
1515
printf("MemoryError: lexer could not allocate memory\n");
@@ -19,7 +19,7 @@ void do_str(const char *src) {
1919
nlr_buf_t nlr;
2020
if (nlr_push(&nlr) == 0) {
2121
qstr source_name = lex->source_name;
22-
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT);
22+
mp_parse_node_t pn = mp_parse(lex, input_kind);
2323
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true);
2424
mp_call_function_0(module_fun);
2525
nlr_pop();
@@ -51,7 +51,8 @@ int main(int argc, char **argv) {
5151
#else
5252
pyexec_friendly_repl();
5353
#endif
54-
//do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\\n')");
54+
//do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\\n')", MP_PARSE_SINGLE_INPUT);
55+
//do_str("for i in range(10):\r\n print(i)", MP_PARSE_FILE_INPUT);
5556
mp_deinit();
5657
return 0;
5758
}

qemu-arm/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "py/gc.h"
1313
#include "py/repl.h"
1414

15-
void do_str(const char *src) {
15+
void do_str(const char *src, mp_parse_input_kind_t input_kind) {
1616
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
1717
if (lex == NULL) {
1818
return;
@@ -21,7 +21,7 @@ void do_str(const char *src) {
2121
nlr_buf_t nlr;
2222
if (nlr_push(&nlr) == 0) {
2323
qstr source_name = lex->source_name;
24-
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT);
24+
mp_parse_node_t pn = mp_parse(lex, input_kind);
2525
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true);
2626
mp_call_function_0(module_fun);
2727
nlr_pop();
@@ -36,7 +36,7 @@ int main(int argc, char **argv) {
3636
void *heap = malloc(16 * 1024);
3737
gc_init(heap, (char*)heap + 16 * 1024);
3838
mp_init();
39-
do_str("print('hello world!')");
39+
do_str("print('hello world!')", MP_PARSE_SINGLE_INPUT);
4040
mp_deinit();
4141
return 0;
4242
}

0 commit comments

Comments
 (0)