private bool EvaluateTopicConditions(Stack <ConditionToken> conditions)
    {
        bool finalResult = false;

        if (conditions.Count <= 0)
        {
            return(true);
        }

        //TODO evaluate RPN
        Stack <bool> values = new Stack <bool>();

        while (conditions.Count > 0)
        {
            ConditionToken token = conditions.Pop();
            if (token.IsOperator)
            {
                //Debug.Log("operator : " + ((DialogueConditionOperator)token).Op);
                //pop two values from values and calculate
                DialogueConditionOperator op = (DialogueConditionOperator)token;
                bool value1, value2;
                try
                {
                    value1 = values.Pop();
                    value2 = values.Pop();
                }
                catch (Exception e)
                {
                    //Debug.Log("pop failed");
                    return(false);
                }

                if (op.Op == LogicOperator.And)
                {
                    values.Push(value1 && value2);
                }
                else if (op.Op == LogicOperator.Or)
                {
                    values.Push(value1 || value2);
                }
            }
            else
            {
                //Debug.Log("condition : " + ((DialogueCondition)token).ID);
                //evaluate and then push into values
                bool result = EvaluateCondition((DialogueCondition)token);
                values.Push(result);
            }
        }

        if (values.Count != 1)
        {
            //Debug.Log("there are not 1 value left in values");
            return(false);
        }
        else
        {
            return(values.Pop());
        }
    }
        public void Write(StringBuilder builder, ConditionToken target)
        {
            if (builder == null || target == null)
            {
                return;
            }

            builder.Append(ConditionToken.Keyword);
        }
Exemple #3
0
            public static DataSearcherResolver Create(DataSearcherAttribute[] attributes)
            {
                if (attributes == null || attributes.Length == 0)
                {
                    return(null);
                }

                var mapping = new Dictionary <string, ConditionToken>(StringComparer.OrdinalIgnoreCase);

                foreach (var attribute in attributes)
                {
                    foreach (var pattern in attribute.Patterns)
                    {
                        var index = pattern.IndexOf(':');

                        if (index > 0)
                        {
                            var keys  = pattern.Substring(0, index).Split(',');
                            var token = ConditionToken.Create(pattern.Substring(index + 1).Split(','));

                            if (keys.Length == 1)
                            {
                                mapping[keys[0].Trim()] = token;
                            }
                            else
                            {
                                foreach (var key in keys)
                                {
                                    mapping[key.Trim()] = token;
                                }
                            }
                        }
                        else
                        {
                            var token = ConditionToken.Create(pattern.Split(','));

                            if (token.Fields.Length == 1)
                            {
                                mapping[token.Fields[0].Name] = token;
                            }
                            else
                            {
                                mapping[string.Empty] = token;

                                foreach (var field in token.Fields)
                                {
                                    mapping[field.Name] = token;
                                }
                            }
                        }
                    }
                }

                return(new DataSearcherResolver(mapping));
            }
Exemple #4
0
        private static void Ifndef(CodeProcessorContext context, ConditionToken token, TokenStream nodes)
        {
            int bytes;

            CheckState(context, nodes, (byte)((context.Definitions.ContainsKey(token.Condition.DecodeUInt32(out bytes))) ? 1 : 0));

            if (nodes.CanWrite && token.Child != null)
            {
                GetResult(context, token.Child as PreprocessorToken, nodes);
            }

            EndCondition(context, token.Next, nodes);
        }
