public bool IfThereIsFlowFrom(
            DAGNode fromNode,
            ComputationStep fromCompStep,
            ComputationStep toCompStep)
        {
            uint inputLength = (uint)MEAPSharedContext.Input.Length;

            long L = MEAPSharedContext.MNP.GetLTapeBound(0, inputLength);
            long R = MEAPSharedContext.MNP.GetRTapeBound(0, inputLength);

            if ((toCompStep.kappaTape < L) || (R < toCompStep.kappaTape))
            {
                return(false);
            }

            if (fromCompStep.m == TMDirection.S)
            {
                if (fromCompStep.sNext != toCompStep.s)
                {
                    return(false);
                }
            }

            SortedSet <PropSymbol> procSymPrev = AppHelper.TakeValueByKey(
                propagatedSymbols, fromNode.Id,
                () => new SortedSet <PropSymbol>(new PropSymbolComparer()));

            IEnumerable <PropSymbol> prevPairs = procSymPrev.Where(
                t => (t.Variable == toCompStep.kappaTape));

            IEnumerable <PropSymbol> prevMatchPairs =
                prevPairs.Where(t => t.Symbol == toCompStep.s);

            return(prevMatchPairs.Any());
        }
Ejemplo n.º 2
0
        public static ComputationStep GetSequentialCompStep(
            ComputationStep compStep)
        {
            ComputationStep seqCompStep = new()
            {
                kappaTape = compStep.kappaTape,
                kappaStep = compStep.kappaStep + 1
            };

            switch (compStep.m)
            {
            case TMDirection.L:
            {
                seqCompStep.kappaTape -= compStep.Shift;
                break;
            }

            case TMDirection.R:
            {
                seqCompStep.kappaTape += compStep.Shift;
                break;
            }
            }

            return(seqCompStep);
        }

        #endregion
    }
Ejemplo n.º 3
0
        public void Execute(Context context, params string[] args)
        {
            using (var input = new MemoryStream())
                using (var output = new MemoryStream())
                {
                    if (args.Any())
                    {
                        var writer = new StreamWriter(input);
                        writer.Write(args.First());
                        writer.Flush();
                        input.Position = 0;
                    }

                    var prn = PrnComposer.GetPrn(context.TokenSequence, context.Labels, context.Variables);

                    foreach (var identifier in prn.OfType <IdentifierToken>())
                    {
                        context.Variables[identifier] = new ConstantToken <float>(0);
                    }
                    var prnExecutor = new PrnExpressionExecutor(input, output, Logger);
                    prnExecutor.Output          += s => Output?.Invoke(s);
                    prnExecutor.ComputationStep += (s, i, stack) => ComputationStep?.Invoke(s, i, stack);
                    prnExecutor.ComputeExpression(prn, context.Variables);
                }
        }
