diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-11 14:28:52 +0530 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-11 14:28:52 +0530 |
commit | 62c6b6a9a90119e6750be2bdd4f546c26f6d7b84 (patch) | |
tree | 1e23257ae4d11b1765a2e56b2a0505a5861c199c | |
parent | b272374fdde09d4691d3b3090e10266f5b64e64e (diff) |
refactor members to have a m_ prefix
-rw-r--r-- | include/filehandler.h | 6 | ||||
-rw-r--r-- | include/lexer.h | 7 | ||||
-rw-r--r-- | include/list.h | 8 | ||||
-rw-r--r-- | include/token.h | 9 | ||||
-rw-r--r-- | src/filehandler.cc | 32 | ||||
-rw-r--r-- | src/lexer.cc | 20 | ||||
-rw-r--r-- | src/list.cc | 28 | ||||
-rw-r--r-- | src/token.cc | 20 |
8 files changed, 69 insertions, 61 deletions
diff --git a/include/filehandler.h b/include/filehandler.h index 6bb41f5..859f0c3 100644 --- a/include/filehandler.h +++ b/include/filehandler.h @@ -7,9 +7,9 @@ class Filehandler { private: - FILE *f; - char *path; - char *buffer; + FILE *m_file; + char *m_path; + char *m_buffer; private: unsigned int size(void); diff --git a/include/lexer.h b/include/lexer.h index e912747..4c0e606 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -7,12 +7,15 @@ class Lexer { private: - char *script; - List *tokens; + char *m_script; + List *m_tokens; public: void init(char *script); + void scan_tokens(void); + List *tokens(void); + void clean(void); }; diff --git a/include/list.h b/include/list.h index 3efdbae..fcade38 100644 --- a/include/list.h +++ b/include/list.h @@ -10,10 +10,10 @@ class List { private: - size_t element_size; - size_t max; - size_t current; - uint8_t *elements; + size_t m_element_size; + size_t m_max; + size_t m_current; + uint8_t *m_elements; public: void init(size_t element_size); diff --git a/include/token.h b/include/token.h index 165f100..9041a2e 100644 --- a/include/token.h +++ b/include/token.h @@ -55,15 +55,14 @@ enum TokenType { class Token { private: - TokenType type; - char *lexeme; + TokenType m_type; + char *m_lexeme; // literal - unsigned int line; - char *string; + unsigned int m_line; + char *m_string; public: void init(TokenType type, char *lexeme, unsigned int line); - char *to_string(void); void clean(void); }; diff --git a/src/filehandler.cc b/src/filehandler.cc index 4856702..915657e 100644 --- a/src/filehandler.cc +++ b/src/filehandler.cc @@ -5,17 +5,17 @@ void Filehandler::init(char *path) { - f = NULL; - buffer = NULL; - this->path = path; + m_file = NULL; + m_buffer = NULL; + m_path = path; } bool Filehandler::open(void) { - f = fopen(path, "r"); - if (f == NULL) { - printf("Unable to open %s\n", path); + m_file = fopen(m_path, "r"); + if (m_file == NULL) { + printf("Unable to open %s\n", m_path); return false; } @@ -25,32 +25,32 @@ Filehandler::open(void) unsigned int Filehandler::size(void) { - unsigned int current = ftell(f); + unsigned int current = ftell(m_file); - fseek(f, 0, SEEK_END); - unsigned int s = ftell(f); + fseek(m_file, 0, SEEK_END); + unsigned int s = ftell(m_file); - fseek(f, current, SEEK_SET); + fseek(m_file, current, SEEK_SET); return s; } char * Filehandler::read(void) { - fseek(f, 0, SEEK_SET); + fseek(m_file, 0, SEEK_SET); - buffer = (char *) calloc(size(), sizeof(char)); - int bytesread = fread(buffer, sizeof(char), size(), f); + m_buffer = (char *) calloc(size(), sizeof(char)); + int bytesread = fread(m_buffer, sizeof(char), size(), m_file); if (bytesread < 0) { return NULL; } - return buffer; + return m_buffer; } void Filehandler::close(void) { - fclose(f); - free(buffer); + fclose(m_file); + free(m_buffer); } diff --git a/src/lexer.cc b/src/lexer.cc index 1ae7e9d..ee0e4a3 100644 --- a/src/lexer.cc +++ b/src/lexer.cc @@ -8,22 +8,28 @@ void Lexer::init(char *script) { - this->script = (char *) malloc(strlen(script) + 1); - strcpy(this->script, script); + m_script = (char *) malloc(strlen(script) + 1); + strcpy(m_script, script); - tokens = (List *) calloc(1, sizeof(List)); - tokens->init(sizeof(Token)); + m_tokens = (List *) calloc(1, sizeof(List)); + m_tokens->init(sizeof(Token)); } void Lexer::scan_tokens(void) { - printf("%s\n", script); + printf("%s\n", m_script); +} + +List * +Lexer::tokens(void) +{ + return m_tokens; } void Lexer::clean(void) { - tokens->clean(); - free(script); + m_tokens->clean(); + free(m_script); } diff --git a/src/list.cc b/src/list.cc index 6ada790..6104ec7 100644 --- a/src/list.cc +++ b/src/list.cc @@ -8,48 +8,48 @@ void List::init(size_t element_size) { - this->element_size = element_size; - current = 0; - max = START_SIZE; - elements = (uint8_t *) calloc(max, element_size); + m_element_size = element_size; + m_current = 0; + m_max = START_SIZE; + m_elements = (uint8_t *) calloc(m_max, element_size); } void List::add(void *element) { - if (current == max) { - max += INCREMENT_BY; - elements = (uint8_t *) realloc(elements, element_size * max); - if (elements == NULL) { + if (m_current == m_max) { + m_max += INCREMENT_BY; + m_elements = (uint8_t *) realloc(m_elements, m_element_size * m_max); + if (m_elements == NULL) { /* TODO: Handle error */ printf("Failed to reallocate array\n"); return; } } - void *new_element = elements + element_size * current; - new_element = memcpy(new_element, element, element_size); + void *new_element = m_elements + m_element_size * m_current; + new_element = memcpy(new_element, element, m_element_size); if (new_element == NULL) printf("Failed to add a new element\n"); - current++; + m_current++; } void * List::get(size_t i) { - return elements + (i * element_size); + return m_elements + (i * m_element_size); } size_t List::size(void) { - return current; + return m_current; } void List::clean(void) { - free(elements); + free(m_elements); } diff --git a/src/token.cc b/src/token.cc index ad9dd92..6045f30 100644 --- a/src/token.cc +++ b/src/token.cc @@ -6,22 +6,22 @@ void Token::init(TokenType type, char *lexeme, unsigned int line) { - this->type = type; - this->lexeme = lexeme; - this->line = line; - this->string = NULL; + m_type = type; + m_lexeme = lexeme; + m_line = line; + m_string = NULL; } char * Token::to_string(void) { - unsigned int line_length = snprintf(NULL, 0, "%ul", line) - 1; + unsigned int line_length = snprintf(NULL, 0, "%ul", m_line) - 1; /* 2: ": ", 1: "\0" */ - unsigned int final_size = strlen(lexeme) + line_length + 2 + 1; + unsigned int final_size = strlen(m_lexeme) + line_length + 2 + 1; char *result = (char *) calloc(1, final_size); - snprintf(result, final_size, "%d: %s", line, lexeme); - string = result; + snprintf(result, final_size, "%d: %s", m_line, m_lexeme); + m_string = result; return result; } @@ -29,6 +29,6 @@ Token::to_string(void) void Token::clean(void) { - if (string != NULL) - free(string); + if (m_string != NULL) + free(m_string); } |