Exercise
Visual Basic (VB.Net) to Pascal converter
Objetive
Create a program that converts simple Visual Basic (VB.Net) programs, such as the following one, to the Pascal language.
Code
Imports System
Imports System.IO
Namespace CSharpToPascal
Class Program
Private Shared Sub Main(ByVal args As String())
Dim inputFile As StreamReader
Dim outputFile As StreamWriter
Dim line As String
Dim name As String
If args.Length < 1 Then
Console.WriteLine("Not enough parameters!")
Console.WriteLine("Enter file name: ")
name = Console.ReadLine()
Else
name = args(0)
End If
inputFile = File.OpenText(name)
Dim pointPosition As Integer = name.LastIndexOf(".")
Dim newName As String = name.Substring(0, pointPosition) & ".pascal"
outputFile = File.CreateText(newName)
Do
line = inputFile.ReadLine()
If line IsNot Nothing Then
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 ") Then
line = line.Replace("int ", "var ")
line = line.Replace(";", ": integer;")
End If
If line.Contains(" = Convert.ToInt32(Console.ReadLine());") Then
line = line.Replace(" = Convert.ToInt32(Console.ReadLine());", "")
line = "ReadLn(" & line & ");"
End If
outputFile.WriteLine(line)
End If
Loop While line IsNot Nothing
inputFile.Close()
outputFile.Close()
End Sub
End Class
End Namespace