Ejemplo n.º 4
0
        public void Run(
            SortedDictionary <long, SortedSet <long> > varToVarNodes,
            long currentLevel)
        {
            defUsePairSet = RunReachDefAnalysis(varToVarNodes, currentLevel);

            meapContext.TConsistPairSet = new SortedSet <CompStepNodePair>(
                new CompStepNodePairComparer());
            SortedDictionary <long, TASGNodeInfo> idToInfoMap = meapContext.TArbSeqCFG.IdToNodeInfoMap;

            foreach (KeyValuePair <long, SortedSet <DefUsePair> > varToDefUsePair in defUsePairSet)
            {
                long variable = varToDefUsePair.Key;

                foreach (DefUsePair defUsePair in varToDefUsePair.Value)
                {
                    long defNodeId = defUsePair.DefNode;
                    long useNodeId = defUsePair.UseNode;

                    ComputationStep defCompStep = idToInfoMap[defNodeId].CompStep;
                    ComputationStep useCompStep = idToInfoMap[useNodeId].CompStep;

                    bool consistent = false;

                    if (meapContext.TArbSeqCFG.IsSinkNode(useNodeId))
                    {
                        consistent = true;
                    }
                    else if (meapContext.TArbSeqCFG.IsSourceNode(defNodeId))
                    {
                        consistent = (useCompStep.s ==
                                      meapContext.MEAPSharedContext.InitInstance.TapeSymbol(
                                          (int)useCompStep.kappaTape));
                    }
                    else
                    {
                        consistent = (defCompStep.sNext == useCompStep.s);
                    }

                    if (consistent)
                    {
                        meapContext.TConsistPairSet.Add(
                            new CompStepNodePair
                            (
                                variable: defUsePair.Variable,
                                uNode: defUsePair.DefNode,
                                vNode: defUsePair.UseNode
                            ));

                        meapContext.TConsistPairCount++;
                    }

                    meapContext.DUPairCount++;
                }
            }

            Trace();
        }
        public void ExtractTConsistSeq()
        {
            log.Info("MExistsAcceptingPath.Compute: path");

            tapeSegContext.TapeSegTConsistPath.Remove(tapeSegContext.TapeSegTConsistPath.Last());
            tapeSegContext.TapeSegTConsistPath.ForEach(s =>
            {
                log.InfoFormat("node = {0}", s.ToString());
            });

            List <KeyValuePair <long, ComputationStep> > pathCompSteps =
                tapeSegContext.TapeSegTConsistPath.ConvertAll(uId =>
                                                              new KeyValuePair <long, ComputationStep>(uId, meapContext.TArbSeqCFG.IdToNodeInfoMap[uId].CompStep));

            pathCompSteps.ForEach(s =>
            {
                log.InfoFormat(
                    "id = {0}, comp.step = {1}",
                    s.Key.ToString(), s.Value);
            });

            TMInstance tmInstance = new
                                    (
                meapContext.MEAPSharedContext.MNP,
                meapContext.MEAPSharedContext.Input
                                    );

            meapContext.MEAPSharedContext.MNP.PrepareTapeFwd(
                meapContext.MEAPSharedContext.Input,
                tmInstance);

            foreach (KeyValuePair <long, ComputationStep> compStepPair in pathCompSteps)
            {
                ComputationStep compStep = compStepPair.Value;

                TMInstance.MoveToNextConfiguration(
                    new StateSymbolDirectionTriple
                    (
                        state: compStep.qNext,
                        symbol: compStep.sNext,
                        direction: compStep.m,
                        shift: compStep.Shift
                    ),
                    tmInstance);

                if (tmInstance.IsInFinalState())
                {
                    break;
                }
            }

            tapeSegContext.TapeSegOutput = meapContext.MEAPSharedContext.MNP.GetOutput(
                tmInstance,
                meapContext.mu,
                (uint)meapContext.MEAPSharedContext.Input.Length);
        }
        public void Run()
        {
            log.Info("Build TConsist pair set");

            CreateVariableSets();
            defUsePairSet = RunReachDefAnalysis();

            meapContext.TConsistPairSet = new SortedSet <CompStepNodePair>(
                new CompStepNodePairComparer());
            SortedDictionary <long, DAGNode>      nodeEnumeration = meapContext.TArbSeqCFG.NodeEnumeration;
            SortedDictionary <long, TASGNodeInfo> idToInfoMap     = meapContext.TArbSeqCFG.IdToNodeInfoMap;

            foreach (DefUsePair defUsePair in defUsePairSet)
            {
                long defNodeId = defUsePair.DefNode;
                long useNodeId = defUsePair.UseNode;

                ComputationStep defCompStep = idToInfoMap[defNodeId].CompStep;
                ComputationStep useCompStep = idToInfoMap[useNodeId].CompStep;

                bool consistent = false;

                if (meapContext.TArbSeqCFG.IsSinkNode(useNodeId))
                {
                    consistent = true;
                }
                else if (meapContext.TArbSeqCFG.IsSourceNode(defNodeId))
                {
                    consistent = (useCompStep.s ==
                                  meapContext.MEAPSharedContext.InitInstance.TapeSymbol(
                                      (int)useCompStep.kappaTape));
                }
                else
                {
                    consistent = (defCompStep.sNext == useCompStep.s);
                }

                if (consistent)
                {
                    meapContext.TConsistPairSet.Add(
                        new CompStepNodePair
                        (
                            variable: defUsePair.Variable,
                            uNode: defUsePair.DefNode,
                            vNode: defUsePair.UseNode
                        ));

                    meapContext.TConsistPairCount++;
                }

                meapContext.DUPairCount++;
            }

            Trace();
        }