Exemple #5
0
        private static void Ifndef(CodeProcessorContext context, ConditionToken token, List <FileDescriptor> referenceFiles, List <string> fileLookups, TokenStream nodes)
        {
            int bytes;

            Preprocessor.CheckState(context, nodes, (byte)((context.Definitions.ContainsKey(token.Condition.DecodeUInt32(out bytes))) ? 1 : 0));

            if (nodes.CanWrite && token.Child != null)
            {
                GetResult(context, token.Child as PreprocessorToken, referenceFiles, fileLookups, nodes);
            }

            EndCondition(context, token.Next, nodes);
        }
        public static void SplitLeaf(ConditionToken token, out bool isNegated, out bool isState, out string property, out string op, out string compareTo)
        {
            string rIsNegated = @"(!?)";
            string rIsState = @"(\$?)";
            string rProperty = @"([a-zA-Z]+[a-zA-Z0-9_]*)";
            string rOperator = @"((==|\[gt\]|\[lt\]|\[lteq\]|\[gteq\]|!=)";
            string rCompareTo = @"([a-zA-Z0-9_.]+))?";
            Regex regex = new Regex(String.Format("{0}{1}{2}{3}{4}", rIsNegated, rIsState, rProperty, rOperator, rCompareTo));
            Match match = regex.Match(token.Symbol);

            if (!match.Success)
                throw new ConditionFormatException("Invalid syntax", token.Position, token.Symbol.Length);

            isNegated = match.Groups[1].Value.Length == 1;
            isState = match.Groups[2].Value.Length == 0;
            property = match.Groups[3].Value;
            op = match.Groups[5].Value;
            compareTo = match.Groups[6].Value;
        }
        public static void SplitLeaf(ConditionToken token, out bool isNegated, out bool isState, out string property, out string op, out string compareTo)
        {
            string rIsNegated = @"(!?)";
            string rIsState   = @"(\$?)";
            string rProperty  = @"([a-zA-Z]+[a-zA-Z0-9_]*)";
            string rOperator  = @"((==|\[gt\]|\[lt\]|\[lteq\]|\[gteq\]|!=)";
            string rCompareTo = @"([a-zA-Z0-9_.]+))?";
            Regex  regex      = new Regex(String.Format("{0}{1}{2}{3}{4}", rIsNegated, rIsState, rProperty, rOperator, rCompareTo));
            Match  match      = regex.Match(token.Symbol);

            if (!match.Success)
            {
                throw new ConditionFormatException("Invalid syntax", token.Position, token.Symbol.Length);
            }

            isNegated = match.Groups[1].Value.Length == 1;
            isState   = match.Groups[2].Value.Length == 0;
            property  = match.Groups[3].Value;
            op        = match.Groups[5].Value;
            compareTo = match.Groups[6].Value;
        }
        private IConditionTreeItem ParseToken(Queue <ConditionToken> r, ComponentDescription description)
        {
            if (r.Count == 0)
            {
                throw new ConditionFormatException("Invalid condition", 0, 0);
            }

            ConditionToken t = r.Dequeue();

            if (t.Type == ConditionToken.TokenType.Symbol)
            {
                return(ParseLeaf(t, description));
            }
            else if (t.Type == ConditionToken.TokenType.Operator && t.Operator == ConditionToken.OperatorType.AND)
            {
                IConditionTreeItem right = ParseToken(r, description);
                IConditionTreeItem left  = ParseToken(r, description);

                return(new ConditionTree(
                           ConditionTree.ConditionOperator.AND,
                           left,
                           right));
            }
            else if (t.Type == ConditionToken.TokenType.Operator && t.Operator == ConditionToken.OperatorType.OR)
            {
                IConditionTreeItem right = ParseToken(r, description);
                IConditionTreeItem left  = ParseToken(r, description);

                return(new ConditionTree(
                           ConditionTree.ConditionOperator.OR,
                           left,
                           right));
            }
            else
            {
                throw new ArgumentException("Invalid queue.", "r");
            }
        }
Exemple #9
0
        private static void If(CodeProcessorContext context, ConditionToken token, TokenStream nodes, bool useExistingState)
        {
            TokenStream expression         = new TokenStream();
            Lazy <HashSet <UInt32> > stack = new Lazy <HashSet <UInt32> >();

            while (!LineBreak(context, token.Condition, expression))
            {
                ProcessNoComments(context, token.Condition, expression, stack, true);
            }

            int result; if (useExistingState)

            {
                result = context.States.Pop();
            }

            else
            {
                result = 0;
            }

            if (result >= 4)
            {
                result = 2;
            }

            result |= (PreprocessorEvaluator.Evaluate(expression) ? 0 : 1);
            CheckState(context, nodes, result);

            if (nodes.CanWrite && token.Child != null)
            {
                GetResult(context, token.Child as PreprocessorToken, nodes);
            }

            EndCondition(context, token.Next, nodes);
        }
