⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 symtab.c

📁 1.小型编译器 2。支持整数
💻 C
📖 第 1 页 / 共 2 页
字号:
	hashnumber = hash(name);
	pnew = (FunEntry *)malloc(sizeof(FunEntry));
	pnew->name = copyString(name);
	pnew->type.type = Integer;
	pnew->para = NULL;
	pnew->ret_val = AbsC;
	pnew->next = SysFunTable[hashnumber];
	SysFunTable[hashnumber] = pnew;

	strcpy(name, "floor");
	hashnumber = hash(name);
	pnew = (FunEntry *)malloc(sizeof(FunEntry));
	pnew->name = copyString(name);
	pnew->type.type = Double;
	pnew->para = NULL;
	pnew->ret_val = FloorC;
	pnew->next = SysFunTable[hashnumber];
	SysFunTable[hashnumber] = pnew;

	strcpy(name, "ceil");
	hashnumber = hash(name);
	pnew = (FunEntry *)malloc(sizeof(FunEntry));
	pnew->name = copyString(name);
	pnew->type.type = Double;
	pnew->para = NULL;
	pnew->ret_val = CeilC;
	pnew->next = SysFunTable[hashnumber];
	SysFunTable[hashnumber] = pnew;

	strcpy(name, "fabs");
	hashnumber = hash(name);
	pnew = (FunEntry *)malloc(sizeof(FunEntry));
	pnew->name = copyString(name);
	pnew->type.type = Double;
	pnew->para = NULL;
	pnew->ret_val = FAbsC;
	pnew->next = SysFunTable[hashnumber];
	SysFunTable[hashnumber] = pnew;

	strcpy(name, "fmod");
	hashnumber = hash(name);
	pnew = (FunEntry *)malloc(sizeof(FunEntry));
	pnew->name = copyString(name);
	pnew->type.type = Double;
	pnew->para = NULL;
	pnew->ret_val = FModC;
	pnew->next = SysFunTable[hashnumber];
	SysFunTable[hashnumber] = pnew;

	strcpy(name, "strcmp");
	hashnumber = hash(name);
	pnew = (FunEntry *)malloc(sizeof(FunEntry));
	pnew->name = copyString(name);
	pnew->type.type = Void;
	pnew->para = NULL;
	pnew->ret_val = StrCmpC;
	pnew->next = SysFunTable[hashnumber];
	SysFunTable[hashnumber] = pnew;

	strcpy(name, "strcat");
	hashnumber = hash(name);
	pnew = (FunEntry *)malloc(sizeof(FunEntry));
	pnew->name = copyString(name);
	pnew->type.type = Void;
	pnew->para = NULL;
	pnew->ret_val = StrCatC;
	pnew->next = SysFunTable[hashnumber];
	SysFunTable[hashnumber] = pnew;

	strcpy(name, "strcpy");
	hashnumber = hash(name);
	pnew = (FunEntry *)malloc(sizeof(FunEntry));
	pnew->name = copyString(name);
	pnew->type.type = Void;
	pnew->para = NULL;
	pnew->ret_val = StrCpyC;
	pnew->next = SysFunTable[hashnumber];
	SysFunTable[hashnumber] = pnew;

	printf("Insert_SysFun end\n");
	
	return;
}

/* lookup a function in the symbol table */
FunEntry * Lookup_SysFun(char * name)
{
	int hashnumber = hash(name);
	FunEntry * pEntry;

	for (pEntry = SysFunTable[hashnumber]; pEntry != NULL; pEntry = pEntry->next)
		if (strcmp(pEntry->name, name) == 0)
			return pEntry;
	return NULL;
}

StructEntry * Insert_Struct(char * name, TreeNode * pTreeNode)
{
	int hashnumber = hash(name), size = 0, nSize;
	StructEntry * pnew = (StructEntry *)malloc(sizeof(StructEntry));
	ValEntry * pfield;
	TreeNode * ptemp;

	pnew->name = copyString(name);
	pnew->field = NULL;
	//printf("name: %s\n", name);
	if (pTreeNode != NULL) {
		//printf("field name: %s\n", pTreeNode->child[0]->attr.name);
		pnew->field = (ValEntry *)malloc(sizeof(ValEntry));
		pnew->field->name = copyString(pTreeNode->child[0]->attr.name);
		pnew->field->type.type = pTreeNode->type.type;
		pnew->field->offset = size;
		
		switch (pnew->field->type.type) {
		case Integer:	nSize = sizeofint;	break;
		case Double:	nSize = sizeofdouble;	break;
		case Char:		nSize = sizeofchar;	break;
		default:		nSize = sizeofint;
		}
		if (pTreeNode->child[0]->child[0] != NULL)
			nSize = nSize * pTreeNode->child[0]->child[0]->attr.val.i;
		size += nSize;
		//printf("total size: %d field size: %d\n", size, nSize);
		
		for (ptemp = pTreeNode->sibling, pfield = pnew->field;
			ptemp != NULL; ptemp = ptemp->sibling, pfield = pfield->next) {
			//printf("field name: %s\n", ptemp->child[0]->attr.name);
			pfield->next = (ValEntry *)malloc(sizeof(ValEntry));
			pfield->next->name = copyString(ptemp->child[0]->attr.name);
			pfield->next->type.type = ptemp->type.type;
			pfield->next->offset = size;
			
			switch (pfield->next->type.type) {
			case Integer:	nSize = sizeofint;	break;
			case Double:	nSize = sizeofdouble;	break;
			case Char:		nSize = sizeofchar;	break;
			default:		nSize = sizeofint;
			}
			
			if (ptemp->child[0]->child[0] != NULL)
				nSize = nSize * ptemp->child[0]->child[0]->attr.val.i;

			size += nSize;
			//printf("total size: %d field size: %d\n", size, nSize);
		}
		pfield->next = NULL;
	}
	pnew->ret_val = size;
	//printf("record size: %d\n", size);
	pnew->next = StructTable[hashnumber];
	StructTable[hashnumber] = pnew;
	return pnew;
}

