Example #1
0
    public override AstNode ShallowClone()
    {
        var prop = new StructList <AstObjectProperty>();

        prop.AddRange(Properties);
        return(new AstClass(Source, Start, End, Name, Extends, ref prop));
    }
Example #2
0
        public void TruePrint(ReadOnlySpan <Char> text)
        {
            if (text.Length == 0)
            {
                return;
            }
            if (_frequencyCounting)
            {
                for (int i = 0; i < text.Length; i++)
                {
                    var ch = text[i];
                    if (ch < 128)
                    {
                        _frequency[ch]++;
                    }
                }
                _lastChar = text[text.Length - 1];
                return;
            }
            _storage.AddRange(text);
            _currentPos += text.Length;
            _lastChar    = text[text.Length - 1];
            _currentCol += text.Length;
            var pos = text.IndexOf('\n');

            while (pos >= 0)
            {
                text = text.Slice(pos + 1);
                _currentLine++;
                _currentCol = text.Length;
                pos         = text.IndexOf('\n');
            }
        }
Example #3
0
        public void AddIntelligently(AstNode node)
        {
            if (node == TreeTransformer.Remove)
            {
                return;
            }
            if (node is AstSequence seq)
            {
                Expressions.AddRange(seq.Expressions);
                return;
            }

            Expressions.Add(node);
        }
Example #4
0
        internal static MethodInfo SelectEligibleMethod(IList <MethodInfo> methodInfos, Expression[] arguments, StructList <ParameterConversion> winningConversions)
        {
            if (methodInfos.Count == 0)
            {
                return(null);
            }

            if (methodInfos.Count == 1)
            {
                if (CheckCandidate(new Candidate(methodInfos[0].GetParameters()), arguments, out int unused, winningConversions))
                {
                    return(methodInfos[0]);
                }

                return(null);
            }

            StructList <Candidate> candidates = StructList <Candidate> .GetMinSize(methodInfos.Count);

            StructList <ParameterConversion> conversions = StructList <ParameterConversion> .Get();

            int argCount = arguments.Length;

            for (int i = 0; i < methodInfos.Count; i++)
            {
                MethodInfo      methodInfo     = methodInfos[i];
                ParameterInfo[] parameterInfos = methodInfo.GetParametersCached();

                if (parameterInfos.Length == argCount)
                {
                    candidates.Add(new Candidate(methodInfo, parameterInfos));
                }
                else if (parameterInfos.Length > argCount)
                {
                    bool valid = true;
                    for (int j = 0; j < parameterInfos.Length; j++)
                    {
                        if (!parameterInfos[j].HasDefaultValue)
                        {
                            valid = false;
                            break;
                        }
                    }

                    if (valid)
                    {
                        candidates.Add(new Candidate(methodInfo, parameterInfos));
                    }
                }
            }

            int winner       = -1;
            int winnerPoints = -1;

            for (int i = 0; i < candidates.Count; i++)
            {
                int candidatePoints;
                conversions.QuickClear();

                if (!CheckCandidate(candidates[i], arguments, out candidatePoints, conversions))
                {
                    continue;
                }

                // todo -- handle the ambiguous case
                if (BestScoreSoFar(candidatePoints, winnerPoints))
                {
                    winner       = i;
                    winnerPoints = candidatePoints;

                    winningConversions.QuickClear();
                    winningConversions.AddRange(conversions);
                }
            }

            StructList <ParameterConversion> .Release(ref conversions);

            if (winner != -1)
            {
                MethodInfo retn = candidates[winner].methodInfo;
                StructList <Candidate> .Release(ref candidates);

                return(retn);
            }

            return(null);
        }
