summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-06-22 22:38:47 +0100
committerDamien George <damien.p.george@gmail.com>2015-06-22 22:38:47 +0100
commitc4b592d3793bae6effe13a9b606083c9097d2779 (patch)
treefef35f95a0641015c14be2c53c1d42147217ebc1
parentfe99ea9aabb1916581441b850fee6e0888bf1af4 (diff)
downloadmicropython-c4b592d3793bae6effe13a9b606083c9097d2779.tar.gz
micropython-c4b592d3793bae6effe13a9b606083c9097d2779.zip
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).
-rw-r--r--bare-arm/main.c7
-rw-r--r--minimal/main.c7
-rw-r--r--qemu-arm/main.c6
3 files changed, 11 insertions, 9 deletions
diff --git a/bare-arm/main.c b/bare-arm/main.c
index ca32dc459d..6f5707afea 100644
--- a/bare-arm/main.c
+++ b/bare-arm/main.c
@@ -7,7 +7,7 @@
#include "py/runtime.h"
#include "py/repl.h"
-void do_str(const char *src) {
+void do_str(const char *src, mp_parse_input_kind_t input_kind) {
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
if (lex == NULL) {
return;
@@ -16,7 +16,7 @@ void do_str(const char *src) {
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
qstr source_name = lex->source_name;
- mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT);
+ mp_parse_node_t pn = mp_parse(lex, input_kind);
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true);
mp_call_function_0(module_fun);
nlr_pop();
@@ -28,7 +28,8 @@ void do_str(const char *src) {
int main(int argc, char **argv) {
mp_init();
- do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\n')");
+ do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\\n')", MP_PARSE_SINGLE_INPUT);
+ do_str("for i in range(10):\n print(i)", MP_PARSE_FILE_INPUT);
mp_deinit();
return 0;
}
diff --git a/minimal/main.c b/minimal/main.c
index f6041267a2..11f1d3ad6b 100644
--- a/minimal/main.c
+++ b/minimal/main.c
@@ -9,7 +9,7 @@
#include "py/gc.h"
#include "pyexec.h"
-void do_str(const char *src) {
+void do_str(const char *src, mp_parse_input_kind_t input_kind) {
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
if (lex == NULL) {
printf("MemoryError: lexer could not allocate memory\n");
@@ -19,7 +19,7 @@ void do_str(const char *src) {
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
qstr source_name = lex->source_name;
- mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT);
+ mp_parse_node_t pn = mp_parse(lex, input_kind);
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true);
mp_call_function_0(module_fun);
nlr_pop();
@@ -51,7 +51,8 @@ int main(int argc, char **argv) {
#else
pyexec_friendly_repl();
#endif
- //do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\\n')");
+ //do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\\n')", MP_PARSE_SINGLE_INPUT);
+ //do_str("for i in range(10):\r\n print(i)", MP_PARSE_FILE_INPUT);
mp_deinit();
return 0;
}
diff --git a/qemu-arm/main.c b/qemu-arm/main.c
index 0507744126..a629ceafb5 100644
--- a/qemu-arm/main.c
+++ b/qemu-arm/main.c
@@ -12,7 +12,7 @@
#include "py/gc.h"
#include "py/repl.h"
-void do_str(const char *src) {
+void do_str(const char *src, mp_parse_input_kind_t input_kind) {
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
if (lex == NULL) {
return;
@@ -21,7 +21,7 @@ void do_str(const char *src) {
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
qstr source_name = lex->source_name;
- mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT);
+ mp_parse_node_t pn = mp_parse(lex, input_kind);
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true);
mp_call_function_0(module_fun);
nlr_pop();
@@ -36,7 +36,7 @@ int main(int argc, char **argv) {
void *heap = malloc(16 * 1024);
gc_init(heap, (char*)heap + 16 * 1024);
mp_init();
- do_str("print('hello world!')");
+ do_str("print('hello world!')", MP_PARSE_SINGLE_INPUT);
mp_deinit();
return 0;
}