added token_result_array function
This commit is contained in:
parent
f7b1436f87
commit
e968058249
|
@ -52,13 +52,27 @@ typedef struct TokenResult {
|
||||||
typedef struct {
|
typedef struct {
|
||||||
SyncResultType type;
|
SyncResultType type;
|
||||||
union {
|
union {
|
||||||
struct TokenResult* result;
|
TokenResult* result;
|
||||||
GeneralError error;
|
GeneralError error;
|
||||||
};
|
};
|
||||||
} LexerResult;
|
} LexerResult;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
size_t length;
|
||||||
|
Token* tokens;
|
||||||
|
} TokenArray;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
SyncResultType type;
|
||||||
|
union {
|
||||||
|
TokenArray result;
|
||||||
|
GeneralError error;
|
||||||
|
};
|
||||||
|
} TokenArrayResult;
|
||||||
|
|
||||||
void lexer_init(Lexer* lexer, const char* filename, const char* source);
|
void lexer_init(Lexer* lexer, const char* filename, const char* source);
|
||||||
LexerResult lexical_analysis(Lexer* lexer);
|
LexerResult lexical_analysis(Lexer* lexer);
|
||||||
|
TokenArrayResult token_result_array(TokenResult* result);
|
||||||
void clean_token_result(TokenResult* head);
|
void clean_token_result(TokenResult* head);
|
||||||
|
|
||||||
#endif // SYNC_LEXER_H
|
#endif // SYNC_LEXER_H
|
||||||
|
|
22
src/lexer.c
22
src/lexer.c
|
@ -260,6 +260,28 @@ LexerResult lexical_analysis(Lexer *lexer) {
|
||||||
return (LexerResult){SYNC_RESULT, .result = head};
|
return (LexerResult){SYNC_RESULT, .result = head};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TokenArrayResult token_result_array(TokenResult* result) {
|
||||||
|
TokenResult* head = result;
|
||||||
|
size_t length = 0;
|
||||||
|
while (head != NULL) {
|
||||||
|
if (head->type == SYNC_ERROR)
|
||||||
|
return (TokenArrayResult){SYNC_ERROR, .error = (GeneralError){"Invalid Token List.", 1}};
|
||||||
|
length++;
|
||||||
|
head = head->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
Token* token_array = (Token*)malloc(length * sizeof(Token));
|
||||||
|
if (token_array == NULL)
|
||||||
|
return (TokenArrayResult){SYNC_ERROR, .error = (GeneralError){"Failed to allocate memory.", 1}};
|
||||||
|
head = result;
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
token_array[i] = head->result;
|
||||||
|
head = head->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (TokenArrayResult){SYNC_RESULT, length, .result = (TokenArray){length, token_array}};
|
||||||
|
}
|
||||||
|
|
||||||
void clean_token_result(TokenResult* head) {
|
void clean_token_result(TokenResult* head) {
|
||||||
while (head != NULL) {
|
while (head != NULL) {
|
||||||
TokenResult* temp = head;
|
TokenResult* temp = head;
|
||||||
|
|
|
@ -68,7 +68,16 @@ int main(void) {
|
||||||
token_result = token_result->next;
|
token_result = token_result->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TokenArrayResult array_result = token_result_array(lexer_result.result);
|
||||||
clean_token_result(lexer_result.result);
|
clean_token_result(lexer_result.result);
|
||||||
|
if (array_result.type == SYNC_ERROR) {
|
||||||
|
fprintf(stderr, "Error: %s\n", array_result.error.message);
|
||||||
|
free(source);
|
||||||
|
return array_result.error.code;
|
||||||
|
}
|
||||||
|
TokenArray tokens = array_result.result;
|
||||||
|
|
||||||
|
free(tokens.tokens);
|
||||||
free(source);
|
free(source);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in New Issue