Example #5
0
        internal static ConstructorInfo SelectEligibleConstructor(Type type, Expression[] arguments, StructList <ParameterConversion> winningConversions)
        {
            ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public);

            if (constructors.Length == 0)
            {
                return(null);
            }

            if (constructors.Length == 1)
            {
                if (CheckCandidate(new Candidate(constructors[0].GetParametersCached()), arguments, out int unused, winningConversions))
                {
                    return(constructors[0]);
                }

                return(null);
            }

            StructList <Candidate> candidates = StructList <Candidate> .GetMinSize(constructors.Length);

            StructList <ParameterConversion> conversions = StructList <ParameterConversion> .Get();

            for (int i = 0; i < constructors.Length; i++)
            {
                candidates[i] = new Candidate(constructors[i].GetParametersCached());
            }

            int winner       = -1;
            int winnerPoints = 0;

            for (int i = 0; i < constructors.Length; i++)
            {
                int candidatePoints;
                conversions.QuickClear();

                if (!CheckCandidate(candidates[i], arguments, out candidatePoints, conversions))
                {
                    continue;
                }

                // todo -- handle the ambiguous case
                if (BestScoreSoFar(candidatePoints, winnerPoints))
                {
                    winner       = i;
                    winnerPoints = candidatePoints;
                    winningConversions.QuickClear();
                    winningConversions.AddRange(conversions);
                }
            }

            StructList <Candidate> .Release(ref candidates);

            StructList <ParameterConversion> .Release(ref conversions);

            if (winner != -1)
            {
                return(constructors[winner]);
            }

            return(null);
        }
Example #6
0
            public void ConvertToAssignmentAndInsertBeforeVarNode()
            {
                if (AstVarDef.Value == null)
                {
                    throw new InvalidOperationException("Can not convert to assignment if there is no initial value");
                }
                if (Parent != ParentBlock)
                {
                    if (Parent is AstFor astFor)
                    {
                        if (astFor.Init == AstVar)
                        {
                            if (AstVar.Definitions.Count == 1)
                            {
                                astFor.Init = ConvertVariableDefinitionToAssignStatement(false);
                                RemoveVarDefFromVar();
                                return;
                            }
                            var expressionsPlaceholders = new StructList <AstNode>();
                            expressionsPlaceholders.AddRange(VarDefsWithInitialization());
                            astFor.Init = new AstSequence(null, AstVar.Start, AstVar.End, ref expressionsPlaceholders);
                        }

                        if (astFor.Init is AstSequence astSequence)
                        {
                            astSequence.Expressions.ReplaceItem(AstVarDef, ConvertVariableDefinitionToAssignStatement(false));
                            RemoveVarDefFromVar();
                            return;
                        }
                    }

                    if (Parent is AstIf astIf)
                    {
                        if (astIf.Body == AstVar)
                        {
                            var block = new AstBlock(AstVar);
                            CreatedIfBlocks.Add(block);
                            block.Body.AddRange(VarDefsWithInitialization());
                            block.Body.ReplaceItem(AstVarDef, ConvertVariableDefinitionToAssignStatement());
                            astIf.Body = block;
                            RemoveVarDefFromVar();
                            return;
                        }

                        if (astIf.Alternative == AstVar)
                        {
                            var block = new AstBlock(AstVar);
                            CreatedIfBlocks.Add(block);
                            block.Body.AddRange(VarDefsWithInitialization());
                            block.Body.ReplaceItem(AstVarDef, ConvertVariableDefinitionToAssignStatement());
                            astIf.Alternative = block;
                            RemoveVarDefFromVar();
                            return;
                        }

                        if (astIf.Body is AstBlock bodyBlock &&
                            CreatedIfBlocks.Contains(bodyBlock))
                        {
                            bodyBlock.Body.ReplaceItem(AstVarDef, ConvertVariableDefinitionToAssignStatement());
                            RemoveVarDefFromVar();
                            return;
                        }

                        if (astIf.Alternative is AstBlock alternativeBlock &&
                            CreatedIfBlocks.Contains(alternativeBlock))
                        {
                            alternativeBlock.Body.ReplaceItem(AstVarDef, ConvertVariableDefinitionToAssignStatement());
                            RemoveVarDefFromVar();
                            return;
                        }
                    }
                    throw new NotImplementedException();
                }
                ParentBlock.Body.Insert(ParentBlock.Body.IndexOf(AstVar)) = ConvertVariableDefinitionToAssignStatement();
                RemoveVarDefFromVar();
            }
