Skip to content

Commit

Permalink
flamingo: Update
Browse files Browse the repository at this point in the history
  • Loading branch information
obiwac committed Jan 17, 2025
1 parent 6905934 commit 138e13e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/flamingo/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static inline int parse_unary_expr(flamingo_t* flamingo, TSNode node, flamingo_v
static inline int parse_binary_expr(flamingo_t* flamingo, TSNode node, flamingo_val_t** val);
static inline int access_find_var(flamingo_t* flamingo, TSNode node, flamingo_var_t** var, flamingo_val_t** accessed_val);
static inline int parse_access(flamingo_t* flamingo, TSNode node, flamingo_val_t** val, flamingo_val_t** accessed_val);
static inline int parse_index(flamingo_t* flamingo, TSNode node, flamingo_val_t** val);
static inline int parse_index(flamingo_t* flamingo, TSNode node, flamingo_val_t** val, bool lhs);
static inline int parse_statement(flamingo_t* flamingo, TSNode node);
static inline int parse_block(flamingo_t* flamingo, TSNode node, flamingo_scope_t** inner_scope);
static inline int parse_print(flamingo_t* flamingo, TSNode node);
Expand Down
3 changes: 1 addition & 2 deletions src/flamingo/grammar/assignment.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "expr.h"

#include "../common.h"
#include "../scope.h"
#include "../val.h"
#include "../var.h"

Expand Down Expand Up @@ -59,7 +58,7 @@ static int parse_assignment(flamingo_t* flamingo, TSNode node) {
}

else if (strcmp(left_type, "index") == 0) {
if (parse_index(flamingo, left_node, &val) < 0) {
if (parse_index(flamingo, left_node, &val, true) < 0) {
return -1;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/flamingo/grammar/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static int parse_expr(flamingo_t* flamingo, TSNode node, flamingo_val_t** val, f
}

if (strcmp(type, "index") == 0) {
return parse_index(flamingo, child, val);
return parse_index(flamingo, child, val, false);
}

return error(flamingo, "unknown expression type: %s", type);
Expand Down
26 changes: 23 additions & 3 deletions src/flamingo/grammar/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
#include "expr.h"

#include "../common.h"
#include "../scope.h"

static int parse_index(flamingo_t* flamingo, TSNode node, flamingo_val_t** val) {
static int parse_index(flamingo_t* flamingo, TSNode node, flamingo_val_t** val, bool lhs) {
assert(strcmp(ts_node_type(node), "index") == 0);

// Get indexed expression.
Expand Down Expand Up @@ -99,7 +98,7 @@ static int parse_index(flamingo_t* flamingo, TSNode node, flamingo_val_t** val)
}

else if (is_map) {
size_t const indexed_count = indexed_val->map.count;
size_t indexed_count = indexed_val->map.count;
flamingo_val_t** const indexed_keys = indexed_val->map.keys;
flamingo_val_t** const indexed_vals = indexed_val->map.vals;

Expand All @@ -116,9 +115,30 @@ static int parse_index(flamingo_t* flamingo, TSNode node, flamingo_val_t** val)
}
}

// No key found.
// Create a new value.

*val = val_alloc();
(*val)->kind = FLAMINGO_VAL_KIND_NONE;

// Add that new entry to the map if we're on the LHS.

if (lhs) {
indexed_count = ++indexed_val->map.count;

indexed_val->map.keys = realloc(indexed_val->map.keys, indexed_count * sizeof *indexed_val->map.keys);
assert(indexed_val->map.keys != NULL);

indexed_val->map.vals = realloc(indexed_val->map.vals, indexed_count * sizeof *indexed_val->map.vals);
assert(indexed_val->map.vals != NULL);

indexed_val->map.keys[indexed_count - 1] = index_val;
indexed_val->map.vals[indexed_count - 1] = *val;

val_incref(index_val);
val_incref(*val);
}

return 0;
}

Expand Down

0 comments on commit 138e13e

Please sign in to comment.