aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-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.c (renamed from src/main.cc)20
-rw-r--r--src/token.c34
-rw-r--r--src/token.cc33
9 files changed, 192 insertions, 156 deletions
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);
-}
diff --git a/src/main.cc b/src/main.c
index 294e7d8..cb6cbff 100644
--- a/src/main.cc
+++ b/src/main.c
@@ -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);
-}