From d94294846a39c3a75a0dfed051df10863a258d9b Mon Sep 17 00:00:00 2001 From: trillian Date: Tue, 19 Mar 2024 04:29:12 +0200 Subject: [PATCH] rework multiline comments --- docs/newbook/src/formatting.md | 2 +- src/asar/main.cpp | 76 +++++++++++++++++++--------------- tests/comments.asm | 5 +++ 3 files changed, 48 insertions(+), 35 deletions(-) diff --git a/docs/newbook/src/formatting.md b/docs/newbook/src/formatting.md index 12f62a24..afc02425 100644 --- a/docs/newbook/src/formatting.md +++ b/docs/newbook/src/formatting.md @@ -17,7 +17,7 @@ There are also multiline comments, which are started with `;[[` and ended with ` ```asar lda $00 ;[[ this is a comment still a comment -more comment ]] lda $01 +more comment ]] : lda $01 ``` ## Brackets diff --git a/src/asar/main.cpp b/src/asar/main.cpp index de3dd1c0..933e9cdc 100644 --- a/src/asar/main.cpp +++ b/src/asar/main.cpp @@ -824,49 +824,57 @@ void assemblefile(const char * filename) sourcefile& newfile = filecontents.create(absolutepath); newfile.contents =split(temp, '\n'); newfile.data = temp; - bool in_block_comment = false; - int block_comment_start = -1; - string block_comment_start_line; for (int i=0;newfile.contents[i];i++) { newfile.numlines++; - char * line= newfile.contents[i]; - if(in_block_comment) { - char * end = strstr(line, "]]"); - if(!end) { - *line = 0; - continue; - } - line = end+2; - in_block_comment = false; - } -redo_line: - char * comment = strqchr(line, ';'); - if(comment) { + char * line = newfile.contents[i]; + int i_temp = i; + char * comment; + while((comment = strqchr(line, ';'))) { if(comment[1] == '[' && comment[2] == '[') { - in_block_comment = true; - block_comment_start = i; - block_comment_start_line = line; - // this is a little messy because a multiline comment could - // end right on the line where it started. so if we find - // the end, cut the comment out of the line and recheck for - // more comments. - char * end = strstr(line, "]]"); - if(end) { - memmove(comment, end+2, strlen(end+2)+1); - in_block_comment = false; - goto redo_line; + // block comment - find where it ends + char* theline = comment + 3; + char* comment_end = strstr(theline, "]]"); + while(comment_end == nullptr) { + i_temp++; + char* new_line = newfile.contents[i_temp]; + if(new_line == nullptr) { + callstack_push cs_push(callstack_entry_type::LINE, line, i); + asar_throw_error(0, error_type_null, error_id_unclosed_block_comment); + // make sure this line is still parsed correctly + *comment = 0; + // but don't go looking at any other lines + goto break_outer; + } + comment_end = strstr(new_line, "]]"); + // this line is itself part of the comment, so ignore it + //new_line[0] = 0; + // except not like that^, because that will break the + // memmove below + static char junk[]=""; + // using a static here should be fine, since if the line + // doesn't contain ',' or '\' we won't go mutating it + newfile.contents[i_temp] = junk; } + // comment_end+2 is a valid pointer, since comment_end is + // guaranteed to start with ]] + comment_end += 2; + // stitch together the part of the line before the comment, + // and the part of the line after it + memmove(comment, comment_end, strlen(comment_end) + 1); + // and then recheck for ; in the line again... + } else { + *comment = 0; } - *comment = 0; } - if (!confirmquotes(line)) { callstack_push cs_push(callstack_entry_type::LINE, line, i); asar_throw_error(0, error_type_null, error_id_mismatched_quotes); line[0] = '\0'; } + break_outer: + if (!confirmquotes(line)) { + callstack_push cs_push(callstack_entry_type::LINE, line, i); + asar_throw_error(0, error_type_null, error_id_mismatched_quotes); + line[0] = '\0'; + } newfile.contents[i] = strip_whitespace(line); } - if(in_block_comment) { - callstack_push cs_push(callstack_entry_type::LINE, block_comment_start_line, block_comment_start); - asar_throw_error(0, error_type_null, error_id_unclosed_block_comment); - } for(int i=0;newfile.contents[i];i++) { char* line = newfile.contents[i]; diff --git a/tests/comments.asm b/tests/comments.asm index e9d330bd..5039c82b 100644 --- a/tests/comments.asm +++ b/tests/comments.asm @@ -1,6 +1,7 @@ ;P>this is not a comment; h ;P>back to asm ;P>no comment +;P>line joining ;`errEunclosed_block_comment ; hello! @@ -14,5 +15,9 @@ m u l t i l i n e ;[[ one line ]] print "no ", ;[[ test ]] "comment" +pri;[[ test +words ]]nt "line ", ;[[ more +shit ]] "joining" + ;[[ woah im gonna escape!!