Quine

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
Hated Moron

Quine

Post by Hated Moron »

This has come up before, but since it was recently mentioned at Facebook I thought it would be appropriate to revisit it. It's also now possible to run the code in your browser.

A quine is "a computer program which takes no input and produces a copy of its own source code as its only output". Traditionally, a BASIC quine may not incorporate a command like LIST because that makes it too easy.

It's possible to write a true quine in BBC BASIC, but because it depends on the format in which the program is stored in memory there are slightly different versions for my BASICs and for Sophie's BASICs. Here is a version for my BASICs:

Code: Select all

PRINT $(PAGE+22)$(PAGE+21):REM PRINT $(PAGE+22)$(PAGE+21):REM
You can run this in your browser by clicking here, type LIST to list the program and RUN to run it again.

Here is a version for Sophie's BASICs (it doesn't print the line number, but that's not strictly 'part of the program'):

Code: Select all

10 PRINT $(PAGE+23)$(PAGE+23):REM PRINT $(PAGE+23)$(PAGE+23):REM
You can run this in JSBeeb by clicking here, type LIST to list the program and RUN to run it again.

Note that you can't run the version for my BASICs from SDLIDE, because that adds some 'hidden' code. Instead run it from BBCEdit, or save it to a file 'quine.bbc' and run it from the command prompt or by double-clicking.
Hated Moron

Re: Quine

Post by Hated Moron »

If you want a more 'generic' BASIC quine (at the expense of complexity) this will run in many BASICs:

Code: Select all

a$="a$=:PRINT LEFT$(a$,3);CHR$(34);a$;CHR$(34);MID$(a$,4)":PRINT LEFT$(a$,3);CHR$(34);a$;CHR$(34);MID$(a$,4)
Click here to run it in your browser. Type LIST to see the code and RUN to run it again. If you can simplify it I would be very interested.
User avatar
hellomike
Posts: 184
Joined: Sat 09 Jun 2018, 09:47
Location: Amsterdam

Re: Quine

Post by hellomike »

Slightly related to this, is a computer program that computes and shows its own entropy.
It is a Rosetta code task for which I recently wrote the following BBC BASIC solution:

Code: Select all

      DIM Freq%(255)
      FOR I%=PAGE TO LOMEM Freq%(?I%)+=1 NEXT
      Size=LOMEM - PAGE
      FOR I%=0 TO 255
        IF Freq%(I%) Entropy+=Freq%(I%) / Size * LN(Freq%(I%) / Size) / LN(2)
      NEXT
      PRINT "My size is ";Size " bytes and my entropy is ";-Entropy "!"
      END
See: Rosetta Code

Strangely enough the same source give different results in BB4W and the latest BBSDL.

Ultimately, the following 3 'tokens' produce different outcome, namely 7 and 82 bytes.

Code: Select all

      PRINT?PAGE
The outcome of 7 fits what the manual says.
Program storage in memory

Why is BBCSDL different and will it also effect Quine between versions?
I.e. this message is NOT meant to be critic, I'm merely curious. And yes, in the Rosetta solution I deliberately omitted ':' in the second line, just as an experiment.

Mike
Hated Moron

Re: Quine

Post by Hated Moron »

hellomike wrote: Thu 07 Dec 2023, 14:32 Why is BBCSDL different and will it also effect Quine between versions?
I think it's very unlikely that BBCSDL is different, I have mentioned several times before that (as far as the 32-bit Windows editions are concerned) BB4W and BBCSDL use virtually the identical interpreter.

I strongly suspect that the reason it appears to be different is that you are running it from SDLIDE. Try running your program from Andy Parkes' BBCEdit IDE, or from the command line, or by double-clicking on the .bbc file, or by compiling it to an executable with all the Crunch options disabled.

I expect that if you run it in any of those ways you will get the same result as in BB4W.
Hated Moron

Re: Quine

Post by Hated Moron »

hellomike wrote: Thu 07 Dec 2023, 14:32

Code: Select all

      FOR I%=PAGE TO LOMEM Freq%(?I%)+=1 NEXT
That code will crash 64-bit editions of BBC BASIC. It should read:

Code: Select all

      FOR I%%=PAGE TO LOMEM : Freq%(?I%%)+=1 : NEXT
Pointers (memory addresses) should be 64-bit integers, not 32-bit integers.
User avatar
hellomike
Posts: 184
Joined: Sat 09 Jun 2018, 09:47
Location: Amsterdam

Re: Quine

Post by hellomike »

Thanks for the feedback!
Yes, the code was executed directly from SDLIDE.
And oh :oops: 64 bits pointers..... Sorry about that. Not sure why it's so hard for me to catch on.
Hated Moron

Re: Quine

Post by Hated Moron »

hellomike wrote: Fri 08 Dec 2023, 08:43 Yes, the code was executed directly from SDLIDE.
I believe I have worked out a way of modifying SDLIDE so that this side-effect does not occur, and programs which 'look at their own code' (like the Quine and the Entropy program) will work properly from within the IDE as well as when run directly.

I will aim to make the necessary changes in a future release. Thank you for drawing this issue to my attention, I knew about it but hadn't realised that it had any practical consequences.
Hated Moron

Re: Quine

Post by Hated Moron »

Hated Moron wrote: Wed 13 Dec 2023, 14:37 I believe I have worked out a way of modifying SDLIDE so that this side-effect does not occur, and programs which 'look at their own code' (like the Quine and the Entropy program) will work properly from within the IDE as well as when run directly.
I have updated the Windows edition only to version 1.38b, which incorporates this modification to SDLIDE (there were consequential changes to sdldebug.bbc and profiler.bbc too). You might like to try it to confirm that the Quine program and the Entropy program now work when run from SDLIDE.

Updating the other desktop editions (MacOS, 32 and 64-bit Linux, 32 and 64-bit PiOS) is more work and I don't intend to do that until the next major release. If anybody wants this change incorporated in one of these editions let me know.
User avatar
hellomike
Posts: 184
Joined: Sat 09 Jun 2018, 09:47
Location: Amsterdam

Re: Quine

Post by hellomike »

Great work Richard. After installing BBCSDL 1.38b, the program published at
Entropy/Narcissist program on Rosetta

now works exactly the same (i.e. same output) running it from either the BB4W or the BBCSDL editor.