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

📄 translator.java

📁 该文件为编译器解释器的代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/* 说  明 如果成功从当前行中取得非空字符,函数返回TRUE	*/
/*	  否则,函数返回FALSE			        */
/********************************************************/
boolean nonBlank()
{ 
    /* 在当前代码行in_Line中,当前字符位置inCol中为空格字符	*   
     * 在当前代码行in_Line中,当前字符位置inCol下移,略过空格	*/
    while ((inCol < lineLen) && (in_Line[inCol] == ' ') )
        inCol++;

    /* 在当前代码行in_Line中,遇到非空字符 */
    if (inCol < lineLen)
    { 
        /* 取当前字符位置inCol中的字符送入ch,		*
	 * 函数返回TRUE(已定义为1),ch中得到非空字符	*/
	ch = in_Line[inCol];
        return true; 
    }
    /* 当前代码行已经读完,将当前字符ch 赋为空格,	*
     * 函数返回FALSE(已定义为0),ch中为空格字符	*/
    else
    { 
        ch = ' ';
        return false; 
    }
} 

/****************************************************/
/* 函数名 getCh					    */
/* 功  能 字符获取函数				    */
/* 说  明 如果当前行中字符未读完,则函数返回当前字符 */
/*	  否则,函数返回空格字符			    */
/****************************************************/
void getCh()
{ 
    /* 在当前代码行in_Line中,当前字符列数inCol未超过代码行实际长度lineLen *
     * 取得当前行中当前位置的字符,送入ch		*/
    if (++inCol < lineLen)
        ch = in_Line[inCol];

    /* 如果inCol超出当前代码行长度范围,则ch赋为空格 */
    else ch = ' ';
} 

/****************************************************************/
/* 函数名 getNum						*/
/* 功  能 数值获取函数						*/
/* 说  明 将代码行中连续出现的有加减运算的数term合并计数,       */
/*        所的数值送入为num.如果成功得到数值,则函数返回TRUE;	*/
/*        否则,函数返回FALSE					*/
/****************************************************************/
boolean getNum()
{ 
    int sign;				/* 符号因子 */
    int term;				/* 用于记录当前录入的局部数值 */
    boolean temp = false;		/* 记录函数返回值,初始为假 */
    num = 0;				/* 用于记录所有加减运算后的最终数值结果 */

    do
    { 
        sign = 1;			/* 符号因子初始为1 */

        /* 调用函数nonBlank()略过当前位置的空格后,			*
         * 所得到的当前非空字符ch为+或-.(+/-的连续出现处理)	*/
        while (nonBlank() && ((ch == '+') || (ch == '-')))
        { 
            temp = false;

	    /* 当前字符ch为"-"时,符号因子sign设为-1 */
	    if(ch == '-')  
                sign = - sign;

	    /* 取当前代码行中下一字符到当前字符ch中 */
            getCh();
        }
        term = 0;		/* 当前录入的局部数值初始为0 */
        nonBlank();		/* 略过当前位置上的空格 */

	/* 当前字符ch为数字,局部数值的循环处理 */
        while (isdigit(ch))				
        { 
            temp = true;		/* 函数返回值设为TRUE,成功得到数字 */

	    /* 将字符序列转化为数值形式,进行进位累加 */
            term = term * 10 + ( (int)ch - (int)('0') );

            getCh();			/* 取当前代码行中下一字符到当前字符ch中 */

        }
	/* 将局部数值带符号累加,得到最终数值num */
        num = num + (term * sign);
    } while ((nonBlank()) && ((ch == '+') || (ch == '-')));
    return temp;
}

/****************************************************/
/* 函数名  isdigit				    */
/* 功  能  检查参数c是不是数字			    */
/* 说  明  					    */
/****************************************************/
boolean isdigit(char c)
{
    if ((c=='0')||(c=='1')||(c=='2')||(c=='3')||(c=='4')||(c=='5')||(c=='6')||        (c=='7')||(c=='8')||(c=='9'))
        return true;
    else return false;
}

/****************************************************/
/* 函数名 getWord				    */
/* 功  能 单词获取函数				    */
/* 说  明 函数从当前代码行中获取单词.如果得到字符,  */
/*	  则函数返回TRUE;否则,函数返回FALSE	    */
/****************************************************/
boolean getWord()
{ 	
    boolean temp = false;			/* 函数返回值初始为FALSE */
    int length = 0;			/* 单词长度初始为0 */
    char gword[]=new char[20];

    /* 在当前代码行中成功获取非空字符ch */
    if(nonBlank())
    {
        /* 当前非空字符ch为字母或数字 */
	while(isalpha(ch))
        {
            /* 当前单词word未超过规定字长WORDSIZE-1(为单词结束字符留一空位)	*
	     * 将当前字符ch读入到单词末尾		*/
	    if (length < WORDSIZE-1)
                gword[length++] = ch;

            getCh();			/* 取当前代码行中下一字符 */
        }
	/* 给当前单词word加入结束字符 */
        word=new String(gword);
        word=word.trim();

	/* 设置函数返回值,当读入字word非空的时候为TRUE */
        temp = (length != 0);
    }
    return temp;
} 

