Exemplo n.º 1
0
        /// <inheritdoc />
        public override void VisitIntervalFloatValue(FloatIntervalValue value)
        {
            // When comparing, both operands are converted to boolean
            switch (operation)
            {
            case Operations.BitOr:
            case Operations.BitXor:
                IntervalValue <int> integerInterval;
                if (TypeConversion.TryConvertToIntegerInterval(OutSet, value, out integerInterval))
                {
                    result = integerInterval;
                }
                else
                {
                    result = OutSet.AnyIntegerValue;
                }
                break;

            case Operations.Mod:
                result = ModuloOperation.Modulo(flow, TypeConversion.ToInteger(leftOperand), value);
                break;

            default:
                result = ArithmeticOperation.Arithmetic(flow, operation,
                                                        TypeConversion.ToFloat(leftOperand), value);
                if (result != null)
                {
                    break;
                }

                base.VisitIntervalFloatValue(value);
                break;
            }
        }
Exemplo n.º 2
0
    static void Main(string[] args)
    {
        ArithmeticOperation.ArOpDel additionDelegate = ArithmeticOperation.Addition;

        Console.WriteLine(ArithmeticOperation.ConvertResultToString(additionDelegate, 5, 6));
        Console.ReadKey();
    }
Exemplo n.º 3
0
        public void VisitArithmeticOperation(ArithmeticOperation node)
        {
            node.LeftOperand.Accept(this);
            node.RightOperand.Accept(this);

            switch (node.Operator.Tag)
            {
            case TokenTag.PLUS: _emitter.Emit(OpCodes.Add); return;

            case TokenTag.MINUS: _emitter.Emit(OpCodes.Sub); return;

            case TokenTag.MULTIPLY: _emitter.Emit(OpCodes.Mul); return;

            case TokenTag.DIVIDE: _emitter.Emit(OpCodes.Div); return;

            case TokenTag.MOD: _emitter.Emit(OpCodes.Rem); return;

            case TokenTag.LEFT_SHIFT: _emitter.Emit(OpCodes.Shl); return;

            case TokenTag.RIGHT_SHIFT: _emitter.Emit(OpCodes.Shr); return;

            case TokenTag.BIT_OR: _emitter.Emit(OpCodes.Or); return;

            case TokenTag.BIT_AND: _emitter.Emit(OpCodes.And); return;

            case TokenTag.BIT_XOR: _emitter.Emit(OpCodes.Xor); return;
            }

            throw new SemanticException("Not supported operator: {0}", node.Operator);
        }
Exemplo n.º 4
0
        /// <inheritdoc />
        public override void VisitStringValue(StringValue value)
        {
            result = Comparison.AbstractCompare(OutSet, operation);
            if (result != null)
            {
                return;
            }

            int    integerValue;
            double floatValue;
            bool   isInteger;

            TypeConversion.TryConvertToNumber(value.Value, true, out integerValue,
                                              out floatValue, out isInteger);

            result = isInteger
                ? ArithmeticOperation.LeftAbstractArithmetic(flow, operation, integerValue)
                : ArithmeticOperation.LeftAbstractArithmetic(flow, operation, floatValue);

            if (result != null)
            {
                return;
            }

            base.VisitStringValue(value);
        }
Exemplo n.º 5
0
        /// <inheritdoc />
        public override void VisitIntervalFloatValue(FloatIntervalValue value)
        {
            switch (operation)
            {
            case Operations.Identical:
                result = OutSet.CreateBool(false);
                break;

            case Operations.NotIdentical:
                result = OutSet.CreateBool(true);
                break;

            case Operations.Mod:
                result = ModuloOperation.AbstractModulo(flow, value);
                break;

            default:
                result = ArithmeticOperation.LeftAbstractArithmetic(flow, operation, value);
                if (result != null)
                {
                    break;
                }

                base.VisitIntervalFloatValue(value);
                break;
            }
        }
