fix seg fault in dictionary creation
This commit is contained in:
@@ -16,7 +16,6 @@
|
||||
ParsedValueReturn parse_access(char *file, DArray *tokens, size_t *index,
|
||||
ParsedValue *to_access) {
|
||||
Token *first_token = darray_get(tokens, *index);
|
||||
(*index)++;
|
||||
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
|
||||
if (first_token->type == TOKEN_DOT) {
|
||||
ParsedAccess *parsedAccess = checked_malloc(sizeof(ParsedAccess));
|
||||
@@ -24,6 +23,7 @@ ParsedValueReturn parse_access(char *file, DArray *tokens, size_t *index,
|
||||
parsedAccess->access = NULL;
|
||||
parsedValue->type = AST_ACCESS;
|
||||
parsedValue->data = parsedAccess;
|
||||
(*index)++;
|
||||
ArErr err = error_if_finished(file, tokens, index);
|
||||
if (err.exists) {
|
||||
free_parsed(parsedValue);
|
||||
|
||||
@@ -199,7 +199,7 @@ ArErr parser(char *file, DArray *parsed, DArray *tokens, bool inline_flag) {
|
||||
if (expecting_new_line) {
|
||||
Token *token = darray_get(tokens, old_index);
|
||||
return create_err(token->line, token->column, token->length, file,
|
||||
"Syntax Error", "expected new line");
|
||||
"Syntax Error", "invalid syntax");
|
||||
}
|
||||
expecting_new_line = true;
|
||||
darray_push(parsed, parsed_code.value);
|
||||
|
||||
@@ -25,26 +25,42 @@ struct hashmap_GC *createHashmap_GC() {
|
||||
t->inline_count = 0;
|
||||
return t;
|
||||
}
|
||||
void hashmap_GC_to_array(struct hashmap_GC *t, struct node_GC**array,
|
||||
size_t *array_length) {
|
||||
|
||||
static int compare_node_asc(const void *a, const void *b) {
|
||||
const struct node_GC *na = (const struct node_GC *)a;
|
||||
const struct node_GC *nb = (const struct node_GC *)b;
|
||||
|
||||
// Ascending order (smallest order first)
|
||||
if (na->order < nb->order) return -1;
|
||||
if (na->order > nb->order) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void hashmap_GC_to_array(struct hashmap_GC *t, struct node_GC **array,
|
||||
size_t *array_length) {
|
||||
size_t array_size = 8;
|
||||
*array_length = 0;
|
||||
*array = ar_alloc(array_size * sizeof(struct node_GC));
|
||||
|
||||
for (size_t i = 0; i < t->inline_count; i++) {
|
||||
if (*array_length >=array_size) {
|
||||
array_size*=2;
|
||||
*array=ar_realloc(*array, array_size * sizeof(struct node_GC));
|
||||
if (*array_length >= array_size) {
|
||||
array_size *= 2;
|
||||
*array = ar_realloc(*array, array_size * sizeof(struct node_GC));
|
||||
}
|
||||
(*array)[(*array_length)++] = t->inline_values[i];
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < t->size; i++) {
|
||||
if (!t->list[i]) continue;
|
||||
if (*array_length >=array_size) {
|
||||
array_size*=2;
|
||||
*array=ar_realloc(*array, array_size * sizeof(struct node_GC));
|
||||
if (!t->list[i])
|
||||
continue;
|
||||
if (*array_length >= array_size) {
|
||||
array_size *= 2;
|
||||
*array = ar_realloc(*array, array_size * sizeof(struct node_GC));
|
||||
}
|
||||
(*array)[(*array_length)++] = *t->list[i];
|
||||
}
|
||||
|
||||
qsort(*array, *array_length, sizeof(struct node_GC), compare_node_asc);
|
||||
}
|
||||
|
||||
void clear_hashmap_GC(struct hashmap_GC *t) {
|
||||
|
||||
@@ -189,6 +189,7 @@ size_t translate_parsed(Translated *translated, ParsedValue *parsedValue,
|
||||
|
||||
push_instruction_byte(translated, OP_LOAD_SETITEM_METHOD);
|
||||
uint8_t setitemRegister = translated->registerAssignment++;
|
||||
set_registers(translated, translated->registerAssignment);
|
||||
|
||||
push_instruction_byte(translated, OP_COPY_TO_REGISTER);
|
||||
push_instruction_byte(translated, 0);
|
||||
|
||||
2
tests/hashmap_creation_seg_fault.ar
Normal file
2
tests/hashmap_creation_seg_fault.ar
Normal file
@@ -0,0 +1,2 @@
|
||||
while (true) do
|
||||
{"z": 1, "b": 2, "c": 3, "a": 7}
|
||||
6
tests/hashmap_order.ar
Normal file
6
tests/hashmap_order.ar
Normal file
@@ -0,0 +1,6 @@
|
||||
x = {}
|
||||
x.z = 1
|
||||
x.b = 2
|
||||
x.c = 3
|
||||
x.a = 7
|
||||
term.log(x)
|
||||
Reference in New Issue
Block a user