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); token = darray_get(tokens, *index);
} else { } else {
while ((*index) < tokens->size) { while ((*index) < tokens->size) {
skip_newlines_and_indents(tokens, index);
error_if_finished(file, tokens, index);
token = darray_get(tokens, *index); token = darray_get(tokens, *index);
if (token->type != TOKEN_IDENTIFIER) { if (token->type != TOKEN_IDENTIFIER) {
fprintf(stderr, fprintf(stderr,
@@ -104,7 +106,7 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) {
break; break;
token = darray_get(tokens, *index); token = darray_get(tokens, *index);
if (token->type != TOKEN_COMMA) { if (token->type != TOKEN_COMMA) {
(*index)-=count; (*index) -= count;
break; break;
} }
(*index)++; (*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) { ParsedValue *convert_to_operation(DArray * to_operate_on, DArray * operations) {
if (to_operate_on->size == 1) { if (to_operate_on->size == 1) {
ParsedValue * parsedValue = malloc(sizeof(ParsedValue)); return darray_get(to_operate_on, 0);
memcpy(parsedValue, darray_get(to_operate_on, 0), sizeof(ParsedValue));
return parsedValue;
} }
TokenType operation = 0; TokenType operation = 0;
DArray positions; DArray positions;

View File

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

View File

@@ -42,3 +42,9 @@ else term.log("bruh")
mm=1/2/4/2/4354/534/534//534//3422*404203420234+3432423324&&430234230||4320423040230423^384239423043024923%4432042304920.3432423423 mm=1/2/4/2/4354/534/534//534//3422*404203420234+3432423324&&430234230||4320423040230423^384239423043024923%4432042304920.3432423423
x = [
'hello world',
'wow',
10
]