Exemplo n.º 6
0
        /// <inheritdoc />
        public override void VisitAnyStringValue(AnyStringValue value)
        {
            switch (operation)
            {
            case Operations.Identical:
                result = OutSet.CreateBool(false);
                break;

            case Operations.NotIdentical:
                result = OutSet.CreateBool(true);
                break;

            case Operations.Mod:
                result = ModuloOperation.AbstractModulo(flow);
                break;

            default:
                result = Comparison.AbstractCompare(OutSet, operation);
                if (result != null)
                {
                    break;
                }

                result = ArithmeticOperation.AbstractFloatArithmetic(Snapshot, operation);
                if (result != null)
                {
                    // A string can be converted into floating point number too.
                    break;
                }

                base.VisitAnyStringValue(value);
                break;
            }
        }
Exemplo n.º 7
0
        /// <inheritdoc />
        public override void VisitAssociativeArray(AssociativeArray value)
        {
            result = Comparison.RightAlwaysGreater(OutSet, operation);
            if (result != null)
            {
                return;
            }

            result = LogicalOperation.Logical(OutSet, operation, leftOperand,
                                              TypeConversion.ToNativeBoolean(Snapshot, value));
            if (result != null)
            {
                return;
            }

            result = BitwiseOperation.Bitwise(OutSet, operation);
            if (result != null)
            {
                return;
            }

            if (ArithmeticOperation.IsArithmetic(operation))
            {
                // TODO: This must be fatal error
                SetWarning("Unsupported operand type: Arithmetic of array and numeric type");
                result = OutSet.AnyValue;
                return;
            }

            base.VisitAssociativeArray(value);
        }
Exemplo n.º 8
0
 public IActionResult Index(ArithmeticOperation model, string btnAction)
 {
     if (btnAction == "+")
     {
         ViewBag.ans = model.no1 + model.no2;
     }
     else if (btnAction == "-")
     {
         ViewBag.ans = model.no1 - model.no2;
     }
     else if (btnAction == "*")
     {
         ViewBag.ans = model.no1 * model.no2;
     }
     else
     {
         if (model.no1 != 0 && model.no2 != 0)
         {
             ViewBag.ans = model.no1 / model.no2;
         }
         else
         {
             ViewBag.ans = 0;
         }
     }
     return(View());
 }
Exemplo n.º 9
0
    public double Calculate(string text)
    {
        var parsedValues = Parse(text);

        switch (parsedValues.Item2)
        {
        case "+":
            Operation = ArithmeticOperation.Addition;
            return(parsedValues.Item1 + parsedValues.Item3);

        case "-":
            Operation = ArithmeticOperation.Subtraction;
            return(parsedValues.Item1 - parsedValues.Item3);

        case "*":
            Operation = ArithmeticOperation.Multiplication;
            return(parsedValues.Item1 * parsedValues.Item3);

        case "/":
            Operation = ArithmeticOperation.Division;
            if (parsedValues.Item3 == 0)
            {
                throw new DivideByZeroException("Can't divide by 0");
            }
            return((parsedValues.Item1 * (1.0d)) / ((1.0d) * parsedValues.Item3));
        }
        return(0.0);
    }
Exemplo n.º 10
0
        /// <inheritdoc />
        public override void VisitAnyStringValue(AnyStringValue value)
        {
            switch (operation)
            {
            case Operations.Equal:
            case Operations.NotEqual:
            case Operations.GreaterThanOrEqual:
            case Operations.LessThan:
            case Operations.Or:
            case Operations.Xor:
                result = OutSet.AnyBooleanValue;
                break;

            case Operations.Mod:
                result = ModuloOperation.AbstractModulo(flow);
                break;

            case Operations.BitOr:
            case Operations.BitXor:
                result = OutSet.AnyIntegerValue;
                break;

            default:
                result = ArithmeticOperation.AbstractFloatArithmetic(Snapshot, operation);
                if (result != null)
                {
                    break;
                }

                base.VisitAnyStringValue(value);
                break;
            }
        }
