예제 #1
0
        public override void Translate(VarTable varTable, LabelStack labelStack, List <FourExp> fourExpList)
        {
            string label = labelStack.NewLabel();

            fourExpList.Add(FourExpFac.GenLabel(label));
            statement.Translate(varTable, labelStack, fourExpList);
            string condition = expression.GetValue(varTable, labelStack, fourExpList);

            fourExpList.Add(FourExpFac.GenJne(condition, 0 + "", label));
        }
예제 #2
0
 public override void Translate(VarTable varTable, LabelStack labelStack, List <FourExp> fourExpList)
 {
     if (statement1 == null)
     {
         string label     = labelStack.NewLabel();
         string condition = expression.GetValue(varTable, labelStack, fourExpList);
         fourExpList.Add(FourExpFac.GenJe(condition, 0 + "", label));
         statement1.Translate(varTable, labelStack, fourExpList);
         fourExpList.Add(FourExpFac.GenLabel(label));
     }
     else
     {
         string label1    = labelStack.NewLabel();
         string label2    = labelStack.NewLabel();
         string condition = expression.GetValue(varTable, labelStack, fourExpList);
         fourExpList.Add(FourExpFac.GenJe(condition, 0 + "", label1));
         statement1.Translate(varTable, labelStack, fourExpList);
         fourExpList.Add(FourExpFac.GenJmp(label2));
         fourExpList.Add(FourExpFac.GenLabel(label1));
         statement2.Translate(varTable, labelStack, fourExpList);
         fourExpList.Add(FourExpFac.GenLabel(label2));
     }
 }
예제 #3
0
        public override string GetValue(VarTable varTable, LabelStack labelStack, List <FourExp> fourExpList)
        {
            string returnValue = "";
            string label, t1, t2, var;

            switch (this.op.Type)
            {
            case OperatorType.not:              //非
                label = labelStack.NewLabel();
                var   = expression.GetValue(varTable, labelStack, fourExpList);
                fourExpList.Add(FourExpFac.GenJe(var, 0 + "", label));
                t1 = varTable.NewTemp(VariableType.BOOL);
                fourExpList.Add(FourExpFac.GenMov(1 + "", t1));
                var = t1;
                fourExpList.Add(FourExpFac.GenLabel(label));
                t2 = varTable.NewTemp(VariableType.BOOL);
                fourExpList.Add(FourExpFac.GenNot(t1, t2));
                returnValue = t2;
                break;

            case OperatorType.selfaddhead:          //自增前置
                var = expression.GetValue(varTable, labelStack, fourExpList);
                fourExpList.Add(FourExpFac.GenAdd(var, 1 + "", var));
                returnValue = var;
                break;

            case OperatorType.selfsubhead:          //自减前置
                var = expression.GetValue(varTable, labelStack, fourExpList);
                fourExpList.Add(FourExpFac.GenSub(var, 1 + "", var));
                returnValue = var;
                break;

            case OperatorType.selfaddtail:          //自增后置
                var = expression.GetValue(varTable, labelStack, fourExpList);
                t1  = varTable.NewTemp(var);
                fourExpList.Add(FourExpFac.GenAdd(var, 1 + "", var));
                returnValue = t1;
                break;

            case OperatorType.selfsubtail:          //自减后置
                var = expression.GetValue(varTable, labelStack, fourExpList);
                t1  = varTable.NewTemp(var);
                fourExpList.Add(FourExpFac.GenSub(var, 1 + "", var));
                returnValue = t1;
                break;

            case OperatorType.bitnot:           //按位非
                var = expression.GetValue(varTable, labelStack, fourExpList);
                t1  = varTable.NewTemp(var);
                fourExpList.Add(FourExpFac.GenNot(var, t1));
                returnValue = t1;
                break;

            case OperatorType.neg:              //取反
                var = expression.GetValue(varTable, labelStack, fourExpList);
                t1  = varTable.NewTemp(var);
                fourExpList.Add(FourExpFac.GenNeg(var, t1));
                returnValue = t1;
                break;
            }
            return(returnValue);
        }