/****************************************************/
/* 函数名  isalpha				    */
/* 功  能  检查参数c是不是字母			    */
/* 说  明  					    */
/****************************************************/
boolean isalpha(char c)
{
    if ((c=='a')||(c=='b')||(c=='c')||(c=='d')||(c=='e')||(c=='f')||(c=='g')||        (c=='h')||(c=='i')||(c=='j')||(c=='k')||(c=='l')||(c=='m')||(c=='n')||(c=='o')||(c=='p')||(c=='q')||(c=='r')||(c=='s')||(c=='t')||(c=='u')||(c=='v')||(c=='w')||(c=='x')||(c=='y')||(c=='z'))
        return true;
    else if ((c=='A')||(c=='B')||(c=='C')||(c=='D')||(c=='E')||(c=='F')||(c=='G')||(c=='H')||(c=='I')||(c=='J')||(c=='K')||(c=='L')||(c=='M')||(c=='N')||(c=='O')||(c=='P')||(c=='Q')||(c=='R')||(c=='S')||(c=='T')||(c=='U')||(c=='V')||(c=='W')||(c=='X')||(c=='Y')||(c=='Z'))
         return true; 
    else return false;
}

/************************************************************/
/* 函数名 skipCh					    */
/* 功  能 字符空过函数					    */
/* 说  明 如果当前位置上字符为函数指定的字符,则空过该字符,  */
/*        函数返回TRUE;否则函数返回FALSE	            */
/************************************************************/
boolean skipCh(char c)
{ 
    boolean temp = false;

    /* 当前位置上字符为函数指定字符c */
    if(nonBlank() && (ch == c))
    { 
        getCh();        /* 空过当前字符c,取下一字符 */
        temp = true;	/* 空过指定字符c,函数返回TRUE */
    }
    return temp;
} 

/************************************/
/* 函数名 atEOL			    */
/* 功  能 行结束判断函数	    */
/* 说  明 当前行是否结束的判断函数  */
/************************************/	
boolean atEOL()
{ 
    return (!nonBlank());	/* 如果当前行中没有非空字符,则函数返回TRUE */
} 

/****************************************************/
/* 函数名 error					    */
/* 功  能 错误处理函数				    */
/* 说  明 函数输出错误行号,指令地址标号和错误信息   */
/****************************************************/
boolean error(String msg,int lineNo,int instNo)
{ 
    String s;
    s="Line "+String.valueOf(lineNo);

    /* 输出错误指令地址标号instNo */
    if (instNo >= 0) 
        s=s+" (Instruction "+String.valueOf(instNo)+")";

    /* 输出错误信息msg */
    s=s+"   "+msg+"\n";

    xwin=new XWindow("ERROR",s);
    xwin.setVisible(true);

    return false;
}

/****************************************************/
/* 函数名 enterCom				    */
/* 功  能 读入指令函数				    */
/* 说  明 交互执行,处理用户输入的TM命令             */
/****************************************************/
void enterCom()
{
    win1=new Mywindow("Enter command: ");
    /* 屏幕显示提示信息,提示用户输入TM命令 */
    win1.setVisible(true);
    (win1.button1).addActionListener(this);
}

/****************************************************/
/* 函数名 enterData			            */
/* 功  能 读入指令函数				    */
/* 说  明 交互执行,处理用户输入的数值               */
/****************************************************/
void enterData()
{
    win2=new Mywindow("Enter value for IN instruction: ");
    /* 屏幕显示提示信息,提示用户输入TM命令 */
    win2.setVisible(true);
    (win2.button1).addActionListener(this);
}

