Exemple #1
0
        /// <summary>
        /// Transform constrains to struct state
        /// </summary>
        public static StateStruct TransformToStructOrNull(ConstrainsState descendant, StateStruct ancStruct)
        {
            if (descendant.NoConstrains)
            {
                return(ancStruct);
            }

            if (descendant.HasDescendant && descendant.Descedant is StateStruct structDesc)
            {
                //descendant is struct.
                if (structDesc.IsSolved)
                {
                    return(structDesc); //if it is solved - return it
                }
                // For perfomance
                bool allFieldsAreSolved = true;

                var newFields = new Dictionary <string, TicNode>(structDesc.MembersCount);
                foreach (var field in structDesc.Fields)
                {
                    var nrField = field.Value.GetNonReference();
                    allFieldsAreSolved = allFieldsAreSolved && nrField.IsSolved;
                    newFields.Add(field.Key, nrField);
                }
                return(new StateStruct(newFields));
            }
            return(null);
        }
Exemple #2
0
        /// <summary>
        /// Transform constrains state to array state
        /// </summary>
        public static StateArray TransformToArrayOrNull(object descNodeName, ConstrainsState descendant)
        {
            if (descendant.NoConstrains)
            {
                var constrains = new ConstrainsState();
                var eName      = "e" + descNodeName.ToString().ToLower() + "'";

                var node = TicNode.CreateTypeVariableNode(eName, constrains);
                return(new StateArray(node));
            }
            else if (descendant.HasDescendant && descendant.Descedant is StateArray arrayEDesc)
            {
                if (arrayEDesc.Element is StateRefTo)
                {
                    var origin = arrayEDesc.ElementNode.GetNonReference();
                    if (origin.IsSolved)
                    {
                        return(new StateArray(origin));
                    }
                }
                else if (arrayEDesc.ElementNode.IsSolved)
                {
                    return(arrayEDesc);
                }
            }

            return(null);
        }
Exemple #3
0
        public bool Apply(ConstrainsState ancestor, ConstrainsState descendant, TicNode ancestorNode, TicNode descendantNode)
        {
            var result = ancestor.MergeOrNull(descendant);

            if (result == null)
            {
                return(false);
            }

            if (result is StatePrimitive)
            {
                descendantNode.State = ancestorNode.State = result;
                return(true);
            }

            if (ancestorNode.Type == TicNodeType.TypeVariable ||
                descendantNode.Type != TicNodeType.TypeVariable)
            {
                ancestorNode.State   = result;
                descendantNode.State = new StateRefTo(ancestorNode);
            }
            else
            {
                descendantNode.State = result;
                ancestorNode.State   = new StateRefTo(descendantNode);
            }
            descendantNode.RemoveAncestor(ancestorNode);
            return(true);
        }
Exemple #4
0
 public bool Apply(ConstrainsState ancestor, StatePrimitive descendant, TicNode ancestorNode, TicNode descendantNode)
 {
     if (ancestor.Fits(descendant))
     {
         ancestorNode.State = descendant;
     }
     return(true);
 }
Exemple #5
0
        public void GetMergedStateOrNull_ConstrainsAndPrimitive_ReturnsPrimitive()
        {
            var a      = new ConstrainsState(StatePrimitive.U16, StatePrimitive.Real);
            var b      = StatePrimitive.I32;
            var merged = SolvingFunctions.GetMergedStateOrNull(a, b);

            Assert.AreEqual(StatePrimitive.I32, merged);
        }
Exemple #6
0
 public bool Apply(ICompositeState ancestor, ConstrainsState descendant, TicNode ancestorNode, TicNode descendantNode)
 {
     if (descendant.Fits(ancestor))
     {
         descendantNode.State = new StateRefTo(ancestorNode);
         descendantNode.RemoveAncestor(ancestorNode);
     }
     return(true);
 }
Exemple #7
0
 public bool Apply(ConstrainsState ancestor, StatePrimitive descendant, TicNode ancestorNode,
                   TicNode descendantNode)
 {
     if (!ancestor.HasAncestor)
     {
         return(true);
     }
     return(descendant.CanBeImplicitlyConvertedTo(ancestor.Ancestor));
 }
Exemple #8
0
        public void ConstrainsTestsAreNotEqual(PrimitiveTypeName?desc, PrimitiveTypeName?anc)
        {
            var descState = desc == null ? null : new StatePrimitive(desc.Value);
            var ancState  = anc == null ? null : new StatePrimitive(anc.Value);
            var state1    = new ConstrainsState(descState, ancState);
            var state2    = new ConstrainsState(null, StatePrimitive.Any);

            Assert.IsFalse(state1.Equals(state2));
            Assert.AreNotEqual(state1, state2);
        }
