home *** CD-ROM | disk | FTP | other *** search
/ Transactor / Transactor_16_1987_Transactor_Publishing.d64 / struct.src < prev    next >
Text File  |  2023-02-26  |  12KB  |  567 lines

  1. ;
  2. ;structured programming (parser)
  3. ;by frank e. digioia
  4. ;11/12/85
  5. ;
  6.  * = $c000    ;convenient start
  7. ;
  8. chrget = $0073  ;get byte of text
  9. chrgot = $0079  ;get same byte
  10. igone  = $0308  ;evaluation vector
  11. ;
  12. init = *       ;initialize routine
  13.  lda #<struct
  14.  sta igone
  15.  lda #>struct
  16.  sta igone+1
  17.  lda #<note
  18.  ldy #>note
  19.  jmp $ab1e
  20. ;
  21. note .byte '> structured commands'
  22. .byte ' enabled.',$0d,$00
  23. ;
  24. struct = *
  25.  jsr chrget ;get a byte of text
  26.  jsr chkout ;structured command?
  27.  jmp $a7ae  ;intepreter loop
  28. ;
  29. rem jmp $a93b ;rem command
  30. ;
  31. newrun jsr kill ;kill edit mode
  32.  jmp basic      ;give to basic
  33. ;
  34. chkout cmp #$27 ;single quote?
  35.  beq rem       ;classy rem
  36.  cmp #$8b    ;can't have new cmds
  37.  bne *+5     ;without a new if
  38.  jmp if
  39.  cmp #$8a    ;'run' token
  40.  beq newrun  ;end edit and run
  41.  tax         ;set flags
  42.  bmi basic   ;token/give to basic
  43. ;
  44.  ldy #$0a   ;check on 'wend'
  45.  sty count  ;point to 'wend'
  46.  cmp #'w'   ;current char = 'w'?
  47.  bne setup  ;no/not wend
  48.  ldy #$01   ;yes/check next char
  49.  lda ($7a),y ;next byte of text
  50.  cmp #$80    ;'end'?
  51.  beq exec    ;yes/execute wend
  52. ;
  53. setup lda #$00 ;clear all regs
  54.  sta count  ;and keyword counter
  55.  tax
  56.  tay
  57.  dey        ;pre-loop decrement
  58. ;
  59. loop iny      ;incr text index
  60.  lda table,x  ;get table byte
  61.  beq basic    ;end of table
  62.  inx          ;incr table pointer
  63.  cmp ($7a),y  ;cmpare with text
  64.  bne next     ;find next word
  65.  beq loop     ;match/keep looking
  66. ;
  67. next dex      ;bump .x down once
  68.  lda table,x  ;end of table word?
  69.  bpl find     ;no/find end of word
  70.  and #$7f     ;yes/mask flag
  71.  cmp ($7a),y  ;is it a match?
  72.  beq exec     ;hooray!!!
  73.  bne x1       ;go back for more
  74. ;
  75. find inx      ;find end of word
  76.  lda table,x  ;look for negative
  77.  beq basic    ;end of table
  78.  bpl find     ;keep looking
  79. ;
  80. x1 inx        ;point to next word
  81.  inc count    ;word # in table
  82.  ldy #$ff     ;reset text index
  83.  jmp loop     ;search some more
  84. ;
  85. exec = *      ;execution routine
  86.  tya          ;update text pointer
  87.  clc
  88.  adc $7a
  89.  sta $7a
  90.  bcc *+4
  91.  inc $7b
  92. ;
  93.  lda count    ;get offset in table
  94.  asl a        ;multiply by two
  95.  tax          ;use as index
  96.  lda adrtab+1,x ;hi byte adr
  97.  pha          ;as return adr hi
  98.  lda adrtab,x ;lo byte adr
  99.  pha          ;as return adr lo
  100.  jmp chrget   ;execute routine
  101. ;
  102. basic jsr chrgot ;reset flags
  103.  jmp $a7ed     ;give it to basic
  104. ;
  105. count .byte $00
  106. ;
  107. table .byte 'repea',$d4,'unti',$cc
  108. .byte 'whil',$c5,'exi',$d4,'cal'
  109. .byte $cc,'pro',$c3,'els',$c5
  110. .byte 'edi',$d4,'kil',$cc,'basic'
  111. .byte $b2,$00
  112. ;
  113. adrtab .word repeat-1,until-1
  114. .word while-1,exit-1,call-1
  115. .word xproc-1,else-1,edit-1,kill-1
  116. .word basic2-1,wend-1
  117. ;
  118. ;edit mode commands
  119. ;
  120. edit lda #$ff  ;ignore pi symbol
  121.  sta $81       ;alter chrget
  122.  rts           ;that's it!
  123. ;
  124. kill lda #$20  ;ignore spaces
  125.  sta $81       ;fix chrget
  126.  rts
  127. ;
  128. basic2 lda #$e4 ;fix igone vector
  129.  sta igone
  130.  lda #$a7
  131.  sta igone+1
  132.  lda #<note2    ;notify user
  133.  ldy #>note2
  134.  jmp $ab1e
  135.  rts
  136. note2 .byte '> cmds disabled',$00
  137. ;
  138. ;structured programming module
  139. ;by frank e. digioia
  140. ;11/23/85
  141. ;
  142. ;tokens for lookups & cmp's
  143. ;
  144. whltok = $eb
  145. wndtok = $ec
  146. reptok = $e7
  147. gosubs = $8d
  148. for    = $81
  149. proc   = $e5
  150. ;
  151. stack  = $0100  ;6510 stack area
  152. frmevl = $ad9e  ;evaluate formula
  153. getptr = $a38a  ;pntr to stack id
  154. chkstk = $a3fb  ;check stack space
  155. ;
  156. if = *
  157.  jsr chrget ;get next byte
  158.  jsr $ad9e  ;evaluate expression
  159.  jsr $0079  ;get last char
  160.  cmp #$89   ;"goto" token?
  161.  beq chkexp ;yeah/check result
  162.  lda #$a7   ;"then" token
  163.  jsr $aeff  ;check on "then"
  164. chkexp lda $61 ;expression true?
  165.  bne doit   ;yes/execute cmd
  166.  jsr fndels ;no/look for "else"
  167.  tax        ;eoln?
  168.  bne cmmd   ;no/do else clause
  169.  rts        ;yes/return to interp
  170. ;
  171. doit jsr chrgot ;get last char
  172.  bcs decptr ;not digit/execute it
  173.  jmp $a8a0  ;digit/execute goto
  174. ;
  175. decptr lda $7a ;decrement txtptr
  176.  sec
  177.  sbc #$01
  178.  sta $7a
  179.  bcs *+4
  180.  dec $7b
  181.  ldy #$00   ;clear .y for update
  182. ;
  183. cmmd pla     ;clear return address
  184.  pla
  185.  jmp ($0308) ;execute via vector
  186. ;
  187. fndels jsr $a906   ;find next stmt
  188.  pha               ;save byte
  189.  jsr $a8fb         ;update txtptr
  190.  pla               ;get byte back
  191.  beq noelse        ;end of line?
  192.  ldx #$03          ;compare 4 byte
  193. chkels jsr chrget  ;get a byte
  194.  cmp esle,x        ;comare bkwrd
  195.  bne fndels        ;no/next stmt
  196.  dex               ;bump index
  197.  bpl chkels        ;keep checking
  198. noelse rts
  199. ;
  200. esle .byte 'esle'
  201. ;
  202. else jmp $a93b  ;do a rem
  203. ;
  204. repeat = *
  205.  lda #$03  ;need 6 bytes
  206.  jsr chkstk ;check stack space
  207.  jsr $a8f8 ;point next st'ment
  208.  lda $7b   ;save text pointer
  209.  pha
  210.  lda $7a
  211.  pha
  212.  lda $3a   ;save line number
  213.  pha
  214.  lda $39
  215.  pha
  216.  lda #reptok
  217.  pha
  218.  jmp $a7ae ;interpreter loop
  219. ;
  220. until = *
  221.  jsr getptr  ;find id on stack
  222.  txs         ;replace pointer
  223.  cmp #reptok ;repeat id?
  224.  bne uerr1   ;'missing repeat'
  225.  jsr chrgot  ;condition present?
  226.  beq nocond  ;'missing cond.'
  227.  jsr frmevl  ;evaluate expression
  228.  tsx         ;get stack pointer
  229.  txa         ;place in .a
  230.  clc
  231.  adc #$05    ;backup 5 on stack
  232.  tax
  233.  tay
  234. ;
  235.  lda $61    ;check result (t/f)
  236.  bne utrue  ;true/fix stack
  237. ;
  238.  ldx #01    ;false/copy data from
  239. getdat dey  ;stack into program
  240.  lda stack+1,y ;pointer & curlin
  241.  sta $7a,x  ;to continue execution
  242.  lda stack-1,y ;at top of loop.
  243.  sta $39,x
  244.  dex
  245.  bpl getdat
  246.  jmp $a7ae  ;interpreter loop
  247. ;
  248. utrue txs  ;update stack pointer
  249.  rts
  250. ;
  251. uerr1 lda #$00
  252.  .byte $2c
  253. werr1 lda #$01
  254.  .byte $2c
  255. werr2 lda #$02
  256.  .byte $2c
  257. nocond lda #$03
  258.   jmp error      ;print error msg
  259. ;
  260. while = *
  261.  jsr chrgot ;condition present?
  262.  beq nocond ;no/error mesg
  263.  lda #$03   ;need 6 bytes
  264.  jsr chkstk ;check stack space
  265.  lda $7a    ;save pointer to
  266.  sta t1     ;the conditional
  267.  lda $7b    ;expression for
  268.  sta t2     ;later use.
  269.  jsr frmevl ;evaluate expression
  270.  lda $61    ;true or false?
  271.  bne wtrue  ;true/load up stack
  272.  jmp fndwnd ;false/find wend
  273. ;
  274. wtrue lda t2 ;save pointer to
  275.  pha         ;the logical
  276.  lda t1      ;expression on
  277.  pha         ;stack
  278.  lda $3a     ;save line number
  279.  pha         ;on stack
  280.  lda $39
  281.  pha
  282.  lda #whltok ;save id for while
  283.  pha         ;on stack
  284.  jmp $a7ae
  285. ;
  286. wend jsr getptr ;find id on stack
  287.  txs         ;update pointer
  288.  cmp #whltok ;id for while?
  289.  bne werr1   ;'missing while'
  290.  jsr chrgot  ;end of statement?
  291.  bne werr2   ;no/something wrong
  292. ;
  293.  lda $7b     ;save text pointer
  294.  sta t2
  295.  lda $7a
  296.  sta t1
  297.  lda $3a
  298.  sta ll2
  299.  lda $39
  300.  sta ll1
  301. ;
  302.  tsx         ;get stack pointer
  303.  txa         ;place in .a
  304.  clc
  305.  adc #$05    ;back up 5 on stack
  306.  tax
  307.  stx stkptr  ;store stack pointer
  308.  tay
  309. ;
  310.  ldx #$01    ;get adr of while
  311. whldat dey   ;condition into
  312.  lda stack+1,y ;$7a/$7b and line
  313.  sta $7a,x   ;number into $39/$3a
  314.  lda stack-1,y ;for frmevl to use
  315.  sta $39,x
  316.  dex
  317.  bpl whldat
  318. ;
  319.  jsr frmevl  ;evaluate expression
  320.  lda $61     ;true or false?
  321.  beq wfalse
  322.  jmp $a7ae   ;true/cont execution
  323. ;
  324. wfalse ldx stkptr
  325.  txs        ;update stack pointer
  326.  ldx #$01
  327. wfill lda t1,x ;replace text pntr
  328.  sta $7a,x
  329.  lda ll1,x   ;replace line number
  330.  sta $39,x
  331.  dex
  332.  bpl wfill
  333.  rts        ;continue execution
  334. ;
  335. fndwnd = *  ;find wend statement
  336.  lda #$00
  337.  pha        ;set flag on stack
  338. wsrch jsr $a8f8 ;find next stment
  339.  jsr chrgot ;end of line?
  340.  tax
  341.  beq eoln1  ;yes/deal with it
  342. xx jsr chrget ;get next byte
  343.  tax        ;end of line?
  344.  beq eoln1  ;yes/deal with it
  345.  jsr chkwnd ;cmp #wndtok
  346.  beq xwend
  347.  jsr chkwhl ;cmp #whltok
  348.  beq xwhile
  349.  bne wsrch
  350. ;
  351. eoln1 ldy #$02 ;check for end text
  352.  lda ($7a),y  ;link hi = 0?
  353.  bne *+5      ;no/continue search
  354.  jmp werr2    ;yes/missing wend
  355.  iny          ;no/get line#
  356.  lda ($7a),y  ;save line #
  357.  sta ll1
  358.  iny
  359.  lda ($7a),y
  360.  sta ll2
  361.  jsr $a8fb   ;update text pointer
  362.  jmp xx      ;do search
  363. ;
  364. xwend pla    ;check flag
  365.  beq wndfnd  ;found it!!!
  366.  jmp wsrch
  367. ;
  368. xwhile lda #whltok
  369.  pha
  370.  jmp wsrch
  371. ;
  372. wndfnd lda ll1 ;load line #
  373.  sta $39
  374.  lda ll2
  375.  sta $3a
  376.  jmp $a8f8   ;find next statement
  377. ;
  378. stkptr .byte $00
  379. incrst .byte $00
  380. t1 .byte $00
  381. t2 .byte $00
  382. ll1 .byte $00
  383. ll2 .byte $00
  384. ;
  385. exit = *
  386.  pla        ;find id on stack
  387.  pla
  388.  pla
  389.  cmp #for   ;for command?
  390.  beq getinc ;get # of bytes
  391.  cmp #gosubs ;gosub command?
  392.  beq getinc+3
  393.  cmp #reptok
  394.  beq getinc+3
  395.  cmp #whltok
  396.  beq getinc+3
  397.  lda #$04   ;error number 4
  398.  jmp error  ;'nothing to exit'
  399. ;
  400. getinc lda #$13 ;19 bytes on stack
  401.  .byte $2c     ;skip next instr.
  402.  lda #$06      ;6 bytes on stack
  403.  sta incrst    ;incr for stkptr
  404.  tsx           ;get stack pointer
  405.  txa           ;put in .a for add
  406.  clc
  407.  adc incrst    ;increase stkptr
  408.  tax           ;replace it
  409.  txs           ;stack clean!
  410.  jsr chrgot    ;get last char.
  411.  jsr $a8a0     ;goto command
  412.  jmp $a7ae     ;interpreter loop
  413. ;
  414. call = *
  415.  lda #$03     ;need 6 bytes
  416.  jsr chkstk   ;check stack space
  417.  lda $7b      ;save text pointer
  418.  pha
  419.  lda $7a
  420.  pha
  421.  lda $3a      ;save line number
  422.  pha
  423.  lda $39
  424.  pha
  425.  lda #$8d     ;id for gosub
  426.  pha
  427. ;
  428.  jsr fndprc   ;find procedure adr
  429.  ldx #$01     ;use .x as index
  430. z lda $fb,x
  431.  sta $7a,x    ;update text pointer
  432.  lda $61,x
  433.  sta $39,x    ;update line number
  434.  dex
  435.  bpl z
  436. ;
  437.  jsr $a8f8    ;find next command
  438.  jmp $a7ae    ;to interpreter loop
  439. ;
  440. fndprc = *    ;find procedure
  441.  lda $2b      ;start of basic
  442.  sta $fd      ;as pointer
  443.  lda $2c
  444.  sta $fe
  445. ;
  446. srchlp lda $fd ;update link pntr
  447.  sta $fb
  448.  lda $fe
  449.  sta $fc
  450. ;
  451.  ldy #$01     ;use .y as index
  452.  lda ($fb),y  ;hi byte next line
  453. ;
  454.  bne *+7      ;end of text?
  455.  lda #$05     ;yes/error number 5
  456.  jmp error    ;'proc not found'
  457. ;
  458.  sta $fe      ;save next adr hi
  459.  dey          ;bump pointer
  460.  lda ($fb),y  ;get next adr lo
  461.  sta $fd      ;save it
  462. ;
  463.  ldy #$04     ;point to 1st byte
  464.  lda ($fb),y  ;get the byte
  465.  jsr chkprc   ;cmp #proc
  466.  bne srchlp   ;no/try next line
  467. ;
  468.  ldy #$03     ;yes/get line #
  469.  lda ($fb),y  ;get hi byte
  470.  sta $62      ;save it
  471.  dey
  472.  lda ($fb),y  ;get lo byte
  473.  sta $61      ;save it
  474. ;
  475.  ldy #$07     ;ldy #$04
  476. xspc iny      ;skip leading spaces
  477.  lda ($fb),y  ;get byte of name
  478.  cmp #' '     ;space?
  479.  beq xspc
  480. ;
  481.  tya          ;get offset in .a
  482.  clc
  483.  adc $fb      ;update our txtptr
  484.  sta $fb      ;to first byte of
  485.  bcc *+4      ;procedure name
  486.  inc $fc
  487. ;
  488.  ldy #$ff     ;set .y = -1
  489. compar iny    ;update index
  490. chktxt lda ($7a),y ;byte of name
  491.  beq chklst   ;end of exec name
  492.  cmp #':'     ;end of exec name?
  493.  beq chklst   ;check end procname
  494.  cmp #' '     ;space?
  495.  bne chknam   ;no/check proc name
  496.  inc $7a      ;forget spaces
  497.  bne *+4
  498.  inc $7b
  499.  jmp chktxt
  500. ;
  501. chknam cmp ($fb),y ;cmp proc name
  502.  beq compar   ;match/keep checking
  503.  jmp srchlp   ;no/find next proc
  504. ;
  505. chklst lda ($fb),y ;end procname?
  506.  beq *+6
  507.  cmp #':'
  508.  bne srchlp
  509.  rts
  510. ;
  511. xproc lda #$06  ;error number 6
  512.  jmp error
  513. ;
  514. ;this routine may be omitted if
  515. ;tokens are used (see article).
  516. ;
  517. chkwnd ldx #$04  ;offset for wend
  518.  .byte $2c    ;skip next instr
  519. chkwhl ldx #$07  ;offset to while
  520.  ldy $7a       ;copy text pointer
  521.  sty $fb       ;to $fb/$fc
  522.  ldy $7b
  523.  sty $fc
  524.  ldy #$ff      ;pre-loop index
  525.  bne chkx      ;do the check
  526. ;
  527. chkprc ldx #$ff ;offset for proc
  528.  ldy #$03      ;pre-loop
  529. ;
  530. chkx iny       ;compare loop
  531.  inx           ;bump pointer
  532.  lda name,x    ;get byte of name
  533.  beq xit       ;end of name?
  534.  cmp ($fb),y   ;compare to text
  535.  beq chkx      ;match, keep on
  536. xit rts
  537. ;
  538. name .byte 'proc',$00,'w',$80,$00
  539.  .byte 'while',$00
  540. ;
  541. ;error processor -- prints error
  542. ;messages and passes control to 
  543. ;rom error routines
  544. ;
  545. ;frank e. digioia
  546. ;12/17/85
  547. ;
  548. error asl a   ;mult err# by 2
  549.  tax          ;use as index
  550.  lda errmsg,x ;get mesg address
  551.  sta $22
  552.  lda errmsg+1,x
  553.  jmp $a445    ;process error
  554. ;
  555. errmsg .word u1msg,w1msg,w2msg
  556. .word ncmsg,nemsg,npmsg,nocall
  557. ;
  558. u1msg .byte 'until without repea',$d4
  559. w1msg .byte 'wend without whil',$c5
  560. w2msg .byte 'while without wen',$c4
  561. ncmsg .byte 'missing logical expressio',$ce
  562. nemsg .byte 'no structure to exi',$d4
  563. npmsg .byte 'procedure not foun',$c4
  564. nocall .byte 'proc without cal',$cc
  565. ;
  566. .end
  567.