From 63a2c5144dbd851168e484ef76fb9c332e6756e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Czajka?= <62751+lukaszcz@users.noreply.github.com> Date: Tue, 4 Apr 2023 11:29:02 +0200 Subject: [PATCH] Print quoted strings in the runtime (#1969) * Closes #1968 --- runtime/src/juvix/io.c | 27 +++++++++++++++------- test/Compilation/Positive.hs | 7 +++++- tests/Compilation/positive/out/test048.out | 1 + tests/Compilation/positive/test048.juvix | 7 ++++++ 4 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 tests/Compilation/positive/out/test048.out create mode 100644 tests/Compilation/positive/test048.juvix diff --git a/runtime/src/juvix/io.c b/runtime/src/juvix/io.c index 5108bccbe8..e7dd0e7900 100644 --- a/runtime/src/juvix/io.c +++ b/runtime/src/juvix/io.c @@ -32,6 +32,18 @@ void io_flush() { } } +static void io_print(word_t x) { + if (io_buffer == NULL) { + io_init(); + } + io_index += print_to_buf(io_buffer + io_index, PAGE_SIZE - io_index, x); + if (io_index >= PAGE_SIZE) { + io_buffer[PAGE_SIZE - 1] = 0; + print_msg(io_buffer); + error_exit_msg("error: IO buffer overflow"); + } +} + static void io_write(word_t x) { if (io_buffer == NULL) { io_init(); @@ -53,12 +65,7 @@ static void io_write(word_t x) { } } } else { - io_index += print_to_buf(io_buffer + io_index, PAGE_SIZE - io_index, x); - if (io_index >= PAGE_SIZE) { - io_buffer[PAGE_SIZE - 1] = 0; - print_msg(io_buffer); - error_exit_msg("error: IO buffer overflow"); - } + io_print(x); } } @@ -112,13 +119,17 @@ void io_trace(word_t x) { if (x == OBJ_VOID) { print_msg("void"); } else { - io_print_toplevel(x); + io_write(x); + ASSERT(io_index < PAGE_SIZE); + io_buffer[io_index] = 0; + print_msg(io_buffer); + io_index = 0; } } void io_print_toplevel(word_t x) { if (x != OBJ_VOID) { - io_write(x); + io_print(x); ASSERT(io_index < PAGE_SIZE); io_buffer[io_index] = 0; print_msg(io_buffer); diff --git a/test/Compilation/Positive.hs b/test/Compilation/Positive.hs index 1f3d132ec8..1398e59047 100644 --- a/test/Compilation/Positive.hs +++ b/test/Compilation/Positive.hs @@ -294,5 +294,10 @@ tests = "Test047: Local Modules" $(mkRelDir ".") $(mkRelFile "test047.juvix") - $(mkRelFile "out/test047.out") + $(mkRelFile "out/test047.out"), + posTest + "Test048: String quoting" + $(mkRelDir ".") + $(mkRelFile "test048.juvix") + $(mkRelFile "out/test048.out") ] diff --git a/tests/Compilation/positive/out/test048.out b/tests/Compilation/positive/out/test048.out new file mode 100644 index 0000000000..90f92be791 --- /dev/null +++ b/tests/Compilation/positive/out/test048.out @@ -0,0 +1 @@ +"hello!\n\b\t\r" diff --git a/tests/Compilation/positive/test048.juvix b/tests/Compilation/positive/test048.juvix new file mode 100644 index 0000000000..93282e5661 --- /dev/null +++ b/tests/Compilation/positive/test048.juvix @@ -0,0 +1,7 @@ +-- String quoting +module test048; + +open import Stdlib.Prelude; + +main : String; +main := "hello!\n\b\t\r";