Exemplo n.º 11
0
        /// <inheritdoc />
        public override void VisitAnyObjectValue(AnyObjectValue value)
        {
            var rightBoolean = TypeConversion.ToBoolean(value);

            result = Comparison.Compare(OutSet, operation, leftOperand.Value, rightBoolean);
            if (result != null)
            {
                return;
            }

            result = ArithmeticOperation.RightAbstractArithmetic(flow, operation,
                                                                 TypeConversion.ToInteger(leftOperand.Value));
            if (result != null)
            {
                SetWarning("Object cannot be converted to integer by arithmetic operation",
                           AnalysisWarningCause.OBJECT_CONVERTED_TO_INTEGER);
                return;
            }

            result = LogicalOperation.Logical(OutSet, operation, leftOperand.Value, rightBoolean);
            if (result != null)
            {
                return;
            }

            base.VisitAnyObjectValue(value);
        }
Exemplo n.º 12
0
        private IComparable PerformArithematicOperation(IEvaluable evaluable, IJSONDocument document,
                                                        ArithmeticOperation operation)
        {
            IJsonValue value1, value2;

            if (!Evaluate(out value1, document))
            {
                return(null);
            }
            if (!evaluable.Evaluate(out value2, document))
            {
                return(null);
            }

            TypeCode      actualType1, actualType2;
            FieldDataType fieldDataType1 = JSONType.GetJSONType(value1.Value, out actualType1);
            FieldDataType fieldDataType2 = JSONType.GetJSONType(value2.Value, out actualType2);

            if (fieldDataType1.CompareTo(fieldDataType2) != 0)
            {
                return(null);
            }

            return(Evaluator.PerformArithmeticOperation(value1, actualType1, value2, actualType2, operation,
                                                        fieldDataType1));
        }
Exemplo n.º 13
0
        /// <inheritdoc />
        public override void VisitAnyArrayValue(AnyArrayValue value)
        {
            switch (operation)
            {
            case Operations.Mod:
                result = ModuloOperation.AbstractModulo(flow);
                break;

            default:
                result = BitwiseOperation.Bitwise(OutSet, operation);
                if (result != null)
                {
                    break;
                }

                if (ArithmeticOperation.IsArithmetic(operation))
                {
                    // TODO: This must be fatal error
                    SetWarning("Unsupported operand type: Arithmetic of array and scalar type");
                    result = OutSet.AnyValue;
                    break;
                }

                base.VisitAnyArrayValue(value);
                break;
            }
        }
Exemplo n.º 14
0
        /// <inheritdoc />
        public override void VisitIntervalFloatValue(FloatIntervalValue value)
        {
            switch (operation)
            {
            case Operations.Identical:
                result = OutSet.CreateBool(false);
                break;

            case Operations.NotIdentical:
                result = OutSet.CreateBool(true);
                break;

            case Operations.Mod:
                result = ModuloOperation.Modulo(flow, leftOperand, value);
                break;

            default:
                var floatInterval = TypeConversion.ToFloatInterval(OutSet, leftOperand);
                result = Comparison.IntervalCompare(OutSet, operation, floatInterval, value);
                if (result != null)
                {
                    break;
                }

                result = ArithmeticOperation.Arithmetic(flow, operation, leftOperand, value);
                if (result != null)
                {
                    break;
                }

                base.VisitIntervalFloatValue(value);
                break;
            }
        }
Exemplo n.º 15
0
        /// <inheritdoc />
        public override void VisitUndefinedValue(UndefinedValue value)
        {
            switch (operation)
            {
            case Operations.Identical:
            case Operations.And:
                result = OutSet.CreateBool(false);
                break;

            case Operations.NotIdentical:
                result = OutSet.CreateBool(true);
                break;

            case Operations.BitAnd:
                result = OutSet.CreateInt(0);
                break;

            case Operations.Div:
                result = ArithmeticOperation.DivisionByNull(flow);
                break;

            case Operations.Mod:
                result = ModuloOperation.ModuloByNull(flow);
                break;

            default:
                base.VisitUndefinedValue(value);
                break;
            }
        }
Exemplo n.º 16
0
        /// <inheritdoc />
        public override void VisitBooleanValue(BooleanValue value)
        {
            var leftBoolean = TypeConversion.ToBoolean(leftOperand.Value);

            result = Comparison.Compare(OutSet, operation, leftBoolean, value.Value);
            if (result != null)
            {
                return;
            }

            var rightInteger = TypeConversion.ToInteger(value.Value);

            result = ArithmeticOperation.Arithmetic(flow, operation,
                                                    leftOperand.Value, rightInteger);
            if (result != null)
            {
                return;
            }

            result = LogicalOperation.Logical(OutSet, operation, leftBoolean, value.Value);
            if (result != null)
            {
                return;
            }

            result = BitwiseOperation.Bitwise(OutSet, operation, leftOperand.Value, rightInteger);
            if (result != null)
            {
                return;
            }

            base.VisitBooleanValue(value);
        }
Exemplo n.º 17
0
        /// <inheritdoc />
        public override void VisitIntegerValue(IntegerValue value)
        {
            switch (operation)
            {
            case Operations.Identical:
                result = Comparison.Equal(OutSet, leftOperand, value.Value);
                break;

            case Operations.NotIdentical:
                result = Comparison.NotEqual(OutSet, leftOperand, value.Value);
                break;

            case Operations.Mod:
                result = ModuloOperation.Modulo(flow, leftOperand, value.Value);
                break;

            default:
                result = Comparison.IntervalCompare(OutSet, operation, leftOperand, value.Value);
                if (result != null)
                {
                    break;
                }

                result = ArithmeticOperation.Arithmetic(flow, operation, leftOperand, value.Value);
                if (result != null)
                {
                    break;
                }

                base.VisitIntegerValue(value);
                break;
            }
        }
Exemplo n.º 18
0
        /// <inheritdoc />
        public override void VisitIntervalIntegerValue(IntegerIntervalValue value)
        {
            // When comparing, both operands are converted to boolean
            switch (operation)
            {
            case Operations.BitOr:
            case Operations.BitXor:
                result = value;
                break;

            case Operations.Mod:
                result = ModuloOperation.Modulo(flow, TypeConversion.ToInteger(leftOperand), value);
                break;

            default:
                result = ArithmeticOperation.Arithmetic(flow, operation,
                                                        TypeConversion.ToInteger(leftOperand), value);
                if (result != null)
                {
                    break;
                }

                base.VisitIntervalIntegerValue(value);
                break;
            }
        }
Exemplo n.º 19
0
        /// <inheritdoc />
        public override void VisitAnyObjectValue(AnyObjectValue value)
        {
            if (Comparison.IsOperationComparison(operation))
            {
                // The comparison of string with object depends upon whether the object has
                // the "__toString" magic method implemented. If so, the string comparison is
                // performed. Otherwise, the object is always greater than string. Since we cannot
                // determine whether the abstract object has or has not the method,
                // we must return indeterminate boolean value.
                result = OutSet.AnyBooleanValue;
                return;
            }

            result = LogicalOperation.AbstractLogical(OutSet, operation, TypeConversion.ToBoolean(value));
            if (result != null)
            {
                return;
            }

            result = ArithmeticOperation.AbstractFloatArithmetic(Snapshot, operation);
            if (result != null)
            {
                SetWarning("Object cannot be converted to integer by arithmetic operation",
                           AnalysisWarningCause.OBJECT_CONVERTED_TO_INTEGER);
                return;
            }

            base.VisitAnyObjectValue(value);
        }
Exemplo n.º 20
0
        /// <inheritdoc />
        public override void VisitAnyBooleanValue(AnyBooleanValue value)
        {
            switch (operation)
            {
            case Operations.Mod:
                SetWarning("Object cannot be converted to integer by modulo operation",
                           AnalysisWarningCause.OBJECT_CONVERTED_TO_INTEGER);
                result = ModuloOperation.ModuloByAnyBooleanValue(flow);
                break;

            default:
                result = Comparison.RightAbstractBooleanCompare(OutSet, operation,
                                                                TypeConversion.ToBoolean(leftOperand));
                if (result != null)
                {
                    break;
                }

                result = ArithmeticOperation.RightAbstractBooleanArithmetic(flow, operation);
                if (result != null)
                {
                    SetWarning("Object cannot be converted to integer by arithmetic operation",
                               AnalysisWarningCause.OBJECT_CONVERTED_TO_INTEGER);
                    break;
                }

                base.VisitAnyBooleanValue(value);
                break;
            }
        }
Exemplo n.º 21
0
        /// <inheritdoc />
        public override void VisitAnyStringValue(AnyStringValue value)
        {
            switch (operation)
            {
            case Operations.Mod:
                SetWarning("Object cannot be converted to integer by modulo operation",
                           AnalysisWarningCause.OBJECT_CONVERTED_TO_INTEGER);
                result = ModuloOperation.AbstractModulo(flow);
                break;

            default:
                if (Comparison.IsOperationComparison(operation))
                {
                    // TODO: The comparison of string with object depends upon whether the object has
                    // the "__toString" magic method implemented. If so, the string comparison is
                    // performed. Otherwise, the object is always greater than string.
                    result = OutSet.AnyBooleanValue;
                    break;
                }

                result = ArithmeticOperation.AbstractFloatArithmetic(Snapshot, operation);
                if (result != null)
                {
                    // A string can be converted into floating point number too.
                    break;
                }

                base.VisitAnyStringValue(value);
                break;
            }
        }
Exemplo n.º 22
0
        /// <inheritdoc />
        public override void VisitIntervalFloatValue(FloatIntervalValue value)
        {
            switch (operation)
            {
            case Operations.Mod:
                SetWarning("Object cannot be converted to integer by modulo operation",
                           AnalysisWarningCause.OBJECT_CONVERTED_TO_INTEGER);
                result = ModuloOperation.AbstractModulo(flow, value);
                break;

            default:
                result = ArithmeticOperation.LeftAbstractArithmetic(flow, operation, value);
                if (result != null)
                {
                    break;
                }

                result = LogicalOperation.Logical(OutSet, operation,
                                                  TypeConversion.ToBoolean(leftOperand), value);
                if (result != null)
                {
                    break;
                }

                base.VisitIntervalFloatValue(value);
                break;
            }
        }
Exemplo n.º 23
0
        /// <inheritdoc />
        public override void VisitAnyResourceValue(AnyResourceValue value)
        {
            result = Comparison.AbstractCompare(OutSet, operation);
            if (result != null)
            {
                // Comapring of resource and string makes no sence.
                return;
            }

            result = LogicalOperation.Logical(OutSet, operation,
                                              TypeConversion.ToBoolean(leftOperand.Value), TypeConversion.ToBoolean(value));
            if (result != null)
            {
                return;
            }

            int    integerValue;
            double floatValue;
            bool   isInteger;

            TypeConversion.TryConvertToNumber(leftOperand.Value, true,
                                              out integerValue, out floatValue, out isInteger);

            result = isInteger
                ? ArithmeticOperation.RightAbstractArithmetic(flow, operation, integerValue)
                : ArithmeticOperation.RightAbstractArithmetic(flow, operation, floatValue);

            if (result != null)
            {
                // Arithmetic with resources is nonsence
                return;
            }

            base.VisitAnyResourceValue(value);
        }
Exemplo n.º 24
0
 // Recursively call the lower arithmetic stage until a primitive
 private Func <AbstractExpressionNode> ArithmeticOperation(ArithmeticLevel level)
 {
     return(() =>
     {
         if (level == ArithmeticLevel.Primitive)
         {
             return ArithmeticPrimitive();
         }
         return ExecuteIfOperator
         (
             ArithmeticOperation(level - 1)(),
             ArithmeticOperation(level),
             GetOperators(level)
         );
     });
 }
Exemplo n.º 25
0
        /// <inheritdoc />
        public override void VisitAnyScalarValue(AnyScalarValue value)
        {
            result = Comparison.AbstractCompare(OutSet, operation);
            if (result != null)
            {
                // Ommitted warning message that object cannot be converted to integer
                return;
            }

            result = ArithmeticOperation.AbstractFloatArithmetic(Snapshot, operation);
            if (result != null)
            {
                // Ommitted warning message that object cannot be converted to integer
                // Ommitted error report that array is unsupported operand in arithmetic operation
                return;
            }

            result = LogicalOperation.AbstractLogical(OutSet, operation);
            if (result != null)
            {
                return;
            }

            result = BitwiseOperation.Bitwise(OutSet, operation);
            if (result != null)
            {
                // Ommitted warning message that object cannot be converted to integer
                return;
            }

            base.VisitAnyScalarValue(value);
        }
Exemplo n.º 26
0
        /// <inheritdoc />
        public override void VisitAnyResourceValue(AnyResourceValue value)
        {
            var rightBoolean = TypeConversion.ToBoolean(value);

            result = Comparison.Compare(OutSet, operation, leftOperand.Value, rightBoolean);
            if (result != null)
            {
                return;
            }

            result = LogicalOperation.Logical(OutSet, operation, leftOperand.Value, rightBoolean);
            if (result != null)
            {
                return;
            }

            result = ArithmeticOperation.RightAbstractArithmetic(flow, operation,
                                                                 TypeConversion.ToInteger(leftOperand.Value));
            if (result != null)
            {
                // Arithmetic with resources is nonsence
                return;
            }

            base.VisitAnyResourceValue(value);
        }
Exemplo n.º 27
0
        /// <inheritdoc />
        public override void VisitGenericIntervalValue <T>(IntervalValue <T> value)
        {
            switch (operation)
            {
            case Operations.Identical:
                result = OutSet.CreateBool(false);
                break;

            case Operations.NotIdentical:
                result = OutSet.CreateBool(true);
                break;

            default:
                result = Comparison.AbstractCompare(OutSet, operation);
                if (result != null)
                {
                    break;
                }

                result = ArithmeticOperation.AbstractFloatArithmetic(Snapshot, operation);
                if (result != null)
                {
                    break;
                }

                base.VisitGenericIntervalValue(value);
                break;
            }
        }
Exemplo n.º 28
0
        /// <inheritdoc />
        public override void VisitStringValue(StringValue value)
        {
            switch (operation)
            {
            case Operations.Mod:
                result = ModuloOperation.Modulo(flow, leftOperand, value.Value);
                break;

            default:
                int    integerValue;
                double floatValue;
                bool   isInteger;
                TypeConversion.TryConvertToNumber(value.Value, true,
                                                  out integerValue, out floatValue, out isInteger);

                result = Comparison.IntervalCompare(OutSet, operation, leftOperand, floatValue);
                if (result != null)
                {
                    break;
                }

                result = ArithmeticOperation.Arithmetic(flow, operation, leftOperand, floatValue);
                if (result != null)
                {
                    break;
                }

                base.VisitStringValue(value);
                break;
            }
        }
 public ArithmeticExpression(ArithmeticOperation op, TextExpression exp1, TextExpression exp2)
 {
     this._op      = op;
     this._exp1    = exp1;
     this._exp2    = exp2;
     this.RawValue = exp1.RawValue + (object)op + exp2.RawValue;
 }
Exemplo n.º 30
0
        /// <inheritdoc />
        public override void VisitAnyValue(AnyValue value)
        {
            switch (operation)
            {
            case Operations.Identical:
            case Operations.NotIdentical:
                result = OutSet.AnyBooleanValue;
                break;

            case Operations.Mod:
                // Ommitted warning message that object cannot be converted to integer
                result = ModuloOperation.AbstractModulo(flow);
                break;

            default:
                result = ArithmeticOperation.AbstractFloatArithmetic(Snapshot, operation);
                if (result != null)
                {
                    // Ommitted warning message that object cannot be converted to integer
                    // Ommitted error report that array is unsupported operand in arithmetic operation
                    break;
                }

                result = BitwiseOperation.Bitwise(OutSet, operation);
                if (result != null)
                {
                    // Ommitted warning message that object cannot be converted to integer
                    break;
                }

                base.VisitAnyValue(value);
                break;
            }
        }
Exemplo n.º 31
0
 public NumberChain(byte number, NumberChain previous  = null, ArithmeticOperation? operation = null, ushort? result = null)
 {
     Number = number;
     Previous = previous;
     if (Previous != null)
         Operation = operation;
     Result = result;
     if (Result == null && Previous != null && Operation != null)
         Result = Operation.Value.GetResult(Previous.Result ?? Previous.Number, Number);
 }
Exemplo n.º 32
0
        public virtual TreeNode OnPerformArithmetic(TreeNode [] args, ArithmeticOperation operation)
        {
            double result = 0.0;
            bool as_int = true;

            for(int i = 0; i < args.Length; i++) {
                TreeNode arg = Evaluate(args[i]);

                if(arg is IntLiteral || arg is DoubleLiteral) {
                    double arg_value;

                    if(arg is DoubleLiteral) {
                        as_int = false;
                        arg_value = (arg as DoubleLiteral).Value;
                    } else {
                        arg_value = (int)(arg as IntLiteral).Value;
                    }

                    if(i == 0) {
                        result = arg_value;
                        continue;
                    }

                    switch(operation) {
                        case ArithmeticOperation.Add:
                            result += arg_value;
                            break;
                        case ArithmeticOperation.Subtract:
                            result -= arg_value;
                            break;
                        case ArithmeticOperation.Multiply:
                            result *= arg_value;
                            break;
                        case ArithmeticOperation.Divide:
                            result /= arg_value;
                            break;
                        case ArithmeticOperation.Modulo:
                            if(!(arg is IntLiteral)) {
                                throw new ArgumentException("Modulo requires int arguments");
                            }

                            result %= (int)arg_value;
                            break;
                    }
                } else {
                    throw new ArgumentException("arguments must be double or int");
                }
            }

            return as_int ?
                ((TreeNode)new IntLiteral((int)result)) :
                ((TreeNode)new DoubleLiteral(result));
        }
Exemplo n.º 33
0
        private void ArithmeticPaste(ArithmeticOperation operation)
        {
            if (_systemInterface.Clipboard == null) {
                return;
            }

            var clipboard = _systemInterface.Clipboard;
            var height = clipboard.GetLength(Utils.IndexRowsOrHeight);
            var width = clipboard.GetLength(Utils.IndexColsOrWidth);

            AddUndoItem(new Rectangle(_selectedCells.X, _selectedCells.Y, width, height), UndoOriginalBehavior.Overwrite,
                Resources.UndoText_ArithmeticPaste);

            var minLevel = _sequence.MinimumLevel;
            var maxLevel = _sequence.MaximumLevel;
            var rowOffset = _selectedCells.Top;
            var colOffset = _selectedCells.Left;
            for (var row = 0; (row < height) && ((rowOffset + row) < _sequence.ChannelCount); row++) {
                var channel = GetEventFromChannelNumber(rowOffset + row);
                for (var col = 0; (col < width) && ((colOffset + col) < _sequence.TotalEventPeriods); col++) {
                    var currentValue = _sequence.EventValues[channel, colOffset + col];
                    var clipValue = clipboard[row, col];
                    switch (operation) {
                        case ArithmeticOperation.Addition:
                            currentValue = (byte) Math.Min(currentValue + clipValue, maxLevel);
                            break;

                        case ArithmeticOperation.Subtraction:
                            currentValue = (byte) Math.Max(currentValue - clipValue, minLevel);
                            break;

                        case ArithmeticOperation.Scale:
                            currentValue = (byte) Math.Max(Math.Min(currentValue * (clipValue / (float) Utils.Cell8BitMax), maxLevel), minLevel);
                            break;

                        case ArithmeticOperation.Min:
                            currentValue = Math.Max(Math.Min(clipValue, currentValue), minLevel);
                            break;

                        case ArithmeticOperation.Max:
                            currentValue = Math.Min(Math.Max(clipValue, currentValue), maxLevel);
                            break;
                    }
                    _sequence.EventValues[channel, colOffset + col] = currentValue;
                }
            }
            IsDirty = true;
            pictureBoxGrid.Refresh();
        }
Exemplo n.º 34
0
 public ArithmeticResult Substract(ArithmeticOperation operation)
 {
     return new ArithmeticResult() { Result = operation.FirstOperand - operation.SecondOperand };
 }
Exemplo n.º 35
0
 public ArithmeticResult Add(ArithmeticOperation operation)
 {
     return new ArithmeticResult() { Result = operation.FirstOperand + operation.SecondOperand };
 }
