change over to a linked list system and start trying to fix bug which causes the parser to not output anything meaningful due to memory deallocation
This commit is contained in:
@@ -1,20 +1,23 @@
|
||||
#include "parser.h"
|
||||
#include <stddef.h>
|
||||
|
||||
TaggedValue parse_token(TokenStruct * tokenStruct, int *index) {
|
||||
Token token = tokenStruct->tokens[*index];
|
||||
switch (token.type) {
|
||||
TaggedValue parse_token(LinkedList * tokens, size_t *index) {
|
||||
Token * token = get_element_at(tokens, *index);
|
||||
switch (token->type) {
|
||||
case TOKEN_STRING:
|
||||
index++;
|
||||
return parse_string(token);
|
||||
(*index)++;
|
||||
return parse_string(*token);
|
||||
default:
|
||||
perror("unreachable");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
void parser(TaggedValueStruct * taggedValueStruct, TokenStruct * tokenStruct, bool inline_flag) {
|
||||
int index = 0;
|
||||
while (index < tokenStruct->count) {
|
||||
TaggedValueStruct_append(taggedValueStruct, parse_token(tokenStruct, &index));
|
||||
void parser(LinkedList * parsed, LinkedList * tokens, bool inline_flag) {
|
||||
size_t index = 0;
|
||||
size_t length = list_length(tokens);
|
||||
while (index < length) {
|
||||
TaggedValue parsed_code = parse_token(tokens, &index);
|
||||
append(parsed,&parsed_code);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,6 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void parser(TaggedValueStruct * TaggedValueStruct, TokenStruct * tokenStruct, bool inline_flag);
|
||||
void parser(LinkedList * parsed, LinkedList * tokens, bool inline_flag);
|
||||
|
||||
TaggedValue parse_token(TokenStruct * tokenStruct, int *index);
|
||||
TaggedValue parse_token(LinkedList * tokens, size_t *index);
|
||||
@@ -1,12 +1,14 @@
|
||||
#include "string.h"
|
||||
#include "../../string/string.h"
|
||||
#include "../../lexer/token.h"
|
||||
|
||||
#include <cjson/cJSON.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char *swap_quotes(const char *input) {
|
||||
char *swap_quotes(char *input) {
|
||||
size_t len = strlen(input);
|
||||
char *result = malloc(len + 1);
|
||||
if (!result)
|
||||
@@ -24,7 +26,8 @@ char *swap_quotes(const char *input) {
|
||||
return result;
|
||||
}
|
||||
|
||||
char *unquote(const char *str) {
|
||||
char *unquote(char *str) {
|
||||
return str;
|
||||
if (*str == '\0')
|
||||
return NULL;
|
||||
|
||||
@@ -72,7 +75,7 @@ char *unquote(const char *str) {
|
||||
|
||||
TaggedValue parse_string(Token token) {
|
||||
return (TaggedValue){
|
||||
TYPE_STRING,
|
||||
unquote(token.value)
|
||||
AST_STRING,
|
||||
cloneString(token.value),
|
||||
};
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "../../lexer/token.h"
|
||||
#include "../taggedValue.h"
|
||||
|
||||
char *swap_quotes(const char *input);
|
||||
char *swap_quotes(char *input);
|
||||
|
||||
char *unquote(const char *str);
|
||||
char *unquote(char *str);
|
||||
|
||||
TaggedValue parse_string(Token token);
|
||||
@@ -1,26 +0,0 @@
|
||||
#include "taggedValue.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
TaggedValueStruct init_TaggedValueStruct() {
|
||||
TaggedValueStruct taggedValueStruct = {
|
||||
0,
|
||||
INITIAL_CAPACITY,
|
||||
malloc(sizeof(TaggedValue)*INITIAL_CAPACITY)
|
||||
};
|
||||
return taggedValueStruct;
|
||||
}
|
||||
|
||||
void TaggedValueStruct_append(TaggedValueStruct *TaggedValueStruct,
|
||||
TaggedValue TaggedValue) {
|
||||
if (TaggedValueStruct->count >= TaggedValueStruct->capacity) {
|
||||
TaggedValueStruct->capacity *= 2;
|
||||
TaggedValueStruct->TaggedValue =
|
||||
realloc(TaggedValueStruct->TaggedValue,
|
||||
sizeof(TaggedValue) * TaggedValueStruct->capacity);
|
||||
}
|
||||
TaggedValueStruct[TaggedValueStruct->count].TaggedValue->data =
|
||||
TaggedValue.data;
|
||||
TaggedValueStruct[TaggedValueStruct->count].TaggedValue->type =
|
||||
TaggedValue.type;
|
||||
TaggedValueStruct->count++;
|
||||
}
|
||||
@@ -1,23 +1,11 @@
|
||||
#include "../list/list.h"
|
||||
|
||||
typedef enum {
|
||||
TYPE_STRING,
|
||||
AST_STRING,
|
||||
} ValueType;
|
||||
|
||||
typedef struct {
|
||||
ValueType type;
|
||||
void *data;
|
||||
|
||||
} TaggedValue;
|
||||
|
||||
|
||||
#define INITIAL_CAPACITY 64
|
||||
|
||||
|
||||
typedef struct {
|
||||
int count;
|
||||
int capacity;
|
||||
TaggedValue * TaggedValue;
|
||||
} TaggedValueStruct;
|
||||
|
||||
TaggedValueStruct init_TaggedValueStruct();
|
||||
void TaggedValueStruct_append(TaggedValueStruct *TaggedValueStruct,
|
||||
TaggedValue TaggedValue);
|
||||
} TaggedValue;
|
||||
Reference in New Issue
Block a user