/****************************************************/
/* 函数名 actionPerformed			    */
/* 功  能 处理事件接口函数		            */
/* 说  明 处理事件接口   		            */
/****************************************************/
public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==win1.button1)   /*输入命令窗口*/
    {
        win1.stext=win1.text1.getText();
        lineLen = win1.stext.length();
        in_Line = win1.stext.toCharArray();
        inCol = 0;
        if(getWord())
        {
	    win1.setVisible(false);
            doCom(); 
        }  
    }
    else if(e.getSource()==win2.button1)  /*输入数值窗口*/
    {
        win2.stext=win2.text1.getText();
        lineLen = win2.stext.length();
        in_Line = win2.stext.toCharArray();
        inCol = 0;
        if(getNum())
        {
            reg[in_s] = num;
            win2.setVisible(false);
            if(cmd!='g')   /*决定进入哪个循环*/
            {
        	/* stepcnt此时用于记录将要执行,输出的指令或数据的数量,自减 */
                stepcnt--;
                if((stepcnt > 0) && (stepResult.equals("OKAY")))
                {
    		    /* 取得程序计数器reg[PC_REG]中当前指令地址 */
    		    iloc = reg[PC_REG];

    		    /* 根据执行指令追踪标志traceflag,将当前指令地址iloc上指令输出到屏幕 */
    		    if(traceflag) 
    		    {
        		name=" ";
        		writeInstruction(iloc);
    		    }
                    stepTM();
                }
                else
                    step0();
            }
    	    else
    	    {		
    		/* 执行过指令计数stepcnt加1 */
    		stepcnt++;

    		if(stepResult.equals("OKAY"))
    		{
           	    /* 根据执行指令追踪标志traceflag,将当前地址iloc上指令输出到屏幕 */
    		    iloc = reg[PC_REG];

    		    /* 根据执行指令追踪标志traceflag,将当前指令地址iloc上指令输出到屏幕 */
    		    if(traceflag) 
    		    {
        		name=" ";
        		writeInstruction(iloc);
    		    }
                    stepTM();
   	        }
    		else
    		{
		    /* 根据执行执行数量追踪标志icountflag,显示已经执行过的指令数量 */
        	    if (icountflag)
		    {
            		name="已经执行过的指令数量";
            		expr=expr+"Number of instructions executed ="+String.valueOf(stepcnt)+"\n";
        	    }
        	    step0();
    		}
    	    }
        }
        else    /*不是数值,报错*/
        {
            XWindow xwin2=new XWindow(null,"Illegal value\n");
            xwin2.setVisible(true);
        }         
    }
}

/****************************************************/
/* 函数名 doCom					    */
/* 功  能 处理命令函数				    */
/* 说  明 处理用户输入的命令                        */
/****************************************************/
void doCom()
{
    if(do_com)     /* 输入的命令不是q,继续执行,否则关闭窗口退出 */
       doCommand();  
    else
    {
        win1.setVisible(false);
        /* 虚拟机命令执行完毕 */
        xwin=new XWindow("OVER","Simulation done.");
        xwin.setVisible(true);
    }
}
/****************************************************/
/* 函数名 doCommand				    */
/* 功  能 TM机交互命令处理函数			    */
/* 说  明 函数处理用户输入的TM操作命令,完成相应动作 */
/****************************************************/
void doCommand()
{ 
    int i;
    int printcnt;
    int regNo,loc;
    name=null;

    do_com=true;
    cmd = word.charAt(0);  /* 取输入命令名中的第一个字符给cmd */
    switch(cmd)
    { 
 	/* 该命令用于设置指令执行追踪标志,追踪指令执行 */
        case 't' :
        traceflag = !traceflag;		/* 取反设置追踪标志traceflag */

        /* 输出TM机t命令执行结果信息 */
        String trac;
        trac="Tracing now ";
        if(traceflag) 
            trac=trac+"on.\n"; 
        else 
            trac=trac+"off.\n";

        name="追踪标志";
        expr=trac;

        break;
        /**************************************************************/

        /* 该命令输出帮助信息列表,显示各种命令及其功能 */
        case 'h' :

        String hel="Commands are:";

	/* 按步执行(step)命令:可输入"s(tep <n>"来执行,  *
	 * 可执行n(默认为1)条tm指令.			*/
        hel=hel+"   s(tep <n>	:Execute n (default 1) TM instructions\n";

	/* 执行到结束(go)命令:可输入"g(o"来执行,*
	 * 顺序执行tm指令直到遇到HALT指令		*/
        hel=hel+"   g(o	:Execute TM instructions until HALT\n";

	/* 显示寄存器(regs)命令:可输入"r(egs"来执行,*
	 * 显示各寄存器的内容				*/
        hel=hel+"   r(egs	:Print the contents of the registers\n";

	/* 输出指令(iMem)命令:可输入"i(Mem <b<n>>"来执行,*
	 * 从地址b处输出n条指令				*/
        hel=hel+"   i(Mem <b <n>>	:Print n iMem locations starting at b\n";

⌨️ 快捷键说明

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