Exemple #10
0
        public static bool GetSyntaxTree(Stream data, SyntaxTree <PreprocessorToken> tree, PreprocessorToken root = null)
        {
            while (!data.Eof())
            {
                char c = data.Peek();
                switch ((TokenTypes)c)
                {
                case TokenTypes.IfDefined:
                case TokenTypes.IfNotDefined:
                case TokenTypes.If:
                case TokenTypes.ElseIf:
                {
                    ConditionToken token = new ConditionToken((TokenTypes)c);
                    token.Deserialize(data);

                    if (root == null)
                    {
                        tree.Add(token);
                    }
                    else
                    {
                        tree.AddAppend(root, token);
                    }

                    if (!GetSyntaxTree(data, tree, token))
                    {
                        return(false);
                    }
                }
                break;

                case TokenTypes.Else:
                {
                    PreprocessorToken token = new PreprocessorToken(TokenTypes.Else);
                    token.Deserialize(data);

                    if (root == null)
                    {
                        tree.Add(token);
                    }
                    else
                    {
                        tree.AddAppend(root, token);
                    }

                    if (!GetSyntaxTree(data, tree, token))
                    {
                        return(false);
                    }
                }
                break;

                case TokenTypes.Define:
                case TokenTypes.Undefine:
                case TokenTypes.Include:
                case TokenTypes.Error:
                case TokenTypes.Pragma:
                {
                    TextToken token = new TextToken((TokenTypes)c);
                    token.Deserialize(data);

                    if (root == null)
                    {
                        tree.Add(token);
                    }
                    else
                    {
                        tree.AddAppend(root, token);
                    }
                }
                break;

                case TokenTypes.Text:
                {
                    TextToken token = new TextToken();
                    token.Deserialize(data);

                    if (root == null)
                    {
                        tree.Add(token);
                    }
                    else
                    {
                        tree.AddAppend(root, token);
                    }
                }
                break;

                default:
                {
                    if (root != null && c == 0)
                    {
                        data.Get();
                        return(true);
                    }
                    else
                    {
                        return(ThrowError(new CodeProcessorContext(new CodeProcessorConfig()), PreprocessorCodes.UnexpectedCharacter));
                    }
                }
                }
            }
            return(true);
        }
