private static (DFG <Block>, List <string>) GetNextGraph(CDFG graph, DFG <Block> currentDFG, CommandExecutor <T> executor, Dictionary <string, float> variables, Stack <IControlBlock> controlStack, Stack <List <string> > scopeStack, Dictionary <string, BoardFluid> dropPositions) { List <string> variablesOutOfScope = new List <string>(); { IControlBlock control = graph.DfgToControl[currentDFG]; if (control != null) { DFG <Block> guardedDFG = control.GuardedDFG(variables, executor, dropPositions); if (guardedDFG != null) { controlStack.Push(control); scopeStack.Push(new List <string>()); return(guardedDFG, variablesOutOfScope); } DFG <Block> nextDFG = control.NextDFG(variables, executor, dropPositions); if (nextDFG != null) { return(nextDFG, variablesOutOfScope); } } } while (controlStack.Count > 1) { IControlBlock control = controlStack.Pop(); List <string> newVariablesOutOfScope = scopeStack.Pop(); variablesOutOfScope.AddRange(newVariablesOutOfScope); foreach (string variable in newVariablesOutOfScope) { if (variables.ContainsKey(variable)) { variables.Remove(variable); } else if (dropPositions.ContainsKey(variable)) { dropPositions.Remove(variable); } } DFG <Block> loopDFG = control.TryLoop(variables, executor, dropPositions); if (loopDFG != null) { controlStack.Push(control); scopeStack.Push(new List <string>()); return(loopDFG, variablesOutOfScope); } DFG <Block> nextDFG = control.NextDFG(variables, executor, dropPositions); if (nextDFG != null) { return(nextDFG, variablesOutOfScope); } } return(null, variablesOutOfScope); }
private IControlBlock CreateTextBlock(Match match, IControlBlock parentNode) { IControlBlock textBlock = new TextBlock(false); textBlock.Start = match == null ? 0 : match.Index + match.Length; textBlock.DocumentParser = this; textBlock.Parent = parentNode; textBlock.OpenRegEx = match; parentNode.Children.Add(textBlock); return(textBlock); }
private IControlBlock CloseControlBlock(IControlBlock closingNode /*, int matchIndex*/) { IControlBlock cb = closingNode; cb.MatchEnd = RTFInput.Length; cb.CloseRegEx = null; closingNode.End = RTFInput.Length; closingNode.InnerText = RTFInput.ToString().InnerString(closingNode.Start, closingNode.End).Trim(); // TODO: CAMBIO RTFInput.ToString().InnerString(closingNode.Start, closingNode.End); closingNode = closingNode.Parent; return(closingNode); }
private IControlBlock CloseControlBlock(IControlBlock closingNode, Match match) { IControlBlock cb = (IControlBlock)closingNode; cb.MatchEnd = match.Index + match.Length; cb.CloseRegEx = match; closingNode.End = match.Index; closingNode.InnerText = RTFInput.ToString().InnerString(closingNode.Start, closingNode.End).Trim(); // TODO: CAMBIO RTFInput.ToString().InnerString(closingNode.Start, closingNode.End); closingNode = closingNode.Parent; return(closingNode); }
internal static DFG <Block> ParseDFG(XmlNode node, ParserInfo parserInfo, bool allowDeclarationBlocks = false, bool canFirstBlockBeControlFlow = true) { parserInfo.EnterDFG(); try { IControlBlock controlBlock = null; var dfg = new DFG <Block>(); while (true) { if (IsDFGBreaker(node, dfg) && canFirstBlockBeControlFlow) { controlBlock = ParseDFGBreaker(node, dfg, parserInfo); break; } canFirstBlockBeControlFlow = true; Block block = null; try { block = ParseAndAddNodeToDFG(node, dfg, parserInfo, allowDeclarationBlocks); } catch (ParseException e) { parserInfo.ParseExceptions.Add(e); } allowDeclarationBlocks = block is DeclarationBlock && allowDeclarationBlocks; //move on to the next node or exit if none node = node.TryGetNodeWithName("next"); if (node == null) { break; } node = node.FirstChild; } if (parserInfo.ParseExceptions.Count == 0) { dfg.FinishDFG(); } parserInfo.cdfg.AddNode(controlBlock, dfg); parserInfo.LeftDFG(); return(dfg); } catch (ParseException e) { parserInfo.ParseExceptions.Add(e); parserInfo.LeftDFG(); return(null); } }
private IControlBlock CreateScan(IControlBlock currentNode, Match match, bool hasCondition) { IControlBlock scanBlock = new ScanBlock(match, hasCondition, this); scanBlock.OpenRegEx = match; scanBlock.MatchStart = match.Index; scanBlock.Start = match.Index + match.Length; ((ScanBlock)scanBlock).Table = DataManager.Tables[((ScanBlock)scanBlock).TableName].Table; scanBlock.DocumentParser = this; scanBlock.Parent = currentNode; currentNode.Children.Add(scanBlock); currentNode = scanBlock; return(currentNode); }
private void CheckCopyControl(IControlBlock original, IControlBlock copy) { IEnumerator <DFG <Block> > oEnumerator = original.GetEnumerator(); IEnumerator <DFG <Block> > cEnumerator = copy.GetEnumerator(); while (true) { bool oHasNext = oEnumerator.MoveNext(); bool cHasNext = cEnumerator.MoveNext(); Assert.AreEqual(oHasNext, cHasNext); if (!oHasNext) { break; } CheckCopyDFG(oEnumerator.Current, cEnumerator.Current); } }
private void CheckCopyCDFG(CDFG original, CDFG copy) { CheckCopyDFG(original.StartDFG, copy.StartDFG); Assert.AreEqual(original.Nodes.Count, copy.Nodes.Count); for (int i = 0; i < original.Nodes.Count; i++) { DFG <Block> oDFG = original.Nodes[i].dfg; DFG <Block> cDFG = copy.Nodes[i].dfg; CheckCopyDFG(oDFG, cDFG); IControlBlock oControl = original.Nodes[i].control; IControlBlock cControl = copy.Nodes[i].control; if (oControl != null) { Assert.IsNotNull(cControl); CheckCopyControl(oControl, cControl); } else { Assert.IsNull(cControl); } } }
private void GetControlBlocks() { bool fetchedTrailingText = false; //Locate and put an special mark on the "progamming sentences" so it's easier to find and delete them later. PreprocessText(); Regex regex = CommonMethods.GetRegex(KeywordsRegExString); matchCollection = regex.Matches(RTFInput.ToString()); #region Set up the document's root documentNode = new TextBlock(true); documentNode.Start = 0; if (matchCollection.Count > 0) { documentNode.End = matchCollection[0].Index - 1; } else { documentNode.End = RTFInput.Length; } documentNode.InnerText = RTFInput.ToString().InnerString(documentNode.Start, documentNode.End).Trim(); documentNode.DocumentParser = this; IControlBlock currentNode = documentNode; #endregion int currRegex = 0; foreach (Match match in matchCollection) { #region Create the tree of control blocks switch (GetKeyWord(match)) { case KeyWords.SCAN: { //Closes the Texblok immediately before this SCAN if (!currentNode.IsRoot) { currentNode = CloseControlBlock(currentNode, match); } IControlBlock scan = CreateScan(currentNode, match, false); //Creates a new TextBlock as a child of this scan currentNode = CreateTextBlock(match, scan); break; } case KeyWords.SCAN_FOR: { //Closes the Texblok immediately before this SCAN if (!currentNode.IsRoot) { currentNode = CloseControlBlock(currentNode, match); } IControlBlock scan = CreateScan(currentNode, match, true); currentNode = CreateTextBlock(match, scan); break; } case KeyWords.IF: { //Closes the Texblok immediately before this if if (!currentNode.IsRoot) { currentNode = CloseControlBlock(currentNode, match); } IControlBlock ifBlock = new IfBlock(match, this); ifBlock.OpenRegEx = match; ifBlock.MatchStart = match.Index; ifBlock.Start = match.Index + match.Length; ifBlock.DocumentParser = this; ifBlock.Parent = currentNode; currentNode.Children.Add(ifBlock); //Create a new TextBlock for the "true" part of the if currentNode = CreateTextBlock(match, ifBlock); break; } case KeyWords.ELSE: { //Close the 'true' part of the if currentNode = CloseControlBlock(currentNode, match); // Create the 'Else' part currentNode = CreateTextBlock(match, currentNode); break; } case KeyWords.ENDIF: { //Close the 'true' or the 'false' part of the if currentNode = CloseControlBlock(currentNode, match); //Close the embracing 'if' block currentNode = CloseControlBlock(currentNode, match); currentNode = CreateTextBlock(match, currentNode); if (currRegex == matchCollection.Count - 1) //There are no more regexes ahead { //It's the end of the document, close the textblock right here currentNode = CloseControlBlock(currentNode); fetchedTrailingText = true; } break; } case KeyWords.ENDSCAN: { if (currentNode is TextBlock) { currentNode = CloseControlBlock(currentNode, match); } currentNode = CloseControlBlock(currentNode, match); currentNode = CreateTextBlock(match, currentNode); if (currRegex == matchCollection.Count - 1) //There are no more regexes ahead { //It's the end of the document, close the textblock right here currentNode = CloseControlBlock(currentNode); fetchedTrailingText = true; } break; } } currRegex++; #endregion } Match startMatch = null; if (matchCollection.Count > 0 && !fetchedTrailingText) { startMatch = matchCollection[currRegex - 1]; IControlBlock trailingText = CreateTextBlock(startMatch, currentNode); currentNode = CloseControlBlock(trailingText); } }
private void TransformVariableNames(CDFG cdfg, InlineProgramInfo programInfo, string postfix) { Stack <IEnumerator <DFG <Block> > > stack = new Stack <IEnumerator <DFG <Block> > >(); HashSet <string> readerBlacklist = new HashSet <string>(); HashSet <string> writerBlacklist = new HashSet <string>(); programInfo.InputsFromTo.ForEach(x => readerBlacklist.Add(x.Value.OriginalFluidName)); programInfo.VariablesFromTo.ForEach(x => GetVariableBlockDependencies(x.Value.GetVariableTreeList(new List <VariableBlock>())).ForEach(y => readerBlacklist.Add(y))); programInfo.VariablesFromTo.ForEach(x => readerBlacklist.Add(x.Key)); programInfo.OutputsFromTo.ForEach(x => writerBlacklist.Add(x.Value)); DFG <Block> currentDFG = cdfg.StartDFG; do { foreach (Node <Block> node in currentDFG.Nodes) { Block block = node.value; foreach (FluidInput fluidInput in block.InputFluids) { if (!readerBlacklist.Contains(fluidInput.OriginalFluidName) && !writerBlacklist.Contains(fluidInput.OriginalFluidName)) { fluidInput.OriginalFluidName += postfix; } } for (int i = 0; i < block.InputNumbers.Count; i++) { if (!readerBlacklist.Contains(block.InputNumbers[i])) { block.InputNumbers[i] = block.InputNumbers[i] + postfix; } } if (readerBlacklist.Contains(block.OutputVariable)) { readerBlacklist.Remove(block.OutputVariable); } if (!writerBlacklist.Contains(block.OutputVariable)) { block.OutputVariable += postfix; } } IControlBlock control = cdfg.DfgToControl[currentDFG]; if (control != null) { stack.Push(control.GetEnumerator()); } while (stack.Count > 0) { if (!stack.Peek().MoveNext()) { stack.Pop(); continue; } currentDFG = stack.Peek().Current; break; } } while (stack.Count > 0); }