From a47df1ddb09703fb55f933a6ff5d1ed6ca28fbd2 Mon Sep 17 00:00:00 2001 From: William Bell Date: Wed, 11 Jun 2025 02:08:17 +0100 Subject: [PATCH] add list support to parser --- src/parser/declaration/declaration.c | 4 ++- src/parser/list/list.c | 47 ++++++++++++++++++++++++++++ src/parser/list/list.h | 11 +++++++ src/parser/operations/operations.c | 4 +-- src/parser/parser.c | 19 ++++++++--- src/parser/parser.h | 3 +- test.ar | 8 ++++- 7 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 src/parser/list/list.c create mode 100644 src/parser/list/list.h diff --git a/src/parser/declaration/declaration.c b/src/parser/declaration/declaration.c index b80ebcf..d21abc1 100644 --- a/src/parser/declaration/declaration.c +++ b/src/parser/declaration/declaration.c @@ -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)++; diff --git a/src/parser/list/list.c b/src/parser/list/list.c new file mode 100644 index 0000000..99fedac --- /dev/null +++ b/src/parser/list/list.c @@ -0,0 +1,47 @@ +#include "../../lexer/token.h" +#include "../../memory.h" +#include "../parser.h" +#include +#include +#include + +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); +} \ No newline at end of file diff --git a/src/parser/list/list.h b/src/parser/list/list.h new file mode 100644 index 0000000..bd1524c --- /dev/null +++ b/src/parser/list/list.h @@ -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 \ No newline at end of file diff --git a/src/parser/operations/operations.c b/src/parser/operations/operations.c index f39224c..c35a9ad 100644 --- a/src/parser/operations/operations.c +++ b/src/parser/operations/operations.c @@ -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; diff --git a/src/parser/parser.c b/src/parser/parser.c index 1062b11..c7cff51 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -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 #include #include #include #include #include -#include 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); } \ No newline at end of file diff --git a/src/parser/parser.h b/src/parser/parser.h index 3cf6b66..4979a92 100644 --- a/src/parser/parser.h +++ b/src/parser/parser.h @@ -36,7 +36,8 @@ typedef enum { AST_NULL, AST_BOOLEAN, AST_DOWRAP, - AST_OPERATION + AST_OPERATION, + AST_LIST } ValueType; extern const char* ValueTypeNames[]; diff --git a/test.ar b/test.ar index a3dbb1c..ce738b2 100644 --- a/test.ar +++ b/test.ar @@ -41,4 +41,10 @@ else term.log("bruh") -mm=1/2/4/2/4354/534/534//534//3422*404203420234+3432423324&&430234230||4320423040230423^384239423043024923%4432042304920.3432423423 \ No newline at end of file +mm=1/2/4/2/4354/534/534//534//3422*404203420234+3432423324&&430234230||4320423040230423^384239423043024923%4432042304920.3432423423 + +x = [ + 'hello world', + 'wow', + 10 +] \ No newline at end of file