Exemple #9
0
 public bool Apply(ConstrainsState ancestor, ICompositeState descendant, TicNode ancestorNode, TicNode descendantNode)
 {
     //todo Check all nonrefchildren of constrains?
     if (ancestor.Fits(descendant))
     {
         ancestorNode.State = new StateRefTo(descendantNode);
         descendantNode.RemoveAncestor(ancestorNode);
     }
     return(true);
 }
Exemple #10
0
        public bool Apply(StatePrimitive ancestor, ConstrainsState descendant, TicNode _, TicNode descendantNode)
        {
            descendant.AddAncestor(ancestor);
            var result = descendant.GetOptimizedOrNull();

            if (result == null)
            {
                return(false);
            }
            descendantNode.State = result;
            return(true);
        }
Exemple #11
0
        public bool Apply(ICompositeState ancestor, ConstrainsState descendant, TicNode ancestorNode, TicNode descendantNode)
        {
            if (ancestor is StateArray ancArray)
            {
                var result = SolvingFunctions.TransformToArrayOrNull(descendantNode.Name, descendant);
                if (result == null)
                {
                    return(false);
                }
                result.ElementNode.AddAncestor(ancArray.ElementNode);
                descendantNode.State = result;
                descendantNode.RemoveAncestor(ancestorNode);
            }
            else if (ancestor is StateFun ancFun)
            {
                var result = SolvingFunctions.TransformToFunOrNull(
                    descendantNode.Name, descendant, ancFun);
                if (result == null)
                {
                    return(false);
                }
                result.RetNode.AddAncestor(ancFun.RetNode);
                for (int i = 0; i < result.ArgsCount; i++)
                {
                    result.ArgNodes[i].AddAncestor(ancFun.ArgNodes[i]);
                }
                descendantNode.State = result;
                descendantNode.RemoveAncestor(ancestorNode);
            }
            else if (ancestor is StateStruct ancStruct)
            {
                var result = SolvingFunctions.TransformToStructOrNull(descendant, ancStruct);
                if (result == null)
                {
                    return(false);
                }
                //todo Зачем это тут, если и так структура ссылается на оригинальные поля?

                /*foreach (var ancField in ancStruct.Fields)
                 * {
                 *  var descField = result.GetFieldOrNull(ancField.Key);
                 *  if (descField != ancField.Value)
                 *  {
                 *      descField.AddAncestor(ancField.Value);
                 *  }
                 * }*/
                descendantNode.State = result;
                //descendantNode.RemoveAncestor(ancestorNode);
            }
            return(true);
        }
Exemple #12
0
        public bool Apply(ConstrainsState ancestor, ConstrainsState descendant, TicNode ancestorNode, TicNode descendantNode)
        {
            var ancestorCopy = ancestor.GetCopy();

            ancestorCopy.AddDescedant(descendant.Descedant);
            var result = ancestorCopy.GetOptimizedOrNull();

            if (result == null)
            {
                return(false);
            }
            ancestorNode.State = result;
            return(true);
        }
Exemple #13
0
        private static bool ApplyAncestorConstrains(TicNode ancestorNode, ConstrainsState ancestor, ITypeState typeDesc)
        {
            var ancestorCopy = ancestor.GetCopy();

            ancestorCopy.AddDescedant(typeDesc);
            var result = ancestorCopy.GetOptimizedOrNull();

            if (result == null)
            {
                return(false);
            }
            ancestorNode.State = result;
            return(true);
        }
Exemple #14
0
 public bool Apply(StatePrimitive ancestor, ConstrainsState descendant, TicNode ancestorNode, TicNode descendantNode)
 {
     if (descendant.Fits(ancestor))
     {
         if (descendant.Prefered != null && descendant.Fits(descendant.Prefered))
         {
             descendantNode.State = descendant.Prefered;
         }
         else
         {
             descendantNode.State = ancestor;
         }
     }
     return(true);
 }
Exemple #15
0
        public bool Apply(ConstrainsState ancestor, ConstrainsState descendant, TicNode ancestorNode,
                          TicNode descendantNode)
        {
            if (!ancestor.HasAncestor)
            {
                return(true);
            }

            descendant.AddAncestor(ancestor.Ancestor);
            var result = descendant.GetOptimizedOrNull();

            if (result == null)
            {
                return(false);
            }
            descendantNode.State = result;
            return(true);
        }
