Pascal to C# translator Learn programming C#

Lesson:

File Management


Exercise:

Pascal to C# translator


Objetive:

Create a basic Pascal to C# translator. It will accept program such as:

example program;

var
i: integer;
max: integer;

begin
writeLn("How many times?");
readLn(max);
for i := 1 to max do
writeLn("Hola");
end.

The steps you must follow are:
Read from beginning to end a text file, whose name will be entered by the user in command line or in an interactive way: up to 2 points.

Dump the contents to another text file, whose name will be the same, but with ".cs" extension instead of ".pas": up to 4 points.

Replace "WriteLn" with "Console.WriteLine", " = "with "==", " := " with "=", simple quotes with double quotes, "begin" with "{" and "end;", "end.", "end" (in that order) with "}", : up to 6 points.

Replace "program x;" with "class x {" followed with "Main", Replace "readln(x)" with "x=Convert.ToInt32(Console.RadLine())" ("x" must be any other identifier): up to 8 points.

Eliminate "var" and replace "x: integer" with "int x" (but "x" must be any other identifier): up to 9 points. Give a proper format to "for": up to 10 points.

Create a compilable C# source from the previous Pascal source and similar ones: up to 11 points.


Code:

using System;
using System.IO;
namespace PascalToCSharp
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter name file: ");
            string fileName = Console.ReadLine();


            if (File.Exists(fileName))
            {
                StreamReader filePascal = File.OpenText(fileName);
                StreamWriter fileCSharp = File.CreateText(fileName.Substring(0, fileName.Length - 3) + "cs");
                string line;
                do
                {
                    line = filePascal.ReadLine();
                    if (line != null)
                    {
                        line = line.Replace("writeLn", "Console.WriteLine");
                        line = line.Replace(" = ", "==");
                        line = line.Replace(" :=", "=");
                        line = line.Replace("'", "\"");
                        line = line.Replace("begin", "{");
                        line = line.Replace("end;", "}");
                        line = line.Replace("end.", "}");
                        line = line.Replace("end", "}");

                        if ((line.Contains("program ")) &&
                             (line.Substring(line.Length - 1) == ";"))
                        {
                            line = line.Replace("program ", "class ");
                            line = line.Replace(";", "\n{\n static void Main()\n{");
                        }

                        if (line.Contains("readLn("))
                        {
                            line = line.Replace("readLn(", "");
                            line = line.Replace(");", "");
                            line += " = Convert.ToInt32(Console.RadLine());";
                        }

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

                        /* if ((line.Contains("for ")) &&
                            (line.Contains(" to ")) && 
                            (line.Contains(" do ")))
                         {
                             line = line.Replace("for ", "for (");
                             line = line.Replace(" to", "");
                             line = "int " + line.Trim() + ";";
                         }
                 */

                        fileCSharp.WriteLine(line);
                    }
                }
                while (line != null);
                filePascal.Close();
                fileCSharp.Close();
            }
        }
    }
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.