From 26095748df3a4e46b45a06f48544f808373f9121 Mon Sep 17 00:00:00 2001 From: XOR Date: Sat, 30 Sep 2023 16:51:10 +0200 Subject: [PATCH] Add support for CRLF text files --- header/common.h | 1 + src/lexer.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/header/common.h b/header/common.h index 6a0305b..fb4576a 100755 --- a/header/common.h +++ b/header/common.h @@ -33,6 +33,7 @@ Here, common macros are specified #define ASCII_TAB 9 #define ASCII_SPACE 32 #define ASCII_NEWLINE 10 +#define ASCII_CARRIAGE_RETURN 13 #define INSTR_INP 0b0001 #define INSTR_OUT 0b0010 diff --git a/src/lexer.c b/src/lexer.c index f979418..ea4b382 100755 --- a/src/lexer.c +++ b/src/lexer.c @@ -91,6 +91,30 @@ void lexer(FILE *input_file, dynamic_buffer_t *tokens) is_pos_line_start = 1; break; + case ASCII_CARRIAGE_RETURN: + // Some operating systems use \r\n or just \r for newlines in texts. + + // This is a indice of a new token -> increase token_index + token_index++; + + // This also is a indice of a new instruction beginning + // in the tokens array, instructions are seperated by semicolons + ((char (*)[MAX_TOKEN_SIZE]) tokens->buffer)[token_index][0] = ';'; + tokens->used += MAX_TOKEN_SIZE; + + // Since the Instruction seperator (';') is also a token, we need to increase token_index again + token_index++; + + // Check for \r\n + if((char) prefgetc(input_file) == ASCII_NEWLINE) + { + // Skip over this \n + current_char = fgetc(input_file); + } + + is_pos_line_start = 1; + break; + case ';': // Loop over the comment while (prefgetc(input_file) != ASCII_NEWLINE && prefgetc(input_file) != '\0')