aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/include/lexer.h2
-rw-r--r--src/lexer.c24
-rw-r--r--src/main.c17
3 files changed, 23 insertions, 20 deletions
diff --git a/src/include/lexer.h b/src/include/lexer.h
index bff0a80..cc96216 100644
--- a/src/include/lexer.h
+++ b/src/include/lexer.h
@@ -34,5 +34,7 @@ extern token_t* lexer_next_token(lexer_t* lexer, token_t* token);
extern char* lexer_get_c_as_string(lexer_t* lexer);
+extern void lexer_destroy(lexer_t* lexer);
+
#endif
diff --git a/src/lexer.c b/src/lexer.c
index 345d5c5..acfae12 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -1,7 +1,5 @@
-#include "include/lexer.h"
+#include "include/lexer.h"
#include "include/token.h"
-
-
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
@@ -40,16 +38,12 @@ token_t* lexer_get_next_token(lexer_t* lexer) {
lexer->c == ' ' ||
lexer->c == '\t' ||
lexer->c == '\n'
- ) {
- lexer_pass(lexer);
- }
+ ) { lexer_pass(lexer); }
if (
isalnum(lexer->c) ||
lexer->c == '_'
- ) {
- return lexer_get_keyword(lexer);
- }
+ ) { return lexer_get_keyword(lexer); }
switch (lexer->c) {
case '\'':
@@ -221,19 +215,18 @@ token_t* lexer_get_directive(lexer_t* lexer) {
}
token_t* lexer_get_keyword(lexer_t* lexer) {
- lexer_next(lexer);
-
char* str_so_far = calloc(1, sizeof(char));
str_so_far[0] = '\0';
while (isalnum(lexer->c)) {
char* current = lexer_get_c_as_string(lexer);
str_so_far = realloc(str_so_far, (strlen(str_so_far) + strlen(current) * sizeof(char)));
- strcat(str_so_far, current);
-
+ strcat(current, str_so_far);
+ free(current);
lexer_next(lexer);
}
+ lexer_next(lexer);
return token_init(TOKEN_KEYWORD, str_so_far);
}
@@ -251,3 +244,8 @@ char* lexer_get_c_as_string(lexer_t* lexer) {
return str;
}
+
+void lexer_destroy(lexer_t* lexer) {
+ free(lexer->content);
+ free(lexer);
+}
diff --git a/src/main.c b/src/main.c
index a463a7e..4b4944d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -11,16 +11,20 @@ int main(int argc, char* argv[]) {
char *source;
fsource = fopen ("examples/hello.halk", "rb");
- if (!fsource) { fputs("Source file not found.", stderr); exit(1); };
+ if (!fsource) {
+ fputs("Source file not found.", stderr);
+ exit(1);
+ };
fseek(fsource, 0L, SEEK_END);
fsource_size = ftell(fsource);
rewind(fsource);
source = calloc(1, fsource_size + 1);
+
if (!source) {
fclose(fsource);
- fputs("Memory allocation faled.", stderr);
+ fputs("Memory allocation failed.", stderr);
exit(1);
}
@@ -31,12 +35,9 @@ int main(int argc, char* argv[]) {
exit(1);
}
- lexer_t* lexer = lexer_init(
- source
- );
-
fclose(fsource);
- free(source);
+
+ lexer_t* lexer = lexer_init( source );
printf("\n=== INPUT =======\n%s\n=== END INPUT ===\n", lexer->content);
@@ -46,5 +47,7 @@ int main(int argc, char* argv[]) {
printf("===\ntoken type: %d\ntoken value: %s\n===\n", token->type, token->value);
}
+ lexer_destroy(lexer);
+
return 0;
}