Ejemplo n.º 1
0
        public override void PostForLoopConditionalVisit(ForStat forStat)
        {
            string endForIdentifier = $"endfor_{forStat.Location.line}_{forStat.Location.column}";

            this.Load(forStat.Condition, "r1");
            InstructionStream.Add($"bz r1, {endForIdentifier}");
        }
Ejemplo n.º 2
0
        public override void Visit(ForStat forStat)
        {
            string startForIdentifier = $"for_{forStat.Location.line}_{forStat.Location.column}";
            string endForIdentifier   = $"endfor_{forStat.Location.line}_{forStat.Location.column}";

            InstructionStream.Add(new string[] {
                $"j {startForIdentifier}",
                $"{endForIdentifier}    nop"
            });
        }
Ejemplo n.º 3
0
        public override void PostInitializationVisit(ForStat forStat)
        {
            string startForIdentifier  = $"for_{forStat.Location.line}_{forStat.Location.column}";
            string conditionIdentifier = $"forcond_{forStat.Location.line}_{forStat.Location.column}";

            this.LoadAndStore(forStat.Initialization, forStat.stackOffset, forStat.NodeMemorySize, $"{forStat.Type} {forStat.Id} = {forStat.Initialization}");

            InstructionStream.Add($"j {conditionIdentifier}");
            InstructionStream.Add($"{startForIdentifier}    nop", "Start the for loop");
        }
Ejemplo n.º 4
0
        public override void PreVisit(ForStat forStat)
        {
            if (this._functionScope.Get($"{forStat.Id}-{Classification.Variable}") == null)
            {
                this._forDecl.Push(forStat.Id + forStat.Location);
            }

            this._functionScope.Add(new TableEntry(forStat.Id, Classification.Variable, -1)
            {
                Type = forStat.Type
            }, forStat.IdLocation);
        }
Ejemplo n.º 5
0
        public override void Visit(ForStat forStat)
        {
            if (this._forDecl.Count == 0)
            {
                return;
            }

            if (this._forDecl.Peek() == forStat.Id + forStat.Location)
            {
                this._forDecl.Pop();
                this._functionScope.Remove(forStat.Id, Classification.Variable);
            }
        }
        public override void PreVisit(ForStat forStat)
        {
            var variable = this.FunctionScope.Get(forStat.Id, Classification.Variable);

            if (variable != null)
            {
                if (variable.EntryMemorySize < Sizes[forStat.Type])
                {
                    variable.EntryMemorySize = Sizes[forStat.Type];
                }
            }
            else
            {
                this.FunctionScope.Add(new TableEntry(forStat.Id, Classification.Variable, Sizes[forStat.Type]), (0, 0));
            }
        }
Ejemplo n.º 7
0
        private object NoASS()
        {
            string first = "if for get put return";

            this.SkipErrors(first);

            var    lookaheadToken = this.TokenStream.Peek();
            string lookahead      = lookaheadToken.AToCCFormat();

            if ("if".HasToken(lookahead))
            {
                this.ApplyDerivation("noASS -> 'if' '(' expr ')' 'then' statBlock 'else' statBlock ';'");

                var ifStatement = new IfStat(lookaheadToken.SourceLocation);

                Match("if");
                Match("(");
                var expr = Expr();
                Match(")");
                Match("then");
                var trueBlock = StatBlock();
                Match("else");
                var elseBlock = StatBlock();
                Match(";");

                ifStatement.Condition = expr;
                ifStatement.TrueBlock = trueBlock;
                ifStatement.ElseBlock = elseBlock;

                return(ifStatement);
            }

            if ("for".HasToken(lookahead))
            {
                this.ApplyDerivation("noASS -> 'for' '(' type 'id' '=' expr ';' relExpr ';' assignStat ')' statBlock ';'");

                var forStat = new ForStat(lookaheadToken.SourceLocation);

                Match("for");
                Match("(");
                string type       = Type();
                var    idLocation = this.TokenStream.Peek().SourceLocation;
                string id         = Match("id");
                Match("=");
                var initExpr = Expr();
                Match(";");
                var condition = RelExpr();
                Match(";");
                var assignStat = AssignStat();
                Match(")");
                var statBlock = StatBlock();
                Match(";");

                forStat.Type           = type;
                forStat.Id             = id;
                forStat.Initialization = initExpr;
                forStat.Condition      = condition;
                forStat.Update         = assignStat;
                forStat.LoopBlock      = statBlock;
                forStat.IdLocation     = idLocation;

                return(forStat);
            }

            if ("get".HasToken(lookahead))
            {
                this.ApplyDerivation("noASS -> 'get' '(' variable ')' ';'");

                var getStatement = new GetStat(lookaheadToken.SourceLocation);

                Match("get");
                Match("(");
                var variable = Variable();
                Match(")");
                Match(";");

                getStatement.Variable = variable;

                return(getStatement);
            }

            if ("put".HasToken(lookahead))
            {
                this.ApplyDerivation("noASS -> 'put' '(' expr ')' ';'");

                var putStatement = new PutStat(lookaheadToken.SourceLocation);

                Match("put");
                Match("(");
                var expr = Expr();
                Match(")");
                Match(";");

                putStatement.Expression = expr;

                return(putStatement);
            }

            if ("return".HasToken(lookahead))
            {
                this.ApplyDerivation("noASS -> 'return' '(' expr ')' ';'");

                var returnStatement = new ReturnStat(lookaheadToken.SourceLocation);

                Match("return");
                Match("(");
                var expr = Expr();
                Match(")");
                Match(";");

                returnStatement.ReturnValueExpression = expr;
                return(returnStatement);
            }

            return(null);
        }
