Exercise
C# To Pascal Converter
Objetive
Create a program that converts simple C# programs, such as the following one, to the Pascal language.
Example Code
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();
}
}
}