public static TuringMachine FromText(string[] lines) { TuringMachineData data = null; string initialState = null; string[] goalStates = new string[0]; List <TuringMachineInstruction> instructions = new List <TuringMachineInstruction>(); foreach (var line in lines) { //Check if is comment var cleanedLine = line.Trim(); if (cleanedLine.StartsWith("@")) { continue; } if (cleanedLine.StartsWith("fita ")) { if (data != null) { throw new ArgumentException($"Cannot redefine data", nameof(lines)); } data = new TuringMachineData(cleanedLine.Substring(5)); //Ignore 'fita ' } else if (cleanedLine.StartsWith("init ")) { if (initialState != null) { throw new ArgumentException($"Cannot redefine initial state", nameof(lines)); } initialState = cleanedLine.Substring(5); } else if (cleanedLine.StartsWith("accept ")) { if (goalStates.Any()) { throw new ArgumentException($"Cannot redefine goal states", nameof(lines)); } goalStates = cleanedLine.Substring(7).Trim().Split(","); } else if (!string.IsNullOrWhiteSpace(cleanedLine)) { try { var instruction = TuringMachineInstruction.FromString(cleanedLine); instructions.Add(instruction); } catch (ArgumentException e) { throw new ArgumentException($"Could not parse line {cleanedLine}: {e.Message}", e); } } } return(new TuringMachine(data, initialState, goalStates, instructions.ToArray())); }
private TuringMachine(TuringMachineData data, string initialState, string[] goalStates, TuringMachineInstruction[] instructions) { Data = data; CurrentState = initialState; GoalStates = goalStates; Instructions = instructions; MachineState = State.Ready; Result = null; }