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

📄 algebraicsimplification.java

📁 用Java实现的编译器。把源代码编译成SPARC汇编程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		n.exp1_.accept(this);
		if(n.exp1_.algebraicResult != null) n.exp1_ = n.exp1_.algebraicResult;
		n.exp2_.accept(this);
		if(n.exp2_.algebraicResult != null) n.exp2_ = n.exp2_.algebraicResult;
                // check if the traversal is for just searching for a funciton
                if(searchFunction){
                  if(n.exp1_ instanceof MdCall || n.exp2_ instanceof MdCall){
                    functionPresent = true;
                    return;
                  }else{
                    n.exp1_.accept(this);
                    n.exp2_.accept(this);
                    return;
                  }

                }
                /*
		-------------------------------------------------------------------
		division operation by 1
			a / 1; // return a
		-------------------------------------------------------------------
		*/
		if(n.exp2_ instanceof LtrInt){// e2 is a constant
			if (( (LtrInt) n.exp2_).ltrInt_ == 1){//e2 == 1
				n.algebraicResult = n.exp1_;
				n.exp1_ = null;
				n.exp2_ = null;
				newOptDone = true;
			}else {
				int expvalue = ( (LtrInt) n.exp2_).ltrInt_;
				int lgValExp = (new Double(Math.log(expvalue) / Math.log(2))).intValue();
				int powerValue = (new Double(Math.pow(2, lgValExp))).intValue();
				if (powerValue == expvalue){// e2 is a power of 2
					ExpShiftRight newExp = new ExpShiftRight(n.exp1_, new LtrInt(lgValExp), false);
					n.algebraicResult = new ExpDivide(newExp, new LtrInt(1));
					newExp = null;
					n.exp1_ = null;
					n.exp2_ = null;
					newOptDone = true;
					
				}
			}
		}
		/*
		-------------------------------------------------------------------
		division operation by the same value
			a / a;
		-------------------------------------------------------------------
		*/
		if(n.exp1_ instanceof LtrInt && n.exp2_ instanceof LtrInt){// both e1 and e2 are constants
			if (( (LtrInt) n.exp1_).ltrInt_ == ( (LtrInt) n.exp2_).ltrInt_ && ( (LtrInt) n.exp2_).ltrInt_ != 0){//e1 == e2 and not equal to 0
				n.algebraicResult = new LtrInt(1);
				n.exp1_ = null;
				n.exp2_ = null;
				newOptDone = true;
			}
		}
	}
	public void visit(ExpMod n){
		//accept the 2 sub expressions
		n.exp1_.accept(this);
		if(n.exp1_.algebraicResult != null) n.exp1_ = n.exp1_.algebraicResult;
		n.exp2_.accept(this);
		if(n.exp2_.algebraicResult != null) n.exp2_ = n.exp2_.algebraicResult;
                // check if the traversal is for just searching for a funciton
                if(searchFunction){
                  if(n.exp1_ instanceof MdCall || n.exp2_ instanceof MdCall){
                    functionPresent = true;
                    return;
                  }else{
                    n.exp1_.accept(this);
                    n.exp2_.accept(this);
                    return;
                  }

                }
		/*
		-------------------------------------------------------------------
		mod by 1
			a mod 1 // return 0
		-------------------------------------------------------------------
		*/
		if(n.exp2_ instanceof LtrInt){// e2 is a constant
			if (( (LtrInt) n.exp2_).ltrInt_ == 1){//e2 == 1
				// check if e1 has a function
				searchFunction = true;
				n.exp1_.accept(this);
				if(!functionPresent){
					n.algebraicResult = new LtrInt(Integer.toString(0));
					n.exp2_ = null;
					n.exp1_ = null;
					newOptDone = true;
				}
				searchFunction = false;
			}
		}
	}
	public void visit(ExpUMinus n){
		n.exp_.accept(this);
                // check if the traversal is for just searching for a funciton
                if(searchFunction){
                  if(n.exp_ instanceof MdCall){
                    functionPresent = true;
                    return;
                  }else{
                    n.exp_.accept(this);
                    return;
                  }

                }

	}
	public void visit(ExpShiftLeft n){
		//accept the 2 sub expressions
		n.exp1_.accept(this);
		if(n.exp1_.algebraicResult != null) n.exp1_ = n.exp1_.algebraicResult;
		n.exp2_.accept(this);
		if(n.exp2_.algebraicResult != null) n.exp2_ = n.exp2_.algebraicResult;
                // check if the traversal is for just searching for a funciton
                if(searchFunction){
                  if(n.exp1_ instanceof MdCall || n.exp2_ instanceof MdCall){
                    functionPresent = true;
                    return;
                  }else{
                    n.exp1_.accept(this);
                    n.exp2_.accept(this);
                    return;
                  }

                }
		/*
		-------------------------------------------------------------------
		shift left by 0
			a << 0 // return a
		-------------------------------------------------------------------
		*/
		if(n.exp2_ instanceof LtrInt){// e2 is a constant
			if (( (LtrInt) n.exp2_).ltrInt_ == 0){//e2 == 0
				n.algebraicResult = n.exp1_;
				n.exp2_ = null;
				n.exp1_ = null;
				newOptDone = true;
			}
		}
	}
	public void visit(ExpShiftRight n){
		//accept the 2 sub expressions
		n.exp1_.accept(this);
		if(n.exp1_.algebraicResult != null) n.exp1_ = n.exp1_.algebraicResult;
		n.exp2_.accept(this);
		if(n.exp2_.algebraicResult != null) n.exp2_ = n.exp2_.algebraicResult;
                // check if the traversal is for just searching for a funciton
                if(searchFunction){
                  if(n.exp1_ instanceof MdCall || n.exp2_ instanceof MdCall){
                    functionPresent = true;
                    return;
                  }else{
                    n.exp1_.accept(this);
                    n.exp2_.accept(this);
                    return;
                  }

                }
		/*
		-------------------------------------------------------------------
		shift right by 0
			a >> 0 // return a
		-------------------------------------------------------------------
		*/
		if(n.exp2_ instanceof LtrInt){// e2 is a constant
			if (( (LtrInt) n.exp2_).ltrInt_ == 0){//e2 == 0
				n.algebraicResult = n.exp1_;
				n.exp2_ = null;
				n.exp1_ = null;
				newOptDone = true;
			}
		}
	}
	public void visit(ExpLessThan n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
                // check if the traversal is for just searching for a funciton
                if(searchFunction){
                  if(n.exp1_ instanceof MdCall || n.exp2_ instanceof MdCall){
                    functionPresent = true;
                    return;
                  }else{
                    n.exp1_.accept(this);
                    n.exp2_.accept(this);
                    return;
                  }

                }
	}
	public void visit(ExpGreaterThan n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
                // check if the traversal is for just searching for a funciton
                if(searchFunction){
                  if(n.exp1_ instanceof MdCall || n.exp2_ instanceof MdCall){
                    functionPresent = true;
                    return;
                  }else{
                    n.exp1_.accept(this);
                    n.exp2_.accept(this);
                    return;
                  }

                }
	}
	public void visit(ExpLessThEql n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
                // check if the traversal is for just searching for a funciton
                if(searchFunction){
                  if(n.exp1_ instanceof MdCall || n.exp2_ instanceof MdCall){
                    functionPresent = true;
                    return;
                  }else{
                    n.exp1_.accept(this);
                    n.exp2_.accept(this);
                    return;
                  }

                }
	}
	public void visit(ExpGreaterThEql n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
                // check if the traversal is for just searching for a funciton
                if(searchFunction){
                  if(n.exp1_ instanceof MdCall || n.exp2_ instanceof MdCall){
                    functionPresent = true;
                    return;
                  }else{
                    n.exp1_.accept(this);
                    n.exp2_.accept(this);
                    return;
                  }

                }
	}
	public void visit(ExpEqualTo n){
		//accept the 2 sub expressions
		n.exp1_.accept(this);
		if(n.exp1_.algebraicResult != null) n.exp1_ = n.exp1_.algebraicResult;
		n.exp2_.accept(this);
		if(n.exp2_.algebraicResult != null) n.exp2_ = n.exp2_.algebraicResult;
                  // check if the traversal is for just searching for a funciton
                  if(searchFunction){
                    if(n.exp1_ instanceof MdCall || n.exp2_ instanceof MdCall){
                      functionPresent = true;
                      return;
                    }else{
                      n.exp1_.accept(this);
                      n.exp2_.accept(this);
                      return;
                    }

                  }
		/*
		-------------------------------------------------------------------
		equal to true
			a == true // return a
			true == a //return a
		-------------------------------------------------------------------
		*/
		if(n.exp2_ instanceof LtrBoolTrue){// e2 is true
			n.algebraicResult = n.exp1_;
			n.exp2_ = null;
			n.exp1_ = null;
			newOptDone = true;
		}else if(n.exp1_ instanceof LtrBoolTrue){// e1 is true
			n.algebraicResult = n.exp2_;
			n.exp2_ = null;
			n.exp1_ = null;
			newOptDone = true;
		}

	}
	public void visit(ExpNotEqualTo n){
		//accept the 2 sub expressions
		n.exp1_.accept(this);
		if(n.exp1_.algebraicResult != null) n.exp1_ = n.exp1_.algebraicResult;
		n.exp2_.accept(this);
		if(n.exp2_.algebraicResult != null) n.exp2_ = n.exp2_.algebraicResult;
                // check if the traversal is for just searching for a funciton
                if(searchFunction){
                  if(n.exp1_ instanceof MdCall || n.exp2_ instanceof MdCall){
                    functionPresent = true;
                    return;
                  }else{
                    n.exp1_.accept(this);
                    n.exp2_.accept(this);
                    return;
                  }

                }
		/*
		-------------------------------------------------------------------
		not equal to false
			a != false // return a
			false != a //return a
		-------------------------------------------------------------------
		*/
		if(n.exp2_ instanceof LtrBoolFalse){// e2 is false
			n.algebraicResult = n.exp1_;
			n.exp2_ = null;
			n.exp1_ = null;
			newOptDone = true;
		}else if(n.exp1_ instanceof LtrBoolFalse){// e1 is false
			n.algebraicResult = n.exp2_;
			n.exp2_ = null;
			n.exp1_ = null;
			newOptDone = true;
		}
	}
	public void visit(ExpAndOp n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
                // check if the traversal is for just searching for a funciton
                if(searchFunction){
                  if(n.exp1_ instanceof MdCall || n.exp2_ instanceof MdCall){
                    functionPresent = true;
                    return;
                  }else{
                    n.exp1_.accept(this);
                    n.exp2_.accept(this);
                    return;
                  }

                }
	}
	public void visit(ExpOrOp n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
                // check if the traversal is for just searching for a funciton
                if(searchFunction){
                  if(n.exp1_ instanceof MdCall || n.exp2_ instanceof MdCall){
                    functionPresent = true;
                    return;
                  }else{
                    n.exp1_.accept(this);
                    n.exp2_.accept(this);
                    return;
                  }

                }
	}
	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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -