aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2025-06-10 22:52:21 +0530
committerRaghuram Subramani <raghus2247@gmail.com>2025-06-10 22:52:21 +0530
commit5f7deaa1944016f56d2ac3cba08ca702033dbe7e (patch)
tree075465f7872fd07035686cdf1f59a0ab8c9658c1
parentefafc900db790cac808e0fc6722272bdec451e73 (diff)
Revert "C++->C"
This reverts commit efafc900db790cac808e0fc6722272bdec451e73.
-rw-r--r--CMakeLists.txt24
-rw-r--r--include/filehandler.h19
-rw-r--r--include/lexer.h12
-rw-r--r--include/list.h17
-rw-r--r--include/token.h19
-rw-r--r--src/filehandler.c62
-rw-r--r--src/filehandler.cc55
-rw-r--r--src/lexer.c29
-rw-r--r--src/lexer.cc11
-rw-r--r--src/list.c56
-rw-r--r--src/list.cc48
-rw-r--r--src/main.cc (renamed from src/main.c)20
-rw-r--r--src/token.c34
-rw-r--r--src/token.cc33
14 files changed, 212 insertions, 227 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d9fa7f5..900dda5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,18 +1,18 @@
cmake_minimum_required(VERSION 3.21)
-project(minni C)
+project(minni CXX)
set(SRC
- src/filehandler.c
- src/lexer.c
- src/list.c
- src/main.c
- src/token.c
+ src/filehandler.cc
+ src/lexer.cc
+ src/list.cc
+ src/main.cc
+ src/token.cc
)
add_executable(minni ${SRC})
target_include_directories(minni PRIVATE include)
-set(C_COMPILE_OPTIONS
+set(CXX_COMPILE_OPTIONS
# -O3
-Og
@@ -23,9 +23,15 @@ set(C_COMPILE_OPTIONS
-g
- -std=c99
+ -std=c++98
+
+ -Wno-write-strings
+ -Wno-missing-field-initializers
+ -Wno-c++11-long-long
+ -Wno-c99-extensions
+ -Wno-c++14-binary-literal
)
target_compile_options(minni PRIVATE
- $<$<COMPILE_LANGUAGE:C>: ${C_COMPILE_OPTIONS}>
+ $<$<COMPILE_LANGUAGE:CXX>: ${CXX_COMPILE_OPTIONS}>
)
diff --git a/include/filehandler.h b/include/filehandler.h
index bf52e2c..ac8fa88 100644
--- a/include/filehandler.h
+++ b/include/filehandler.h
@@ -2,18 +2,23 @@
#define __FILEHANDLER_H
#include <stdbool.h>
-#include <stddef.h>
#include <stdio.h>
-typedef struct {
+class Filehandler
+{
+private:
FILE *f;
char *path;
char *buffer;
-} filehandler_t;
-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);
+private:
+ unsigned int size(void);
+
+public:
+ Filehandler(char *path);
+ bool open(void);
+ char *read(void);
+ void close(void);
+};
#endif
diff --git a/include/lexer.h b/include/lexer.h
index 71fa8f5..19b8a97 100644
--- a/include/lexer.h
+++ b/include/lexer.h
@@ -1,10 +1,14 @@
#ifndef __SCANNER_H
#define __SCANNER_H
-#include <list.h>
+class Lexer
+{
+private:
+ char *script;
-void lexer_init(char *script);
-void lexer_scan_tokens(void);
-void lexer_clean(void);
+public:
+ Lexer(char *script);
+ void scan_tokens(void);
+};
#endif
diff --git a/include/list.h b/include/list.h
index 3e35878..f45b90d 100644
--- a/include/list.h
+++ b/include/list.h
@@ -7,17 +7,20 @@
#define START_SIZE 100
#define INCREMENT_BY 25
-typedef struct {
+class List
+{
+private:
size_t element_size;
size_t max;
size_t current;
uint8_t *elements;
-} list_t;
-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);
+public:
+ List(size_t element_size);
+
+ void add(void *element);
+ void *get(size_t i);
+ void clean(void);
+};
#endif
diff --git a/include/token.h b/include/token.h
index a9516d6..c6a61cd 100644
--- a/include/token.h
+++ b/include/token.h
@@ -1,7 +1,7 @@
#ifndef __TOKEN_H
#define __TOKEN_H
-typedef enum {
+enum TokenType {
// Single-character tokens.
LEFT_PAREN,
RIGHT_PAREN,
@@ -50,19 +50,22 @@ typedef enum {
FALSE,
EOFF
-} TokenType;
+};
-typedef struct {
+class Token
+{
+private:
TokenType type;
char *lexeme;
// literal
unsigned int line;
char *string;
-} token_t;
-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);
+public:
+ Token(TokenType type, char *lexeme, unsigned int line);
+
+ char *to_string(void);
+ void clean(void);
+};
#endif
diff --git a/src/filehandler.c b/src/filehandler.c
deleted file mode 100644
index f42a22b..0000000
--- a/src/filehandler.c
+++ /dev/null
@@ -1,62 +0,0 @@
-#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
new file mode 100644
index 0000000..a901fc5
--- /dev/null
+++ b/src/filehandler.cc
@@ -0,0 +1,55 @@
+#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
deleted file mode 100644
index 488b2dc..0000000
--- a/src/lexer.c
+++ /dev/null
@@ -1,29 +0,0 @@
-#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
new file mode 100644
index 0000000..086200e
--- /dev/null
+++ b/src/lexer.cc
@@ -0,0 +1,11 @@
+#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
deleted file mode 100644
index 87b364c..0000000
--- a/src/list.c
+++ /dev/null
@@ -1,56 +0,0 @@
-#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
new file mode 100644
index 0000000..cd0dc7c
--- /dev/null
+++ b/src/list.cc
@@ -0,0 +1,48 @@
+#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);
+}
diff --git a/src/main.c b/src/main.cc
index cb6cbff..294e7d8 100644
--- a/src/main.c
+++ b/src/main.cc
@@ -1,4 +1,3 @@
-
#include <filehandler.h>
#include <lexer.h>
#include <list.h>
@@ -14,28 +13,27 @@ main(int argc, char **argv)
return 1;
}
- filehandler_t *f = (filehandler_t *) malloc(sizeof(filehandler_t));
- filehandler_init(f, argv[1]);
+ Filehandler f(argv[1]);
- bool opened = filehandler_open(f);
+ bool opened = f.open();
if (!opened) {
printf("Unable to open %s\n", argv[1]);
return 1;
}
- char *buffer = filehandler_read(f);
+ char *buffer = f.read();
if (buffer == NULL) {
printf("Unable to read %s\n", argv[1]);
return 1;
}
- lexer_init(buffer);
- lexer_scan_tokens();
+ Lexer s(buffer);
+ s.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();
- filehandler_close(f);
+ f.close();
return 0;
}
diff --git a/src/token.c b/src/token.c
deleted file mode 100644
index 18ec034..0000000
--- a/src/token.c
+++ /dev/null
@@ -1,34 +0,0 @@
-#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
new file mode 100644
index 0000000..a776d01
--- /dev/null
+++ b/src/token.cc
@@ -0,0 +1,33 @@
+#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);
+}