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

📄 kld01dlg.cpp

📁 用EVC4.0实现的一种类似于BASIC的编程语言,可以实现基本的编程并可以执行
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CKld01Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CKld01Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CKld01Dlg::OnOK() 
{
	// TODO: Add extra validation here
	if(MessageBox("真的退出吗?","警告",MB_ICONWARNING+MB_YESNO)==IDYES)
	{
	CDialog::OnOK();
	}
	
}


void CKld01Dlg::OnOpenfile() 
{
	// TODO: Add your control notification handler code here
	UpdateData(false);
	CFileDialog dlg(TRUE,".txt",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
	   "PRG文件(*.PRG)|*.PRG|所有文件(*.*)|*.*||",this);
   if(dlg.DoModal() == IDCANCEL)
   {
	   Invalidate();
	    return;
   }
    m_edit.Empty();
   CFile fileEditText;
	CString filename=dlg.GetFileName();
	CString str;
   if(fileEditText.Open(filename,CFile::modeRead) == 0)
   {
	   str = "打开文件 " + filename + "失败!" ;
       AfxMessageBox(str);
	   return ;
   }
   else
   {
	   char cBuf[512];
	   UINT uBytesRead;
	   while(uBytesRead=fileEditText.Read(cBuf,sizeof(cBuf)-1))
	   {
		   cBuf[uBytesRead]=NULL;
		   m_edit+=CString(cBuf);
	   }
	   fileEditText.Close();
	   CString	tempname;
	   tempname.Format("%s",filename); 
	   m_open_file=tempname;
	   UpdateData(false);
   }
   Invalidate();
}

void CKld01Dlg::OnSavefile() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	CString filename,s;
	CTime t=CTime::GetCurrentTime();
	filename.Format("%d-%d-%d-%d-%d",t.GetYear(),t.GetMonth(),t.GetDay(),t.GetHour(),t.GetMinute());
		
   CFileDialog dlg(false,".PRG",filename,0,"(*.PRG)|*.PRG||",NULL);
   if(dlg.DoModal()!=IDOK)
   {
	   Invalidate();
      return;
   }
   
   CString code=m_edit;
   code.Replace("\r","");			//去掉多余的回车
   filename=dlg.GetPathName();
   CStdioFile f(filename,CFile::modeWrite|CFile::modeCreate);
   f.WriteString(code);

   Invalidate();
  
}

void CKld01Dlg::OnNew() 
{
	// TODO: Add your control notification handler code here

	m_edit.Empty();
	UpdateData(false);
}




void CKld01Dlg::OnClearf() 
{
	// TODO: Add your control notification handler code here	
	UpdateData(true);
	m_f.Empty();
	bool t=IsPrimeNum(4);
	UpdateData(false);	
}

int CKld01Dlg::currentMacroNum()
{
	return MacroTable.macmaxnum; 
}

