public void ShouldEvaluateSimpleBoardOneWhiteTwoLevels()
        {
            BoardPosition    board     = new BoardPosition(new int[] { 1 }, null);
            DynamicEvaluator evaluator = new DynamicEvaluator();

            Assert.IsTrue(evaluator.Evaluate(board, 1, 1, 1) > 0);
        }
        public void ShouldEvaluateSimpleBoardOneRedTwoLevels()
        {
            BoardPosition board = new BoardPosition(null, new int[] { 1 });

            board.Color = Color.Red;

            DynamicEvaluator evaluator = new DynamicEvaluator();

            Assert.IsTrue(evaluator.Evaluate(board, 1, 1, 1) < 0);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Resolves an expression
        /// </summary>
        /// <param name="expression">The function.</param>
        /// <param name="context">The preprocessing context.</param>
        /// <returns>The resulting value</returns>
        private string ResolveExpression(string expression, PreprocessingContext context)
        {
            string           val       = "";
            DynamicEvaluator evaluator = new DynamicEvaluator();

            try
            {
                val = evaluator.EvaluateToString(context, expression);
            }
            catch (Exception ex)
            {
                val = "<!-- " + expression + " not properly formed -->";

                ErrorInfo errorInfo = new ErrorInfo(ErrorCode.ErrorNotWellFormed, context.SourceFile);
                errorInfo.Message = string.Format("The expression '{0}' was not properly formed. {1}", expression, ex.ToString());
                context.Errors.Add(errorInfo);
            }

            return(val);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Process the body of a macro expression.
        /// </summary>
        /// <param name="context">The preprocessing context.</param>
        /// <param name="keyword">Keyword (#if or #ifdef).</param>
        /// <param name="condition">Condition to test.</param>
        /// <param name="body">Body of macro expression.</param>
        /// <returns>Returns the preprocessed result of the macro expression.</returns>
        private string ProcessBody(PreprocessingContext context, string keyword, string condition, string body)
        {
            string ifbody        = null;
            string elsebody      = null;
            bool   hasElse       = false;
            bool   conditionTrue = false;

            ////////////
            //for (Match matchElif = regexElif.Match(body);
            //    matchElif.Success;
            //    matchElif = matchElif.NextMatch())
            //{
            //}
            ////////////

            Match matchElse = regexElse.Match(body);

            if (matchElse.Success)
            {
                hasElse = true;

                ifbody   = body.Substring(0, matchElse.Index);
                elsebody = body.Substring(matchElse.Index + matchElse.Length);
            }
            else
            {
                ifbody = body;
            }

            string result = "";

            conditionTrue = false;
            if (keyword.EndsWith("ifdef"))
            {
                conditionTrue = (!string.IsNullOrEmpty(condition) && context.Properties.ContainsKey(condition));
            }
            else
            {
                DynamicEvaluator evaluator = new DynamicEvaluator();
                conditionTrue = evaluator.EvaluateToBool(context, condition);
            }

            if (context.PreserveMarkup)
            {
                string whitespacePrefix = "";
                for (int i = 0; i < ifbody.Length; i++)
                {
                    if (!Char.IsWhiteSpace(ifbody[i]))
                    {
                        whitespacePrefix = ifbody.Substring(0, i);
                        break;
                    }
                }
                string commentedOutIfBody = RemoveComment(ifbody, false).Trim();
                if (null != commentedOutIfBody && commentedOutIfBody.Length > 0)
                {
                    if (commentedOutIfBody.IndexOf('\n') > -1) // multi-line
                    {
                        result += whitespacePrefix + "<!--" + whitespacePrefix + commentedOutIfBody + whitespacePrefix + "-->";
                    }
                    else // single line
                    {
                        result += whitespacePrefix + "<!-- " + commentedOutIfBody + " -->";
                    }
                }

                if (!hasElse) // always add an else
                {
                    if (keyword.StartsWith("#"))
                    {
                        result += whitespacePrefix + "<!-- #else -->";
                    }
                    else
                    {
                        result += whitespacePrefix + "<!-- else -->";
                    }
                }
                else
                {
                    result += body.Substring(matchElse.Index, matchElse.Length);
                }
            }

            if (conditionTrue)
            {
                if (context.PreserveMarkup && !hasElse)
                {
                    result += ResolveProperties(RemoveComment(ifbody, true), context);
                }

                if (!(context.PreserveMarkup && !hasElse))
                {
                    result += ResolveProperties(RemoveComment(ifbody, true), context);
                }

                if (context.PreserveMarkup && hasElse && 0 == ifbody.Trim().Length)
                {
                    result += "<!-- " + RemoveComment(elsebody, false) + " -->";
                }
            }
            else
            {
                if (hasElse)
                {
                    result += elsebody;
                }
            }

            return(result);
        }