diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-11 13:37:06 +0530 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-11 13:37:06 +0530 |
commit | 2a188a5b1789c87c39c876b448953cb88afed132 (patch) | |
tree | f33b5e365c30d034ba9efd37bc05cf2bee2bec03 | |
parent | 5f7deaa1944016f56d2ac3cba08ca702033dbe7e (diff) |
move from using constructors to init()
-rw-r--r-- | include/filehandler.h | 2 | ||||
-rw-r--r-- | include/lexer.h | 10 | ||||
-rw-r--r-- | include/list.h | 2 | ||||
-rw-r--r-- | include/token.h | 2 | ||||
-rw-r--r-- | src/filehandler.cc | 3 | ||||
-rw-r--r-- | src/lexer.cc | 6 | ||||
-rw-r--r-- | src/list.cc | 5 | ||||
-rw-r--r-- | src/main.cc | 11 | ||||
-rw-r--r-- | src/token.cc | 3 |
9 files changed, 29 insertions, 15 deletions
diff --git a/include/filehandler.h b/include/filehandler.h index ac8fa88..6bb41f5 100644 --- a/include/filehandler.h +++ b/include/filehandler.h @@ -15,7 +15,7 @@ private: unsigned int size(void); public: - Filehandler(char *path); + void init(char *path); bool open(void); char *read(void); void close(void); diff --git a/include/lexer.h b/include/lexer.h index 19b8a97..16ef0c0 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -1,13 +1,17 @@ -#ifndef __SCANNER_H -#define __SCANNER_H +#ifndef __LEXER_H +#define __LEXER_H + +#include <list.h> +#include <token.h> class Lexer { private: char *script; + List *tokens; public: - Lexer(char *script); + void init(char *script); void scan_tokens(void); }; diff --git a/include/list.h b/include/list.h index f45b90d..ffd977c 100644 --- a/include/list.h +++ b/include/list.h @@ -16,7 +16,7 @@ private: uint8_t *elements; public: - List(size_t element_size); + void init(size_t element_size); void add(void *element); void *get(size_t i); diff --git a/include/token.h b/include/token.h index c6a61cd..165f100 100644 --- a/include/token.h +++ b/include/token.h @@ -62,7 +62,7 @@ private: char *string; public: - Token(TokenType type, char *lexeme, unsigned int line); + 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 a901fc5..4856702 100644 --- a/src/filehandler.cc +++ b/src/filehandler.cc @@ -2,7 +2,8 @@ #include <stdio.h> #include <stdlib.h> -Filehandler::Filehandler(char *path) +void +Filehandler::init(char *path) { f = NULL; buffer = NULL; diff --git a/src/lexer.cc b/src/lexer.cc index 086200e..baacdbb 100644 --- a/src/lexer.cc +++ b/src/lexer.cc @@ -1,8 +1,12 @@ #include <lexer.h> #include <stdio.h> -Lexer::Lexer(char *script) : script(script) {} +void +Lexer::init(char *script) +{ + this->script = script; +} void Lexer::scan_tokens(void) { diff --git a/src/list.cc b/src/list.cc index cd0dc7c..4518d89 100644 --- a/src/list.cc +++ b/src/list.cc @@ -5,12 +5,13 @@ #include <stdlib.h> #include <string.h> -List::List(size_t element_size) +void +List::init(size_t element_size) { this->element_size = element_size; current = 0; max = START_SIZE; - elements = (uint8_t *) malloc(element_size * max); + elements = (uint8_t *) calloc(max, element_size); } void diff --git a/src/main.cc b/src/main.cc index 294e7d8..2ebf2a6 100644 --- a/src/main.cc +++ b/src/main.cc @@ -13,7 +13,8 @@ main(int argc, char **argv) return 1; } - Filehandler f(argv[1]); + Filehandler f; + f.init(argv[1]); bool opened = f.open(); if (!opened) { @@ -27,10 +28,12 @@ main(int argc, char **argv) return 1; } - Lexer s(buffer); - s.scan_tokens(); + Lexer l; + l.init(buffer); + l.scan_tokens(); - Token t(EOFF, "EOF", 221); + Token t; + t.init(EOFF, "EOF", 221); printf("%s\n", t.to_string()); t.clean(); diff --git a/src/token.cc b/src/token.cc index a776d01..ad9dd92 100644 --- a/src/token.cc +++ b/src/token.cc @@ -3,7 +3,8 @@ #include <string.h> #include <token.h> -Token::Token(TokenType type, char *lexeme, unsigned int line) +void +Token::init(TokenType type, char *lexeme, unsigned int line) { this->type = type; this->lexeme = lexeme; |