diff --git a/src/parser/dowrap/dowrap.c b/src/parser/dowrap/dowrap.c index bf753ec..940d53c 100644 --- a/src/parser/dowrap/dowrap.c +++ b/src/parser/dowrap/dowrap.c @@ -1,3 +1,4 @@ +#include "dowrap.h" #include "../../lexer/token.h" #include "../../memory.h" #include "../parser.h" @@ -101,4 +102,11 @@ ParsedValue *parse_dowrap(char *file, DArray *tokens, size_t *index) { darray_free(&to_free, free_string_dowrap); return parsedValue; +} + +void free_dowrap(void *ptr) { + ParsedValue *parsedValue = ptr; + DArray *parsed = parsedValue->data; + darray_free(parsed, free_parsed); + free(parsed); } \ No newline at end of file diff --git a/src/parser/if/if.c b/src/parser/if/if.c index 9bbf038..b241ff8 100644 --- a/src/parser/if/if.c +++ b/src/parser/if/if.c @@ -1,13 +1,12 @@ #include "if.h" #include "../../lexer/token.h" +#include "../../memory.h" #include "../parser.h" #include #include #include -#include "../../memory.h" -ParsedValue *parse_if(char *file, DArray *tokens, - size_t *index) { +ParsedValue *parse_if(char *file, DArray *tokens, size_t *index) { (*index)++; error_if_finished(file, tokens, index); @@ -24,7 +23,8 @@ ParsedValue *parse_if(char *file, DArray *tokens, if (token->type != TOKEN_NEW_LINE) break; // no more branches (*index)++; - error_if_finished(file, tokens, index); + if ((*index) >= tokens->size) + break; token = darray_get(tokens, *index); if (token->type == TOKEN_ELSE || token->type == TOKEN_ELSE_IF) { @@ -41,9 +41,8 @@ ParsedValue *parse_if(char *file, DArray *tokens, // Parse ( condition ) token = darray_get(tokens, *index); if (token->type != TOKEN_LPAREN) { - fprintf(stderr, - "%s:%zu:%zu error: expected '(' after if\n", - file, token->line, token->column); + fprintf(stderr, "%s:%zu:%zu error: expected '(' after if\n", file, + token->line, token->column); exit(EXIT_FAILURE); } @@ -66,8 +65,7 @@ ParsedValue *parse_if(char *file, DArray *tokens, } if (token->type != TOKEN_RPAREN) { - fprintf(stderr, - "%s:%zu:%zu error: missing closing ')' in condition\n", + fprintf(stderr, "%s:%zu:%zu error: missing closing ')' in condition\n", file, token->line, token->column); exit(EXIT_FAILURE); } @@ -77,20 +75,19 @@ ParsedValue *parse_if(char *file, DArray *tokens, } // Parse the body - ParsedValue *parsed_content = - parse_token(file, tokens, index, false); + ParsedValue *parsed_content = parse_token(file, tokens, index, false); if (!parsed_content) { - fprintf(stderr, - "%s:%zu:%zu error: expected body after condition\n", - file, token->line, token->column); + fprintf(stderr, "%s:%zu:%zu error: expected body after condition\n", file, + token->line, token->column); exit(EXIT_FAILURE); } ParsedConditional conditional = {condition, parsed_content}; darray_push(parsed_if, &conditional); - expect_conditional = false; // After first iteration, expect newline + else/else if + expect_conditional = + false; // After first iteration, expect newline + else/else if } ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue)); diff --git a/src/parser/parser.c b/src/parser/parser.c index 51251b1..c27e133 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -187,5 +187,8 @@ void free_parsed(void *ptr) { case AST_IF: free_parsed_if(parsed); break; + case AST_DOWRAP: + free_dowrap(parsed); + break; } } \ No newline at end of file diff --git a/test.ar b/test.ar index 0256c80..9abd61f 100644 --- a/test.ar +++ b/test.ar @@ -5,7 +5,9 @@ let a, temp_result, compute_area(radius) = 3.1415, identity(x) = x, - f(x), + f(x)=do + term.log("hello world") + , g(y, z), result, z = 0, @@ -23,4 +25,5 @@ let a, -if (x) term.log("hello world") \ No newline at end of file +if (x) term.log("hello world") +else term.log("bruh") \ No newline at end of file