Example #7
0
        /// <summary>
        /// This clips the subject polygon against the clip polygon (gets the intersection of the two polygons)
        /// </summary>
        public static void GetIntersectedPolygon(StructList <Vector2> subjectPoly, StructList <Vector2> clipPoly, StructList <Vector2> outputList)
        {
            if (subjectPoly.size < 3 || clipPoly.size < 3)
            {
                throw new ArgumentException(string.Format("The polygons passed in must have at least 3 Vector2s: subject={0}, clip={1}", subjectPoly.size.ToString(), clipPoly.size.ToString()));
            }

            outputList.size = 0;
            outputList.AddRange(subjectPoly);

            //	Make sure it's clockwise
            if (!IsClockwise(subjectPoly, out bool invalid))
            {
                outputList.Reverse();
            }

            if (invalid)
            {
                return;
            }

            s_EdgeList  = s_EdgeList ?? new StructList <Edge>();
            s_InputList = s_InputList ?? new StructList <Vector2>(subjectPoly.size);

            IterateEdgesClockwise(clipPoly, s_EdgeList);

            //	Walk around the clip polygon clockwise
            for (int i = 0; i < s_EdgeList.size; i++)
            {
                ref Edge clipEdge = ref s_EdgeList.array[i];
                s_InputList.size = 0;
                s_InputList.AddRange(outputList);
                outputList.size = 0;

                //	Sometimes when the polygons don't intersect, this list goes to zero.  Jump out to avoid an index out of range exception
                if (s_InputList.size == 0)
                {
                    break;
                }

                Vector2 S = s_InputList.array[s_InputList.size - 1];

                for (int index = 0; index < s_InputList.size; index++)
                {
                    ref Vector2 E = ref s_InputList.array[index];
                    if (IsInside(clipEdge, E))
                    {
                        if (!IsInside(clipEdge, S))
                        {
                            Vector2?v = GetIntersect(S, E, clipEdge.from, clipEdge.to);
                            if (v == null)
                            {
                                throw new ApplicationException("Line segments don't intersect"); //	may be colinear, or may be a bug
                            }
                            else
                            {
                                outputList.Add(v.Value);
                            }
                        }

                        outputList.Add(E);
                    }
                    else if (IsInside(clipEdge, S))
                    {
                        Vector2?Vector2 = GetIntersect(S, E, clipEdge.from, clipEdge.to);
                        if (Vector2 == null)
                        {
                            throw new ApplicationException("Line segments don't intersect"); //	may be colinear, or may be a bug
                        }
                        else
                        {
                            outputList.Add(Vector2.Value);
                        }
                    }

                    S = E;
                }
Example #8
0
        public static IEnumerable <GridPoint2> GenerateMazeWalls <TCell>(IGrid <GridPoint2, TCell> grid)
        {
            var walls = grid.CloneStructure <bool>();            //true indicates passable

            foreach (var point in walls.Points)
            {
                walls[point] = point.GetColor(2, 2, 2) == 0;

                //Debug.Log(point);
            }

            var wallPoints = walls
                             .Where(pair => pair.cell)
                             .Select(pair => pair.point);

            foreach (var point in wallPoints)
            {
                yield return(point);
            }

            var wallList = new StructList <GridPoint2>();

            var newMaizePoint = grid.Points.Where(p => p.GetColor(2, 2, 2) == 3).RandomItem();
            var inMaze        = new StructList <GridPoint2> {
                newMaizePoint
            };

            var edges = newMaizePoint
                        .GetVectorNeighbors(RectPoint.OrthogonalDirections)
                        .In(grid);

            wallList.AddRange(edges);

            while (wallList.Any())
            {
                var randomWall = wallList.RandomItem();
                var faces      = GetEdgeFaces(randomWall).Where(grid.Contains);

                //At least one of the two faces must be in the maze
                if (faces.Any(point => !inMaze.Contains(point)))
                {
                    newMaizePoint = faces.First(point => !inMaze.Contains(point));
                    inMaze.Add(newMaizePoint);
                    walls[randomWall] = true;

                    yield return(randomWall);

                    // Add all edges that are not passages
                    edges = newMaizePoint
                            .GetVectorNeighbors(RectPoint.OrthogonalDirections)
                            .In(grid)
                            .Where(edge => !(walls[edge]));

                    wallList.AddRange(edges);
                }
                else
                {
                    wallList.Remove(randomWall);
                }
            }
        }