aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/include/log.h24
-rw-r--r--src/include/util.h15
-rw-r--r--src/lexer.c4
-rw-r--r--src/log.c18
-rw-r--r--src/main.c10
-rw-r--r--src/parser.c11
-rw-r--r--src/util.c19
7 files changed, 43 insertions, 58 deletions
diff --git a/src/include/log.h b/src/include/log.h
deleted file mode 100644
index 1ad592b..0000000
--- a/src/include/log.h
+++ /dev/null
@@ -1,24 +0,0 @@
-#ifndef LOG_H
-#define LOG_H
-
-
-#include <stdio.h>
-
-
-#define KNRM "\x1B[0m"
-#define KRED "\x1B[31m"
-#define KGRN "\x1B[32m"
-#define KYEL "\x1B[33m"
-#define KBLU "\x1B[34m"
-#define KMAG "\x1B[35m"
-#define KCYN "\x1B[36m"
-#define KWHT "\x1B[37m"
-
-
-void log_dbg(char* content);
-void log_inf(char* content);
-void log_war(char* content);
-void log_err(char* content);
-
-
-#endif
diff --git a/src/include/util.h b/src/include/util.h
new file mode 100644
index 0000000..500fb5f
--- /dev/null
+++ b/src/include/util.h
@@ -0,0 +1,15 @@
+#ifndef UTIL_H
+#define UTIL_H
+
+#include <stdarg.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+
+void log_inf(const char* fmt, ...);
+void log_war(const char* fmt, ...);
+void log_err(const char* fmt, ...);
+void die(const char* fmt, ...);
+
+#endif
diff --git a/src/lexer.c b/src/lexer.c
index b2f9894..553a794 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -3,7 +3,7 @@
#include <ctype.h>
#include <stdio.h>
-#include "include/log.h"
+#include "include/util.h"
#include "include/lexer.h"
@@ -193,6 +193,7 @@ token_t* lexer_collect(lexer_t* lexer, char end_char, int fskip, int lskip, int
memcpy(token + len, current, strlen(current) * sizeof(char));
len += strlen(current) * sizeof(char);
+ free(current);
lexer_next(lexer);
}
@@ -213,6 +214,7 @@ token_t* lexer_get_keyword(lexer_t* lexer) {
);
strcat(keyword_so_far, current);
+ free(current);
lexer_next(lexer);
}
diff --git a/src/log.c b/src/log.c
deleted file mode 100644
index 3a899d3..0000000
--- a/src/log.c
+++ /dev/null
@@ -1,18 +0,0 @@
-#include "include/log.h"
-
-
-void log_dbg(char* content) {
- fprintf(stderr, KMAG "[DEBUG] %s%s\n", KNRM, content);
-};
-
-void log_inf(char* content) {
- fprintf(stderr, KBLU "[INFO] %s%s\n", KNRM, content);
-};
-
-void log_war(char* content) {
- fprintf(stderr, KYEL "[WARNING] %s%s\n", KNRM, content);
-};
-
-void log_err(char* content) {
- fprintf(stderr, KRED "[ERROR] %s%s\n", KNRM, content);
-};
diff --git a/src/main.c b/src/main.c
index 0af449b..ceefadd 100644
--- a/src/main.c
+++ b/src/main.c
@@ -53,16 +53,8 @@ int main(int argc, char* argv[]) {
printf("===\ntoken type: %d:\ntoken value: || %s ||\n===\n", token->type, token->value);
}
- //parser_t* parser = parser_init(lexer);
- //log_inf("Parser created");
-
- //tree_t* tree = parser_parse(parser);
- //log_inf("Tree root created");
-
- //printf("TYPE: [%d]\n", tree->type);
- //printf("SIZE: [%d]\n", tree->data.subtree.size);
-
fclose(fsource);
+ free(source);
log_inf("Source file closed");
diff --git a/src/parser.c b/src/parser.c
index 613f30a..c106132 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -3,7 +3,7 @@
#include <string.h>
-#include "include/log.h"
+#include "include/util.h"
#include "include/parser.h"
#include "include/token.h"
@@ -21,11 +21,10 @@ parser_t* parser_init(lexer_t* lexer) {
// check for expected token, or throw syntax error
void parser_token_expect(parser_t* parser, int token_type) {
if (parser->token->type == token_type) {
- log_dbg(strcat("Got expected token", lexer_get_c_as_string(parser->lexer)));
+ log_inf(strcat("Got expected token", lexer_get_c_as_string(parser->lexer)));
parser->token = lexer_get_next_token(parser->lexer);
} else {
- log_err("Unexpected token");
- exit(1);
+ die("Unexpected token type: [%d]", token_type);
}
}
@@ -50,7 +49,7 @@ tree_t* parser_parse_chunk(parser_t* parser) {
break;
}
default: {
- log_dbg("Skipping non-keyword token");
+ log_inf("Skipping non-keyword token");
lexer_next(parser->lexer);
break;
}
@@ -142,7 +141,7 @@ tree_t* parser_parse_var_def(parser_t* parser) {
};
tree_t* parser_parse_str(parser_t* parser) {
- log_dbg("Entered str");
+ log_inf("Entered str");
tree_t* str = tree_init(TREE_STR);
str->data.str.val = parser->token->value;
diff --git a/src/util.c b/src/util.c
new file mode 100644
index 0000000..32cef99
--- /dev/null
+++ b/src/util.c
@@ -0,0 +1,19 @@
+#include "include/util.h"
+
+
+void log_inf(const char* fmt, ...) {
+ va_list ap;
+
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+
+ if (fmt[0] && fmt[strlen(fmt) - 1] == ':') {
+ fputc(' ', stderr);
+ perror(NULL);
+ } else {
+ fputc('\n', stderr);
+ }
+
+ exit(1);
+}