Exemplo n.º 36
0
		public ArithmeticInstruction(int offset, ArithmeticOperation operation, Operand destination, Operand source)
			: base(offset, destination, source)
		{
			this.operation = operation;
		}
Exemplo n.º 37
0
        private static DataArray<double> PerformArithmeticOperation(DataArray<double> data, double value, ArithmeticOperation operation)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data", "DataArray<double> must not be null.");
            }
            var array = new DataArray<double>(data.Count);
            for (int i = 0; i < data.StartIndex; i++)
            {
                array.Add(0.0);
            }
            switch (operation)
            {
                case ArithmeticOperation.Negation:
                    for (int j = data.StartIndex; j < data.Count; j++)
                    {
                        array.Add(-data[j]);
                    }
                    break;

                case ArithmeticOperation.Addition:
                    for (int k = data.StartIndex; k < data.Count; k++)
                    {
                        array.Add(data[k] + value);
                    }
                    break;

                case ArithmeticOperation.Subtraction:
                    for (int m = data.StartIndex; m < data.Count; m++)
                    {
                        array.Add(data[m] - value);
                    }
                    break;

                case ArithmeticOperation.Multiplication:
                    for (int n = data.StartIndex; n < data.Count; n++)
                    {
                        array.Add(data[n] * value);
                    }
                    break;

                case ArithmeticOperation.Division:
                    for (int num6 = data.StartIndex; num6 < data.Count; num6++)
                    {
                        array.Add(data[num6] / value);
                    }
                    break;

                case ArithmeticOperation.Modulus:
                    for (int num7 = data.StartIndex; num7 < data.Count; num7++)
                    {
                        array.Add(data[num7] % value);
                    }
                    break;

                case ArithmeticOperation.Power:
                    for (int num8 = data.StartIndex; num8 < data.Count; num8++)
                    {
                        array.Add(Math.Pow(data[num8], value));
                    }
                    break;
            }
            array.StartIndex = data.StartIndex;
            return array;
        }
Exemplo n.º 38
0
        private static DataArray<double> PerformArithmeticOperation(DataArray<double> data1, DataArray<double> data2,
                                                            ArithmeticOperation operation)
        {
            if (data1 == null)
            {
                throw new ArgumentNullException("data1", "DataArray<double> must not be null.");
            }
            if (data2 == null)
            {
                throw new ArgumentNullException("data2", "DataArray<double> must not be null.");
            }
            if (data1.Count != data2.Count)
            {
                throw new ArgumentException("Array sizes do not match.", "data1");
            }
            var array = new DataArray<double>(data1.Count);
            DataArray<double> array2 = data2;
            if (data1.StartIndex > data2.StartIndex)
            {
                array2 = data1;
            }
            for (int i = 0; i < array2.StartIndex; i++)
            {
                array.Add(0.0);
            }
            switch (operation)
            {
                case ArithmeticOperation.Addition:
                    for (int j = array2.StartIndex; j < array2.Count; j++)
                    {
                        array.Add(data1[j] + data2[j]);
                    }
                    break;

                case ArithmeticOperation.Subtraction:
                    for (int k = array2.StartIndex; k < array2.Count; k++)
                    {
                        array.Add(data1[k] - data2[k]);
                    }
                    break;

                case ArithmeticOperation.Multiplication:
                    for (int m = array2.StartIndex; m < array2.Count; m++)
                    {
                        array.Add(data1[m] * data2[m]);
                    }
                    break;

                case ArithmeticOperation.Division:
                    for (int n = array2.StartIndex; n < array2.Count; n++)
                    {
                        array.Add(data1[n] / data2[n]);
                    }
                    break;

                case ArithmeticOperation.Modulus:
                    for (int num6 = array2.StartIndex; num6 < array2.Count; num6++)
                    {
                        array.Add(data1[num6] % data2[num6]);
                    }
                    break;

                case ArithmeticOperation.Power:
                    for (int num7 = array2.StartIndex; num7 < array2.Count; num7++)
                    {
                        array.Add(Math.Pow(data1[num7], data2[num7]));
                    }
                    break;
            }
            array.StartIndex = array2.StartIndex;
            return array;
        }