Ejemplo n.º 8
0
 public override void Visit(ForStat forStat)
 {
     forStat.stackOffset = this.FunctionScopeLink.Link.GetOffset(forStat.Id, Classification.Variable);
 }
 public override void Visit(ForStat forStat)
 {
     forStat.NodeMemorySize = Sizes[forStat.Type];
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Thread operation fo calculate statistics for [stat].
        ///
        /// [stat] is guarenteed to be unique, however stat.Assignment is not, hence stat.Assignment must be locked.
        ///
        /// Currently only stat.Assignment.AssignmentStatistics is the only member to be R/W locked, since that is all
        /// that is modified.
        /// </summary>
        private static void Thread_CalculateAssignmentStatistics([Const] EClustererStatistics statistics, [MutableUnsafe] ForStat stat, [Const] Cluster[] realClusters, [Const] ConfigurationMetric metric, [MutableSafe] ProgressParallelHandler prog)
        {
            prog.SafeIncrement();

            // STATS: Distance from avg
            if (stat.ClusterVector != null)
            {
                // Euclidean
                if (statistics.HasFlag(EClustererStatistics.EuclideanFromAverage))
                {
                    double ed = Maths.Euclidean(stat.AssignmentVector.Values, stat.ClusterVector);
                    stat.Assignment.AssignmentStatistics.ThreadSafeIndex(CreatePartialKey(stat.ObsFilter, STAT_ASSIGNMENT_EUCLIDEAN_FROM_AVG), ed);
                    stat.Assignment.AssignmentStatistics.ThreadSafeIndex(CreatePartialKey(stat.ObsFilter, STAT_ASSIGNMENT_EUCLIDEAN_FROM_AVG_SQUARED), ed * ed);
                }

                // Custom (if applicable)
                if (metric != null &&
                    statistics.HasFlag(EClustererStatistics.DistanceFromAverage) &&
                    !(metric.Args.Id == Algo.ID_METRIC_EUCLIDEAN && statistics.HasFlag(EClustererStatistics.EuclideanFromAverage)))
                {
                    string key1 = metric.ToString() + STAT_ASSIGNMENT_DISTANCE_FROM_AVG;
                    string key2 = metric.ToString() + STAT_ASSIGNMENT_DISTANCE_FROM_AVG_SQUARED;
                    double dd   = metric.Calculate(stat.AssignmentVector.Values, stat.ClusterVector);

                    stat.Assignment.AssignmentStatistics.ThreadSafeIndex(CreatePartialKey(stat.ObsFilter, key1), dd);
                    stat.Assignment.AssignmentStatistics.ThreadSafeIndex(CreatePartialKey(stat.ObsFilter, key2), dd * dd);
                }
            }

            // STATS: Silhouette
            Cluster nextNearestCluster = null;

            if (statistics.HasFlag(EClustererStatistics.SilhouetteWidth))
            {
                double silhouetteWidth;
                double nextNearestClusterId;

                ClustererStatisticsHelper.CalculateSilhouette(stat, realClusters, out silhouetteWidth, out nextNearestCluster);

                if (!double.TryParse(nextNearestCluster.ShortName, out nextNearestClusterId))
                {
                    nextNearestClusterId = double.NaN;
                }

                // Silhouette
                stat.Assignment.AssignmentStatistics.ThreadSafeIndex(CreatePartialKey(stat.ObsFilter, STAT_ASSIGNMENT_SILHOUETTE_WIDTH), silhouetteWidth);
                stat.Assignment.AssignmentStatistics.ThreadSafeIndex(CreatePartialKey(stat.ObsFilter, STAT_ASSIGNMENT_NEXT_NEAREST_CLUSTER), nextNearestClusterId);
            }

            // STATS: Score
            if (stat.ObsFilter == null)
            {
                // Score
                stat.Assignment.AssignmentStatistics.ThreadSafeIndex(STAT_ASSIGNMENT_SCORE, stat.Assignment.Score);

                // Next nearest cluster
                stat.Assignment.NextNearestCluster = nextNearestCluster; // Only one ForStat per Assignment has ObsFilter == null so thread safe not required
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Determines what needs calculating.
        /// </summary>
        private void Thread_AddFilterToCalculationList([Const] Core core, [Const] ConfigurationMetric metric, [Const] IntensityMatrix vmatrix, [Const] DistanceMatrix dmatrix, [Const] EClustererStatistics statistics, [Const] Cluster[] realClusters, [Const] ObsFilter obsFilter, [MutableUnsafe] List <ForStat> needsCalculating, [MutableSafe] ProgressParallelHandler progP)
        {
            progP.SafeIncrement();

            IntensityMatrix vmatFiltered;
            DistanceMatrix  dmatFiltered;

            int[] filteredIndices;

            if (obsFilter == null)
            {
                vmatFiltered    = vmatrix;
                dmatFiltered    = dmatrix;
                filteredIndices = null;
            }
            else
            {
                filteredIndices = vmatrix.Columns.Which(z => obsFilter.Test(z.Observation)).ToArray();  // TODO: Multuple iteration
                vmatFiltered    = vmatrix.Subset(null, obsFilter, ESubsetFlags.None);
                dmatFiltered    = null;
            }

            Dictionary <Cluster, IReadOnlyList <double> > centreVectors = new Dictionary <Cluster, IReadOnlyList <double> >();

            foreach (Cluster cluster in realClusters)
            {
                /////////////////////
                // ASSIGNMENT STATS
                var centre = cluster.GetCentre(ECentreMode.Average, ECandidateMode.Assignments);
                IReadOnlyList <double> centreVector = centre.Count != 0 ? centre[0] : null;

                if (filteredIndices != null)
                {
                    centreVector = centreVector.Extract(filteredIndices);
                }

                centreVectors.Add(cluster, centreVector);
            }

            foreach (Assignment ass in Assignments)
            {
                ForStat f = new ForStat();
                f.Assignment = ass;
                f.ObsFilter  = obsFilter;

                if (filteredIndices != null)
                {
                    f.AssignmentVector = vmatFiltered.Vectors[ass.Vector.Index];
                }
                else
                {
                    f.AssignmentVector = ass.Vector;
                }

                f.ClusterVector = centreVectors[ass.Cluster];

                if (statistics.HasFlag(EClustererStatistics.SilhouetteWidth))
                {
                    if (dmatFiltered == null)
                    {
                        dmatFiltered = DistanceMatrix.Create(core, vmatrix, metric, ProgressReporter.GetEmpty());
                    }
                }

                f.DistanceMatrix = dmatFiltered;

                lock (needsCalculating)
                {
                    needsCalculating.Add(f);
                }
            }
        }
Ejemplo n.º 12
0
 public virtual void PostForLoopConditionalVisit(ForStat forStat)
 {
 }
Ejemplo n.º 13
0
 public virtual void PostUpdateVisit(ForStat forStat)
 {
 }
Ejemplo n.º 14
0
 public virtual void PostInitializationVisit(ForStat forStat)
 {
 }
Ejemplo n.º 15
0
 public virtual void Visit(ForStat forStat)
 {
 }
Ejemplo n.º 16
0
        public override void PostUpdateVisit(ForStat forStat)
        {
            string conditionIdentifier = $"forcond_{forStat.Location.line}_{forStat.Location.column}";

            InstructionStream.Add($"{conditionIdentifier}   nop");
        }