I wrote the following today (2001-03-27) for Kuro5hin. The topic of the day was OO programming in non-OO languages; Since program written in one Turing-complete language is (in theory) possible to implement in another Turing-complete language, someone asked about OO programming in Commodore 64 basic.

Little did they know what they unleashed... <still cackling evilly>

(Presented here in all-typoful glory.)


To put it another way, let's say you are given a C=64 and are told you need to write a random number picker in BASIC, but in an object oriented fashion - can it be done? Can anyone post examples?

<evil-hacker>Fool! You asked for this, and now you got it! Muhahahahahahhahahaha....</evil-hacker>

Well, in Commodore 64, you'd probably end up doing that in Assembler - this is just due to fact that SID (sound chip) can't be controlled with BASIC¹. (You see, the coolest way to get random numbers is to set SID chip to output noise, and read the noise output register value. AFAIK, this produces more or less true random numbers. Eat that, PC folks =)

But to the topic: It might be possible to make OO-style RNG with C64 BASIC - though OO in BASIC is seriously overkill (and no, VB hasn't changed that a single bit =)

Basically, I would use one array to hold references to "objects", multi-dimensional arrays to store object fields, and subroutines (callable with GOSUB) to implement methods.

Basically, I would do this (exact syntax might be different, as I haven't touched C64 BASIC for some months, the book is on the top self and I'm too lazy to get my ass up from this most uncomfortable and thrououghtly broken chair):

10 REM GENERATE NEW RNG OBJECT
20 GOSUB 500
30 REM (THE RNG REFERENCE IS IN VARIABLE R(0))
40 REM CALL RNG, SET RNG TO BE CALLED
50 C = R(0)
60 GOSUB 550
70 REM READ RESULT
80 RS = CR
90 REM ... REST OF THE CODE FOLLOWS
100 REM RNG CODE STARTS FROM LINE 500, NOT HERE (I HAVE ONLY BAD MEMORIES
110 REM FROM THIS HORRIBLE EXCUSE OF BASIC INTERPRETER FROM MICROSOFT)

Variables used:

R(x)
Array of RNG objects
C
the RNG object to be called (since GOSUBs don't take arguments, everything needs to be passed via global variables - BASIC sucks, eh?)
CR
Function call return value (No, GOSUBs won't return anything either...)

I could implement automatic object reference handler... but I think I'm not insane enough to do that... or maybe it's just that I'm feeling lazy today, I'm definitely insane enough to do that, allright. =)


¹ Update 2002-10-24: Okay, I was in pretty much in the Coffee when I wrote this, because I probably otherwise couldn't come up with all this stuff. As Bowie J. Poag noted, yes, in fact SID can be controlled in BASIC with POKEs. Perharps I meant "can't easily be controlled in BASIC". =)

Log in or register to write something here or to contact authors.