Ejemplo n.º 7
0
        public override void Init()
        {
            G          = new TypedDAG <TASGNodeInfo, StdEdgeInfo>("TASG");
            TapeLBound = 0;
            TapeRBound = 0;

            DAGNode s = new(nodeId++);

            G.AddNode(s);
            G.SetSourceNode(s);

            MEAPSharedContext.NodeLevelInfo.AddNodeAtLevel(s.Id, 0);
            processedMu = 0;

            sCompStep = new ComputationStep
            {
                q         = MEAPSharedContext.MNP.qStart,
                s         = MEAPSharedContext.Input[0],
                qNext     = MEAPSharedContext.MNP.qStart,
                sNext     = MEAPSharedContext.Input[0],
                m         = TMDirection.R,
                Shift     = 1,
                kappaTape = 0,
                kappaStep = 0,
                sTheSame  = OneTapeTuringMachine.blankSymbol
            };

            nodeEnumeration[s.Id] = s;

            idToInfoMap[s.Id] = new TASGNodeInfo
            {
                CompStep = sCompStep
            };

            propSymbolsKeeper = new PropSymbolsKeeperFactCPLTM(MEAPSharedContext);
            propSymbolsKeeper.Init(s.Id);

            CPLTMInfo = MEAPSharedContext.CPLTMInfo;

            endNodeIds.Add(G.GetSourceNodeId());
        }
        public void PropagateSymbol(
            DAGNode fromNode,
            DAGNode toNode,
            ComputationStep toCompStep)
        {
            SortedSet <PropSymbol> procSymPrev = AppHelper.TakeValueByKey(
                propagatedSymbols, fromNode.Id,
                () => new SortedSet <PropSymbol>(new PropSymbolComparer()));

            SortedSet <PropSymbol> nodeProcSym = AppHelper.TakeValueByKey(
                propagatedSymbols, toNode.Id,
                () => new SortedSet <PropSymbol>(new PropSymbolComparer()));

            foreach (PropSymbol p in procSymPrev.Where(
                         t => t.Variable != toCompStep.kappaTape))
            {
                nodeProcSym.Add(p);
            }

            nodeProcSym.Add(new PropSymbol(
                                toCompStep.kappaTape,
                                toCompStep.sNext));
        }
        public bool IfThereIsFlowFrom(
            DAGNode fromNode,
            ComputationStep fromCompStep,
            ComputationStep toCompStep)
        {
            if ((fromCompStep.m == TMDirection.S) &&
                (fromCompStep.sNext != toCompStep.s))
            {
                return(false);
            }

            SortedSet <PropSymbol> procSymPrev = AppHelper.TakeValueByKey(
                propagatedSymbols, fromNode.Id,
                () => new SortedSet <PropSymbol>(new PropSymbolComparer()));

            IEnumerable <PropSymbol> prevPairs = procSymPrev.Where(
                t => (t.Variable == toCompStep.kappaTape));

            IEnumerable <PropSymbol> prevMatchPairs =
                prevPairs.Where(t => t.Symbol == toCompStep.s);

            return(prevMatchPairs.Any());
        }
