public static ProgramRepresentation.Program readProgram(string filename) { List<string> readLines; int lineI; Regex introRegex = new Regex("^<(?<type>[lg]) (?<name>[A-Za-z0-9]+)>"); Regex labelRegex = new Regex("^(?<type>[lg]) (?<name>[A-Za-z0-9]+)"); List<InstructionLinkageHolder> instructionLinkageHolders = new List<InstructionLinkageHolder>(); ProgramRepresentation.Program resultProgram = new Program(); readLines = Misc.TextFile.readLinesFromFile(filename); for( lineI = 0; lineI < readLines.Count; lineI++ ) { string currentLine; Match introRegexMatch; string labelType; string labelName; int remainingLines; string instructionTypeString; InstructionLinkageHolder.LinkageHolder.Type instructionLinkageHolderType; remainingLines = readLines.Count - lineI - 1; currentLine = readLines[lineI]; if( currentLine.Length >= 2 && currentLine.Substring(0, 2) == "//" ) { continue; } if( currentLine.Length == 0 ) { continue; } introRegexMatch = introRegex.Match(currentLine); if( !introRegexMatch.Success ) { throw new ParsingError(lineI+1, "No valid label/instruction!"); } labelType = introRegexMatch.Groups["type"].Value; labelName = introRegexMatch.Groups["name"].Value; if( labelType == "l" ) { // local instructionLinkageHolderType = InstructionLinkageHolder.LinkageHolder.Type.LOCAL; } else if( labelType == "g" ) { // global instructionLinkageHolderType = InstructionLinkageHolder.LinkageHolder.Type.GLOBAL; } else { throw new ParsingError(lineI+1, "label type must be either (g)lobal or (l)ocal!"); } if( remainingLines == 0 ) { throw new ParsingError(lineI+1, "too less lines!"); } instructionTypeString = readLines[lineI+1]; DagDataInfo dagDataInfo = getDagDataInfoByTypeString(lineI + 1, instructionTypeString); bool remainingLengthEnought = checkRemainingLength(dagDataInfo, remainingLines); if( !remainingLengthEnought ) { throw new ParsingError(lineI + 1, "too few lines!"); } lineI++; InstructionLinkageHolder instructionLinkageHolderForThisInstruction; instructionLinkageHolderForThisInstruction = new InstructionLinkageHolder(new InstructionLinkageHolder.LinkageHolder(instructionLinkageHolderType, labelName)); instructionLinkageHolders.Add(instructionLinkageHolderForThisInstruction); int childI; for( childI = 0; childI < dagDataInfo.childIndicesLength; childI++ ) { string childString; Match labelRegexMatch; string childLabelType, childlabelName; InstructionLinkageHolder.LinkageHolder.Type childInstructionLinkageHolderType; InstructionLinkageHolder.LinkageHolder childLinkage; childString = readLines[lineI + 1 + childI]; labelRegexMatch = labelRegex.Match(childString); if( !labelRegexMatch.Success ) { throw new ParsingError(lineI + 1 + childI, "can't parse label"); } childLabelType = labelRegexMatch.Groups["type"].Value; childlabelName = labelRegexMatch.Groups["name"].Value; if( childLabelType == "l" ) { childInstructionLinkageHolderType = InstructionLinkageHolder.LinkageHolder.Type.LOCAL; } else if( childLabelType == "g" ) { childInstructionLinkageHolderType = InstructionLinkageHolder.LinkageHolder.Type.GLOBAL; } else { throw new ParsingError(lineI + 1 + childI, "invalid type"); } childLinkage = new InstructionLinkageHolder.LinkageHolder(childInstructionLinkageHolderType, childlabelName); instructionLinkageHolderForThisInstruction.childLinks.Add(childLinkage); } lineI += dagDataInfo.childIndicesLength + 1; ProgramRepresentation.DagElementData newDagElementData; newDagElementData = new DagElementData(); newDagElementData.type = dagDataInfo.type; string identifierName; string valueString; switch( dagDataInfo.type ) { case DagElementData.EnumType.IDENTIFIERNAME: case DagElementData.EnumType.ARRAYREAD2D: case DagElementData.EnumType.ARRAYREAD1D: case DagElementData.EnumType.PARAM: if( lineI + 1 > readLines.Count ) { throw new ParsingError(lineI + 1, "too few lines!"); } identifierName = readLines[lineI]; lineI++; newDagElementData.identifier = identifierName; break; case DagElementData.EnumType.CONSTINT: if (lineI + 1 > readLines.Count) { throw new ParsingError(lineI + 1, "too few lines!"); } valueString = readLines[lineI]; lineI++; newDagElementData.valueInt = Convert.ToInt32(valueString); break; case DagElementData.EnumType.CONSTFLOAT: if (lineI + 1 > readLines.Count) { throw new ParsingError(lineI + 1, "too few lines!"); } valueString = readLines[lineI]; lineI++; newDagElementData.valueFloat = (float)Convert.ToDouble(valueString); break; } // add instruction resultProgram.dag.addElement(new Datastructures.Dag<DagElementData>.Element(newDagElementData)); } // linkage doLinkage(resultProgram, instructionLinkageHolders); return resultProgram; }
public DagDataInfo(string typeAsString, DagElementData.EnumType type, int childIndicesLength) { this.typeAsString = typeAsString; this.type = type; this.childIndicesLength = childIndicesLength; }