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

📄 search.cpp

📁 该程序是一个MiniPascal语言的编译器
💻 CPP
字号:
#include<string.h>

#include"list.h"
#include"global.h"
#include"keyword.h"
//**********************************************************************************************//

//						根据名字在符号表中查找名字的类型

//*********************************************************************************************//
int searchtype(char TypeName[SIZE]){//查找TypeName的类型 ,但不检查const
	if(!strcmp(TypeName,"integer")){
		return Integer;
	}
	if(!strcmp(TypeName,"boolean")){
		return Boolean;
	}
	proc_list* tempf=new proc_list;
	int i;
	for(tempf=cur_proc;tempf!=NULL;tempf=tempf->father){
		for(i=0;i<tempf->numofnewtype;i++){
			if(!strcmp(typelist[tempf->NewType[i]].name,TypeName)){
				return tempf->NewType[i];
			}
		}
	}
	return -1;
}

//根据name检查是否为可调用的过程名,或可访问的变量名
/*对于过程名的访问,同变量访问规则略有不同。
(1)不同的过程中定义的变量允许重名,但整个程序出现的程序名或过程名都不允许重名。
(2)变量访问是依据最近作用域规则,先找当前过程中所有变量,找到则匹配,若找不到,就上溯至当前过程的父过程中寻找。
     而过程访问比较复杂,因为父过程可以访问子过程,兄弟过程之间也可以后定义的访问先定义的。
所以在查找过程名的时候要沿着三个方向:
向下:查找子过程名
向上:查找父过程名
向左:查找先定义的兄弟过程名
当然,前提都是这些方向存在过程声明。
*/
int search(char name[SIZE],proc_list* pro,var_list* var){//变量访问时候查找类型名
	//成功后取地址(或value)到stack
    //	包括const常量名的检查
	//不能访问子过程定义的变量,但可以访问子过程
	int i=0;
	proc_list* tempf=new proc_list;
	proc_list* tempb=new proc_list;
    var_list* tempc=new var_list;
	tempf=cur_proc->child[0];
	for(;i<cur_proc->numofchild;i++,tempf=cur_proc->child[i]){//是否调用自己的子过程
	    if(strcmp(tempf->name,name)==0){
			pro->offset=tempf->offset;
			pro->level=tempf->level;
			strcpy(pro->name,tempf->name);
			pro->next=tempf->next;//?
			pro->numofparam=tempf->numofparam;
			pro->numofchild=tempf->numofchild;
			pro->ParamLength=tempf->ParamLength;
			pro->VarLength=tempf->VarLength;
    		return 0;//0:PROCEDURE
		}
	}
	for(tempf=cur_proc;tempf!=NULL;tempf=tempf->father){
		if(tempf->father==NULL){//if 主程序
			if(strcmp(tempf->name,name)==0){
	    	pro->offset=tempf->offset;
			pro->level=tempf->level;
			strcpy(pro->name,tempf->name);
			pro->numofchild=tempf->numofchild;
				pro->next=tempf->next;//?
				pro->numofparam=tempf->numofparam;
			pro->ParamLength=tempf->ParamLength;
			pro->VarLength=tempf->VarLength;
				return 0;
			}
		}
		if(tempf->father!=NULL){//是否调用自己或自己的兄弟过程	
			tempb=tempf->father->child[0];
	    	for(i=0;i<tempf->father->numofchild;i++,tempb=tempf->father->child[i]){
				if(strcmp(tempb->name,name)==0){
					pro->offset=tempb->offset;
		        	pro->level=tempb->level;
		        	strcpy(pro->name,tempb->name);
		        	pro->next=tempb->next;//?
		         	pro->numofparam=tempb->numofparam;
		        	pro->numofchild=tempb->numofchild;
		        	pro->ParamLength=tempb->ParamLength;
		        	pro->VarLength=tempb->VarLength;
    	        	return 0;//0:PROCEDURE
				}
		    	if(strcmp(tempb->name,tempf->name)==0){//只能调用自己的哥哥
			    	break;
				}
			}//for
		}//if
//是否为变量
		for(tempc=tempf->next;tempc->type>0;tempc=tempc->next)
		//	if((tempc->type!=Const)&&(strcmp(tempc->name,name)==0)){//新加的
	    	if(strcmp(tempc->name,name)==0){
			pro->offset=tempf->offset;
			pro->level=tempf->level;
			strcpy(pro->name,tempf->name);
			pro->numofchild=tempf->numofchild;
						pro->next=tempf->next;//?
				pro->numofparam=tempf->numofparam;
			pro->ParamLength=tempf->ParamLength;
			pro->VarLength=tempf->VarLength;
			//	pro=tempf;
			var->ConstVal=tempc->ConstVal;
			var->offset=tempc->offset;
			var->type=tempc->type;
			var->typeofconst=tempc->typeofconst;
			strcpy(var->name,tempc->name);
			//	var=tempc;
				return 1;//var
			}//if
	}
	return -1;//not found...
}
bool searchfield(char name[SIZE],type_list* mother,field_table* child){//在record的表项里查找field_table
	field_table* temp=new field_table;
//	for(temp=mother->fieldlist;temp->type!=-1;temp=temp->next)
	for(temp=mother->fieldlist;temp->type>0;temp=temp->next)
		if(strcmp(temp->name,name)==0){
			child->offset=temp->offset;
			child->type=temp->type;
			return true;
		}
	return false;
}

⌨️ 快捷键说明

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