private async Task TranslateInnerAsync(BasicProgram program) { while (true) { string line = await this.reader.ReadLineAsync(); if (line == null) { return; } string[] numberAndStatement = line.Split(new char[] { ' ' }, 2); int lineNumber = int.Parse(numberAndStatement[0]); string[] keywordAndRest = numberAndStatement[1].Split(new char[] { ' ' }, 2); switch (keywordAndRest[0]) { case "REM": program.AddComment(lineNumber, keywordAndRest[1]); break; case "PRINT": program.AddPrint(lineNumber, keywordAndRest[1].Substring(1, keywordAndRest[1].Length - 2)); break; default: program.AddGoto(lineNumber, int.Parse(keywordAndRest[1])); break; } } }
private static async Task MainAsync(string inputPath, string outputPath) { string name = Path.GetFileNameWithoutExtension(inputPath); using (Stream output = Open(outputPath, FileMode.Create, FileAccess.Write, FileShare.None)) { Stream input = Open(inputPath, FileMode.Open, FileAccess.Read, FileShare.Read); await BasicProgram.TranslateAsync(name, input, output); } }
public async Task TranslateAsync(string name, Stream output) { BasicProgram program = new BasicProgram(name); await this.TranslateInnerAsync(program); string outputCode = program.ToString(); byte[] rawOutput = Encoding.UTF8.GetBytes(outputCode.ToString()); await output.WriteAsync(rawOutput, 0, rawOutput.Length); }