Skip to content

Commit

Permalink
map: Check for duplicate key in literal
Browse files Browse the repository at this point in the history
  • Loading branch information
obiwac committed Oct 2, 2024
1 parent d5e48bd commit 8aad00c
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion flamingo/grammar/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static int parse_map(flamingo_t* flamingo, TSNode node, flamingo_val_t** val) {
vals = realloc(vals, count * sizeof *vals);
assert(vals != NULL);

// Parse key and value expressions.
// Parse key expression.

flamingo_val_t* k = NULL;

Expand All @@ -49,6 +49,19 @@ static int parse_map(flamingo_t* flamingo, TSNode node, flamingo_val_t** val) {
return -1;
}

// Make sure key doesn't already exist in map.

for (size_t j = 0; j < count - 1; j++) {
if (val_eq(keys[j], k)) {
free(keys);
free(vals);

return error(flamingo, "duplicate key in map");
}
}

// Parse value expression.

flamingo_val_t* v = NULL;

if (parse_expr(flamingo, val_node, &v, NULL) < 0) {
Expand Down

0 comments on commit 8aad00c

Please sign in to comment.