public void Visit(MinusOperator op)
 {
     op.LeftOperand.Accept(this);
     _resultBuilder.Append(" ");
     op.RightOperand.Accept(this);
     _resultBuilder.Append("-");
 }
Example #2
0
        public void MinusOperatorThrows()
        {
            Operator op = new MinusOperator().Minus;

            MeVariable[] arr = { new MeString("test"), new MeNumber(10) };
            Assert.IsFalse(op.CanExecute(arr));
            TestUtils.CustomExceptionTest(() => op.Execute(arr), typeof(MeContextException));
        }
Example #3
0
 public void Visit(MinusOperator node)
 {
     builder.AppendLine($"Minus");
     builder.Indent();
     node.Left.Accept(this);
     node.Right.Accept(this);
     builder.Outdent();
 }
        public void Visit(MinusOperator op)
        {
            op.LeftOperand.Accept(this);
            op.RightOperand.Accept(this);

            var temp = _stack.Pop();
            _stack.Push(_stack.Pop() - temp);
        }
Example #5
0
        public void Visit(MinusOperator node)
        {
            VisitBinary(node);

            ITerminalWrapper leftWrapper  = ReferenceTables.GetValue(node.Left);
            ITerminalWrapper rightWrapper = ReferenceTables.GetValue(node.Right);

            ReferenceTables.SetValue(node, ((dynamic)leftWrapper) - ((dynamic)rightWrapper));
        }
 public void CreateExpression_TwoExpressions_Minus()
 {
     var left = Expression.Constant(10.0);
     var right = Expression.Constant(2.0);
     var op = new MinusOperator();
     var resExpression = op.CreateExpression(left, right);
     var lambda = Expression.Lambda<Func<double>>(resExpression);
     Assert.That(lambda.Compile()(), Is.EqualTo(8.0).Within(1e-5));
 }
        public void Visit(MinusOperator op)
        {
            op.LeftOperand.Accept(this);
            op.RightOperand.Accept(this);

            var temp = _stack.Pop();

            _stack.Push(_stack.Pop() - temp);
        }
Example #8
0
        public void MinusOperator_Calculate()
        {
            var op = new MinusOperator();

            Assert.AreEqual(op.Calculate(9, 3), 6);
            Assert.AreEqual(op.Calculate(9, -3), 12);
            Assert.AreEqual(op.Calculate(-9, 3), -12);
            Assert.AreEqual(op.Calculate(-9, -3), -6);
        }
        public void Visit(MinusOperator minus)
        {
            minus.Left.Accept(this);
            var leftOperand = tacs.Last().Result;

            minus.Right.Accept(this);
            var rightOperand = tacs.Last().Result;

            tacs.Add(Tac.Minus(leftOperand, rightOperand, MakeNewTemp()));
        }
Example #10
0
        public void MinusOperatorCanSubtract()
        {
            Operator op = new MinusOperator().Minus;

            MeVariable[] arr = { new MeNumber(10), new MeNumber(20) };
            Assert.IsTrue(op.CanExecute(arr));
            const float expected = -10.0f;
            float       result   = op.Execute(arr).Get <float>();

            Assert.AreEqual(expected, result);
        }
Example #11
0
 private void CreateChild(ref AbstractTreeElement node, string expression, ref int curNumber)
 {
     if (curNumber >= expression.Length)
         return;
     while (expression[curNumber] == ' ' || expression[curNumber] == ')')
         ++curNumber;
     if (expression[curNumber] == '(')
     {
         Operator newOpertator;
         switch (expression[curNumber + 1])
         {
             case '+':
                 newOpertator = new PlusOperator();
                 break;
             case '-':
                 newOpertator = new MinusOperator();
                 break;
             case '*':
                 newOpertator = new MultiplicationOperator();
                 break;
             case '/':
                 newOpertator = new DivisionOperator();
                 break;
             default:
                 throw new System.Exception();
         }
         curNumber += 2;
         var newNode = newOpertator.LeftChild;
         CreateChild(ref newNode, expression, ref curNumber);
         newOpertator.LeftChild = newNode;
         if (expression[curNumber] == ')')
         {
             ++curNumber;
         }
         newNode = newOpertator.RightChild;
         CreateChild(ref newNode, expression, ref curNumber);
         newOpertator.RightChild = newNode;
         node = newOpertator;
     }
     else
     {
         Operand newOperand = new Operand();
         newOperand.Element = 0;
         while (expression[curNumber] != ' ')
         {
             if (expression[curNumber] == ')')
                 break;
             newOperand.Element = newOperand.Element * 10 + (expression[curNumber] - '0');
             ++curNumber;
         }
         node = newOperand;
     }
 }
