fix invalid syntax not being called on an invalid assignment

This commit is contained in:
2025-06-02 00:13:24 +01:00
parent d2518afb8e
commit e4c2af3cc7
7 changed files with 131 additions and 569 deletions

View File

@@ -3,8 +3,8 @@
{ {
"name": "Linux", "name": "Linux",
"compilerPath": "/usr/bin/clang", "compilerPath": "/usr/bin/clang",
"cStandard": "c17", "cStandard": "c99",
"cppStandard": "c++17", "cppStandard": "c++99",
"intelliSenseMode": "linux-clang-x64", "intelliSenseMode": "linux-clang-x64",
"includePath": [ "includePath": [
"${workspaceFolder}/**" "${workspaceFolder}/**"

View File

@@ -1,13 +1,14 @@
#include "darray.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "darray.h"
void darray_init(DArray *arr, size_t element_size) { void darray_init(DArray *arr, size_t element_size) {
arr->element_size = element_size; arr->element_size = element_size;
arr->size = 0; arr->size = 0;
arr->capacity = CHUNK_SIZE; arr->capacity = CHUNK_SIZE;
arr->data = malloc(CHUNK_SIZE * element_size); arr->data = malloc(CHUNK_SIZE * element_size);
arr->resizable = true;
if (!arr->data) { if (!arr->data) {
fprintf(stderr, "darray_init: allocation failed\n"); fprintf(stderr, "darray_init: allocation failed\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@@ -15,6 +16,10 @@ void darray_init(DArray *arr, size_t element_size) {
} }
void darray_resize(DArray *arr, size_t new_size) { void darray_resize(DArray *arr, size_t new_size) {
if (!arr->resizable) {
fprintf(stderr, "darray_resize: unresizable darray\n");
exit(EXIT_FAILURE);
}
size_t new_capacity = ((new_size + CHUNK_SIZE) / CHUNK_SIZE) * CHUNK_SIZE; size_t new_capacity = ((new_size + CHUNK_SIZE) / CHUNK_SIZE) * CHUNK_SIZE;
if (new_capacity != arr->capacity) { if (new_capacity != arr->capacity) {
void *new_data = realloc(arr->data, new_capacity * arr->element_size); void *new_data = realloc(arr->data, new_capacity * arr->element_size);
@@ -30,24 +35,30 @@ void darray_resize(DArray *arr, size_t new_size) {
} }
void darray_push(DArray *arr, void *element) { void darray_push(DArray *arr, void *element) {
if (!arr->resizable) {
fprintf(stderr, "darray_resize: unresizable darray\n");
exit(EXIT_FAILURE);
}
if (arr->size >= arr->capacity) { if (arr->size >= arr->capacity) {
darray_resize(arr, arr->size + 1); darray_resize(arr, arr->size + 1);
} else { } else {
arr->size++; arr->size++;
} }
void *target = (void *)arr->data + (arr->size - 1) * arr->element_size; void *target = (char *)arr->data + (arr->size - 1) * arr->element_size;
memcpy(target, element, arr->element_size); memcpy(target, element, arr->element_size);
} }
void darray_pop(DArray *arr, void (*free_data)(void *)) { void darray_pop(DArray *arr, void (*free_data)(void *)) {
if (!arr->resizable) {
fprintf(stderr, "darray_resize: unresizable darray\n");
exit(EXIT_FAILURE);
}
if (arr->size == 0) if (arr->size == 0)
return; return;
arr->size--;
if (free_data) { if (free_data) {
void *target = (void *)arr->data + arr->size * arr->element_size; void *target = (char *)arr->data + (arr->size-1) * arr->element_size;
free_data(target); free_data(target);
} }
@@ -59,13 +70,34 @@ void *darray_get(DArray *arr, size_t index) {
fprintf(stderr, "darray_get: index out of bounds\n"); fprintf(stderr, "darray_get: index out of bounds\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return (void *)arr->data + index * arr->element_size; return (char *)arr->data + index * arr->element_size;
}
DArray darray_slice(DArray *arr, size_t start, size_t end) {
if (start > end || end > arr->size) {
fprintf(stderr, "darray_slice: invalid slice range\n");
exit(EXIT_FAILURE);
}
DArray slice;
slice.data = (char *)arr->data + start * arr->element_size;
slice.size = (end - start);
slice.element_size = arr->element_size;
slice.capacity = ((slice.size + CHUNK_SIZE) / CHUNK_SIZE) * CHUNK_SIZE;
slice.resizable = false;
return slice;
} }
void darray_free(DArray *arr, void (*free_data)(void *)) { void darray_free(DArray *arr, void (*free_data)(void *)) {
if (!arr->resizable) {
// It's a view/slice — don't free
return;
}
if (free_data) { if (free_data) {
for (size_t i = 0; i < arr->size; ++i) { for (size_t i = 0; i < arr->size; ++i) {
void *element = (void *)arr->data + i * arr->element_size; void *element = (char *)arr->data + i * arr->element_size;
free_data(element); free_data(element);
} }
} }

View File

@@ -1,6 +1,7 @@
#ifndef DARRAY_H #ifndef DARRAY_H
#define DARRAY_H #define DARRAY_H
#include <stdbool.h>
#include <stddef.h> // for size_t #include <stddef.h> // for size_t
#define CHUNK_SIZE 16 #define CHUNK_SIZE 16
@@ -10,6 +11,7 @@ typedef struct {
size_t element_size; size_t element_size;
size_t size; size_t size;
size_t capacity; size_t capacity;
bool resizable;
} DArray; } DArray;
// Initializes the dynamic_array // Initializes the dynamic_array
@@ -30,4 +32,6 @@ void darray_free(DArray *arr, void (*free_data)(void *));
// Resizes the array to a new size (internal use, but exposed) // Resizes the array to a new size (internal use, but exposed)
void darray_resize(DArray *arr, size_t new_size); void darray_resize(DArray *arr, size_t new_size);
DArray darray_slice(DArray *arr, size_t start, size_t end);
#endif // DARRAY_H #endif // DARRAY_H

View File

@@ -8,26 +8,20 @@
ParsedValue *parse_assign(char *file, DArray *parsed, DArray *tokens, ParsedValue *parse_assign(char *file, DArray *parsed, DArray *tokens,
ParsedValue *assign_to, size_t *index) { ParsedValue *assign_to, size_t *index) {
Token *token = darray_get(tokens, *index); Token *token = darray_get(tokens, *index);
switch (assign_to->type) {
case AST_IDENTIFIER:
case AST_ASSIGN:
break;
default:
fprintf(stderr, "%s:%u:%u error: can't assign to %s\n", file, token->line,
token->column, ValueTypeNames[assign_to->type]);
exit(EXIT_FAILURE);
}
ParsedAssign *assign = malloc(sizeof(ParsedAssign)); ParsedAssign *assign = malloc(sizeof(ParsedAssign));
assign->to = assign_to; assign->to = assign_to;
assign->type = token->type; assign->type = token->type;
(*index)++; (*index)++;
token = darray_get(tokens, *index); token = darray_get(tokens, *index);
switch (token->type) {
case TOKEN_ASSIGN:
case TOKEN_ASSIGN_CARET:
case TOKEN_ASSIGN_FLOORDIV:
case TOKEN_ASSIGN_MINUS:
case TOKEN_ASSIGN_MODULO:
case TOKEN_ASSIGN_PLUS:
case TOKEN_ASSIGN_SLASH:
case TOKEN_ASSIGN_STAR:
fprintf(stderr, "%s:%u:%u error: invalid syntax\n", file, token->line,
token->column);
exit(EXIT_FAILURE);
default:
break;
}
assign->from = parse_token(file, parsed, tokens, index, true); assign->from = parse_token(file, parsed, tokens, index, true);
ParsedValue *parsedValue = malloc(sizeof(ParsedValue)); ParsedValue *parsedValue = malloc(sizeof(ParsedValue));
parsedValue->type = AST_ASSIGN; parsedValue->type = AST_ASSIGN;

View File

@@ -10,6 +10,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
const char *ValueTypeNames[] = {"string", "assign", "identifier"};
ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens, ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
size_t *index, bool inline_flag) { size_t *index, bool inline_flag) {
Token *token = darray_get(tokens, *index); Token *token = darray_get(tokens, *index);
@@ -34,6 +36,26 @@ ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
fprintf(stderr, "%s:%u:%u error: invalid indentation\n", file, token->line, fprintf(stderr, "%s:%u:%u error: invalid indentation\n", file, token->line,
token->column); token->column);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
case TOKEN_IDENTIFIER:;
ParsedValue *assign_to = parse_identifier(token);
(*index)++;
if (*index >= tokens->size)
return assign_to;
token = darray_get(tokens, *index);
switch (token->type) {
case TOKEN_ASSIGN:
case TOKEN_ASSIGN_CARET:
case TOKEN_ASSIGN_FLOORDIV:
case TOKEN_ASSIGN_MINUS:
case TOKEN_ASSIGN_MODULO:
case TOKEN_ASSIGN_PLUS:
case TOKEN_ASSIGN_SLASH:
case TOKEN_ASSIGN_STAR:;
DArray slice = darray_slice(parsed, parsed->size, parsed->size);
return parse_assign(file, &slice, tokens, assign_to, index);
default:
return assign_to;
}
case TOKEN_ASSIGN: case TOKEN_ASSIGN:
case TOKEN_ASSIGN_CARET: case TOKEN_ASSIGN_CARET:
case TOKEN_ASSIGN_FLOORDIV: case TOKEN_ASSIGN_FLOORDIV:
@@ -42,19 +64,9 @@ ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
case TOKEN_ASSIGN_PLUS: case TOKEN_ASSIGN_PLUS:
case TOKEN_ASSIGN_SLASH: case TOKEN_ASSIGN_SLASH:
case TOKEN_ASSIGN_STAR: case TOKEN_ASSIGN_STAR:
if (parsed->size == 0) {
fprintf(stderr, "%s:%u:%u error: syntax error\n", file, token->line, fprintf(stderr, "%s:%u:%u error: syntax error\n", file, token->line,
token->column); token->column);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
ParsedValue *assign_to = malloc(sizeof(ParsedValue));
memcpy(assign_to, darray_get(parsed, parsed->size - 1),
sizeof(ParsedValue));
darray_resize(parsed, parsed->size - 1);
return parse_assign(file, parsed, tokens, assign_to, index);
case TOKEN_IDENTIFIER:
(*index)++;
return parse_identifier(token);
default: default:
fprintf(stderr, "Panic: unreachable\n"); fprintf(stderr, "Panic: unreachable\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

View File

@@ -10,9 +10,11 @@ typedef struct LinkedList LinkedList;
typedef enum { typedef enum {
AST_STRING, AST_STRING,
AST_ASSIGN, AST_ASSIGN,
AST_IDENTIFIER, AST_IDENTIFIER
} ValueType; } ValueType;
extern const char* ValueTypeNames[];
typedef struct { typedef struct {
ValueType type; ValueType type;
void *data; void *data;

484
test.ar
View File

@@ -1,483 +1 @@
x="hello world" "hello world"=10
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
"hello world"