add dictionaries and add accessing with a value

This commit is contained in:
2025-06-12 04:37:07 +01:00
parent f8207702e1
commit b05408ebec
10 changed files with 190 additions and 38 deletions

View File

@@ -5,7 +5,6 @@
typedef enum {
TOKEN_STRING = 256,
TOKEN_NUMBER,
TOKEN_FRACTION,
TOKEN_IDENTIFIER,
TOKEN_KEYWORD,
TOKEN_NEW_LINE,

View File

@@ -1,18 +1,48 @@
#include "access.h"
#include "../../../lexer/token.h"
#include "../../parser.h"
#include <string.h>
#include <stdlib.h>
#include "../../../memory.h"
#include "../../parser.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
ParsedValue *parse_access(char*file,DArray *tokens, size_t * index, ParsedValue * to_access) {
ParsedValue *parse_access(char *file, DArray *tokens, size_t *index,
ParsedValue *to_access) {
Token *first_token = darray_get(tokens, *index);
(*index)++;
error_if_finished(file, tokens, index);
Token * token = darray_get(tokens, *index);
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
ParsedAccess *parsedAccess = checked_malloc(sizeof(ParsedAccess));
parsedAccess->to_access = to_access;
parsedAccess->access = strcpy(checked_malloc(strlen(token->value) + 1), token->value);
parsedAccess->to_access = *to_access;
free(to_access);
darray_init(&parsedAccess->access, sizeof(ParsedValue));
if (first_token->type == TOKEN_DOT) {
error_if_finished(file, tokens, index);
Token *token = darray_get(tokens, *index);
ParsedValue parsedString;
parsedString.type = AST_STRING;
parsedString.data =
strcpy(checked_malloc(strlen(token->value) + 1), token->value);
darray_push(&parsedAccess->access, &parsedString);
} else {
while (true) {
skip_newlines_and_indents(tokens, index);
error_if_finished(file, tokens, index);
ParsedValue *parsedValue = parse_token(file, tokens, index, true);
darray_push(&parsedAccess->access, parsedValue);
free(parsedValue);
skip_newlines_and_indents(tokens, index);
error_if_finished(file, tokens, index);
Token *token = darray_get(tokens, *index);
if (token->type == TOKEN_RBRACKET) {
break;
} else if (token->type != TOKEN_COLON) {
fprintf(stderr, "%s:%zu:%zu error: syntax error\n", file,
token->line, token->column);
exit(EXIT_FAILURE);
}
(*index)++;
}
}
parsedValue->type = AST_ACCESS;
parsedValue->data = parsedAccess;
(*index)++;
@@ -22,8 +52,7 @@ ParsedValue *parse_access(char*file,DArray *tokens, size_t * index, ParsedValue
void free_parse_access(void *ptr) {
ParsedValue *parsedValue = ptr;
ParsedAccess *parsedAccess = parsedValue->data;
free_parsed(parsedAccess->to_access);
free(parsedAccess->access);
free(parsedAccess->to_access);
free_parsed(&parsedAccess->to_access);
darray_free(&parsedAccess->access, free_parsed);
free(parsedAccess);
}

View File

@@ -4,8 +4,8 @@
#include "../../../lexer/token.h" // for Token
typedef struct {
ParsedValue * to_access;
char * access;
ParsedValue to_access;
DArray access;
} ParsedAccess;
// Function declaration for parsing an identifier

View File

@@ -0,0 +1,88 @@
#include "dictionary.h"
#include "../../lexer/token.h"
#include "../../memory.h"
#include "../parser.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
ParsedValue *parse_dictionary(char *file, DArray *tokens, size_t *index) {
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
parsedValue->type = AST_DICTIONARY;
DArray *dictionary = checked_malloc(sizeof(DArray));
parsedValue->data = dictionary;
darray_init(dictionary, sizeof(ParsedValue));
(*index)++;
skip_newlines_and_indents(tokens, index);
error_if_finished(file, tokens, index);
Token *token = darray_get(tokens, *index);
if (token->type != TOKEN_RBRACE) {
while (true) {
skip_newlines_and_indents(tokens, index);
error_if_finished(file, tokens, index);
size_t keyIndex = *index;
Token *keyToken = darray_get(tokens, *index);
ParsedValue *key = parse_token(file, tokens, index, true);
if (keyToken->type == TOKEN_IDENTIFIER) {
key->type = AST_STRING;
}
skip_newlines_and_indents(tokens, index);
error_if_finished(file, tokens, index);
token = darray_get(tokens, *index);
ParsedValue *value;
bool tobreak = false;
switch (token->type) {
case TOKEN_COLON:
(*index)++;
skip_newlines_and_indents(tokens, index);
error_if_finished(file, tokens, index);
value = parse_token(file, tokens, index, true);
skip_newlines_and_indents(tokens, index);
error_if_finished(file, tokens, index);
token = darray_get(tokens, *index);
if (token->type == TOKEN_RBRACE) {
tobreak = true;
} else if (token->type != TOKEN_COMMA) {
fprintf(stderr, "%s:%zu:%zu error: syntax error\n", file, token->line,
token->column);
exit(EXIT_FAILURE);
}
break;
case TOKEN_RBRACE:
tobreak = true;
/* fall through */
case TOKEN_COMMA:
value = parse_token(file, tokens, &keyIndex, true);
break;
default:
fprintf(stderr, "%s:%zu:%zu error: syntax error\n", file, token->line,
token->column);
exit(EXIT_FAILURE);
}
ParsedDictionaryEntry entry = {key, value};
darray_push(dictionary, &entry);
if (tobreak) {
break;
}
(*index)++;
error_if_finished(file, tokens, index);
}
}
(*index)++;
return parsedValue;
}
void free_dictionary_entry(void *ptr) {
ParsedDictionaryEntry *entry = ptr;
free_parsed(entry->key);
free(entry->key);
free_parsed(entry->value);
free(entry->value);
}
void free_parsed_dictionary(void *ptr) {
ParsedValue *parsedValue = ptr;
DArray *parsed_dictionary = parsedValue->data;
darray_free(parsed_dictionary, free_dictionary_entry);
free(parsedValue->data);
}

View File

@@ -0,0 +1,16 @@
#ifndef DICTIONARY_H
#define DICTIONARY_H
#include "../../lexer/token.h" // for Token
#include "../parser.h"
typedef struct {
ParsedValue * key;
ParsedValue * value;
} ParsedDictionaryEntry;
ParsedValue *parse_dictionary(char *file, DArray *tokens,
size_t *index);
void free_parsed_dictionary(void *ptr);
#endif // DICTIONARY_H

View File

@@ -1,3 +1,4 @@
#include "list.h"
#include "../../lexer/token.h"
#include "../../memory.h"
#include "../parser.h"
@@ -12,6 +13,7 @@ ParsedValue *parse_list(char *file, DArray *tokens, size_t *index) {
parsedValue->data = list;
darray_init(list, sizeof(ParsedValue));
(*index)++;
skip_newlines_and_indents(tokens, index);
error_if_finished(file, tokens, index);
Token *token = darray_get(tokens, *index);
if (token->type != TOKEN_RBRACKET) {

View File

@@ -6,6 +6,7 @@
#include "assignable/call/call.h"
#include "assignable/identifier/identifier.h"
#include "declaration/declaration.h"
#include "dictionary/dictionary.h"
#include "dowrap/dowrap.h"
#include "if/if.h"
#include "list/list.h"
@@ -21,9 +22,9 @@
#include <string.h>
const char *ValueTypeNames[] = {
"string", "assign", "identifier", "number", "if statement",
"access", "call", "declaration", "null", "boolean",
"do wrap", "operations", "list"};
"string", "assign", "identifier", "number", "if statement",
"access", "call", "declaration", "null", "boolean",
"do wrap", "operations", "list", "dictionary"};
void error_if_finished(char *file, DArray *tokens, size_t *index) {
if ((*index) >= tokens->size) {
@@ -114,6 +115,9 @@ ParsedValue *parse_token_full(char *file, DArray *tokens, size_t *index,
case TOKEN_LBRACKET:
output = parse_list(file, tokens, index);
break;
case TOKEN_LBRACE:
output = parse_dictionary(file, tokens, index);
break;
default:
fprintf(stderr, "%s:%zu:%zu error: syntax error\n", file, token->line,
token->column);
@@ -139,6 +143,7 @@ ParsedValue *parse_token_full(char *file, DArray *tokens, size_t *index,
output = parse_call(file, tokens, index, output);
break;
case TOKEN_DOT:
case TOKEN_LBRACKET:
output = parse_access(file, tokens, index, output);
break;
SWITCH_OPERATIONS
@@ -183,6 +188,10 @@ void parser(char *file, DArray *parsed, DArray *tokens, bool inline_flag) {
}
void free_parsed(void *ptr) {
if (!ptr) {
fprintf(stderr, "panic: freeing NULL pointer\n");
exit(EXIT_FAILURE);
}
ParsedValue *parsed = ptr;
switch (parsed->type) {
case AST_IDENTIFIER:
@@ -219,5 +228,8 @@ void free_parsed(void *ptr) {
case AST_LIST:
free_parsed_list(parsed);
break;
case AST_DICTIONARY:
free_parsed_dictionary(parsed);
break;
}
}

View File

@@ -38,7 +38,8 @@ typedef enum {
AST_BOOLEAN,
AST_DOWRAP,
AST_OPERATION,
AST_LIST
AST_LIST,
AST_DICTIONARY
} ValueType;
extern const char* ValueTypeNames[];