Example #12
0
        public void MinusOperator_ShouldPrint()
        {
            var ast = new MinusOperator
                      (
                position: 0,
                left: new Identifier(0, "x"),
                right: new Identifier(0, "y")
                      );

            Assert.That(PrettyPrinter.ToString(ast), Is.EqualTo(
                            @"Minus
    Identifier(x)
    Identifier(y)
"));
        }
Example #13
0
 public void Visit(MinusOperator node)
 {
 }
 public void Visit(MinusOperator op)
 {
     Visit((BinaryOperator)op);
 }
Example #15
0
        private void CreateChild(ref AbstractTreeElement node, string expression, ref int curNumber)
        {
            if (curNumber >= expression.Length)
            {
                return;
            }
            while (expression[curNumber] == ' ' || expression[curNumber] == ')')
            {
                ++curNumber;
            }
            if (expression[curNumber] == '(')
            {
                Operator newOpertator;
                switch (expression[curNumber + 1])
                {
                case '+':
                    newOpertator = new PlusOperator();
                    break;

                case '-':
                    newOpertator = new MinusOperator();
                    break;

                case '*':
                    newOpertator = new MultiplicationOperator();
                    break;

                case '/':
                    newOpertator = new DivisionOperator();
                    break;

                default:
                    throw new System.Exception();
                }
                curNumber += 2;
                var newNode = newOpertator.LeftChild;
                CreateChild(ref newNode, expression, ref curNumber);
                newOpertator.LeftChild = newNode;
                if (expression[curNumber] == ')')
                {
                    ++curNumber;
                }
                newNode = newOpertator.RightChild;
                CreateChild(ref newNode, expression, ref curNumber);
                newOpertator.RightChild = newNode;
                node = newOpertator;
            }
            else
            {
                Operand newOperand = new Operand();
                newOperand.Element = 0;
                while (expression[curNumber] != ' ')
                {
                    if (expression[curNumber] == ')')
                    {
                        break;
                    }
                    newOperand.Element = newOperand.Element * 10 + (expression[curNumber] - '0');
                    ++curNumber;
                }
                node = newOperand;
            }
        }
 public void Visit(MinusOperator op)
 {
     List<Type> parenthesisOperators = new List<Type> { typeof(MinusOperator), typeof(MultiplicationOperator), typeof(DivisionOperator) };
     FormatBinaryOperator(op, ParenthesisNeeded(parenthesisOperators));
 }
Example #17
0
 public void Visit(MinusOperator node)
 {
     VisitBinary(node);
     CheckOperandsAreOfSameType(node);
     CheckOperandsRestrictedToTypes(node, new Number().GetType());
 }
Example #18
0
        /// <summary>
        /// ソースコードを解析してトークン列を生成
        /// </summary>
        /// <param name="targets"></param>
        /// <returns></returns>
        private static IEnumerable <Token> Analysis(string targets)
        {
            if (targets.EndsWith(";") == false || targets.EndsWith("\r\n") == false)
            {
                targets += "\r\n";
            }
            while (targets.Length > 0)
            {
                //スペースとタブは変換しない
                if (targets[0] == ' ' || targets[0] == '\t')
                {
                    targets = targets.Remove(0, 1);
                    continue;
                }

                if (targets[0] == '\"')
                {
                    var literal = targets
                                  .Skip(1)
                                  .TakeWhile(c => c != '\"')
                                  .Aggregate(new StringBuilder(), (sb, c) => sb.Append(c))
                                  .ToString();
                    targets = targets.Remove(0, literal.Length + 2);
                    yield return(new StringLiteral(literal));

                    continue;
                }

                if (LeftParenthesis.IsMatch(targets[0]))
                {
                    targets = targets.Remove(0, 1);
                    yield return(new LeftParenthesis());

                    continue;
                }

                if (RightParenthesis.IsMatch(targets[0]))
                {
                    targets = targets.Remove(0, 1);
                    yield return(new RightParenthesis());

                    continue;
                }

                if (BlockBegin.IsMatch(targets[0]))
                {
                    targets = targets.Remove(0, 1);
                    yield return(new BlockBegin());

                    continue;
                }

                if (BlockEnd.IsMatch(targets[0]))
                {
                    targets = targets.Remove(0, 1);
                    yield return(new BlockEnd());

                    continue;
                }

                if (targets.StartsWith("Writeln"))
                {
                    targets = targets.Remove(0, 7);
                    yield return(new StandardOutput());

                    continue;
                }

                if (targets.StartsWith("Readln()"))
                {
                    targets = targets.Remove(0, 8);
                    yield return(new StandardInput());

                    continue;
                }

                if (Int32Literal.SpecifiedCollection.Contains(targets[0]))
                {
                    var value = targets.TakeWhile(c => Int32Literal.SpecifiedCollection.Contains(c)).ToArray();
                    targets = targets.Remove(0, value.Length);
                    yield return(new Int32Literal(new string(value)));

                    continue;
                }

                if (EndOfLine.IsMatch(targets[0].ToString()) || EndOfLine.IsMatch(new string(targets.Take(2).ToArray())))
                {
                    var value = targets.TakeWhile(i => i == ';' || i == '\r' || i == '\n').Count();
                    targets = targets.Remove(0, value);
                    yield return(new EndOfLine());

                    continue;
                }

                if (targets.Length > 1 && Assign.IsMatch(targets[0]) && !Assign.IsMatch(targets[1]))
                {
                    targets = targets.Remove(0, 1);
                    yield return(new Assign());

                    continue;
                }

                if (targets.Length > 1 && AddAssign.IsMatch(targets[0], targets[1]))
                {
                    targets = targets.Remove(0, 2);
                    yield return(new AddAssign());

                    continue;
                }

                if (targets.StartsWith("var "))
                {
                    var varVariableName = targets.Skip(4)
                                          .TakeWhile(c => c != ' ' && c != '\t' && c != '=')
                                          .Aggregate(new StringBuilder(), (sb, c) => sb.Append(c))
                                          .ToString();
                    if (!(JudgeWord(varVariableName) is Identifier))
                    {
                        throw new Exception("変数にキーワードが使われています。");
                    }
                    targets = targets.Remove(0, 4 + varVariableName.Length);
                    yield return(new VarDeclaration(varVariableName));

                    continue;
                }

                if (PlusOperator.IsMatch(targets[0]))
                {
                    targets = targets.Remove(0, 1);
                    yield return(new PlusOperator());

                    continue;
                }

                if (MinusOperator.IsMatch(targets[0]))
                {
                    targets = targets.Remove(0, 1);
                    yield return(new MinusOperator());

                    continue;
                }

                if (MultiplyOperator.IsMatch(targets[0]))
                {
                    targets = targets.Remove(0, 1);
                    yield return(new MultiplyOperator());

                    continue;
                }

                if (DivideOperator.IsMatch(targets[0]))
                {
                    targets = targets.Remove(0, 1);
                    yield return(new DivideOperator());

                    continue;
                }

                if (ModuloOperator.IsMatch(targets[0]))
                {
                    targets = targets.Remove(0, 1);
                    yield return(new ModuloOperator());

                    continue;
                }

                if (targets.Length > 1 && targets[0] == '=' && targets[1] == '=')
                {
                    targets = targets.Remove(0, 2);
                    yield return(new EqualOperator());

                    continue;
                }

                if (targets.Length > 1 && targets[0] == '!' && targets[1] == '=')
                {
                    targets = targets.Remove(0, 2);
                    yield return(new NotEqualOperator());

                    continue;
                }

                if (targets.Length > 1 && targets[0] == '>')
                {
                    if (targets[1] == '=')
                    {
                        targets = targets.Remove(0, 2);
                        yield return(new GreaterThanOrEqualOperator());

                        continue;
                    }
                    targets = targets.Remove(0, 1);
                    yield return(new GreaterThanOperator());

                    continue;
                }

                if (targets.Length > 1 && targets[0] == '<')
                {
                    if (targets[1] == '=')
                    {
                        targets = targets.Remove(0, 2);
                        yield return(new LessThanOrEqualOperator());

                        continue;
                    }
                    targets = targets.Remove(0, 1);
                    yield return(new LessThanOperator());

                    continue;
                }

                if (targets.Length > 1 && targets[0] == '&' && targets[1] == '&')
                {
                    targets = targets.Remove(0, 2);
                    yield return(new AndAlsoOperator());

                    continue;
                }

                if (targets.Length > 1 && targets[0] == '|' && targets[1] == '|')
                {
                    targets = targets.Remove(0, 2);
                    yield return(new OrElseOperator());

                    continue;
                }

                var word = targets.TakeWhile(IdentifierTake)
                           .Aggregate(new StringBuilder(), (sb, c) => sb.Append(c))
                           .ToString();
                if (word.Length == 0)
                {
                    throw new Exception("変数名なのに文字数がゼロ");
                }
                targets = targets.Remove(0, word.Length);
                yield return(JudgeWord(word));
            }
        }