Conversor de C# a Pascal Curso de Programación C# Sharp

Lección:

Administración de Archivos


Ejercicio:

Conversor de C# a Pascal 11


Objetivo:

Crear un programa para convertir programas simples de C#, como el siguiente, al lenguaje Pascal


Código:

using System;
using System.IO;
namespace CSharpToPascal
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader inputFile;
            StreamWriter outputFile;
            string line;
            string name;

            if (args.Length < 1)
            {
                Console.WriteLine("Not enough parameters!");
                Console.WriteLine("Enter file name: ");
                name = Console.ReadLine();
            }
            else
                name = args[0];

            inputFile = File.OpenText(name);
            int pointPosition = name.LastIndexOf(".");
            string newName = name.Substring(0, pointPosition) + ".pascal";
            outputFile = File.CreateText(newName);

            do
            {
                line = inputFile.ReadLine();
                if (line != null)
                {
                    // Automatic changes
                    line = line.Replace("Console.WriteLine", "WriteLn");
                    line = line.Replace("\"", "\'");
                    line = line.Replace("==", " = ");
                    line = line.Replace("=", " = ");
                    line = line.Replace("public static void Main()", "");
                    line = line.Replace("{", "begin");
                    line = line.Replace("}", "end;");
                    line = line.Replace("}", "end.");
                    line = line.Replace("}", "end");
                    line = line.Replace("using System;", "");
                    line = line.Replace("public class", "program");

                    if (line.Contains("int "))
                    {
                        line = line.Replace("int ", "var ");
                        line = line.Replace(";", ": integer;");
                    }

                    if (line.Contains(" = Convert.ToInt32(Console.ReadLine());"))
                    {
                        line = line.Replace(" = Convert.ToInt32(Console.ReadLine());", "");
                        line = "ReadLn(" + line + ");";
                    }
                    // ...
                    // Changes are saved
                    outputFile.WriteLine(line);
                }

            } while (line != null);

            inputFile.Close();
            outputFile.Close();
        }
    }
}