Exemple #16
0
        /// <summary>
        /// Transform constrains to fun state
        /// </summary>
        public static StateFun TransformToFunOrNull(object descNodeName, ConstrainsState descendant, StateFun ancestor)
        {
            if (descendant.NoConstrains)
            {
                var argNodes = new TicNode[ancestor.ArgsCount];
                for (int i = 0; i < ancestor.ArgsCount; i++)
                {
                    var argNode = TicNode.CreateTypeVariableNode("a'" + descNodeName + "'" + i, new ConstrainsState());
                    argNode.AddAncestor(ancestor.ArgNodes[i]);
                    argNodes[i] = argNode;
                }

                var retNode = TicNode.CreateTypeVariableNode("r'" + descNodeName, new ConstrainsState());
                retNode.AddAncestor(ancestor.RetNode);

                return(StateFun.Of(argNodes, retNode));
            }

            if (descendant.Descedant is StateFun funDesc &&
                funDesc.ArgsCount == ancestor.ArgsCount)
            {
                if (funDesc.IsSolved)
                {
                    return(funDesc);
                }

                // For perfomance
                bool allArgsAreSolved = true;
                var  nrArgNodes       = new TicNode[funDesc.ArgNodes.Length];
                for (int i = 0; i < funDesc.ArgNodes.Length; i++)
                {
                    nrArgNodes[i]    = funDesc.ArgNodes[i].GetNonReference();
                    allArgsAreSolved = allArgsAreSolved && nrArgNodes[i].IsSolved;
                }

                var nrRetNode = funDesc.RetNode.GetNonReference();
                if (allArgsAreSolved && nrRetNode.IsSolved)
                {
                    return(StateFun.Of(nrArgNodes, nrRetNode));
                }
            }
            return(null);
        }
Exemple #17
0
 public bool Apply(StatePrimitive ancestor, ConstrainsState descendant, TicNode _, TicNode __)
 => !descendant.HasDescendant || descendant.Descedant.CanBeImplicitlyConvertedTo(ancestor);
Exemple #18
0
 public bool Apply(ConstrainsState ancestor, StatePrimitive descendant, TicNode ancestorNode, TicNode descendantNode)
 => ApplyAncestorConstrains(ancestorNode, ancestor, descendant);
Exemple #19
0
 public bool Apply(ConstrainsState ancestor, ICompositeState descendant, TicNode ancestorNode, TicNode descendantNode)
 => ApplyAncestorConstrains(ancestorNode, ancestor, descendant);
Exemple #20
0
 public static GenericConstrains FromTicConstrains(ConstrainsState constrainsState)
 => new GenericConstrains(constrainsState.Ancestor, constrainsState.Descedant as StatePrimitive, constrainsState.IsComparable);
Exemple #21
0
        public bool Apply(
            ICompositeState ancestor,
            ConstrainsState descendant,
            TicNode ancestorNode,
            TicNode descendantNode)
        {
            // if ancestor is composite type then descendant HAS to have same composite type
            // y:int[] = a:[..]  # 'a' has to be an array
            if (ancestor is StateArray ancArray)
            {
                var result = SolvingFunctions.TransformToArrayOrNull(descendantNode.Name, descendant);
                if (result == null)
                {
                    return(false);
                }
                if (result.ElementNode == ancArray.ElementNode)
                {
                    descendantNode.RemoveAncestor(ancestorNode);
                    return(true);
                }

                result.ElementNode.AddAncestor(ancArray.ElementNode);
                descendantNode.State = result;
                descendantNode.RemoveAncestor(ancestorNode);
                SolvingFunctions.PushConstraints(result.ElementNode, ancArray.ElementNode);
                return(true);
            }
            // y:f(x) = a:[..]  # 'a' has to be a functional variable
            if (ancestor is StateFun ancFun)
            {
                var descFun = SolvingFunctions.TransformToFunOrNull(descendantNode.Name, descendant, ancFun);
                if (descFun == null)
                {
                    return(false);
                }
                if (!descendantNode.State.Equals(descFun))
                {
                    descendantNode.State = descFun;
                    PushFunTypeArgumentsConstraints(descFun, ancFun);
                }

                return(true);
            }
            // y:user = a:[..]  # 'a' has to be a struct, that converts to type of 'user'
            if (ancestor is StateStruct ancStruct)
            {
                var descStruct = SolvingFunctions.TransformToStructOrNull(descendant, ancStruct);
                if (descStruct == null)
                {
                    return(false);
                }
                if (descendantNode.State.Equals(descStruct))
                {
                    descendantNode.RemoveAncestor(ancestorNode);
                    return(true);
                }
                descendantNode.State = descStruct;
                if (TryMergeStructFields(ancStruct, descStruct))
                {
                    descendantNode.RemoveAncestor(ancestorNode);
                    return(true);
                }
            }
            return(false);
        }
Exemple #22
0
 public bool Apply(ConstrainsState ancestor, ICompositeState descendant, TicNode _, TicNode __) =>
 !ancestor.HasAncestor || ancestor.Ancestor.Equals(StatePrimitive.Any);