04-29-2024, 03:02 PM
A "quine" is a program that when you run it, generates a listing of itself, such that if you took its output and copied into QB64, it would produce the same thing, itself.
Well, I tried writing one, yeah I could get the program to list itself, but generating the data to allow that program to display itself, and have it look exactly the same is the problem. Because you have to act one step back, meaning if you have a string, you have to enclose it in quotation marks, which means you have to show the quote mark, Chr$(34). It's still a pain.
So I decided to see other ways this could be done. And one said to write a program to list itself as contents from an array. That made it trivial.
What I did was write the program to list itself. But now it needs the data. So to get that, I wrote another program. This one reads a file and converts it into DATA statements containing the byte values as data statements. I copied them, added an extra 0 at the end as an end-of-file sentinel, and sure enough, it works perfectly.
First, here is the program, which I call dataconvert.bas, that translates a file into data statements:
The files Common_Dialog_Prefix.bi and Common_Dialog_Suffix.bi are included in the attached archive, and simply enable the use of the Open File dialog on Windows.
Now, here is the program to list itself:
For brevity, the data statements are stripped out here, but are included in the copy below. If you want to try this, be sure you include a 0 as the last data item.
Writing a quine was a fun intellectual enterprise, and I'll probably do a different one soon. Doing this one gave me ideas on how to fix the other one. I hope you find looking at/exploring this one as much fun as I had writing it!
Paul
Well, I tried writing one, yeah I could get the program to list itself, but generating the data to allow that program to display itself, and have it look exactly the same is the problem. Because you have to act one step back, meaning if you have a string, you have to enclose it in quotation marks, which means you have to show the quote mark, Chr$(34). It's still a pain.
So I decided to see other ways this could be done. And one said to write a program to list itself as contents from an array. That made it trivial.
What I did was write the program to list itself. But now it needs the data. So to get that, I wrote another program. This one reads a file and converts it into DATA statements containing the byte values as data statements. I copied them, added an extra 0 at the end as an end-of-file sentinel, and sure enough, it works perfectly.
First, here is the program, which I call dataconvert.bas, that translates a file into data statements:
Code: (Select All)
$Console:Only
'$Include:'Common_Dialog_Prefix.bi'
ReDim As _Unsigned _Byte Prg(1), L
Dim As Long I, J, K, Size
' Invoke Open Read File dialog
Filter$ = "Basic Programs (*.bas,bi.bm)|*.bas,*.bi,*.bm|All files (*.*)|*.*"
Flags& = OFN_FILEMUSTEXIST + OFN_NOCHANGEDIR + OFN_READONLY ' add flag constants here
F$ = GetOpenFileName$("Select name of Basic/QB64/qb64pe source code file", ".\", Filter$, 1, Flags&, Hwnd&)
If F$ = "" Then
Print "Operation cancelled."
End
End If
Open F$ For Binary Access Read As #1
I = LOF(1)
ReDim Prg(1 To I)
Get #1, , Prg()
Close #1
For J = 1 To I
If L = 0 Then Print "Data ";
Print LTrim$(Str$(Prg(J)));
If J < I Then
L = L + 1
If L = 20 Then
Print
L = 0
Else
Print ",";
End If
Else
Print
End If
Next
End
'$Include:'Common_Dialog_Suffix.bi'
The files Common_Dialog_Prefix.bi and Common_Dialog_Suffix.bi are included in the attached archive, and simply enable the use of the Open File dialog on Windows.
Now, here is the program to list itself:
Code: (Select All)
$Console:Only
Option _Explicit
Dim As _Unsigned _Byte Prg(1 To 100000), L
Dim As Long I, J, K
Do
I = I + 1
Read Prg(I)
If Prg(I) = 0 Then Exit Do
Loop
For J = 1 To I
Print Chr$(Prg(J));
Next
For J = 1 To I
If L = 0 Then Print "Data ";
Print LTrim$(Str$(Prg(J)));
If J < I Then
L = L + 1
If L = 20 Then
Print
L = 0
Else
Print ",";
End If
Else
Print
End If
Next
End
For brevity, the data statements are stripped out here, but are included in the copy below. If you want to try this, be sure you include a 0 as the last data item.
Writing a quine was a fun intellectual enterprise, and I'll probably do a different one soon. Doing this one gave me ideas on how to fix the other one. I hope you find looking at/exploring this one as much fun as I had writing it!
Paul
While 1
Fix Bugs
report all bugs fixed
receive bug report
end while
Fix Bugs
report all bugs fixed
receive bug report
end while