Purple  0.1
Standard Language Specification
Functions
symbol_table.c File Reference

Logic for global and local symbol tables. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "translate/symbol_table.h"
#include "utils/hash.h"
#include "utils/logging.h"
Include dependency graph for symbol_table.c:

Functions

SymbolTableStacknew_symbol_table_stack (void)
 Create a new Symbol Table Stack. More...
 
SymbolTableStacknew_nonempty_symbol_table_stack (void)
 Create a new Symbol Table Stack with one empty Symbol Table at the bottom. More...
 
void free_symbol_table_stack (SymbolTableStack *stack)
 Free all memory in a Symbol Table Stack. More...
 
void push_symbol_table (SymbolTableStack *stack)
 Push new empty Symbol Table onto Symbol Table Stack. More...
 
void push_existing_symbol_table (SymbolTableStack *stack, SymbolTable *new_table)
 Push existing Symbol Table onto Symbol Table Stack. More...
 
SymbolTablepop_symbol_table (SymbolTableStack *stack)
 Remove Symbol Table from Symbol Table Stack and return its pointer. More...
 
void pop_and_free_symbol_table (SymbolTableStack *stack)
 Remove Symbol Table from Symbol Table Stack and free its memory. More...
 
SymbolTablepeek_symbol_table (SymbolTableStack *stack)
 Get pointer of the top Symbol Table from Symbol Table Stack. More...
 
SymbolTablenew_symbol_table (void)
 Get pointer to empty Symbol Table with length SYMBOL_TABLE_DEFAULT_LENGTH. More...
 
SymbolTablenew_symbol_table_with_length (int length)
 Get pointer to empty Symbol Table with a custom length. More...
 
void resize_symbol_table (SymbolTable *table)
 Double the size of a Symbol Table's buckets array. More...
 
SymbolTableEntryfind_symbol_table_entry (SymbolTable *table, char *symbol_name)
 Find the entry of a symbol in the provided Symbol Table if it exists. More...
 
SymbolTableEntryfind_symbol_table_stack_entry (SymbolTableStack *stack, char *symbol_name)
 Find the entry of a symbol in the provided Symbol Table Stack if it exists, working from the top of the stack to the bottom. More...
 
SymbolTableEntrynew_symbol_table_entry (char *symbol_name)
 Get pointer to new Symbol Table Entry. More...
 
SymbolTableEntryadd_symbol_table_entry (SymbolTable *table, char *symbol_name, Type type)
 Hash symbol_name with hash function FNV-1 and put it into the chained Symbol Table. More...
 

Detailed Description

Logic for global and local symbol tables.

Author
Charles Averill
Date
16-Sep-2022

Function Documentation

◆ add_symbol_table_entry()

SymbolTableEntry * add_symbol_table_entry ( SymbolTable table,
char *  symbol_name,
Type  type 
)

Hash symbol_name with hash function FNV-1 and put it into the chained Symbol Table.

Parameters
tableTable to put new Symbol Table Entry into
symbol_nameName of symbol to add
typeType of symbol to add
Returns
SymbolTableEntry* Pointer to new Symbol Table Entry

◆ find_symbol_table_entry()

SymbolTableEntry * find_symbol_table_entry ( SymbolTable table,
char *  symbol_name 
)

Find the entry of a symbol in the provided Symbol Table if it exists.

Parameters
tableTable to search in
symbol_nameName of symbol to search for
Returns
SymbolTableEntry* Pointer to entry if it exists, else NULL

◆ find_symbol_table_stack_entry()

SymbolTableEntry * find_symbol_table_stack_entry ( SymbolTableStack stack,
char *  symbol_name 
)

Find the entry of a symbol in the provided Symbol Table Stack if it exists, working from the top of the stack to the bottom.

Parameters
stackSymbol Table Stack to search
symbol_nameName of symbol to find
Returns
SymbolTableEntry* Pointer to entry if it exists, else NULL

◆ free_symbol_table_stack()

void free_symbol_table_stack ( SymbolTableStack stack)

Free all memory in a Symbol Table Stack.

◆ new_nonempty_symbol_table_stack()

SymbolTableStack * new_nonempty_symbol_table_stack ( void  )

Create a new Symbol Table Stack with one empty Symbol Table at the bottom.

Returns
SymbolTableStack* Pointer to new non-empty Symbol Table Stack

◆ new_symbol_table()

SymbolTable * new_symbol_table ( void  )

Get pointer to empty Symbol Table with length SYMBOL_TABLE_DEFAULT_LENGTH.

Returns
SymbolTable* Pointer to new empty Symbol Table

◆ new_symbol_table_entry()

SymbolTableEntry * new_symbol_table_entry ( char *  symbol_name)

Get pointer to new Symbol Table Entry.

Parameters
symbol_nameName of new symbol
Returns
SymbolTableEntry* Pointer to new Symbol Table Entry

◆ new_symbol_table_stack()

SymbolTableStack * new_symbol_table_stack ( void  )

Create a new Symbol Table Stack.

Returns
SymbolTableStack* Pointer to new empty Symbol Table Stack

◆ new_symbol_table_with_length()

SymbolTable * new_symbol_table_with_length ( int  length)

Get pointer to empty Symbol Table with a custom length.

Returns
SymbolTable* Pointer to new empty Symbol Table

◆ peek_symbol_table()

SymbolTable * peek_symbol_table ( SymbolTableStack stack)

Get pointer of the top Symbol Table from Symbol Table Stack.

Parameters
stackStack to ppek at
Returns
SymbolTable* Pointer to peeked Symbol Table

◆ pop_and_free_symbol_table()

void pop_and_free_symbol_table ( SymbolTableStack stack)

Remove Symbol Table from Symbol Table Stack and free its memory.

Parameters
stackStack to pop from

◆ pop_symbol_table()

SymbolTable * pop_symbol_table ( SymbolTableStack stack)

Remove Symbol Table from Symbol Table Stack and return its pointer.

Parameters
stackStack to pop from
Returns
SymbolTable* Pointer to popped Symbol Table

◆ push_existing_symbol_table()

void push_existing_symbol_table ( SymbolTableStack stack,
SymbolTable new_table 
)

Push existing Symbol Table onto Symbol Table Stack.

Parameters
stackSymbol Table Stack to push existing table onto
new_tableNew table to push onto stack

◆ push_symbol_table()

void push_symbol_table ( SymbolTableStack stack)

Push new empty Symbol Table onto Symbol Table Stack.

Parameters
stackSymbol Table Stack to push new table onto

◆ resize_symbol_table()

void resize_symbol_table ( SymbolTable table)

Double the size of a Symbol Table's buckets array.

Parameters
tableTable to double the size of