Exemple #1
0
        public static RateRule CreateFromExpression(string expression, CurrencyPair currencyPair)
        {
            var ex = RateRules.CreateExpression(expression);

            RateRules.TryParse("", out var rules);
            return(new RateRule(rules, currencyPair, ex));
        }
Exemple #2
0
            public override SyntaxNode VisitInvocationExpression(InvocationExpressionSyntax node)
            {
                var exchangeName = node.Expression.ToString();

                if (exchangeName.StartsWith("ERR_", StringComparison.OrdinalIgnoreCase))
                {
                    Errors.Add(RateRulesErrors.PreprocessError);
                    return(base.VisitInvocationExpression(node));
                }

                var currencyPair = node.ArgumentList.ChildNodes().FirstOrDefault()?.ToString();

                if (currencyPair == null || !CurrencyPair.TryParse(currencyPair, out var pair))
                {
                    Errors.Add(RateRulesErrors.InvalidCurrencyIdentifier);
                    return(RateRules.CreateExpression($"ERR_INVALID_CURRENCY_PAIR({node.ToString()})"));
                }
                else
                {
                    var rate = Rates.GetRate(exchangeName, pair);
                    if (rate == null)
                    {
                        Errors.Add(RateRulesErrors.RateUnavailable);
                        return(RateRules.CreateExpression($"ERR_RATE_UNAVAILABLE({exchangeName}, {pair.ToString()})"));
                    }
                    else
                    {
                        return(RateRules.CreateExpression(rate.ToString()));
                    }
                }
            }
Exemple #3
0
            public override SyntaxNode VisitInvocationExpression(InvocationExpressionSyntax node)
            {
                if (IsInvocation)
                {
                    Errors.Add(RateRulesErrors.InvalidCurrencyIdentifier);
                    return(RateRules.CreateExpression($"ERR_INVALID_CURRENCY_PAIR({node.ToString()})"));
                }
                IsInvocation  = true;
                _ExchangeName = node.Expression.ToString();
                var result = base.VisitInvocationExpression(node);

                IsInvocation = false;
                return(result);
            }
Exemple #4
0
 public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax node)
 {
     if (
         (!IsInvocation || IsArgumentList) &&
         CurrencyPair.TryParse(node.Identifier.ValueText, out var currentPair))
     {
         var replacedPair = new CurrencyPair(left: currentPair.Left == "X" ? pair.Left : currentPair.Left,
                                             right: currentPair.Right == "X" ? pair.Right : currentPair.Right);
         if (IsInvocation) // eg. replace bittrex(BTC_X) to bittrex(BTC_USD)
         {
             ExchangeRates.Add(new ExchangeRate()
             {
                 CurrencyPair = replacedPair, Exchange = _ExchangeName
             });
             return(SyntaxFactory.IdentifierName(replacedPair.ToString()));
         }
         else // eg. replace BTC_X to BTC_USD, then replace by the expression for BTC_USD
         {
             var bestCandidate = parent.FindBestCandidate(replacedPair);
             if (nested > MaxNestedCount)
             {
                 Errors.Add(RateRulesErrors.TooMuchNestedCalls);
                 return(RateRules.CreateExpression($"ERR_TOO_MUCH_NESTED_CALLS({replacedPair})"));
             }
             var innerFlatten = CreateNewContext(replacedPair);
             var replaced     = innerFlatten.Visit(bestCandidate);
             if (replaced is ExpressionSyntax expression)
             {
                 var hasBinaryOps = new HasBinaryOperations();
                 hasBinaryOps.Visit(expression);
                 if (hasBinaryOps.Result)
                 {
                     replaced = SyntaxFactory.ParenthesizedExpression(expression);
                 }
             }
             if (Errors.Contains(RateRulesErrors.TooMuchNestedCalls))
             {
                 return(RateRules.CreateExpression($"ERR_TOO_MUCH_NESTED_CALLS({replacedPair})"));
             }
             return(replaced);
         }
     }
     return(base.VisitIdentifierName(node));
 }