add list support to parser

This commit is contained in:
2025-06-11 02:08:17 +01:00
parent 55c1500453
commit a47df1ddb0
7 changed files with 85 additions and 11 deletions

View File

@@ -50,6 +50,8 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) {
token = darray_get(tokens, *index);
} else {
while ((*index) < tokens->size) {
skip_newlines_and_indents(tokens, index);
error_if_finished(file, tokens, index);
token = darray_get(tokens, *index);
if (token->type != TOKEN_IDENTIFIER) {
fprintf(stderr,
@@ -104,7 +106,7 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) {
break;
token = darray_get(tokens, *index);
if (token->type != TOKEN_COMMA) {
(*index)-=count;
(*index) -= count;
break;
}
(*index)++;

47
src/parser/list/list.c Normal file
View File

@@ -0,0 +1,47 @@
#include "../../lexer/token.h"
#include "../../memory.h"
#include "../parser.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
ParsedValue *parse_list(char *file, DArray *tokens, size_t *index) {
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
parsedValue->type = AST_LIST;
DArray *list = checked_malloc(sizeof(DArray));
parsedValue->data = list;
darray_init(list, sizeof(ParsedValue));
(*index)++;
error_if_finished(file, tokens, index);
Token *token = darray_get(tokens, *index);
if (token->type != TOKEN_RBRACKET) {
while (true) {
skip_newlines_and_indents(tokens, index);
ParsedValue *parsedValue = parse_token(file, tokens, index, true);
darray_push(list, parsedValue);
free(parsedValue);
error_if_finished(file, tokens, index);
skip_newlines_and_indents(tokens, index);
error_if_finished(file, tokens, index);
token = darray_get(tokens, *index);
if (token->type == TOKEN_RBRACKET) {
break;
} else if (token->type != TOKEN_COMMA) {
fprintf(stderr, "%s:%zu:%zu error: syntax error\n", file, token->line,
token->column);
exit(EXIT_FAILURE);
}
(*index)++;
error_if_finished(file, tokens, index);
}
}
(*index)++;
return parsedValue;
}
void free_parsed_list(void *ptr) {
ParsedValue *parsedValue = ptr;
DArray *parsed_list = parsedValue->data;
darray_free(parsed_list, free_parsed);
free(parsedValue->data);
}

11
src/parser/list/list.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef LIST_H
#define LIST_H
#include "../../lexer/token.h" // for Token
#include "../parser.h"
ParsedValue *parse_list(char *file, DArray *tokens,
size_t *index);
void free_parsed_list(void *ptr);
#endif // LIST_H

View File

@@ -8,9 +8,7 @@
ParsedValue *convert_to_operation(DArray * to_operate_on, DArray * operations) {
if (to_operate_on->size == 1) {
ParsedValue * parsedValue = malloc(sizeof(ParsedValue));
memcpy(parsedValue, darray_get(to_operate_on, 0), sizeof(ParsedValue));
return parsedValue;
return darray_get(to_operate_on, 0);
}
TokenType operation = 0;
DArray positions;

View File

@@ -6,22 +6,24 @@
#include "assignable/call/call.h"
#include "assignable/identifier/identifier.h"
#include "declaration/declaration.h"
#include "operations/operations.h"
#include "dowrap/dowrap.h"
#include "if/if.h"
#include "list/list.h"
#include "literals/literals.h"
#include "number/number.h"
#include "operations/operations.h"
#include "string/string.h"
#include <gmp.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gmp.h>
const char *ValueTypeNames[] = {
"string", "assign", "identifier", "number", "if statement", "access",
"call", "declaration", "null", "boolean", "do wrap", "operations"};
"string", "assign", "identifier", "number", "if statement",
"access", "call", "declaration", "null", "boolean",
"do wrap", "operations", "list"};
void error_if_finished(char *file, DArray *tokens, size_t *index) {
if ((*index) >= tokens->size) {
@@ -109,6 +111,9 @@ ParsedValue *parse_token_full(char *file, DArray *tokens, size_t *index,
case TOKEN_DO:
output = parse_dowrap(file, tokens, index);
break;
case TOKEN_LBRACKET:
output = parse_list(file, tokens, index);
break;
default:
fprintf(stderr, "%s:%zu:%zu error: syntax error\n", file, token->line,
token->column);
@@ -136,7 +141,7 @@ ParsedValue *parse_token_full(char *file, DArray *tokens, size_t *index,
case TOKEN_DOT:
output = parse_access(file, tokens, index, output);
break;
SWITCH_OPERATIONS
SWITCH_OPERATIONS
if (process_operations) {
output = parse_operations(file, tokens, index, output);
break;
@@ -211,5 +216,9 @@ void free_parsed(void *ptr) {
case AST_DOWRAP:
free_dowrap(parsed);
break;
case AST_LIST:
free_parsed_list(parsed);
break;
}
printf("%d\n",parsed->type);
}

View File

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