diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-10 22:39:25 +0530 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-10 22:39:25 +0530 |
commit | efafc900db790cac808e0fc6722272bdec451e73 (patch) | |
tree | 6122d91ebe111ba8fd44f973a7429341e0116739 | |
parent | 4765153a04cd4198df2ddfe8e9d7e4719d89794c (diff) |
C++->C
beautiful.
-rw-r--r-- | CMakeLists.txt | 24 | ||||
-rw-r--r-- | include/filehandler.h | 19 | ||||
-rw-r--r-- | include/lexer.h | 12 | ||||
-rw-r--r-- | include/list.h | 17 | ||||
-rw-r--r-- | include/token.h | 19 | ||||
-rw-r--r-- | src/filehandler.c | 62 | ||||
-rw-r--r-- | src/filehandler.cc | 55 | ||||
-rw-r--r-- | src/lexer.c | 29 | ||||
-rw-r--r-- | src/lexer.cc | 11 | ||||
-rw-r--r-- | src/list.c | 56 | ||||
-rw-r--r-- | src/list.cc | 48 | ||||
-rw-r--r-- | src/main.c (renamed from src/main.cc) | 20 | ||||
-rw-r--r-- | src/token.c | 34 | ||||
-rw-r--r-- | src/token.cc | 33 |
14 files changed, 227 insertions, 212 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 900dda5..d9fa7f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,18 +1,18 @@ cmake_minimum_required(VERSION 3.21) -project(minni CXX) +project(minni C) set(SRC - src/filehandler.cc - src/lexer.cc - src/list.cc - src/main.cc - src/token.cc + src/filehandler.c + src/lexer.c + src/list.c + src/main.c + src/token.c ) add_executable(minni ${SRC}) target_include_directories(minni PRIVATE include) -set(CXX_COMPILE_OPTIONS +set(C_COMPILE_OPTIONS # -O3 -Og @@ -23,15 +23,9 @@ set(CXX_COMPILE_OPTIONS -g - -std=c++98 - - -Wno-write-strings - -Wno-missing-field-initializers - -Wno-c++11-long-long - -Wno-c99-extensions - -Wno-c++14-binary-literal + -std=c99 ) target_compile_options(minni PRIVATE - $<$<COMPILE_LANGUAGE:CXX>: ${CXX_COMPILE_OPTIONS}> + $<$<COMPILE_LANGUAGE:C>: ${C_COMPILE_OPTIONS}> ) diff --git a/include/filehandler.h b/include/filehandler.h index ac8fa88..bf52e2c 100644 --- a/include/filehandler.h +++ b/include/filehandler.h @@ -2,23 +2,18 @@ #define __FILEHANDLER_H #include <stdbool.h> +#include <stddef.h> #include <stdio.h> -class Filehandler -{ -private: +typedef struct { FILE *f; char *path; char *buffer; +} filehandler_t; -private: - unsigned int size(void); - -public: - Filehandler(char *path); - bool open(void); - char *read(void); - void close(void); -}; +void filehandler_init(filehandler_t *fh, char *path); +bool filehandler_open(filehandler_t *fh); +char *filehandler_read(filehandler_t *fh); +void filehandler_close(filehandler_t *fh); #endif diff --git a/include/lexer.h b/include/lexer.h index 19b8a97..71fa8f5 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -1,14 +1,10 @@ #ifndef __SCANNER_H #define __SCANNER_H -class Lexer -{ -private: - char *script; +#include <list.h> -public: - Lexer(char *script); - void scan_tokens(void); -}; +void lexer_init(char *script); +void lexer_scan_tokens(void); +void lexer_clean(void); #endif diff --git a/include/list.h b/include/list.h index f45b90d..3e35878 100644 --- a/include/list.h +++ b/include/list.h @@ -7,20 +7,17 @@ #define START_SIZE 100 #define INCREMENT_BY 25 -class List -{ -private: +typedef struct { size_t element_size; size_t max; size_t current; uint8_t *elements; +} list_t; -public: - List(size_t element_size); - - void add(void *element); - void *get(size_t i); - void clean(void); -}; +void list_init(list_t *list, size_t element_size); +void list_add(list_t *list, void *element); +void *list_get(list_t *list, size_t i); +size_t list_length(list_t *list); +void list_clean(list_t *list); #endif diff --git a/include/token.h b/include/token.h index c6a61cd..a9516d6 100644 --- a/include/token.h +++ b/include/token.h @@ -1,7 +1,7 @@ #ifndef __TOKEN_H #define __TOKEN_H -enum TokenType { +typedef enum { // Single-character tokens. LEFT_PAREN, RIGHT_PAREN, @@ -50,22 +50,19 @@ enum TokenType { FALSE, EOFF -}; +} TokenType; -class Token -{ -private: +typedef struct { TokenType type; char *lexeme; // literal unsigned int line; char *string; +} token_t; -public: - Token(TokenType type, char *lexeme, unsigned int line); - - char *to_string(void); - void clean(void); -}; +void +token_init(token_t *token, TokenType type, char *lexeme, unsigned int line); +char *token_to_string(token_t *token); +void token_clean(token_t *token); #endif diff --git a/src/filehandler.c b/src/filehandler.c new file mode 100644 index 0000000..f42a22b --- /dev/null +++ b/src/filehandler.c @@ -0,0 +1,62 @@ +#include <filehandler.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +static size_t +size(filehandler_t *fh) +{ + unsigned int current = ftell(fh->f); + + fseek(fh->f, 0, SEEK_END); + unsigned int s = ftell(fh->f); + + fseek(fh->f, current, SEEK_SET); + return s; +} + +void +filehandler_init(filehandler_t *fh, char *path) +{ + fh->f = NULL; + fh->buffer = NULL; + + fh->path = (char *) calloc(strlen(path) + 1, sizeof(char)); + strcpy(fh->path, path); +} + +bool +filehandler_open(filehandler_t *fh) +{ + fh->f = fopen(fh->path, "r"); + if (fh->f == NULL) { + printf("Unable to open %s\n", fh->path); + return false; + } + + return true; +} + +char * +filehandler_read(filehandler_t *fh) +{ + fseek(fh->f, 0, SEEK_SET); + + size_t buf_size = size(fh); + + fh->buffer = (char *) calloc(buf_size, sizeof(char)); + int bytesread = fread(fh->buffer, sizeof(char), buf_size, fh->f); + + if (bytesread < 0) { + return NULL; + } + return fh->buffer; +} + +void +filehandler_close(filehandler_t *fh) +{ + fclose(fh->f); + free(fh->buffer); +} diff --git a/src/filehandler.cc b/src/filehandler.cc deleted file mode 100644 index a901fc5..0000000 --- a/src/filehandler.cc +++ /dev/null @@ -1,55 +0,0 @@ -#include <filehandler.h> -#include <stdio.h> -#include <stdlib.h> - -Filehandler::Filehandler(char *path) -{ - f = NULL; - buffer = NULL; - this->path = path; -} - -bool -Filehandler::open(void) -{ - f = fopen(path, "r"); - if (f == NULL) { - printf("Unable to open %s\n", path); - return false; - } - - return true; -} - -unsigned int -Filehandler::size(void) -{ - unsigned int current = ftell(f); - - fseek(f, 0, SEEK_END); - unsigned int s = ftell(f); - - fseek(f, current, SEEK_SET); - return s; -} - -char * -Filehandler::read(void) -{ - fseek(f, 0, SEEK_SET); - - buffer = (char *) calloc(size(), sizeof(char)); - int bytesread = fread(buffer, sizeof(char), size(), f); - - if (bytesread < 0) { - return NULL; - } - return buffer; -} - -void -Filehandler::close(void) -{ - fclose(f); - free(buffer); -} diff --git a/src/lexer.c b/src/lexer.c new file mode 100644 index 0000000..488b2dc --- /dev/null +++ b/src/lexer.c @@ -0,0 +1,29 @@ +#include <lexer.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <token.h> + +char *l_script; +list_t *l_tokens; + +void +lexer_init(char *script) +{ + l_script = (char *) calloc(strlen(script) + 1, sizeof(char)); + strcpy(l_script, script); + + // l_tokens = new List(sizeof(Token)); +} + +void +lexer_scan_tokens(void) +{ + printf("%s\n", l_script); +} + +void +lexer_clean(void) +{ + // free(tokens); +} diff --git a/src/lexer.cc b/src/lexer.cc deleted file mode 100644 index 086200e..0000000 --- a/src/lexer.cc +++ /dev/null @@ -1,11 +0,0 @@ -#include <lexer.h> -#include <stdio.h> - -Lexer::Lexer(char *script) : script(script) {} - -void -Lexer::scan_tokens(void) -{ - (void) script; - printf("%s\n", script); -} diff --git a/src/list.c b/src/list.c new file mode 100644 index 0000000..87b364c --- /dev/null +++ b/src/list.c @@ -0,0 +1,56 @@ +#include <list.h> +#include <stddef.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +void +list_init(list_t *list, size_t element_size) +{ + list->element_size = element_size; + list->current = 0; + list->max = START_SIZE; + list->elements = (uint8_t *) malloc(element_size * START_SIZE); +} + +void +list_add(list_t *list, void *element) +{ + if (list->current == list->max) { + list->max += INCREMENT_BY; + list->elements + = (uint8_t *) realloc(list->elements, list->element_size * list->max); + if (list->elements == NULL) { + /* TODO: Handle error */ + printf("Failed to reallocate array\n"); + return; + } + } + + void *new_element = list->elements + list->element_size * list->current; + new_element = memcpy(new_element, element, list->element_size); + + if (new_element == NULL) + printf("Failed to add a new element\n"); + + list->current++; +} + +void * +list_get(list_t *list, size_t i) +{ + return list->elements + (i * list->element_size); +} + +size_t +list_length(list_t *list) +{ + return list->current - 1; +} + +void +list_clean(list_t *list) +{ + free(list->elements); +} diff --git a/src/list.cc b/src/list.cc deleted file mode 100644 index cd0dc7c..0000000 --- a/src/list.cc +++ /dev/null @@ -1,48 +0,0 @@ -#include <list.h> -#include <stddef.h> -#include <stdint.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -List::List(size_t element_size) -{ - this->element_size = element_size; - current = 0; - max = START_SIZE; - elements = (uint8_t *) malloc(element_size * max); -} - -void -List::add(void *element) -{ - if (current == max) { - max += INCREMENT_BY; - elements = (uint8_t *) realloc(elements, element_size * max); - if (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); - - if (new_element == NULL) - printf("Failed to add a new element\n"); - - current++; -} - -void * -List::get(size_t i) -{ - return elements + (i * element_size); -} - -void -List::clean(void) -{ - free(elements); -} @@ -1,3 +1,4 @@ + #include <filehandler.h> #include <lexer.h> #include <list.h> @@ -13,27 +14,28 @@ main(int argc, char **argv) return 1; } - Filehandler f(argv[1]); + filehandler_t *f = (filehandler_t *) malloc(sizeof(filehandler_t)); + filehandler_init(f, argv[1]); - bool opened = f.open(); + bool opened = filehandler_open(f); if (!opened) { printf("Unable to open %s\n", argv[1]); return 1; } - char *buffer = f.read(); + char *buffer = filehandler_read(f); if (buffer == NULL) { printf("Unable to read %s\n", argv[1]); return 1; } - Lexer s(buffer); - s.scan_tokens(); + lexer_init(buffer); + lexer_scan_tokens(); - Token t(EOFF, "EOF", 221); - printf("%s\n", t.to_string()); - t.clean(); + // Token t(EOFF, "EOF", 221); + // printf("%s\n", t.to_string()); + // t.clean(); - f.close(); + filehandler_close(f); return 0; } diff --git a/src/token.c b/src/token.c new file mode 100644 index 0000000..18ec034 --- /dev/null +++ b/src/token.c @@ -0,0 +1,34 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <token.h> + +void +token_init(token_t *token, TokenType type, char *lexeme, unsigned int line) +{ + token->type = type; + token->lexeme = lexeme; + token->line = line; + token->string = NULL; +} + +char * +token_to_string(token_t *token) +{ + unsigned int line_length = snprintf(NULL, 0, "%ul", token->line) - 1; + /* 2: ": ", 1: "\0" */ + unsigned int final_size = strlen(token->lexeme) + line_length + 2 + 1; + + char *result = (char *) calloc(1, final_size); + snprintf(result, final_size, "%d: %s", token->line, token->lexeme); + token->string = result; + + return token->string; +} + +void +token_clean(token_t *token) +{ + if (token->string != NULL) + free(token->string); +} diff --git a/src/token.cc b/src/token.cc deleted file mode 100644 index a776d01..0000000 --- a/src/token.cc +++ /dev/null @@ -1,33 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <token.h> - -Token::Token(TokenType type, char *lexeme, unsigned int line) -{ - this->type = type; - this->lexeme = lexeme; - this->line = line; - this->string = NULL; -} - -char * -Token::to_string(void) -{ - unsigned int line_length = snprintf(NULL, 0, "%ul", line) - 1; - /* 2: ": ", 1: "\0" */ - unsigned int final_size = strlen(lexeme) + line_length + 2 + 1; - - char *result = (char *) calloc(1, final_size); - snprintf(result, final_size, "%d: %s", line, lexeme); - string = result; - - return result; -} - -void -Token::clean(void) -{ - if (string != NULL) - free(string); -} |