Exemple #11
0
        private static OperatorToken OperatorToken(string token, int index)
        {
            OperatorToken t = null;

            if (token.StartsWith(CONST_AND))
            {
                t = new OperatorToken();
            }
            else if (token.StartsWith(CONST_OR))
            {
                t = new OperatorToken();
            }
            else if (token.StartsWith(CONST_BIT_LEFT))
            {
                t = new BitOperator();
            }
            else if (token.StartsWith(CONST_BIT_RIGHT))
            {
                t = new BitOperator();
            }
            else if (token.StartsWith(CONST_NOT_EQUALS))
            {
                t = new ConditionToken();
            }
            else if (token.StartsWith(CONST_GREATER_EQUALS))
            {
                t = new ConditionToken();
            }
            else if (token.StartsWith(CONST_LESS_EQUALS))
            {
                t = new ConditionToken();
            }
            else if (token.StartsWith(CONST_EQUALS))
            {
                t = new ConditionToken();
            }
            else if (token.StartsWith(CONST_GREATER))
            {
                t = new ConditionToken();
            }
            else if (token.StartsWith(CONST_LESS))
            {
                t = new ConditionToken();
            }
            else if (token.StartsWith(CONST_PLUS))
            {
                t = new MathOperator();
            }
            else if (token.StartsWith(CONST_MINUS))
            {
                t = new MathOperator();
            }
            else if (token.StartsWith(CONST_MULT))
            {
                t = new MathOperator();
            }
            else if (token.StartsWith(CONST_DIVD))
            {
                t = new MathOperator();
            }
            else if (token.StartsWith(CONST_MOD))
            {
                t = new MathOperator();
            }
            else if (token.StartsWith(CONST_BIT_AND))
            {
                t = new BitOperator();
            }
            else if (token.StartsWith(CONST_BIT_LEFT))
            {
                t = new BitOperator();
            }
            else if (token.StartsWith(CONST_BIT_NOT))
            {
                t = new BitOperator();
            }
            else if (token.StartsWith(CONST_BIT_OR))
            {
                t = new BitOperator();
            }
            else if (token.StartsWith(CONST_BIT_RIGHT))
            {
                t = new BitOperator();
            }
            else if (token.StartsWith(CONST_BIT_XOR))
            {
                t = new BitOperator();
            }
            t.Value  = token;
            t.Index  = index;
            t.Length = token.Length;

            return(t);
        }
        public IConditionTreeItem Parse(ComponentDescription description, string input)
        {
            // Tokenize

            var output    = new Queue <ConditionToken>();
            var operators = new Stack <ConditionToken>();

            var            reader = new PositioningReader(new StringReader(input));
            ConditionToken?token  = ReadToken(reader);

            while (token.HasValue)
            {
                ConditionToken t = token.Value;

                if (t.Type == ConditionToken.TokenType.Symbol)
                {
                    output.Enqueue(t);
                }
                else if (t.Type == ConditionToken.TokenType.LeftBracket)
                {
                    operators.Push(t);
                }
                else if (t.Type == ConditionToken.TokenType.RightBracket)
                {
                    ConditionToken n = operators.Pop();
                    while (n.Type != ConditionToken.TokenType.LeftBracket)
                    {
                        if (operators.Count == 0)
                        {
                            // Not enough operators
                            throw new ConditionFormatException("Syntax error", 0, 0);
                        }

                        output.Enqueue(n);
                        n = operators.Pop();
                    }
                    if (operators.Count > 0 && operators.Peek() == ConditionToken.AND)
                    {
                        output.Enqueue(operators.Pop());
                    }
                }
                else if (t == ConditionToken.AND)
                {
                    while (operators.Count > 0 && (operators.Peek() == ConditionToken.AND || operators.Peek() == ConditionToken.OR))
                    {
                        output.Enqueue(operators.Pop());
                    }
                    operators.Push(t);
                }
                else if (t == ConditionToken.OR)
                {
                    while (operators.Count > 0 &&
                           (operators.Peek().Type == ConditionToken.TokenType.Operator && operators.Peek().Operator == ConditionToken.OperatorType.OR))
                    {
                        output.Enqueue(operators.Pop());
                    }
                    operators.Push(t);
                }

                token = ReadToken(reader);
            }

            while (operators.Count > 0)
            {
                output.Enqueue(operators.Pop());
            }

            // Convert to tree
            Queue <ConditionToken> reversed = new Queue <ConditionToken>(output.Reverse());

            return(ParseToken(reversed, description));
        }
        private ConditionTreeLeaf ParseLeaf(ConditionToken token, ComponentDescription description)
        {
            bool   isNegated;
            bool   isState;
            string propertyName;
            string comparisonStr;
            string compareToStr;

            SplitLeaf(token, out isNegated, out isState, out propertyName, out comparisonStr, out compareToStr);

            if (compareToStr == String.Empty)
            {
                compareToStr = "true"; // Implicit true
            }
            ConditionComparison comparison;

            switch (comparisonStr)
            {
            case "==":
                comparison = ConditionComparison.Equal;
                break;

            case "!=":
                comparison = ConditionComparison.NotEqual;
                break;

            case "[gt]":
                comparison = ConditionComparison.Greater;
                break;

            case "[lt]":
                comparison = ConditionComparison.Less;
                break;

            case "[gteq]":
                comparison = ConditionComparison.GreaterOrEqual;
                break;

            case "[lteq]":
                comparison = ConditionComparison.LessOrEqual;
                break;

            default:
                comparison = ConditionComparison.Truthy;
                break;
            }

            if (isNegated && comparison == ConditionComparison.Equal)
            {
                comparison = ConditionComparison.NotEqual;
            }
            else if (isNegated && comparison == ConditionComparison.Truthy)
            {
                comparison = ConditionComparison.Falsy;
            }
            else if (isNegated)
            {
                // Operator cannot be negated
                throw new ConditionFormatException("Comparison cannot be negated (illegal '!')", token.Position, token.Symbol.Length);
            }

            if (isState)
            {
                if (!legalStateNames.Contains(propertyName))
                {
                    throw new ConditionFormatException(String.Format("Unknown component state '{0}'", propertyName), token.Position, token.Symbol.Length);
                }

                return(new ConditionTreeLeaf(ConditionType.State, propertyName, comparison, PropertyValue.Parse(compareToStr, PropertyValue.Type.Boolean)));
            }
            else
            {
                ComponentProperty property;
                if ((property = description.Properties.FirstOrDefault(x => x.Name == propertyName)) == null)
                {
                    throw new ConditionFormatException(String.Format("Unknown property '{0}'", propertyName), token.Position, token.Symbol.Length);
                }

                return(new ConditionTreeLeaf(ConditionType.Property, propertyName, comparison, PropertyValue.Parse(compareToStr, ToSimplePropertyType(property.Type))));
            }
        }
        private ConditionTreeLeaf ParseLeaf(ConditionToken token, ParseContext context)
        {
            bool isNegated;
            bool isState;
            string property;
            string comparisonStr;
            string compareToStr;
            SplitLeaf(token, out isNegated, out isState, out property, out comparisonStr, out compareToStr);

            if (compareToStr == String.Empty)
                compareToStr = "true"; // Implicit true

            ConditionComparison comparison;
            switch(comparisonStr)
            {
                case "==":
                    comparison = ConditionComparison.Equal;
                    break;
                case "!=":
                    comparison = ConditionComparison.NotEqual;
                    break;
                case "[gt]":
                    comparison = ConditionComparison.Greater;
                    break;
                case "[lt]":
                    comparison = ConditionComparison.Less;
                    break;
                case "[gteq]":
                    comparison = ConditionComparison.GreaterOrEqual;
                    break;
                case "[lteq]":
                    comparison = ConditionComparison.LessOrEqual;
                    break;
                default:
                    comparison = ConditionComparison.Equal;
                    break;
            }

            if (isNegated && comparison == ConditionComparison.Equal)
                comparison = ConditionComparison.NotEqual;
            else if (isNegated)
            {
                // Operator cannot be negated
                throw new ConditionFormatException("Comparison cannot be negated (illegal '!')", token.Position, token.Symbol.Length);
            }

            if (isState)
            {
                if (!legalStateNames.Contains(property))
                    throw new ConditionFormatException(String.Format("Unknown component state '{0}'", property), token.Position, token.Symbol.Length);

                return new ConditionTreeLeaf(ConditionType.State, property, comparison, new PropertyUnion(compareToStr, PropertyUnionType.Boolean));
            }
            else
            {
                PropertyUnionType propertyType;
                if (!context.PropertyTypes.TryGetValue(property, out propertyType))
                    throw new ConditionFormatException(String.Format("Unknown property '{0}'", property), token.Position, token.Symbol.Length);

                return new ConditionTreeLeaf(ConditionType.Property, property, comparison, new PropertyUnion(compareToStr, propertyType));
            }
        }
