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

Lesson:

File Management


Exercise:

Pascal to Visual Basic (VB.net) translator


Objetive:

Create a basic Pascal to Visual Basic (VB.net) 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 Visual Basic (VB.net) source from the previous Pascal source and similar ones: up to 11 points.


Code:

Imports System
Imports System.IO
Namespace PascalToCSharp
    Class Program
        Private Shared Sub Main()
            Console.Write("Enter name file: ")
            Dim fileName As String = Console.ReadLine()

            If File.Exists(fileName) Then
                Dim filePascal As StreamReader = File.OpenText(fileName)
                Dim fileCSharp As StreamWriter = File.CreateText(fileName.Substring(0, fileName.Length - 3) & "cs")
                Dim line As String

                Do
                    line = filePascal.ReadLine()

                    If line IsNot Nothing Then
                        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 ")) AndAlso (line.Substring(line.Length - 1) = ";") Then
                            line = line.Replace("program ", "class ")
                            line = line.Replace(";", vbLf & "{" & vbLf & " static void Main()" & vbLf & "{")
                        End If

                        If line.Contains("readLn(") Then
                            line = line.Replace("readLn(", "")
                            line = line.Replace(");", "")
                            line += " = Convert.ToInt32(Console.RadLine());"
                        End If

                        line = line.Replace("var", "")

                        If line.Contains(": integer;") Then
                            line = line.Replace(": integer;", "")
                            line = "int " & line.Trim() & ";"
                        End If

                        fileCSharp.WriteLine(line)
                    End If
                Loop While line IsNot Nothing

                filePascal.Close()
                fileCSharp.Close()
            End If
        End Sub
    End Class
End Namespace

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