예제 #4
0
        public override string GetValue(VarTable varTable, LabelStack labelStack, List <FourExp> fourExpList)
        {
            string returnValue = "";

            //加、减、乘、除、按位与、按位或
            if (this.op.Type == OperatorType.add || this.op.Type == OperatorType.sub || this.op.Type == OperatorType.mul || this.op.Type == OperatorType.div || this.op.Type == OperatorType.bitand || this.op.Type == OperatorType.bitor)
            {
                string arg1 = expression1.GetValue(varTable, labelStack, fourExpList);
                string arg2 = expression2.GetValue(varTable, labelStack, fourExpList);
                string t    = varTable.NewTemp(arg1, arg2);
                switch (this.op.Type)
                {
                case OperatorType.add:              //+
                    fourExpList.Add(FourExpFac.GenAdd(arg1, arg2, t));
                    break;

                case OperatorType.sub:              //-
                    fourExpList.Add(FourExpFac.GenSub(arg1, arg2, t));
                    break;

                case OperatorType.mul:              //*
                    fourExpList.Add(FourExpFac.GenMul(arg1, arg2, t));
                    break;

                case OperatorType.div:              ///
                    fourExpList.Add(FourExpFac.GenDiv(arg1, arg2, t));
                    break;

                case OperatorType.bitand:           //按位与
                    fourExpList.Add(FourExpFac.GenAnd(arg1, arg2, t));
                    break;

                case OperatorType.bitor:            //按位或
                    fourExpList.Add(FourExpFac.GenOr(arg1, arg2, t));
                    break;

                default:
                    break;
                }
                returnValue = t;
            }
            //与、或
            else if (this.op.Type == OperatorType.and || this.op.Type == OperatorType.or)
            {
                string label1 = labelStack.NewLabel();
                string arg1   = expression1.GetValue(varTable, labelStack, fourExpList);
                fourExpList.Add(FourExpFac.GenJe(arg1, 0 + "", label1));
                string t1 = varTable.NewTemp(VariableType.BOOL);
                fourExpList.Add(FourExpFac.GenMov(1 + "", t1));
                arg1 = t1;
                fourExpList.Add(FourExpFac.GenLabel(label1));

                string label2 = labelStack.NewLabel();
                string arg2   = expression2.GetValue(varTable, labelStack, fourExpList);
                fourExpList.Add(FourExpFac.GenJe(arg2, 0 + "", label2));
                string t2 = varTable.NewTemp(VariableType.BOOL);
                fourExpList.Add(FourExpFac.GenMov(1 + "", t2));
                arg2 = t2;
                fourExpList.Add(FourExpFac.GenLabel(label2));

                string t3 = varTable.NewTemp(VariableType.BOOL);
                switch (this.op.Type)
                {
                case OperatorType.and:          //与
                    fourExpList.Add(FourExpFac.GenAnd(arg1, arg2, t3));
                    break;

                case OperatorType.or:           //或
                    fourExpList.Add(FourExpFac.GenOr(arg1, arg2, t3));
                    break;
                }
                returnValue = t3;
            }
            //左移、右移
            else if (this.op.Type == OperatorType.leftmove || this.op.Type == OperatorType.rightmove)
            {
                string arg1 = expression1.GetValue(varTable, labelStack, fourExpList);
                string arg2 = expression2.GetValue(varTable, labelStack, fourExpList);
                string t    = varTable.NewTemp(arg1);
                switch (this.op.Type)
                {
                case OperatorType.leftmove:                 //左移
                    fourExpList.Add(FourExpFac.GenLeftshift(arg1, arg2, t));
                    break;

                case OperatorType.rightmove:                //右移
                    fourExpList.Add(FourExpFac.GenRightshift(arg1, arg2, t));
                    break;

                default:
                    break;
                }
                returnValue = t;
            }
            //小于、小于等于、大于、大于等于、等于、不等于
            else if (this.op.Type == OperatorType.less || this.op.Type == OperatorType.lessequal || this.op.Type == OperatorType.greater || this.op.Type == OperatorType.greatereuqal || this.op.Type == OperatorType.equal || this.op.Type == OperatorType.notequal)
            {
                string label1 = labelStack.NewLabel();
                string label2 = labelStack.NewLabel();
                string t      = varTable.NewTemp(VariableType.BOOL);
                string arg1   = expression1.GetValue(varTable, labelStack, fourExpList);
                string arg2   = expression2.GetValue(varTable, labelStack, fourExpList);
                switch (this.op.Type)
                {
                case OperatorType.less:             //<
                    fourExpList.Add(FourExpFac.GenJl(arg1, arg2, label1));
                    break;

                case OperatorType.lessequal:        //<=
                    fourExpList.Add(FourExpFac.GenJle(arg1, arg2, label1));
                    break;

                case OperatorType.greater:          //>
                    fourExpList.Add(FourExpFac.GenJg(arg1, arg2, label1));
                    break;

                case OperatorType.greatereuqal:     //>=
                    fourExpList.Add(FourExpFac.GenJge(arg1, arg2, label1));
                    break;

                case OperatorType.equal:            //==
                    fourExpList.Add(FourExpFac.GenJe(arg1, arg2, label1));
                    break;

                case OperatorType.notequal:         //!=
                    fourExpList.Add(FourExpFac.GenJne(arg1, arg2, label1));
                    break;

                default:
                    //
                    break;
                }
                fourExpList.Add(FourExpFac.GenMov(0 + "", t));
                fourExpList.Add(FourExpFac.GenJmp(label2));
                fourExpList.Add(FourExpFac.GenLabel(label1));
                fourExpList.Add(FourExpFac.GenMov(1 + "", t));
                fourExpList.Add(FourExpFac.GenLabel(label2));
                returnValue = t;
            }
            else
            {
                //错误处理
            }
            return(returnValue);
        }