comp_dict_item_t *add_or_update_symbol_line(char *lexeme, int symbol_length, int type, int line) { // trim key quotes char *symbol = (char *) malloc((symbol_length + 1) * sizeof(char)); strncpy(symbol, lexeme, symbol_length); symbol[symbol_length] = '\0'; int index = dict_index(symbol); comp_dict_item_t *item = symbols[index]; // find key in the bucket while ((item != NULL) && ((strcmp(item->key, symbol) != 0) || (item->type != type))) { item = item->next; } // create new entry if not found if (item == NULL) { item = (comp_dict_item_t *) malloc(sizeof(comp_dict_item_t)); item->key = symbol; switch (type) { case IKS_SIMBOLO_LITERAL_INT: item->symbol.int_value = atoi(item->key); break; case IKS_SIMBOLO_LITERAL_FLOAT: item->symbol.float_value = atof(item->key); break; case IKS_SIMBOLO_LITERAL_CHAR: item->symbol.char_value = item->key[0]; break; case IKS_SIMBOLO_LITERAL_STRING: case IKS_SIMBOLO_IDENTIFICADOR: item->symbol.string_value = item->key; break; case IKS_SIMBOLO_LITERAL_BOOL: item->symbol.bool_value = !strcmp(item->key, "true"); break; } item->type = type; item->next = symbols[index]; symbols[index] = item; } // free symbol if found, since the entry already has the symbol and only the line needs update else { free(symbol); } item->last_seen = line; return item; }
void dict_delete(struct dict *dict, const char *key) { int idx = dict_index(dict, key); if (idx == -1) return; dict->buckets[idx].deleted = 1; dict->length--; }
static awlval* awlenv_lookup(awlenv* e, char* k) { int i = dict_index(e->internal_dict, k); if (i != -1) { return dict_get_at(e->internal_dict, i); } /* check parent if not found */ if (e->parent) { return awlenv_lookup(e->parent, k); } else { return awlval_err("unbound symbol '%s'", k); } }
int get_symbol_line(char *key, int type) { int index = dict_index(key); comp_dict_item_t *item = symbols[index]; while ((item != NULL) && ((strcmp(item->key, key) != 0) || (item->type != type))) { item = item->next; } if (item == NULL) { return -1; } return item->last_seen; }
int awlenv_index(awlenv* e, awlval* k) { return dict_index(e->internal_dict, k->sym); }
bool awlval_haskey_dict(awlval* x, awlval* k) { return dict_index(x->d, k->sym) != -1; }
void *dict_get(struct dict *dict, const char *key) { int idx = dict_index(dict, key); if (idx == -1) return NULL; return dict->buckets[idx].data; }