working on lexer, and updated vector (still needs testing)

This commit is contained in:
herbglitch 2024-08-28 20:04:18 -06:00
parent cdd6c3976b
commit 6e814f12e6
4 changed files with 195 additions and 3 deletions

View file

@ -0,0 +1,73 @@
#include "arc/std/lexer.h"
#include "arc/std/bool.h"
#include "arc/std/string.h"
#include "arc/std/vector.h"
#include "arc/std/io.h"
#include <stdlib.h>
struct ARC_Lexer {
ARC_Vector *tokens;
};
void ARC_Lexer_Create(ARC_Lexer **lexer){
*lexer = (ARC_Lexer *)malloc(sizeof(ARC_Lexer));
//TODO: add compare and delete callbacks
ARC_Vector_Create(&(*lexer)->tokens, NULL, NULL);
}
void ARC_Lexer_Destroy(ARC_Lexer *lexer){
ARC_Vector_Destroy(lexer->tokens);
free(lexer);
}
void ARC_Lexer_RegisterToken(ARC_Lexer *lexer, ARC_LexerToken token){
ARC_LexerToken *storedToken = (ARC_LexerToken *)malloc(sizeof(ARC_LexerToken));
*storedToken = token;
ARC_Vector_Add(lexer->tokens, storedToken);
}
void ARC_Lexer_LexFile(ARC_Lexer *lexer, ARC_String *path){
ARC_String *data;
ARC_IO_FileToStr(path, &data);
if(data != NULL){
ARC_String_Destroy(data);
}
}
ARC_Bool ARC_Lexer_AutomataMatchCharFn(ARC_String **string, ARC_String **tokenData, void *automataData){
*tokenData = NULL;
if((*string)->data[0] == *(char *)automataData){
if((*string)->length == 1){
ARC_String_Destroy(*string);
*string = NULL;
}
ARC_String_ReplaceWithSubstring(string, 1, (*string)->length - 1);
return ARC_True;
}
return ARC_False;
}
ARC_Bool ARC_Lexer_AutomataMatchStringFn(ARC_String **string, ARC_String **tokenData, void *automataData){
*tokenData = NULL;
ARC_String *automataDataString = (ARC_String *)automataData;
if(ARC_String_Equals(*string, automataDataString)){
if((*string)->length == automataDataString->length){
ARC_String_Destroy(*string);
*string = NULL;
}
ARC_String_ReplaceWithSubstring(string, automataDataString->length, (*string)->length - automataDataString->length);
return ARC_True;
}
return ARC_False;
}

View file

@ -12,7 +12,8 @@ struct ARC_Vector {
void **data;
ARC_Vector_CompareDataFn compareDataFn;
ARC_Vector_CompareDataFn compareDataFn;
ARC_Vector_DeleteDataFn *deleteDataFn;
};
//this is a private function used as the default check for removing data from a given pointer
@ -24,7 +25,7 @@ ARC_Bool ARC_Vector_CompareDataDefaultFn(void *dataA, void *dataB){
return ARC_False;
}
void ARC_Vector_Create(ARC_Vector **vector, ARC_Vector_CompareDataFn *compareDataFn){
void ARC_Vector_Create(ARC_Vector **vector, ARC_Vector_CompareDataFn *compareDataFn, ARC_Vector_DeleteDataFn *deleteDataFn){
//create the vector
*vector = (ARC_Vector *)malloc(sizeof(ARC_Vector));
@ -38,9 +39,23 @@ void ARC_Vector_Create(ARC_Vector **vector, ARC_Vector_CompareDataFn *compareDat
if(compareDataFn != NULL){
(*vector)->compareDataFn = *compareDataFn;
}
//set NULL as a default for deleteDataFn, then copy the delete data function callback if it exists
(*vector)->deleteDataFn = NULL;
if(deleteDataFn != NULL){
(*vector)->deleteDataFn = (ARC_Vector_DeleteDataFn *)malloc(sizeof(ARC_Vector_DeleteDataFn));
*((*vector)->deleteDataFn) = *deleteDataFn;
}
}
void ARC_Vector_Destroy(ARC_Vector *vector){
//TODO: clear vector before destroying
//free the delete data function if it exists
if(vector->deleteDataFn){
free(vector->deleteDataFn);
}
//free everything stored in the vector
free(vector->data);
@ -96,6 +111,11 @@ void ARC_Vector_RemoveIndex(ARC_Vector *vector, uint32_t index){
return;
}
//call delete data to clean up item if delete data function exists
if(vector->deleteDataFn != NULL){
(*(vector->deleteDataFn))(vector->data[index]);
}
//we will be using index to iterate as we will not use it again, so we can skip the first part of the for loop
for(; index + 1 < vector->currentSize; index++){
//override the data from index to the end by shifting it back one