Metamorphing Machine I rather be this walking metamorphosis
than having that old formed opinion about everything!

Let's build a transpiler! Part 5

This is the fifth post in a series of building a transpiler. You can find the previous ones here.

Last time, we said we'd need to scan line breaks.
You may not know, but there's no consensus about what makes a line break among OSes.
In Linux, it is a line feed ("\n", character 10).
In Windows, it is a carriage return ("\r", character 13) followed by a line feed.
In other not-so-common OSes, it is a carriage return alone.

As I intend to create a transpiler that does not depend on the operating system, we'll deal with all of the cases above.

Plus, there are some VB6 features we'll need to take into account:
  1. To separate one statement from another but keep them in the same line, one can use a colon (":"). Example: If SomeCondition Then : Debug.Print "SomeCondition happened!" : Stop
  2. To break a logical line into more than one physical line, one can use a white-space followed by an underscore, followed by the actual line break.
Regarding the first point, we'll report a line break whenever we see a line feed or a carriage return optionally followed by a line feed.

I did not mention before, but white-spaces are ignored in VB, except in a very specific scenario we will yet to come.
Regarding the second point, we will equate a white-space + underscore + line break with a lone white-space.
Which take us to our first function:

Private Function IsSpace(ByVal CodePoint As Integer) As Boolean
Const NULL_CHAR As Integer = 0
Const VERTICAL_TAB As Integer = 9
Const EOM As Integer = 25
Const WHITE_SPACE As Integer = 32
Const NO_BREAK_SPACE As Integer = 160
Const OGHAM_SPACE_MARK As Integer = &H1680
Const MONGOLIAN_VOWEL_SEPARATOR As Integer = &H180E
Const EN_QUAD As Integer = &H2000
Const HAIR_SPACE As Integer = &H200A
Const NARROW_NO_BREAK_SPACE As Integer = &H202F
Const MEDIUM_MATHEMATICAL_SPACE As Integer = &H205F
Const IDEOGRAPHIC_SPACE As Integer = &H3000

Select Case CodePoint
Case NULL_CHAR, WHITE_SPACE, VERTICAL_TAB, EOM, NO_BREAK_SPACE, OGHAM_SPACE_MARK, MONGOLIAN_VOWEL_SEPARATOR, _
EN_QUAD To HAIR_SPACE, NARROW_NO_BREAK_SPACE, MEDIUM_MATHEMATICAL_SPACE, IDEOGRAPHIC_SPACE
IsSpace = True
End Select
End Function

Now, let's add this to our sub Main:

(...)
Case ":"
Debug.Print "Soft line-break"

Case vbLf:
Debug.Print "Hard line-break"

Case vbCr:
If GetCodePoint <> 10 Then UngetChar
Debug.Print "Hard line-break"

Case Else
If IsSpace(Cp) Then
If Not EOF(FileHandle_) Then
If GetChar = "_" Then
If EOF(FileHandle_) Then
UngetChar
Else
Cp = GetCodePoint
If Cp <> 10 And Cp <> 13 Then UngetChar 2
End If
Else
UngetChar
End If
End If

Debug.Print "White-space"

ElseIf IsLetter(Cp) Then
(...)

Next week, comments.

Andrej Biasic
2020-08-12