10

What are the rules for Applesoft BASIC formatting for code? I've been doing some BASIC coding and noticed that:

10 input "some question?"; name$

Will be reformatted as:

10  INPUT "some question?";NAME$

I did not notice this formatting before because BASIC was my first programming language. Coming back to BASIC from many different programming editors I'm more cognizant of the formatting that code gets. To my more experienced eyes the formatting copies some of how FORTRAN is arranged, which I'm sure is not an accident.

16

This is an AppleSoft issue where it puts spaces either side of a token. PET/CBM BASIC (based off the same code base) doesn't do this.

Tokenising ignores spaces, this means it can be difficult on the PET to see some of the problems that the tokeniser can cause.

For example LET AFOR=ONER gets parsed as LET A FOR = ON ER

Which is very difficult to see on a PET, I suspect someone at Apple changed this (IIRC the 8080 Basic is the same as a PET).

For the version I wrote in .Net I ended up only adding spaces on the left for Functions and both sides for Statements.

I seem to recall the Apple displays 10 PRINT SPC( 3) which I thought was quite ugly and distinguishing for functions meant I could get 10 PRINT SPC(3) which does look nicer.

So the original 6502 basic code from Microsoft (using the DEC cross assembler syntax) looks like this:

    LDYI    255     ;LOOK AT RES'D WORD LIST.
RESRCH: DEX         ;IS THIS THE RES'D WORD?
    BEQ PRIT3       ;YES, GO TOSS IT UP..
RESCR1: INY
    LDA RESLST,Y,   ;END OF ENTRY?
    BPL RESCR1      ;NO, CONTINUE PASSING.
    BMI RESRCH
PRIT3:  INY
    LDA RESLST,Y
    BMI PRIT4       ;END OF RESERVED WORD.
    JSR OUTDO       ;PRINT IT.
    BNE PRIT3       ;END OF ENTRY? NO, TYPE REST

But the AppleSoft version looks like this (I've tidied up some of the labels so it's a better match):

                    LDY #$FF
RESRCH              DEX                             ; SKIP KEYWORDS UNTIL REACH THIS ONE
                    BEQ PRIT3                       ; 
RESCR1              JSR GETCHR                      ; BUMP Y, GET CHAR FROM TABLE
                    BPL RESCR1                      ; NOT AT END OF KEYWORD YET
                    BMI RESRCH                      ; END OF KEYWORD, ALWAYS BRANCHES
PRIT3               LDA #LOCHAR(' ')                ; FOUND THE RIGHT KEYWORD
                    JSR OUTDO                       ; PRINT LEADING SPACE
L_LIST_4_4          JSR GETCHR                      ; PRINT THE KEYWORD
                    BMI L_LIST_4_5                  ; LAST CHAR OF KEYWORD
                    JSR OUTDO                       ; 
                    BNE L_LIST_4_4                  ; ...ALWAYS
L_LIST_4_5          JSR OUTDO                       ; PRINT LAST CHAR OF KEYWORD
                    LDA #LOCHAR(' ')                ; PRINT TRAILING SPACE
                    BNE LIST_1                      ; ...ALWAYS, BACK TO ACTUAL LINE