/* lookup a struct in the symbol table */
StructEntry * Lookup_Struct(char * name)
{
	int hashnumber = hash(name);
	StructEntry * pEntry;

	for (pEntry = StructTable[hashnumber]; pEntry != NULL; pEntry = pEntry->next)
		if (strcmp(pEntry->name, name) == 0)
			return pEntry;
	return NULL;
}

int Lookup_Struct_Field(StructEntry *pStruct, char *name, ValEntry * pEntry)
{
	ValEntry * ptemp;
	for (ptemp = pStruct->field; ptemp != NULL; ptemp = ptemp->next)
	if (strcmp(name, ptemp->name) == 0) {
		pEntry->name = copyString(ptemp->name);
		pEntry->type.type = ptemp->type.type;
		pEntry->offset = ptemp->offset;
		pEntry->next = NULL;
		return 1;
	}
	return 0;
}


/* procedure printFunTab prints a formatted 
 * listing of the function table contents 
 * to the listing file
 */
void printFunTab(void)
{
	int i;
	fprintf(listing, "\nFunction table:\n");
	fprintf(listing,"\nFunction Name  Type    \n");
	fprintf(listing,"-------------  ----    \n");
	for (i=0; i<SIZE; ++i) {
		FunEntry * pEntry;
		for (pEntry = FunTable[i]; pEntry != NULL; pEntry = pEntry->next) {
			ValEntry * para;
			fprintf(listing, "%-14s ", pEntry->name);
			printType(pEntry->type);
			fprintf(listing, "\nParameter\n");
			fprintf(listing, "---------\n");
			for (para = pEntry->para ; para != NULL; para = para->next) {
				fprintf(listing, "%-14s ", para->name);
				printType(para->type);
				if(para->refFlag == 1)
					fprintf(listing, " ref ");
				fprintf(listing, "  %-d\n", para->offset);				
			}
		}
	}
}

void printStructTab(void)
{
	int i;
	fprintf(listing, "\nStruct table:\n");
	fprintf(listing,"\nStruct Name  \n");
	fprintf(listing,"-------------  \n");
	for (i=0; i<SIZE; ++i) {
		StructEntry * pEntry;
		for (pEntry = StructTable[i]; pEntry != NULL; pEntry = pEntry->next) {
			ValEntry * para;
			fprintf(listing, "%-14s ", pEntry->name);
			fprintf(listing, "\nField\n");
			fprintf(listing, "---------\n");
			for (para = pEntry->field ; para != NULL; para = para->next) {
				fprintf(listing, "%-14s ", para->name);
				printType(para->type);
				fprintf(listing, "  %-d\n", para->offset);
			}
		}
	}
}


/* procedure printSymTab prints a formatted 
 * listing of the symbol table contents 
 * to the listing file
 */
void printSymTab(TreeNode * tree)
{ 
	static int Globalprinted = FALSE;
	Symtab * pTable;

	if (!Globalprinted) {
		int i;
		fprintf(listing, "\nSymbol table:\n");
		fprintf(listing, "\nNestlevel: %d\n", GlobalTable->nestlevel); 
		fprintf(listing,"Variable Name  Type && Offset\n");
		fprintf(listing,"-------------  --------------\n");
		for (i=0; i<SIZE; ++i) {
				ValEntry * pEntry;
				for (pEntry = GlobalTable->valTable[i]; pEntry != NULL; pEntry = pEntry->next) {
					fprintf(listing, "%-14s ", pEntry->name);
					printType(pEntry->type);
					fprintf(listing, "  %-d\n", pEntry->offset);
				}
		}
		Globalprinted = TRUE;
	}
	while (tree != NULL) {
		int i;
		if (tree ->nodekind == DecK && tree->kind.dec == CompK) {
			pTable = tree->attr.table;    
			fprintf(listing, "\nNestlevel: %d\n", pTable->nestlevel); 
			fprintf(listing,"Variable Name  Type && Offset\n");
			fprintf(listing,"-------------  --------------\n");
			for (i=0; i<SIZE; ++i) {
				ValEntry * pEntry;
				for (pEntry = pTable->valTable[i]; pEntry != NULL; pEntry = pEntry->next) {
					fprintf(listing, "%-14s ", pEntry->name);
					printType(pEntry->type);
					fprintf(listing, "  %-d\n", pEntry->offset);
				}
			}
		}
		for (i=0; i<MAXCHILDREN; i++)
			printSymTab(tree->child[i]);
		tree = tree->sibling;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -