Esempio n. 1
0
            public override object VisitExpression(CurrencyComputerParser.ExpressionContext context)
            {
                var amounts       = context.amountComposite();
                var left          = (AmountSigned)VisitAmountComposite(amounts[0]);
                var leftConverted = ConvertTo(left, _targetCurrency);

                var right = amounts.Length == 2
                    ? (AmountSigned)VisitAmountComposite(amounts[1])
                    : (AmountSigned)VisitExpression(context.expression());
                var rightConverted = ConvertTo(right, _targetCurrency);


                var operationKey   = $"{leftConverted.Sign}{rightConverted.Sign}";
                var resultComputed = Operations[operationKey](leftConverted.Amount.Value, rightConverted.Amount.Value);

                return(new AmountSigned
                {
                    Sign = resultComputed < 0
                        ? "-"
                        : "+",
                    Amount = new Amount
                    {
                        Currency = _targetCurrency,
                        Value = Math.Abs(resultComputed)
                    }
                });
            }
Esempio n. 2
0
            public override object VisitExpression(CurrencyComputerParser.ExpressionContext context)
            {
                var amounts       = context.amountComposite();
                var left          = (AmountSigned)VisitAmountComposite(amounts[0]);
                var leftConverted = ConvertTo(left, _targetCurrency);

                if (leftConverted != left)
                {
                    _logger?.LogDebug("{OperationNumber}:Converted from {Source} to {Dest}.", _operationsLogger++, left, leftConverted);
                }

                var right = amounts.Length == 2
                    ? (AmountSigned)VisitAmountComposite(amounts[1])
                    : (AmountSigned)VisitExpression(context.expression());
                var rightConverted = ConvertTo(right, _targetCurrency);

                if (rightConverted != right)
                {
                    _logger?.LogDebug("{OperationNumber}:Converted from {Source} to {Dest}.", _operationsLogger++, right, rightConverted);
                }


                var operationKey   = $"{leftConverted.Sign}{rightConverted.Sign}";
                var resultComputed = Operations[operationKey](leftConverted.Amount.Value, rightConverted.Amount.Value);

                var result = new AmountSigned
                {
                    Sign = resultComputed < 0
                        ? "-"
                        : "+",
                    Amount = new Amount
                    {
                        Currency = _targetCurrency,
                        Value    = Math.Abs(resultComputed)
                    }
                };

                _logger?.LogDebug("{OperationNumber}:Result {Result} from left {Left} and right {Right} tokens.", _operationsLogger++, result, leftConverted, rightConverted);

                return(result);
            }