From ed489e20708528e084d189126f443902f613a4f3 Mon Sep 17 00:00:00 2001 From: lyon1998 Date: Thu, 24 Mar 2022 13:55:02 +0800 Subject: [PATCH] fix '\r' err in pikaRunAsm --- port/linux/test/parse-test.cpp | 28 ++++++++++++++++++++++++++++ src/PikaParser.c | 19 +++++++++++-------- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/port/linux/test/parse-test.cpp b/port/linux/test/parse-test.cpp index 3f65026b0..30e38500a 100644 --- a/port/linux/test/parse-test.cpp +++ b/port/linux/test/parse-test.cpp @@ -2432,3 +2432,31 @@ TEST(asmer, asm_to_bytecodeArray) { byteCodeFrame_deinit(&bytecode_frame); EXPECT_EQ(pikaMemNow(), 0); } + +TEST(asmer, asm_to_bytecode_0x0d) { + char* pikaAsm =(char*) + "B0\n" + "0 RUN __init__\n"; + char* pikaAsm1 =(char*) + "B0\r\n" + "0 RUN __init__\r\n"; + + ByteCodeFrame bytecode_frame; + byteCodeFrame_init(&bytecode_frame); + byteCodeFrame_appendFromAsm(&bytecode_frame, pikaAsm); + byteCodeFrame_printAsArray(&bytecode_frame); + + ByteCodeFrame bytecode_frame1; + byteCodeFrame_init(&bytecode_frame1); + byteCodeFrame_appendFromAsm(&bytecode_frame1, pikaAsm1); + byteCodeFrame_printAsArray(&bytecode_frame1); + + EXPECT_EQ(bytecode_frame1.const_pool.size, bytecode_frame.const_pool.size); + EXPECT_EQ(bytecode_frame1.instruct_array.size, + bytecode_frame.instruct_array.size); + + /* deinit */ + byteCodeFrame_deinit(&bytecode_frame1); + byteCodeFrame_deinit(&bytecode_frame); + EXPECT_EQ(pikaMemNow(), 0); +} diff --git a/src/PikaParser.c b/src/PikaParser.c index 709e08f3a..1ac972ff0 100644 --- a/src/PikaParser.c +++ b/src/PikaParser.c @@ -1517,14 +1517,18 @@ ByteCodeFrame* byteCodeFrame_appendFromAsm(ByteCodeFrame* self, char* pikaAsm) { .is_new_line = 0, .line_pointer = pikaAsm, }; - uint16_t const_pool_offset; - char* data; - uint16_t exist_offset; - + uint16_t const_pool_offset; + char* data; + uint16_t exist_offset; + char line_buff[PIKA_CONFIG_PATH_BUFF_SIZE] = {0}; for (int i = 0; i < strCountSign(pikaAsm, '\n'); i++) { char* line = strGetLine(line_buff, asmer.line_pointer); - InstructUnit ins_unit = {0}; + InstructUnit ins_unit = {0}; + /* remove '\r' */ + if (line[strGetSize(line) - 1] == '\r') { + line[strGetSize(line) - 1] = 0; + } /* process block deepth flag*/ if ('B' == line[0]) { asmer.block_deepth_now = line[1] - '0'; @@ -1538,8 +1542,7 @@ ByteCodeFrame* byteCodeFrame_appendFromAsm(ByteCodeFrame* self, char* pikaAsm) { const_pool_offset = 0; data = line + 6; - exist_offset = - constPool_getOffsetByData(&(self->const_pool), data); + exist_offset = constPool_getOffsetByData(&(self->const_pool), data); /* get const offset */ if (strEqu(data, "")) { @@ -1611,7 +1614,7 @@ static void __handler_instructArray_output_file(InstructArray* self, __platform_fwrite(ins_unit, 1, instructUnit_getSize(), self->output_f); } -/* +/* need implament : __platform_fopen() __platform_fwrite()