void CKld01Dlg::OnRun() 
{
	if (RUNTTING==false)
	{
		// TODO: Add your control notification handler code here
	
	UpdateData(true);
	macdefinesymbol =0;						//执行方式为指令串方式
	JumpTable.jumpmaxnum=0;
	VarTable.varmaxnum =0;
	MacroTable.macmaxnum=0;
	MacroTable.macsymbol=-1;
	CycShed.cycmaxnum=-1; 	
	m_macronum=0;
	for (int i=0;i<defineVarNum;i++)

		AddNewVar(defineVar[i],1);			//加入内部定义变量
	
	CString hello=m_edit;
	err=0;									//错误标识初始化
	int length=hello.GetLength();			//取串长	
	int NumP=0;
	MAXNUM=0;plag=0;
	CString temp="";
	DataTemp[NumP]="";
	bool Plag=false,Plag1=false;
	for (int j=0;j<length;j++)				//扫描串
	{
		bool plag=false;
		do{									 
			if (j>=length)					//串扫描完毕
			{
				plag=true;
				break;
			}
			if (hello.GetAt(j)>64) break;	//滤掉非法字符,遇到字母为止
			
			j++;
		}while(1);
		if (plag==true) break;
		
		do
		{
			temp+=hello.GetAt(j++);
			Plag=true;
			if (j==length) break;	//是数字,命令字检测完成
			if (hello.GetAt(j)<='9'&&hello.GetAt(j)>='0'||hello.GetAt(j)==' '||hello.GetAt(j)=='-') NumP++;
		}while (!(hello.GetAt(j)<='9'||hello.GetAt(j)==' ')||hello.GetAt(j)=='-'||hello.GetAt(j)=='=');
	
		do{	
			if (j==length)
			{
				DataTemp[NumP]+=hello.GetAt(j-1);break;
			}
			if (hello.GetAt(j)!=13)
			{
				if (hello.GetAt(j)=='\"'&&plag==0)							//字符串开始,记录一个标志
				{
					plag=1;
				}
				else if (hello.GetAt(j)=='\"'&&plag==1)						//字符串结束,复位标志
				{
					plag=0;
				}
				
				DataTemp[NumP]+=hello.GetAt(j++);if (j==length) break;
				if (hello.GetAt(j)==','||hello.GetAt(j)=='='&&plag==0)  //只使用逗号作为分隔符
				{				//如果不是在字符串中遇到","  ,则作为分隔符号看待
					NumP++;
					j++;
					DataTemp[NumP]="";
				}
		
			}
		
		}while(hello.GetAt(j)!=13);
		
		int s;
		for (int t=0;t<NumP+1;t++)				//对已分析的参数后续处理
		{
			DataTemp[t].TrimLeft();
			DataTemp[t].TrimRight();			//删除前导和后导空格
			Rbt4IC[MAXNUM].PAR[t]=DataTemp[t];	//保存到参数表
			int l=DataTemp[t].GetLength();		//取参数长度
		    s=0;
			for (int b=0;b<l;b++)				//对参数扫描
			{
				bool plag3=false;
				for (int c=0;c<l;c++)
				{
					if (DataTemp[t].GetAt(c)=='x') 
					{
						plag3=true;
						b=c+1;
						break;
					}
				}
				if(plag3==true)
				{				
					for (int a=b;a<l;a++)
					{
						if (DataTemp[t].GetAt(a)>='A')
							s=s*16+DataTemp[t].GetAt(a)-55;
						else if(DataTemp[t].GetAt(a)>=0)
							s=s*16+DataTemp[t].GetAt(a)-48;
					}
					Rbt4IC[MAXNUM].N[t]=s;	
					s=0;
					break;
				}
				else
				{
					Rbt4IC[MAXNUM].N[t]=atoi(DataTemp[t]);			
					break;
				}
			}		
		}
		
		temp.MakeUpper();						//转换为大写
		Rbt4IC[MAXNUM].parlength=NumP;			//保存参数实际长度 
		Rbt4IC[MAXNUM].codename=temp;			//命令字的名字
		Rbt4IC[MAXNUM].err=0;					//错误号
		CString tt1;
		Plag1=false;
		for (int k=0;k<CodeTableLength;k++)
		{
			temp.MakeUpper(); 
			if (temp==CodeTable[k]) 
			{
				Plag1=1;
				if (!(codelength[k]==NumP||(codelength[k]<0&&NumP<=-codelength[k])))//长度<=指令长度
				{
					if (codelength[k]<NumP&&codelength[k]>0)
						
						toerr(MAXNUM,0,1);
					else
						toerr(MAXNUM,0,2);
				}
				break;
			}
		}
		Rbt4IC[MAXNUM].code=k+1;temp="";		//保存参数代码
		if (Plag1==1) 
		{
			Rbt4IC[MAXNUM].err=0;				//正确命令字
			Rbt4IC[MAXNUM].errline=-1;			
		}
	
		else
		{
			err=3;Rbt4IC[MAXNUM].err=err;		//错误代码
			errline=MAXNUM;
			Rbt4IC[MAXNUM].errline=MAXNUM;		//发生错误的行号
		
		}
		
		if (j>=length||(j<length&&hello.GetAt(j)==13))
		
		{
			Plag=false;Plag1=false;				//初始化
			for (int u=0;u<4;u++)
				DataTemp[u]="";	
			NumP=0;MAXNUM++;
			temp="";
		
		}
		 
	}
	 
	/********************************变量表建立*********************************************/
	m_varIndex.ResetContent();	
	m_var=CreateVarTable();
	
	temp.Format("%d",m_var);
	GetDlgItem(IDC_EDIT5)->SetWindowText(temp);
	GetDlgItem(IDC_EDIT2)->SetWindowText(m_f);
	/********************************标号表建立*********************************************/
	
	
	CString mytemp;
	m_f.Empty();
	m_jumpIndex.ResetContent();					//清标号表显示
	CreateJumpTable();							//扫描程序区创建标号表
	CString temp1;
	int lineno=0,Forsymbol=0;
	for (int i1=0;i1<MAXNUM;i1++)				//扫描转移和条件转移指令,以确定其后标号
	{											//是否存在
		temp1=Rbt4IC[i1].codename;
		temp1.MakeUpper();
		if (temp1=="FOR") Forsymbol++;			//FOR和NEXT配对检查,完成检查后Forsymbol不为0表示不配对
		if (temp1=="NEXT") Forsymbol--;
		MakeJumps(i1,temp1);
	}
	
	if (Forsymbol>0)							//无NEXT匹配
	{
		toerr(-1,0,18);
	}
	else if (Forsymbol<0)						//无FOR匹配
	{
		toerr(-1,0,19);
	}
	/*********************************下面完成宏定义****************************************/

	m_defMacro.ResetContent();	
	
	for (i=0;i<MAXNUM;i++)
	{
		if (Rbt4IC[i].codename=="MACRO")		//宏创建指令
		{
			findendmac=i;
			if (MacroTable.macsymbol==-1)
			{
				Rbt4IC[i].PAR[1].TrimLeft();
				
				if (CreateNewMacro(i)!=-1)		//注册宏成功
				{			
					;
					//mytemp.Format("定义%s宏,%d参数\r\n",Rbt4IC[i].PAR[1],Rbt4IC[i].parlength-1 );
					//m_f=m_f+mytemp;	
				}
				else
				{	
					toerr(i,0,4);				//失败,说明宏重复定义
				}

			}
			else
			{
				toerr(i,0,6);			
			}
		}
		else if (Rbt4IC[i].codename=="ENDMAC")
		{
			mytemp.Format("结束定义名称为%s 的宏\r\n",MacroTable.macname[MacroTable.macmaxnum] );
			//m_f=m_f+mytemp;
			
			if (MacroTable.macsymbol==0)		//宏定义状态,0表示已执行过MACRO指令
			{
				MacroTable.macsymbol=-1;		//已完成正常宏定义
				m_macronum=currentMacroNum();
				MacroTable.macend[MacroTable.macmaxnum]= i;

				m_defMacro.AddString(MacroTable.macname[MacroTable.macmaxnum]);
			}	
			else if (MacroTable.macsymbol==-1)
			{
				toerr(i,0,7);					//无MACRO匹配
			}
		}
	}
	if (MacroTable.macsymbol==0)				
	{
		toerr(findendmac,0,6);					//无ENDMAC匹配
	}
	
	temp.Format("%d",m_macronum);
	GetDlgItem(IDC_EDIT4)->SetWindowText(temp);
/***********************************宏定义结束******************************************/

	if (macdefinesymbol==true)	
		m_CommandMode="宏定义方式";
	else
		m_CommandMode="单模块方式";
	/*指令合法性处理*/
	
	int p;
	if (err!=18&&err!=19&&err!=13)
	{
		p=0;
		for (i=0;i<MAXNUM;i++)
		{
			CString temp=Rbt4IC[i].codename;			//取出该命令串
			if (Rbt4IC[i].err==3)						//不是命令字
			{
			
				if (searchMacro(temp)==-1)				//进一步进行宏指令检查
				{
					toerr(i,0,3);p=1;
				}
			}
		}
	}//报错		
	
	if (p==0) err=0;
		/*
			下面完成指令的执行
		*/

	if (err==0)											//解释无错误,允许执行程序
	{
		RUNTTING=true;
		this->GetDlgItem(IDC_RUN)->SetWindowText("正在运行");

⌨️ 快捷键说明

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