Skip to content

Commit

Permalink
"Starts with" lookup for symbols in symtab
Browse files Browse the repository at this point in the history
  • Loading branch information
BojanStipic committed Jun 24, 2019
1 parent 6668402 commit 30827ab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
13 changes: 13 additions & 0 deletions symtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ int lookup_symbol(const char *name, unsigned kind) {
return -1;
}

int lookup_starts_with(int *results, const char *name_part) {
int found_num = 0;
int i;
for(i = first_empty - 1; i > FUN_REG; i--) {
const char *symbol_name = symbol_table[i].name;
if(strstr(symbol_name, name_part) == symbol_name) {
results[found_num] = i;
++found_num;
}
}
return found_num;
}

void set_name(int index, char *name) {
if(index > -1 && index < SYMBOL_TABLE_LENGTH)
symbol_table[index].name = name;
Expand Down
8 changes: 8 additions & 0 deletions symtab.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ int insert_literal(char *str, unsigned type);
*/
int lookup_symbol(const char *name, unsigned kind);

/*
* Searches for symbols which names start with `name_part`.
* Indices of matching elements are pushed to the `results` array.
*
* Return value is number of elements found.
*/
int lookup_starts_with(int *results, const char *name_part);

// Setters and getters for element fields.
void set_name(int index, char *name);
char* get_name(int index);
Expand Down

0 comments on commit 30827ab

Please sign in to comment.