From 9d08afd1a174fafabc765c0e9c87b5436813654f Mon Sep 17 00:00:00 2001 From: overflowerror Date: Sat, 18 May 2024 19:18:15 +0200 Subject: [PATCH] fix: support for carriage return --- compiler/src/lexer.l | 4 +++- compiler/test/cases/001b-bug-carriage-return-in-char.in | 1 + compiler/test/cases/001b-bug-carriage-return-in-char.out | 1 + compiler/test/cases/002b-bug-carriage-return-in-str.in | 1 + compiler/test/cases/002b-bug-carriage-return-in-str.out | 1 + 5 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 compiler/test/cases/001b-bug-carriage-return-in-char.in create mode 100644 compiler/test/cases/001b-bug-carriage-return-in-char.out create mode 100644 compiler/test/cases/002b-bug-carriage-return-in-str.in create mode 100644 compiler/test/cases/002b-bug-carriage-return-in-str.out diff --git a/compiler/src/lexer.l b/compiler/src/lexer.l index 58b1924..de27ac9 100644 --- a/compiler/src/lexer.l +++ b/compiler/src/lexer.l @@ -13,7 +13,7 @@ map "map" rmap "rmap" in "in" -char "'"([^\\]|[\\]n|[\\]t|[\\]{2})"'" +char "'"([^\\]|[\\]n|[\\]t|[\\]r|[\\]{2})"'" id [a-zA-Z_][a-zA-Z0-9_]* include "#include" @@ -58,6 +58,7 @@ char char_escape(const char* yytext) { if (yytext[2] == '\\') return '\\'; else if (yytext[2] == 'n') return '\n'; else if (yytext[2] == 't') return '\t'; + else if (yytext[2] == 'r') return '\r'; else lex_panic("unknown escape sequence: %s\n", yytext); } else { return yytext[1]; @@ -118,6 +119,7 @@ int nested_braces = 0; [^\\"\n]* { strbuf_append(strbuf, yytext); } \\n { strbuf_append_c(strbuf, '\n'); } \\t { strbuf_append_c(strbuf, '\t'); } +\\r { strbuf_append_c(strbuf, '\r'); } \\[0-7]* { strbuf_append_c(strbuf, strtol(yytext+1, 0, 8)); } \\[\\"] { strbuf_append_c(strbuf, yytext[1]); } \" { yylval.str = strdup(strbuf); BEGIN INITIAL; return STR; } diff --git a/compiler/test/cases/001b-bug-carriage-return-in-char.in b/compiler/test/cases/001b-bug-carriage-return-in-char.in new file mode 100644 index 0000000..e85b83b --- /dev/null +++ b/compiler/test/cases/001b-bug-carriage-return-in-char.in @@ -0,0 +1 @@ +print('a', '\r', 'b', '\n'); \ No newline at end of file diff --git a/compiler/test/cases/001b-bug-carriage-return-in-char.out b/compiler/test/cases/001b-bug-carriage-return-in-char.out new file mode 100644 index 0000000..4c1a1f3 --- /dev/null +++ b/compiler/test/cases/001b-bug-carriage-return-in-char.out @@ -0,0 +1 @@ +[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++<<<.>.>.>. diff --git a/compiler/test/cases/002b-bug-carriage-return-in-str.in b/compiler/test/cases/002b-bug-carriage-return-in-str.in new file mode 100644 index 0000000..7db131a --- /dev/null +++ b/compiler/test/cases/002b-bug-carriage-return-in-str.in @@ -0,0 +1 @@ +print("a\rb\n"); \ No newline at end of file diff --git a/compiler/test/cases/002b-bug-carriage-return-in-str.out b/compiler/test/cases/002b-bug-carriage-return-in-str.out new file mode 100644 index 0000000..4c1a1f3 --- /dev/null +++ b/compiler/test/cases/002b-bug-carriage-return-in-str.out @@ -0,0 +1 @@ +[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++<<<.>.>.>.