Exemplo n.º 1
0
        public void WhileTest1()
        {
            BinaryExpression   eq       = new BinaryExpression();
            BinaryExpression   sumadder = new BinaryExpression();
            BinaryExpression   oneadder = new BinaryExpression();
            ConstExpression    zero     = new ConstExpression();
            ConstExpression    one      = new ConstExpression();
            ConstExpression    six      = new ConstExpression();
            VariableExpression vari     = new VariableExpression();
            VariableExpression sum      = new VariableExpression();

            zero.SetValue(0);
            one.SetValue(1);
            six.SetValue(6);

            vari.Name = "VAR";
            sum.Name  = "SUM";

            sumadder.Operator     = BinaryExpression.OperatorEnum.Add;
            sumadder.LeftOperand  = vari;
            sumadder.RightOperand = sum;

            oneadder.Operator     = BinaryExpression.OperatorEnum.Add;
            oneadder.LeftOperand  = vari;
            oneadder.RightOperand = one;

            eq.Operator     = BinaryExpression.OperatorEnum.Sml;
            eq.LeftOperand  = vari;
            eq.RightOperand = six;

            SetVarBlock initblk   = new SetVarBlock();
            SetVarBlock initblk2  = new SetVarBlock();
            WhileBlock  wblk      = new WhileBlock();
            SetVarBlock addblk    = new SetVarBlock();
            SetVarBlock sumaddblk = new SetVarBlock();


            initblk.VarName  = "SUM";
            initblk.ValueExp = zero;

            initblk2.VarName  = "VAR";
            initblk2.ValueExp = zero;

            wblk.Condition = eq;

            addblk.VarName  = "VAR";
            addblk.ValueExp = oneadder;

            sumaddblk.VarName  = "SUM";
            sumaddblk.ValueExp = sumadder;

            initblk.NextBlock  = initblk2;
            initblk2.NextBlock = wblk;
            wblk.InnerBlock    = addblk;
            addblk.NextBlock   = sumaddblk;

            initblk.Execute();

            Assert.AreEqual(vari.Value, 6);
        }
Exemplo n.º 2
0
 internal void Execute(Context context)
 {
     while ((dynamic)ConditionCallback(context))
     {
         WhileBlock.Execute();
     }
 }
Exemplo n.º 3
0
        private WhileBlock TranslateWhile(ICodeBlock parent, string condition, int startPos, out int endPos)
        {
            var result = new WhileBlock(parent, Parser.ParseLine(condition), null);

            endPos      = FindBlockEnd(startPos + 1) - 1;
            result.Code = Translate(result, startPos + 1, endPos + 1);

            return(result);
        }
Exemplo n.º 4
0
        private IAlgorithmBlock CreateBlockData(BlockData data)
        {
            IAlgorithmBlock result = null;

            switch (data.type)
            {
            case BlockType.Begin:
            {
                BeginBlock block = new BeginBlock();
                block.Data = data;
                result     = block;
            }
            break;

            case BlockType.Action:
            {
                ActionBlock block = new ActionBlock();
                block.Data = data;
                block.Name = data.name;
                result     = block;
            }
            break;

            case BlockType.If:
            {
                IfBlock block = new IfBlock();
                block.Data = data;
                result     = block;
            }
            break;

            case BlockType.While:
            {
                WhileBlock block = new WhileBlock();
                block.Data = data;
                result     = block;
            }
            break;

            case BlockType.Set:
            {
                SetBlock block = new SetBlock();
                block.Data = data;
                result     = block;
            }
            break;
            }

            return(result);
        }
Exemplo n.º 5
0
        private MiraeBuildError BuildWhileBlock(WhileBlock environment, IBlock prev, byte conditionBlockId, out IBlock block)
        {
            ConditionBlock conditionBlock = environment.Condition;

            if (conditionBlock == null)
            {
                MiraeBuildError error = BuildConditionBlock(conditionBlockId, out conditionBlock);
                if (error > MiraeBuildError.Success)
                {
                    block = null;
                    return(error);
                }
            }

            block = new WhileBlock(environment.Id, prev, conditionBlock);
            return(MiraeBuildError.Success);
        }
Exemplo n.º 6
0
        public WhileBlock Build()
        {
            var whileBlock = new WhileBlock(_block);

            throwErrorIfHasNoNextOrNext("incompleted while block;");

            throwErrorIfOperatorTypeNotMatch(OperatorType.LeftParenthesis);

            whileBlock.CheckExpression.Root = new AstNodeBuilder(_reader, _block, _intercepChars).Build();

            if (!whileBlock.CheckExpression.Root.OutputType.IsBool())
            {
                throwError("inter check-expression requires a bool result,but it's not;");
            }

            if (!_reader.HasNext())
            {
                throwError("incompleted while block;");
            }

            whileBlock.Body = new OrderedBlockBuilder(_reader, "while", whileBlock).Build();

            return(whileBlock);
        }
Exemplo n.º 7
0
 private MiraeRuntimeError RunWhile(WhileBlock block)
 {
     return(RunIf(block));
 }