예제 #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)
                    }
                });
            }
예제 #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);
            }
예제 #3
0
 /// <summary>
 /// Visit a parse tree produced by <see cref="CurrencyComputerParser.expression"/>.
 /// <para>
 /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
 /// on <paramref name="context"/>.
 /// </para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 /// <return>The visitor result.</return>
 public virtual Result VisitExpression([NotNull] CurrencyComputerParser.ExpressionContext context)
 {
     return(VisitChildren(context));
 }