Exemple #15
0
        private ConditionTreeLeaf ParseLeaf(ConditionToken token, ParseContext context)
        {
            bool   isNegated;
            bool   isState;
            string property;
            string comparisonStr;
            string compareToStr;

            SplitLeaf(token, out isNegated, out isState, out property, out comparisonStr, out compareToStr);

            if (compareToStr == String.Empty)
            {
                compareToStr = "true"; // Implicit true
            }
            ConditionComparison comparison;

            switch (comparisonStr)
            {
            case "==":
                comparison = ConditionComparison.Equal;
                break;

            case "!=":
                comparison = ConditionComparison.NotEqual;
                break;

            case "[gt]":
                comparison = ConditionComparison.Greater;
                break;

            case "[lt]":
                comparison = ConditionComparison.Less;
                break;

            case "[gteq]":
                comparison = ConditionComparison.GreaterOrEqual;
                break;

            case "[lteq]":
                comparison = ConditionComparison.LessOrEqual;
                break;

            default:
                comparison = ConditionComparison.Equal;
                break;
            }

            if (isNegated && comparison == ConditionComparison.Equal)
            {
                comparison = ConditionComparison.NotEqual;
            }
            else if (isNegated)
            {
                // Operator cannot be negated
                throw new ConditionFormatException("Comparison cannot be negated (illegal '!')", token.Position, token.Symbol.Length);
            }

            if (isState)
            {
                if (!legalStateNames.Contains(property))
                {
                    throw new ConditionFormatException(String.Format("Unknown component state '{0}'", property), token.Position, token.Symbol.Length);
                }

                return(new ConditionTreeLeaf(ConditionType.State, property, comparison, PropertyValue.Parse(compareToStr, PropertyValue.Type.Boolean)));
            }
            else
            {
                PropertyValue.Type propertyType;
                if (!context.PropertyTypes.TryGetValue(property, out propertyType))
                {
                    throw new ConditionFormatException(String.Format("Unknown property '{0}'", property), token.Position, token.Symbol.Length);
                }

                return(new ConditionTreeLeaf(ConditionType.Property, property, comparison, PropertyValue.Parse(compareToStr, propertyType)));
            }
        }