Visual Basic (VB.net) to Pascal converter Learn programming Visual Basic (VB.net)

Lesson:

File Management


Exercise:

Visual Basic (VB.net) to Pascal converter 21


Objetive:

Create a program to convert simple Visual Basic (VB.net) programs, such as the following one, to 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