constantfolding.java

来自「用Java实现的编译器。把源代码编译成SPARC汇编程序」· Java 代码 · 共 616 行 · 第 1/2 页

JAVA
616
字号
			int i = ((LtrInt)n.exp1_).ltrInt_ % ((LtrInt)n.exp2_).ltrInt_;			
			if(i >= 0) n.compileResult = parser.convPos(new LtrInt(Integer.toString(i)), n.expLineNo);
			else n.compileResult = parser.convNeg(new LtrInt(((String)(Integer.toString(i))).substring(1)), n.expLineNo);			
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}
	}
	public void visit(ExpUMinus n){
		if(n.exp_ instanceof LtrInt){
			int i = -((LtrInt)n.exp_).ltrInt_;			
			if(i >= 0) n.compileResult = parser.convPos(new LtrInt(Integer.toString(i)), n.expLineNo);
			else n.compileResult = parser.convNeg(new LtrInt(((String)(Integer.toString(i))).substring(1)), n.expLineNo);			
			n.exp_ = null; newOptDone=true; 
		}else{
			n.exp_.accept(this);  
			if(n.exp_.compileResult != null) n.exp_ = n.exp_.compileResult;						
		}
		if(n.exp_ instanceof LtrInt){
			int i = -((LtrInt)n.exp_).ltrInt_;			
			if(i >= 0) n.compileResult = parser.convPos(new LtrInt(Integer.toString(i)), n.expLineNo);
			else n.compileResult = parser.convNeg(new LtrInt(((String)(Integer.toString(i))).substring(1)), n.expLineNo);			
			n.exp_ = null; newOptDone=true;
		}
	}
	public void visit(ExpShiftLeft n){
		if(n.exp1_ instanceof LtrInt && n.exp2_ instanceof LtrInt){
			int i = ((LtrInt)n.exp1_).ltrInt_ << ((LtrInt)n.exp2_).ltrInt_;			
			if(i >= 0) n.compileResult = parser.convPos(new LtrInt(Integer.toString(i)), n.expLineNo);
			else n.compileResult = parser.convNeg(new LtrInt(((String)(Integer.toString(i))).substring(1)), n.expLineNo);			
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else{
			n.exp1_.accept(this);  
			if(n.exp1_.compileResult != null) n.exp1_ = n.exp1_.compileResult;			
			n.exp2_.accept(this);
			if(n.exp2_.compileResult != null) n.exp2_ = n.exp2_.compileResult;			
		}
		if(n.exp1_ instanceof LtrInt && n.exp2_ instanceof LtrInt){
			int i = ((LtrInt)n.exp1_).ltrInt_ << ((LtrInt)n.exp2_).ltrInt_;			
			if(i >= 0) n.compileResult = parser.convPos(new LtrInt(Integer.toString(i)), n.expLineNo);
			else n.compileResult = parser.convNeg(new LtrInt(((String)(Integer.toString(i))).substring(1)), n.expLineNo);			
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}
	}
	public void visit(ExpShiftRight n){
		if(n.exp1_ instanceof LtrInt && n.exp2_ instanceof LtrInt){
			int i = ((LtrInt)n.exp1_).ltrInt_ >> ((LtrInt)n.exp2_).ltrInt_;			
			if(i >= 0) n.compileResult = parser.convPos(new LtrInt(Integer.toString(i)), n.expLineNo);
			else n.compileResult = parser.convNeg(new LtrInt(((String)(Integer.toString(i))).substring(1)), n.expLineNo);			
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else{
			n.exp1_.accept(this);  
			if(n.exp1_.compileResult != null) n.exp1_ = n.exp1_.compileResult;			
			n.exp2_.accept(this);
			if(n.exp2_.compileResult != null) n.exp2_ = n.exp2_.compileResult;			
		}
		if(n.exp1_ instanceof LtrInt && n.exp2_ instanceof LtrInt){
			int i = ((LtrInt)n.exp1_).ltrInt_ >> ((LtrInt)n.exp2_).ltrInt_;			
			if(i >= 0) n.compileResult = parser.convPos(new LtrInt(Integer.toString(i)), n.expLineNo);
			else n.compileResult = parser.convNeg(new LtrInt(((String)(Integer.toString(i))).substring(1)), n.expLineNo);			
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}
	}
	public void visit(ExpLessThan n){
		if(n.exp1_ instanceof LtrInt && n.exp2_ instanceof LtrInt){
			boolean b = ((LtrInt)n.exp1_).ltrInt_ < ((LtrInt)n.exp2_).ltrInt_;						
			if (b) n.compileResult = new LtrBoolTrue();	
			else n.compileResult = new LtrBoolFalse();			
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else{
			n.exp1_.accept(this);  
			if(n.exp1_.compileResult != null) n.exp1_ = n.exp1_.compileResult;			
			n.exp2_.accept(this);
			if(n.exp2_.compileResult != null) n.exp2_ = n.exp2_.compileResult;			
		}
		if(n.exp1_ instanceof LtrInt && n.exp2_ instanceof LtrInt){
			boolean b = ((LtrInt)n.exp1_).ltrInt_ < ((LtrInt)n.exp2_).ltrInt_;						
			if (b) n.compileResult = new LtrBoolTrue();	
			else n.compileResult = new LtrBoolFalse();			
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}
	}
	public void visit(ExpGreaterThan n){
		if(n.exp1_ instanceof LtrInt && n.exp2_ instanceof LtrInt){
			boolean b = ((LtrInt)n.exp1_).ltrInt_ > ((LtrInt)n.exp2_).ltrInt_;						
			if (b) n.compileResult = new LtrBoolTrue();	
			else n.compileResult = new LtrBoolFalse();			
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else{
			n.exp1_.accept(this);  
			if(n.exp1_.compileResult != null) n.exp1_ = n.exp1_.compileResult;			
			n.exp2_.accept(this);
			if(n.exp2_.compileResult != null) n.exp2_ = n.exp2_.compileResult;			
		}
		if(n.exp1_ instanceof LtrInt && n.exp2_ instanceof LtrInt){
			boolean b = ((LtrInt)n.exp1_).ltrInt_ > ((LtrInt)n.exp2_).ltrInt_;						
			if (b) n.compileResult = new LtrBoolTrue();	
			else n.compileResult = new LtrBoolFalse();			
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}
	}
	public void visit(ExpLessThEql n){
		if(n.exp1_ instanceof LtrInt && n.exp2_ instanceof LtrInt){
			boolean b = ((LtrInt)n.exp1_).ltrInt_ <= ((LtrInt)n.exp2_).ltrInt_;						
			if (b) n.compileResult = new LtrBoolTrue();	
			else n.compileResult = new LtrBoolFalse();			
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else{
			n.exp1_.accept(this);  
			if(n.exp1_.compileResult != null) n.exp1_ = n.exp1_.compileResult;			
			n.exp2_.accept(this);
			if(n.exp2_.compileResult != null) n.exp2_ = n.exp2_.compileResult;			
		}
		if(n.exp1_ instanceof LtrInt && n.exp2_ instanceof LtrInt){
			boolean b = ((LtrInt)n.exp1_).ltrInt_ <= ((LtrInt)n.exp2_).ltrInt_;						
			if (b) n.compileResult = new LtrBoolTrue();	
			else n.compileResult = new LtrBoolFalse();			
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}
	}
	public void visit(ExpGreaterThEql n){
		if(n.exp1_ instanceof LtrInt && n.exp2_ instanceof LtrInt){
			boolean b = ((LtrInt)n.exp1_).ltrInt_ >= ((LtrInt)n.exp2_).ltrInt_;						
			if (b) n.compileResult = new LtrBoolTrue();	
			else n.compileResult = new LtrBoolFalse();			
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else{
			n.exp1_.accept(this);  
			if(n.exp1_.compileResult != null) n.exp1_ = n.exp1_.compileResult;			
			n.exp2_.accept(this);
			if(n.exp2_.compileResult != null) n.exp2_ = n.exp2_.compileResult;			
		}
		if(n.exp1_ instanceof LtrInt && n.exp2_ instanceof LtrInt){
			boolean b = ((LtrInt)n.exp1_).ltrInt_ >= ((LtrInt)n.exp2_).ltrInt_;						
			if (b) n.compileResult = new LtrBoolTrue();	
			else n.compileResult = new LtrBoolFalse();			
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}
	}
	public void visit(ExpEqualTo n){
		
		if(n.exp1_ instanceof LtrInt && n.exp2_ instanceof LtrInt){
			boolean b = ((LtrInt)n.exp1_).ltrInt_ == ((LtrInt)n.exp2_).ltrInt_;						
			if (b) n.compileResult = new LtrBoolTrue();	
			else n.compileResult = new LtrBoolFalse();			
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolTrue && n.exp2_ instanceof LtrBoolTrue){
			n.compileResult = new LtrBoolTrue();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolTrue && n.exp2_ instanceof LtrBoolFalse){
			n.compileResult = new LtrBoolFalse();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolFalse && n.exp2_ instanceof LtrBoolTrue){
			n.compileResult = new LtrBoolFalse();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolFalse && n.exp2_ instanceof LtrBoolFalse){
			n.compileResult = new LtrBoolTrue();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else{
			n.exp1_.accept(this);  
			if(n.exp1_.compileResult != null) n.exp1_ = n.exp1_.compileResult;			
			n.exp2_.accept(this);
			if(n.exp2_.compileResult != null) n.exp2_ = n.exp2_.compileResult;			
		}
		
		if(n.exp1_ instanceof LtrInt && n.exp2_ instanceof LtrInt){
			boolean b = ((LtrInt)n.exp1_).ltrInt_ == ((LtrInt)n.exp2_).ltrInt_;						
			if (b) n.compileResult = new LtrBoolTrue();	
			else n.compileResult = new LtrBoolFalse();			
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolTrue && n.exp2_ instanceof LtrBoolTrue){
			n.compileResult = new LtrBoolTrue();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolTrue && n.exp2_ instanceof LtrBoolFalse){
			n.compileResult = new LtrBoolFalse();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolFalse && n.exp2_ instanceof LtrBoolTrue){
			n.compileResult = new LtrBoolFalse();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolFalse && n.exp2_ instanceof LtrBoolFalse){
			n.compileResult = new LtrBoolTrue();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}
		
	}
	public void visit(ExpNotEqualTo n){
		if(n.exp1_ instanceof LtrInt && n.exp2_ instanceof LtrInt){
			boolean b = ((LtrInt)n.exp1_).ltrInt_ != ((LtrInt)n.exp2_).ltrInt_;						
			if (b) n.compileResult = new LtrBoolTrue();	
			else n.compileResult = new LtrBoolFalse();			
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolTrue && n.exp2_ instanceof LtrBoolTrue){
			n.compileResult = new LtrBoolFalse();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolTrue && n.exp2_ instanceof LtrBoolFalse){
			n.compileResult = new LtrBoolTrue();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolFalse && n.exp2_ instanceof LtrBoolTrue){
			n.compileResult = new LtrBoolTrue();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolFalse && n.exp2_ instanceof LtrBoolFalse){
			n.compileResult = new LtrBoolFalse();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else{
			n.exp1_.accept(this);  
			if(n.exp1_.compileResult != null) n.exp1_ = n.exp1_.compileResult;			
			n.exp2_.accept(this);
			if(n.exp2_.compileResult != null) n.exp2_ = n.exp2_.compileResult;			
		}
		
		if(n.exp1_ instanceof LtrInt && n.exp2_ instanceof LtrInt){
			boolean b = ((LtrInt)n.exp1_).ltrInt_ != ((LtrInt)n.exp2_).ltrInt_;						
			if (b) n.compileResult = new LtrBoolTrue();	
			else n.compileResult = new LtrBoolFalse();			
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolTrue && n.exp2_ instanceof LtrBoolTrue){
			n.compileResult = new LtrBoolFalse();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolTrue && n.exp2_ instanceof LtrBoolFalse){
			n.compileResult = new LtrBoolTrue();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolFalse && n.exp2_ instanceof LtrBoolTrue){
			n.compileResult = new LtrBoolTrue();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolFalse && n.exp2_ instanceof LtrBoolFalse){
			n.compileResult = new LtrBoolFalse();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}
	}
	public void visit(ExpAndOp n){
		if(n.exp1_ instanceof LtrBoolTrue && n.exp2_ instanceof LtrBoolTrue){
			n.compileResult = new LtrBoolTrue();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolTrue && n.exp2_ instanceof LtrBoolFalse){
			n.compileResult = new LtrBoolFalse();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolFalse && n.exp2_ instanceof LtrBoolTrue){
			n.compileResult = new LtrBoolFalse();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolFalse && n.exp2_ instanceof LtrBoolFalse){
			n.compileResult = new LtrBoolFalse();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else{
			n.exp1_.accept(this);  
			if(n.exp1_.compileResult != null) n.exp1_ = n.exp1_.compileResult;			
			n.exp2_.accept(this);
			if(n.exp2_.compileResult != null) n.exp2_ = n.exp2_.compileResult;			
		}
		if(n.exp1_ instanceof LtrBoolTrue && n.exp2_ instanceof LtrBoolTrue){
			n.compileResult = new LtrBoolTrue();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolTrue && n.exp2_ instanceof LtrBoolFalse){
			n.compileResult = new LtrBoolFalse();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolFalse && n.exp2_ instanceof LtrBoolTrue){
			n.compileResult = new LtrBoolFalse();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolFalse && n.exp2_ instanceof LtrBoolFalse){
			n.compileResult = new LtrBoolFalse();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}
	}
	public void visit(ExpOrOp n){
		if(n.exp1_ instanceof LtrBoolTrue && n.exp2_ instanceof LtrBoolTrue){
			n.compileResult = new LtrBoolTrue();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolTrue && n.exp2_ instanceof LtrBoolFalse){
			n.compileResult = new LtrBoolTrue();						
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolFalse && n.exp2_ instanceof LtrBoolTrue){
			n.compileResult = new LtrBoolTrue();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolFalse && n.exp2_ instanceof LtrBoolFalse){
			n.compileResult = new LtrBoolFalse();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else{
			n.exp1_.accept(this);  
			if(n.exp1_.compileResult != null) n.exp1_ = n.exp1_.compileResult;			
			n.exp2_.accept(this);
			if(n.exp2_.compileResult != null) n.exp2_ = n.exp2_.compileResult;			
		}
		if(n.exp1_ instanceof LtrBoolTrue && n.exp2_ instanceof LtrBoolTrue){
			n.compileResult = new LtrBoolTrue();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolTrue && n.exp2_ instanceof LtrBoolFalse){
			n.compileResult = new LtrBoolTrue();						
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolFalse && n.exp2_ instanceof LtrBoolTrue){
			n.compileResult = new LtrBoolTrue();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}else if(n.exp1_ instanceof LtrBoolFalse && n.exp2_ instanceof LtrBoolFalse){
			n.compileResult = new LtrBoolFalse();							
			n.exp1_ = null; newOptDone=true; n.exp2_ = null; newOptDone=true;
		}
	}
	public void visit(LtrBoolFalse n){
	}
	public void visit(LtrBoolTrue n){
	}
	public void visit(LtrChar n){
	}
	public void visit(LtrInt n){
	}
	public void visit(LtrString n){
	}
	public void visit(Identifier n){
	}
	
}

⌨️ 快捷键说明

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