Thursday, December 10, 2015

Introducing QBIC - QBasic's belated entry into the Code Golfing arena

I've been dabbling in Code Golfing, specifically on Stack Exchange: codegolf.stackexchange.com/.

The idea is that somebody sets a challenge / goal, and one must write the shortest possible code to achieve it. There are other kinds of puzzles than 'shortest!' but when I take one on, it;s one of these, And my weapon of choice: QBasic. (Or PHP, if the fancy takes me; but mostly QB). I know this language pretty well, feel comfortable using it. Compared to class-based languages like Java and C#, it's surprisingly concise. However, compared to the real Golfing deal, it doesn't hold a candle to Pyth, CJam etc.

So, I've taught QB some new tricks. Introducing QBIC, a QBasic pre-processor. Now, instead of writing

FOR A=1 TO 5
FOR B=1 TO 5 STEP 2
PRINT a * b
NEXT B
NEXT A

I get to do this:
[1|5:[1|5||2:?a*b]]

Notice anything different?

-- Get QBIC here --




The readme (can also be found in the package linked):

  QBIC, Qbasic's Interpreter for Code Golfing

  Preface
  
QBIC is a fairly straight-forward abbreviation-tool for QBasic, making it more 
competitive in Code Golf.

Why QBasic? Because I really like the simplicity of it,and we share a long 
history (I use elements of it even today, in the form of the closely-related 
VBA for Excel). 

  
  Prerequiste
  
1. A Qbasic editor/cmpiler
2. (on Windows > Vista) DosBox (or other VM capable of running 16-bit apps)

  
  Installation and usage
  
1. Create a folder for QBIC and place in it
 - QBIC.BAS  the compiler, of sorts
 - QBIC-IN.txt this will contain our source code
 - QBIC-OUT.BAS the results of our compilation process
2. Edit QBIC-IN.txt and enter any code you like.
3. Run QBIC.BAS; this will:
 - Open both QBIC-IN.txt and QBIC-OUT.BAS
 - Parse each line from QBIC-IN, writing the 
   compiled Basic to QBIC-OUT
 - then run QBIC-OUT.BAS

RECOMMENDED: For ease of access, I've altered the DosBox shortcut to:
 C:\...\DOSBox-0.74\DOSBox.exe -c "mount c c:\\qbasic"  -c "c:" -c "qbasic /run qbic\qbic.bas > qbic\screen.txt"
I've created the QBIC folder as a subfolder to QBasic, and the output that 
would be placed on the screen is written to screen.txt (in that same folder).


  Writing in QBIC

QBIC code is written in the file QBIC-IN.txt. No other files can (at this time)
be fed into the compiler, QBIC allows any valid Basic. So for instance:

 CLS
 PRINT "QBIC's awesome"
 BEEP

is perfectly valid code. But where does glofing come into play?
QBIC has a number of shortcuts over classic QB:

QBIC code Qbasic compiled source
 #   Comment, ignored by compiler
  {    DO:
  }    :LOOP:
  _C    CLS:
  _T    TIME$:
  _D    DATE$:
  _E    ELSE:
  _I    END IF:
  [    FOR (x)%=
  |     TO
  ||    STEP 
  ]    :NEXT (x)%:  
  
So, to Golf our example:
 _C
 PRINT "QBIC's awesome"
 BEEP

And, applying QBasic Golfing practise on top of this:
 _C:?"QBIC's awesome":BEEP

In the (near) future, I expect to add additional functionality to the compiler,
that creates a less literal mapping between QBIC and QBasic. For instance, the
FOR loop construct already does this to some extent. This:

 FOR A=1 TO 5
  FOR B=1 TO 5 STEP 2
   PRINT a * b
  NEXT B
 NEXT A

can be written in QBIC as:
 [1|5:[1|5||2:?a*b]]

No comments:

Post a Comment