Conversor de Visual Basic a Pascal Curso de Programación Visual Basic

Lección:

Administración de Archivos


Ejercicio:

Conversor de Visual Basic a Pascal 18


Objetivo:

Crear un programa para convertir programas simples de Visual Basic, como el siguiente, al lenguaje Pascal


Código:

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