aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authors-over-42023-06-21 17:17:11 -0400
committers-over-42023-06-21 17:17:11 -0400
commite7d1e397730c271ccb11ce547cd941d740f5a2fc (patch)
tree8b82021d1d958d350cebe98b57d6783a2c2b71ba /src
parent46f4397bf0718fbaf2cee5b4ffd56d4a36f2c4d8 (diff)
NO MORE OTHER BORKED SHIAT :D
Diffstat (limited to 'src')
-rw-r--r--src/lexer.c18
-rw-r--r--src/main.c7
-rw-r--r--src/tree.c2
3 files changed, 19 insertions, 8 deletions
diff --git a/src/lexer.c b/src/lexer.c
index 776b043..731210e 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -104,8 +104,11 @@ token_t* lexer_next_token(lexer_t* lexer, int token_type) {
return token;
}
+// get the current character as a string
char* lexer_get_c_as_string(lexer_t* lexer) {
- char* str = calloc(2, sizeof(char));
+ char* str; // the string to return
+
+ str = calloc(2, sizeof(char));
str[0] = lexer->c;
str[1] = '\0';
@@ -115,14 +118,19 @@ char* lexer_get_c_as_string(lexer_t* lexer) {
// fskip: skip first char?
// lskip: skip last char?
token_t* lexer_collect(lexer_t* lexer, int (*end_char)(char), int fskip, int lskip, int type) {
- if (fskip) { lexer_next(lexer); }
+ size_t len; // length of collected token so far
+ char* token; // collected token so far
- size_t len = 0; // length of collected token so far
- char* token = calloc(len, sizeof(char));
+ len = 1;
+ token = calloc(len, sizeof(char));
token[0] = '\0';
+ if (fskip) { lexer_next(lexer); }
+
while (end_char(lexer->c)) {
- char* current = lexer_get_c_as_string(lexer);
+ char* current;
+
+ current = lexer_get_c_as_string(lexer);
token = realloc(
token,
(len + sizeof(char) * strlen(current))
diff --git a/src/main.c b/src/main.c
index a054b16..4a6a370 100644
--- a/src/main.c
+++ b/src/main.c
@@ -5,7 +5,6 @@
#include "include/token.h"
#include "include/lexer.h"
-
int main(int argc, char* argv[]) {
FILE* fsource;
long fsource_size;
@@ -13,6 +12,7 @@ int main(int argc, char* argv[]) {
lexer_t* lexer;
int in_file;
+
fsource = fopen(argv[1], "rb");
if (!fsource) { free(fsource); die("source file not found"); };
fseek(fsource, 0L, SEEK_END);
@@ -34,9 +34,12 @@ int main(int argc, char* argv[]) {
while (in_file) {
token_t* token;
+ char* type;
token = lexer_get_next_token(lexer);
- log_inf("token type: [%02d]\t\ttoken value: [%s]", token->type, token->value);
+ type = token_get_type(token->type);
+
+ log_inf("token type: [%s]\t\ttoken value: [%s]", type, token->value);
(token->type == TOKEN_EOF) && (in_file = 0);
token_destroy(token);
diff --git a/src/tree.c b/src/tree.c
index 10d564a..bb162cd 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -7,7 +7,7 @@ prim_t* prim_init(int type) {
prim->type = type;
prim->is_mutable = 0;
- switch(type) {
+ switch (type) {
case STR:
prim->val.prim_str.len = NULL;
prim->val.prim_str.val = NULL;