semi-complete semantic
This commit is contained in:
90
src/symtab.h
90
src/symtab.h
@@ -9,16 +9,23 @@
|
||||
|
||||
#include "globals.h"
|
||||
|
||||
/* SYMTAB_SIZE is the size of the hash table */
|
||||
/**
|
||||
* it is the size of the hash table
|
||||
*/
|
||||
#define SYMTAB_SIZE 211
|
||||
|
||||
#define MAX_SCOPE_DEPTH 32
|
||||
#define MAX_SCOPE_COUNT 1557
|
||||
|
||||
#define MAX_PARAM_COUNT 13
|
||||
|
||||
/* the list of line numbers of the source
|
||||
* code in which a variable is referenced
|
||||
*/
|
||||
typedef struct LineListEntry {
|
||||
int lineno;
|
||||
struct LineListEntry *next;
|
||||
} *LineList;
|
||||
} * LineList;
|
||||
|
||||
/* The record in the bucket lists for
|
||||
* each variable, including name,
|
||||
@@ -29,61 +36,80 @@ typedef struct LineListEntry {
|
||||
typedef struct BucketListEntry {
|
||||
char *name;
|
||||
LineList lines;
|
||||
SymbolKind symbolKind;
|
||||
|
||||
ExpType type;
|
||||
ExpType param_types[MAX_PARAM_COUNT];
|
||||
int param_count;
|
||||
ExpType returnType;
|
||||
|
||||
int memloc; /* memory location for variable */
|
||||
struct BucketListEntry *next;
|
||||
} *BucketList;
|
||||
} * BucketList;
|
||||
|
||||
typedef struct Scope {
|
||||
struct Scope {
|
||||
char *name;
|
||||
int depth;
|
||||
|
||||
struct Scope *parent;
|
||||
struct Scope *child;
|
||||
struct Scope *child_last;
|
||||
struct Scope *next_sibling;
|
||||
|
||||
int location;
|
||||
BucketList hashTable[SYMTAB_SIZE];
|
||||
} *Scope;
|
||||
};
|
||||
|
||||
Scope scope_global;
|
||||
extern Scope scope_global;// no sibling no parent
|
||||
|
||||
static Scope scope_list[SYMTAB_SIZE];
|
||||
static int size_list = 0;
|
||||
extern Scope scope_stack[MAX_SCOPE_DEPTH];
|
||||
extern int scope_stack_top;
|
||||
|
||||
static Scope scope_stack[SYMTAB_SIZE];
|
||||
static int top_stack = -1;
|
||||
/**
|
||||
* before using the symbol table, initialize the global scope
|
||||
*/
|
||||
void st_init(void);
|
||||
|
||||
/**
|
||||
* create a new scope with given name
|
||||
* @note it does not link parent or insert into stack/list
|
||||
* @param scope_name: name of the scope
|
||||
* @return the created scope
|
||||
*/
|
||||
Scope scope_new(char *scope_name);
|
||||
/**
|
||||
* pop the current scope from the scope stack
|
||||
* pop the current scope from the scope stack
|
||||
*/
|
||||
void pop_scope(void);
|
||||
/**
|
||||
* push a scope into the scope stack
|
||||
* @note it does link the parent or siblings to construct tree
|
||||
* @param scope: the scope to be pushed
|
||||
*/
|
||||
void push_scope(Scope scope);
|
||||
/**
|
||||
* insert a scope into the scope list
|
||||
*/
|
||||
void insert_scope_to_list(Scope scope);
|
||||
/**
|
||||
* get the top of the scope stack
|
||||
* get the top of the scope stack wit
|
||||
* @return the current scope or NULL if the stack is empty
|
||||
*/
|
||||
Scope curr_scope(void);
|
||||
/**
|
||||
* insert a variable into the symbol table
|
||||
* or update a variable if it already exists
|
||||
* @param scope_name name of the scope
|
||||
* insert a variable into the symbol table of the current scope
|
||||
* or add a line number if it already exists
|
||||
* @param name name of the variable
|
||||
* @param symbolkind kind of the symbol
|
||||
* @param type type of the variable
|
||||
* @param lineno line number of the variable
|
||||
* @param loc memory location of the variable
|
||||
* @return 0 if success, -1 if failure
|
||||
*/
|
||||
int st_try_insert(char *name, ExpType type, int loc);
|
||||
BucketList st_try_insert(char *name, SymbolKind symbolkind, ExpType type, int lineno);
|
||||
|
||||
/**
|
||||
* insert a line number into the variable's line list
|
||||
* @param entry the bucket list entry of the variable
|
||||
* @param lineno the line number to be inserted
|
||||
*/
|
||||
void st_entry_insert_line(BucketList entry, int lineno);
|
||||
|
||||
/**
|
||||
* lookup a variable in the current scope
|
||||
* @param name name of the variable to lookup
|
||||
@@ -91,22 +117,24 @@ int st_try_insert(char *name, ExpType type, int loc);
|
||||
*/
|
||||
BucketList st_lookup_current(char *name);
|
||||
/**
|
||||
* lookup a variable from the given scope to root
|
||||
* lookup a variable from the top scope to root
|
||||
* @param name name of the variable to lookup
|
||||
* @return the bucket list entry of the variable or NULL if not found
|
||||
*/
|
||||
BucketList st_lookup(char *name);
|
||||
|
||||
/**
|
||||
* find a scope from the scope list
|
||||
* @param scope_name name of the scope to find
|
||||
* @return the scope or NULL if not found
|
||||
*/
|
||||
Scope find_scope(char *scope_name);
|
||||
/**
|
||||
* Procedure printSymTab prints a formatted
|
||||
* listing of the symbol table contents
|
||||
* to the listing file
|
||||
* lookup a variable from the given scope to root
|
||||
* @param name name of the variable to lookup
|
||||
* @param scope the scope to start lookup from
|
||||
* @return the bucket list entry of the variable or NULL if not found
|
||||
*/
|
||||
BucketList st_lookup_from(char *name, Scope scope);
|
||||
|
||||
void printScope(FILE *listing, Scope scope);
|
||||
|
||||
void printScopeTree(FILE *listing);
|
||||
|
||||
void printSymTab(FILE *listing);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user