*-------------------------------
*
* compiler.s
*
* This program is the macro compiler.  It just
* goes through the code, compiling as it goes!
*
* SLJ begun: 11/17/96
*     finished:
*
* Last updated: 6/98
*     - New commands: VAR, DEF, MUL, DIV
*     - Allow indexed index (e.g. FREQ,L1)
*
* 9/98
*     - Compiler is now 2-pass, to efficiently handle
*       WAIT in subroutines.  Variables no longer
*       need to be defined before being used.
*

* ORG $8000
         REL
         DSK '@0:compiler'

         PUT "diskio.ext.s"
         PUT "playvars.k.s" ;Player variables
                          ;(global/local variables, etc.)

VARPNT   = $57            ;Pointer to variable list
VAREND   = $59            ;Pointer to end of variables

TEXTP    EQU $61          ;Pointer into the code text
CODEP    EQU $63          ;Pointer to code location
TEMP     EQU $65          ;Temporary location/pointer
*ERROR EQU $FB
STACK    EQU $CE00        ;My stack...
QUEUE    EQU $CF00        ;...and my queueueu

LOOPFLAG EQU 01           ;Is this a loop address?
WHENFLAG EQU 2
ELSEFLAG EQU 3
LONGFLAG EQU 4

* KERNAL
CHROUT   EQU $FFD2

COMPILER ENT              ;Entry point
         JSR STROUT
         TXT 'Compiling...'
         DFB 191,00

         LDA #00
         STA ENDVTAB      ;Reset variable list
         JSR ENTRY        ;Set entry point to CODEP

         LDA #<ENDVTAB    ;End of variable table
         STA VAREND
         LDA #>ENDVTAB
         STA VAREND+1

* LDA VARTAB ;Initialize variable pointers
* STA VARPNT
* LDA VARTAB+1
* STA VARPNT+1

         LDA #$FF
         STA NEWQ         ;Begins at top of stack
         STA NEWQTOP
         STA PASS         ;Will INC, below
         LDX #3           ;Save TEXTP and CODEP
:L       LDA TEXTP,X
         STA TEXTTMP,X
         DEX
         BPL :L

* LDA #$20 ;Source at $2000
* STA POINT+1
* LDA #$C2 ;Code at $C200
* STA CODEP+1

*
* Pass 1 and 2 loop
*
PASSLOOP 
         INC PASS         ;0 or 1
         LDX #3           ;Restore TEXTP and CODEP
:L       LDA TEXTTMP,X
         STA TEXTP,X
         DEX
         BPL :L
         LDA #00
         STA CURLINE
         STA STACKP
         STA QFRONT
         STA QBACK
         STA QSIZE
         STA LASTXI       ;Last LDX instruction.
         STA LASTXLO
         STA LASTXHI
         STA TESTFLAG     ;Flag for TEST command
         STA SUBFLAG      ;Subroutine flag
         STA ENTFLAG      ;ENTRY flag


*
* Main program loop
*

MAIN     LDY #00
         STY ERROR
* LDA (TEXTP),Y
* BEQ :ATEOF
         JSR LINE2BUF     ;Copy line to line buffer
         LDY #00
         STY CURPOS
         LDX ERROR
         BNE :ERR
:RDCHAR  JSR NEXTCHAR     ;Find next relevant char
         BEQ :ATEOF       ;Z set means end of file
         BCC :ADVANCE     ;Blank line, spare chars, etc.
         JSR PARSE        ;Parse the next command
         LDX ERROR
         BNE :ERR         ;ERROR=0 means all is fine
:ADVANCE LDA TEXTLEN      ;Advance pointer
         CLC
         ADC TEXTP
         STA TEXTP
         BCC :CHECK
         INC TEXTP+1
:CHECK   LDA LASTCHAR     ;At the end of the line?
         BNE MAIN
:ATEOF   
         JSR STROUT
         DFB 157,18,191,146,00 ;left, rvs, etc.
         LDA PASS
         BEQ PASSLOOP

         JSR STROUT
         DFB 13,13,00
         LDA STACKP       ;Anything still on the stack?
         BNE :D1          ;Oops, print error.
         LDA SUBFLAG      ;Any unclosed subroutines?
         BNE :D2
         LDA ENTFLAG      ;ENTRY set?
         BNE :DONE
         LDX #52
         JSR :PRERR       ;Print a warning
:DONE    JSR STROUT
         TXT 'Compilation successful!                 '
         DFB 13,13,00
         RTS

:D1      LDX #30
         DFB $2C
:D2      LDX #50

:ERR     
         LDA #13
         JSR CHROUT
         JSR CHROUT
         STY TEMP         ;Print out the line where the
         LDY #00          ;error occured
:L10     CPY TEMP
         BEQ :LOUT
         LDA LINEBUF,Y
         JSR CHROUT
         CMP #13          ;It is possible to move through
         BEQ :PRERR       ;the entire line.
         INY
         BNE :L10
:LOUT    LDA #18          ;Reverse on
         JSR CHROUT
         LDA LINEBUF,Y    ;The exact spot of the error
:L11     JSR CHROUT
         INY
         LDA LINEBUF,Y
         BEQ :C2          ;Can be at EOF
         CMP #13
         BNE :L11
:C2      JSR :CR          ;Clear line
* LDA #32
*:L12 JSR CHROUT
* INY
* CPY #40
* BCC :L12
*:C2 LDA #13
* JSR CHROUT
* LDA #146 ;Rvsoff
* JSR CHROUT

:PRERR   
         LDA ERRLIST,X    ;Find the address of the error
         STA TEMP         ;text.
         INX
         LDA ERRLIST,X
         STA TEMP+1
         LDY #00
:LOOP    LDA (TEMP),Y
         BEQ :CR
         JSR CHROUT
         INY
         BNE :LOOP
:CR      LDA #32
:C1      JSR CHROUT       ;Clear to end of line
         INY
         CPY #40
         BCC :C1
         LDA #13
         JMP CHROUT       ;And exit

ERROR    ENT              ;Enrty point
         DFB 00           ;This will contain error number
CURLINE  ENT
         DFB 00           ;This is the current line compiler
                          ;is processing.
PASS     DFB 00           ;2-pass compiler now
TEXTTMP  DA 00            ;Temp textp
CODETMP  DA 00            ;Temporary storage for CODEP

         PUT "computil.jj.s" ;Compiler utilities --
                          ;parser routines, stack/queue,
                          ;readarg, etc.

         PUT "operators.g.s" ;Operator commands
                          ;ADD MOV AND etc.

         PUT "misc.v.s"   ;Miscellaneous commands
                          ;INC/DEC, EMBED, etc.

         PUT "keywords.o.s" ;Must be last file
