From 5842197142ab9537c9f27547eaa612e05d401097 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Fri, 11 Oct 2024 03:57:32 -0600 Subject: [PATCH 1/2] added number checking to string --- include/arc/std/string.h | 13 +++++++++++-- src/std/string.c | 17 ++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/include/arc/std/string.h b/include/arc/std/string.h index fc1b281..56d36eb 100644 --- a/include/arc/std/string.h +++ b/include/arc/std/string.h @@ -176,9 +176,18 @@ ARC_Bool ARC_String_SubstringEqualsCString(ARC_String *string, uint64_t offset, * * @param string string to check * - * @return 1 if alphabetic, 0 if not alphabetic + * @return ARC_True if alphabetic, ARC_False if not alphabetic */ -uint8_t ARC_String_Alpha(ARC_String *string); +ARC_Bool ARC_String_IsAlpha(ARC_String *string); + +/** + * @brief checks if string is made out of only numbers + * + * @param string string to check + * + * @return ARC_True if it is numeric, ARC_False if it is not numeric + */ +ARC_Bool ARC_String_IsNumeric(ARC_String *string); /** * @brief converst substring from string to uint64_t diff --git a/src/std/string.c b/src/std/string.c index 310ccc8..d03e911 100644 --- a/src/std/string.c +++ b/src/std/string.c @@ -173,7 +173,8 @@ ARC_Bool ARC_String_SubstringEqualsCString(ARC_String *string, uint64_t offset, return ARC_True; } -uint8_t ARC_String_Alpha(ARC_String *string){ +//TODO: fix this +ARC_Bool ARC_String_IsAlpha(ARC_String *string){ for(uint64_t length = string->length; length; length--){ if(string->data[length - 1] >= 'a' && string->data[length - 1] <= 'z'){ continue; @@ -183,10 +184,20 @@ uint8_t ARC_String_Alpha(ARC_String *string){ continue; } - return 1; + return ARC_True; } - return 0; + return ARC_False; +} + +ARC_Bool ARC_String_IsNumeric(ARC_String *string){ + for(uint64_t index = 0; index < string->length; index++){ + if(string->data[index] < '0' || string->data[index] > '9'){ + return ARC_False; + } + } + + return ARC_True; } uint64_t ARC_String_ToUint64_t(ARC_String *string){ From 60acfd075a5a92428dfbf5db83a3487f6e552978 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Wed, 16 Oct 2024 05:14:53 -0600 Subject: [PATCH 2/2] creation and destruction in parser, also added some stuff to gitingore --- .gitignore | 4 ++- CMakeLists.txt | 4 ++- include/arc/std/lexer.h | 3 +-- include/arc/std/parser.h | 58 ++++++++++++++++++++++++++++++++++++++++ src/std/parser.c | 41 ++++++++++++++++++++++++++++ tests/std/parser.c | 45 +++++++++++++++++++++++++++++++ 6 files changed, 151 insertions(+), 4 deletions(-) create mode 100644 tests/std/parser.c diff --git a/.gitignore b/.gitignore index c7eb1b3..cd76ae7 100644 --- a/.gitignore +++ b/.gitignore @@ -526,4 +526,6 @@ FodyWeavers.xsd doxygen/html [Bb][Uu][Ii][Ll][Dd] -.ccls \ No newline at end of file +.ccls +.vscode +tests/test_error_out.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c56225..55ec589 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,6 +63,7 @@ set(ARCHEUS_STD_SOURCES src/std/hashtable.c src/std/io.c src/std/lexer.c + src/std/parser.c src/std/queue.c src/std/stack.c src/std/string.c @@ -127,7 +128,8 @@ if(ARCHEUS_STD_TESTS) tests/test.c #tests/std/vector.c - tests/std/lexer.c + #tests/std/lexer.c + tests/std/parser.c ${ARCHEUS_STD_SOURCES} ) diff --git a/include/arc/std/lexer.h b/include/arc/std/lexer.h index c94d768..b57fc78 100644 --- a/include/arc/std/lexer.h +++ b/include/arc/std/lexer.h @@ -43,7 +43,6 @@ typedef ARC_Bool (* ARC_LexerTokenRule_AutomataFn)(ARC_String **string, ARC_Stri */ typedef void (* ARC_LexerTokenRule_DestroyAutomataDataFn)(void *automataData); - /** * @brief a lexer token rule type */ @@ -59,7 +58,7 @@ typedef struct ARC_LexerTokenRule { /** * @brief creates an ARC_Lexer type * - * @param[out] lexer + * @param[out] lexer ARC_Lexer to create */ void ARC_Lexer_Create(ARC_Lexer **lexer); diff --git a/include/arc/std/parser.h b/include/arc/std/parser.h index e69de29..29331be 100644 --- a/include/arc/std/parser.h +++ b/include/arc/std/parser.h @@ -0,0 +1,58 @@ +#ifndef ARC_STD_PARSER_H_ +#define ARC_STD_PARSER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "arc/std/array.h" +//#include "arc/std/bool.h" +//#include "arc/std/lexer.h" +#include + +/** + * @brief a lexer type +*/ +typedef struct ARC_Parser ARC_Parser; + +/** + * @brief a lexer type +*/ +typedef struct ARC_ParserLanguageTag { + uint32_t tagId; + + uint32_t **tokensOrTags; + uint32_t tokensOrTagsSize; +} ARC_ParserLanguageTag; + +/** + * @brief creates an ARC_Parser type + * + * @TODO: probs want to move the note to another file + * @note array of tokens for langauge? like + * ARC_ParserTag tag = { + * VARIABLE_NAME, //tagId + * { + * { 2, CHAR_OR_NUM, VARIABLE_NAME }, + * { 1, LAMBDA }, + * }, //components + * 2 //componentsSize + * }; + * + * @param[out] parser ARC_Parser to create + * @param[in] language an arry of ARC_ParserLanguageTags defining a langauge +*/ +void ARC_Parser_Create(ARC_Parser **parser, ARC_Array *language); + +/** + * @brief destroys an ARC_Parser type + * + * @param[in] parser ARC_Parser to free +*/ +void ARC_Parser_Destroy(ARC_Parser *parser); + +#ifdef __cplusplus +} +#endif + +#endif // !ARC_STD_PARSER_H_ diff --git a/src/std/parser.c b/src/std/parser.c index e69de29..61699ad 100644 --- a/src/std/parser.c +++ b/src/std/parser.c @@ -0,0 +1,41 @@ +#include "arc/std/parser.h" +#include "arc/std/lexer.h" +#include +#include +#include + +struct ARC_Parser { + ARC_Array language; + + ARC_Lexer *lexer; +}; + +void ARC_Parser_Create(ARC_Parser **parser, ARC_Array *language){ + *parser = (ARC_Parser *)malloc(sizeof(ARC_Parser)); + + //set the language size to 0 and data to NULL in case the language is NULL + (*parser)->language.size = 0; + (*parser)->language.data = NULL; + + //if the language exists, copy the language + if(language != NULL){ + (*parser)->language.size = language->size; + (*parser)->language.data = malloc(sizeof(ARC_ParserLanguageTag) * language->size); + + memcpy((*parser)->language.data, language->data, language->size); + } + + //create the lexer + ARC_Lexer_Create(&((*parser)->lexer)); + + //TODO: register instructions to the lexer + //TODO: probs want to pass in a callback for this +} + +void ARC_Parser_Destroy(ARC_Parser *parser){ + free(parser->language.data); + + ARC_Lexer_Destroy(parser->lexer); + + free(parser); +} diff --git a/tests/std/parser.c b/tests/std/parser.c new file mode 100644 index 0000000..8a67599 --- /dev/null +++ b/tests/std/parser.c @@ -0,0 +1,45 @@ +#include "../test.h" +#include "arc/std/parser.h" + +#define LAMBDA 0 +#define CHAR 1 +#define NUM 2 +#define CHAR_OR_NUM 3 +#define VARIABLE_NAME 4 +#define VARIABLE 5 + +ARC_TEST(Lexer_Char_Match){ + ARC_Parser *parser; + + uint32_t *charOrNumTokens[] = { (uint32_t[]){ 1, CHAR }, (uint32_t[]){ 1, NUM } }; + uint32_t *variableNameTags[] = { (uint32_t[]){ 2, CHAR_OR_NUM, VARIABLE_NAME }, (uint32_t[]){ 1, LAMBDA } }; + uint32_t *variableTokensOrTags[] = { (uint32_t[]){ 2, CHAR, VARIABLE_NAME } }; + ARC_ParserLanguageTag testTags[3] = { + { + CHAR_OR_NUM, //tagId + charOrNumTokens, //tokensOrTags + 2 //tokenOrTagsSize + }, + { + VARIABLE_NAME, //tagId + variableNameTags, //tokensOrTags + 2 //tokenOrTagsSize + }, + { + VARIABLE, //tagId + variableTokensOrTags, //tokensOrTags + 1 //tokenOrTagsSize + } + }; + + ARC_Array languageArray = { + 3, //size + testTags //data + }; + + ARC_Parser_Create(&parser, &languageArray); + + ARC_Parser_Destroy(parser); + + ARC_CHECK(1 == 1); +}