As can clearly be seen there are an extra couple of spaces printed. Also worth pointing out that mathematical operators like +-/* are tokens, I think <= double separates so comes out as < =. Finally there is some weirdness when things go beyond column 33 (I'd guess 80 column mode doesn't update MON_CH zero page so it doesn't trigger).

10

Here is a program I wrote several years ago in AppleSoft for the fun of it. What it simply does is disassemble itself from the tokenized storage into a listing. If you run it, it will look the same as if you did a LIST command.

In general, this gives you all the rules of AppleSoft formatting but in the form of a program :-)

0  REM  ** DISASSEMBLE MYSELF
1  REM  ** AUGUST 2012, BRIAN J. BERNSTEIN
9  REM  -- SETUP STORAGE AND MEMORY LOCATIONS
10  DIM T$(255): REM   TABLE OF TOKENS AND CHARACTERS
20 TS = 53456:TE = 53885: REM   TOKEN WORD TABLE START/END    
30 TP = 0: REM    LAST PRINTED A TOKEN; 0=NO, 1=YES 
39  REM   PS/PE IS PROGRAM START/END MEM LOCATION   
40 PS =  PEEK (103) + ( PEEK (104) * 256):PE =  PEEK (175) + ( PEEK (176) * 256)
50 PE = PE - 4: REM   IGNORE LAST 4 BYTES
59  REM   -- BUILD UP CHAR AND TOKEN TABLE                 
60  FOR X = 31 TO 127:T$(X) =  CHR$ (X): NEXT X
70 TN = 128
80  FOR X = TS TO TE
90 T$(TN) = T$(TN) +  CHR$ ( PEEK (X))
100  IF  PEEK (X) > 128 THEN TN = TN + 1
110  IF TN > 255 THEN 130
120  NEXT X
130 PC = PS: REM   SETUP PROGRAM COUNTER TO START 
200  REM   -- SETUP FOR NEW LINE
209  REM   LE IS MEMLOC FOR WHERE LINE ENDS, FOLLOWED BY PRINTING LINE NUMBER
210 LE =  PEEK (PC) + ( PEEK (PC + 1) * 256) - 1
220  PRINT  CHR$ (13);( PEEK (PC + 2) + ( PEEK (PC + 3) * 256));" ";
230 PC = PC + 4
300  REM   -- DISPLAY LINE
310  FOR X = PC TO LE
320  IF  PEEK (X) < 128 AND TP = 0 THEN  GOTO 340
330  PRINT " ";
340  PRINT T$( PEEK (X));
350 TP = 0: IF  PEEK (X) > 127 THEN TP = 1
360  NEXT X
370 PC = LE + 1
380  IF PC > PE THEN  END 
390  GOTO 200
  • Hey, that looks interesting. Maybe I'll try typing that into my Laser 128 this weekend. :-) – Geo... Jul 23 '18 at 21:20
  • What a cool surprise answer. – Wayne Conrad Jul 24 '18 at 17:14
5

10 input "some question?"; name$

Will be reformatted as:

10 INPUT "some question?" ; NAME$

That's not quite right (confirmed just now with an emulator). It will be reformatted as:

10  INPUT "some question?";NAME$

i.e. there are no spaces before and after the semicolon.

You can see what the formatting algorithm looks like in C++ in this formatter, which mimics Applesoft. Note in particular BufPrintf(" %s ", &gApplesoftTokens, i.e. each token is printed with a space before and after. Non-tokens are printed as-is with no spaces, and Applesoft doesn't store spaces outside of strings and REM statements.

@Peterl noted the oddness of SPC() formatting:

20  PRINT  SPC( 3)

It comes out like this because SPC( is a token, rather than SPC.

If you're curious about the tokenizing process, a text-to-Applesoft converter in C++ can be found here.

  • 2
    you are correct, I reformatted the original question with the exact spacing of my screenshot from the IIgs. I used real Apple II hardware so I transcribed the spacing wrong in the question. – Michael Shopsin Jul 23 '18 at 18:06
4

What are the rules for Applesoft BASIC formatting for code?

The most basic thein to know is that there is no source code in the sense of plain text files. Only the tokenized basic lines. When listing a programm. The tokens get converted back to their (uppercase) keywords with spaces being added after each.

This even adds some quirks when using screen editing mode (moving the cursor via ESC sequences and copying parts of the screen into the input buffer).

  • You can EXEC a properly-formatted text file of AppleSoft BASIC source, and it will import as a program, though. – scruss Jul 24 '18 at 15:52
  • @scruss Or just type it, which is about the same ;) – Raffzahn Jul 24 '18 at 21:04

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.