some changes to cminus for symtab

This commit is contained in:
2025-12-01 03:35:09 +09:00
parent a9855535cb
commit 1204abb4f9
3 changed files with 173 additions and 97 deletions

View File

@@ -1,9 +1,7 @@
/****************************************************/
/* File: symtab.h */
/* Symbol table interface for the TINY compiler */
/* (allows only one symbol table) */
/* Compiler Construction: Principles and Practice */
/* Kenneth C. Louden */
/* Symbol table interface for the CMINUS COMPILER */
/* Modified by Yenru0 */
/****************************************************/
#ifndef _SYMTAB_H_
@@ -11,22 +9,116 @@
#include "globals.h"
/* Procedure st_insert inserts line numbers and
* memory locations into the symbol table
* loc = memory location is inserted only the
* first time, otherwise ignored
*/
void st_insert( char * name, int lineno, int loc );
/* SYMTAB_SIZE is the size of the hash table */
#define SYMTAB_SIZE 211
/* Function st_lookup returns the memory
* location of a variable or -1 if not found
/* the list of line numbers of the source
* code in which a variable is referenced
*/
int st_lookup ( char * name );
typedef struct LineListRec {
int lineno;
struct LineListRec *next;
} *LineList;
/* Procedure printSymTab prints a formatted
/* The record in the bucket lists for
* each variable, including name,
* assigned memory location, and
* the list of line numbers in which
* it appears in the source code
*/
typedef struct BucketListRec {
char *name;
TreeNode *treeNode;
LineList lines;
ExpType type;
int memloc; /* memory location for variable */
struct BucketListRec *next;
} *BucketList;
typedef struct ScopeListRec {
char *name;
int depth;
struct ScopeListRec *parent;
BucketList hashTable[SYMTAB_SIZE];
} *Scope;
Scope scope_global;
static Scope scope_list[SYMTAB_SIZE];
static int size_list = 0;
static Scope scope_stack[SYMTAB_SIZE];
static int top_stack = -1;
/**
* create a new scope with given name
* @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
*/
void pop_scope(void);
/**
* push a scope into the scope stack
* @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
* @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
* @param name name of the variable
* @param type type of the variable
* @param treeNode syntax tree node
* @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 *scope_name,
char *name,
ExpType type,
TreeNode *treeNode,
int loc);
/**
* lookup a variable in the current scope
* @param name name of the variable to lookup
* @return the bucket list entry of the variable or NULL if not found
*/
BucketList st_lookup_current(char *name);
/**
* lookup a variable from the given scope
* @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
*/
void printSymTab(FILE * listing);
void printSymTab(FILE *listing);
#endif