Ejemplo n.º 10
0
        public float?ComputeExpression(IList <Token> prn, VariableStore identifierValues)
        {
            var stack = new Stack <Token>();

            var context = new ExecutorContext(stack, identifierValues, prn)
            {
                InputStream  = InputStream,
                OutputStream = OutputStream,
                Logger       = Logger
            };

            for (var index = 0; index < prn.Count; index++)
            {
                ComputationStep?.Invoke(prn, index, stack);

                var token     = prn[index];
                var operation = token as IOperation;
                if (operation != null)
                {
                    var nextIndex = index + 1;
                    operation.Execute(context);
                    if (context.NextPosition != null)
                    {
                        nextIndex            = context.NextPosition.Value;
                        context.NextPosition = null;
                    }

                    if (context.WrittenString != null)
                    {
                        Output?.Invoke(context.WrittenString);
                        context.WrittenString = null;
                    }

                    index = nextIndex - 1;
                    continue;
                }

                if (token.Substring == ":")
                {
                    continue;
                }

                ProcessIndentifiersConstants(token, stack);
                ProcessUnarySubtraction(identifierValues, token, stack);
                ProcessBoolean(identifierValues, token, stack);
                ProcessArithmeticOperations(identifierValues, token, stack);
                ProcessLogics(identifierValues, token, stack);
                ProcessAssignment(identifierValues, token, stack);
            }

            if (stack.Any())
            {
                var popped = stack.Pop();
                if (popped is ConstantToken <float> || popped is IdentifierToken)
                {
                    return((popped as ConstantToken <float>)?.Value ?? identifierValues[popped as IdentifierToken].Value);
                }
            }

            return(null);
        }
        public void Run()
        {
            TypedDAG <TASGNodeInfo, StdEdgeInfo>  cfg         = meapContext.TArbSeqCFG;
            SortedDictionary <long, TASGNodeInfo> idToInfoMap = meapContext.TArbSeqCFG.IdToNodeInfoMap;

            long kStepA = kSteps.Item1;
            long kStepB = kSteps.Item2;
            long kStepC = kSteps.Item3;

            foreach (long nodeId in nodeVLevels[kStepB])
            {
                currDUPairs.Add(new KeyValuePair <long, long>(nodeId, nodeId));
            }

            for (long i = kStepB; i >= (kStepA + 1); i--)
            {
                foreach (KeyValuePair <long, long> duPair in currDUPairs)
                {
                    long uNodeId = duPair.Key;
                    long vNodeId = duPair.Value;

                    DAGNode uNode = cfg.GetNode(uNodeId);
                    DAGNode vNode = cfg.GetNode(vNodeId);

                    foreach (DAGEdge uEdge in uNode.InEdges)
                    {
                        foreach (DAGEdge vEdge in vNode.OutEdges)
                        {
                            long defNodeId = uEdge.FromNode.Id;
                            long useNodeId = vEdge.ToNode.Id;

                            ComputationStep defCompStep = idToInfoMap[defNodeId].CompStep;
                            ComputationStep useCompStep = idToInfoMap[useNodeId].CompStep;

                            if ((defCompStep.kappaTape == useCompStep.kappaTape) &&
                                (defCompStep.sNext == useCompStep.s))
                            {
                                nextDUPairs.Add(new KeyValuePair <long, long>(defNodeId, useNodeId));
                            }
                        }
                    }
                }

                currDUPairs.Clear();
                nextDUPairs.ForEach(t => currDUPairs.Add(t));

                foreach (KeyValuePair <long, long> duPair in currDUPairs)
                {
                    long            defNodeId   = duPair.Key;
                    long            useNodeId   = duPair.Value;
                    ComputationStep defCompStep = idToInfoMap[defNodeId].CompStep;

                    meapContext.TConsistPairSet.Add(
                        new CompStepNodePair(
                            variable: defCompStep.kappaTape,
                            uNode: duPair.Key,
                            vNode: duPair.Value
                            ));

                    meapContext.TConsistPairCount++;
                }
            }
        }