1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
|
; Altirra BASIC - LIST module
; Copyright (C) 2014 Avery Lee, All Rights Reserved.
;
; Copying and distribution of this file, with or without modification,
; are permitted in any medium without royalty provided the copyright
; notice and this notice are preserved. This file is offered as-is,
; without any warranty.
?list_start = *
;==========================================================================
; LIST [filespec,] [lineno [,lineno]]
;
; If filespec is specified, IOCB #7 is used to send to that output.
;
; If one lineno is specified, only that line is listed. If two linenos
; are specified, lines within that range inclusive are listed. If the range
; is inverted, no lines are listed.
;
; Errors:
; Error 3 if lineno not in [0,65535]
; Error 7 if lineno in [32768, 65535]
;
; Unusual as it may be, it is perfectly OK to have a LIST statement inside
; of a running program. Therefore, we have to be careful not to disturb
; running execution state. We can, however, take over the argument stack
; area as well as the parser pointers.
;
; Another quirk in Atari BASIC is that if LIST is executed in deferred
; mode and Break is pressed, execution continues with the next statement
; instead of a stop occurring. We don't emulate this behavior right now.
;
.proc stList
_endline = stScratch2 ;and stScratch3
_eos = stScratch4
;init start and end lines
lda #$ff
sta _endline
lsr
sta _endline+1
lda #0
sta parptr
sta parptr+1
;assume IOCB #0
sta iocbidx
;evaluate it
jsr evaluate
;check if there is an argument
ldy argsp
beq no_lineno
;test if it is a filespec
lda expType
bpl not_filespec
;it's a filespec -- set and close IOCB #7
jsr IoSetupIOCB7
;open IOCB for write
lda #$08
jsr IoDoOpenWithFilename
;do listing
jsr do_list
;close IOCB and exit
jmp IoClose
do_list:
;check if we have more arguments
jsr ExecTestEnd
beq no_lineno
;parse first line number after filename
jsr ExprSkipCommaAndEval
not_filespec:
jsr ExprConvFR0IntPos
stx parptr
sta parptr+1
;check for a second line number
jsr ExecTestEnd
beq no_lineno2
jsr ExprSkipCommaAndEvalPopIntPos
no_lineno2:
stx _endline
mva fr0+1 _endline+1
no_lineno:
;init first statement
ldx parptr
lda parptr+1
jsr exFindLineInt
sta parptr
sty parptr+1
;turn on LIST mode display
sec
ror dspflg
lineloop:
;check that we haven't hit the end line; we'll always eventually
;hit the immediate mode line
ldy #0
clc
lda (parptr),y
sta fr0
sbc _endline
iny
lda (parptr),y
sta fr0+1
sbc _endline+1
bcc not_done
;turn off LIST mode display
asl dspflg
;we're done
rts
not_done:
;convert line number; this will also set INBUFF = LBUFF
jsr IoPrintInt
;add a space
jsr IoPutSpace
;begin processing statements
ldy #3
statement_loop:
;read and cache the end of statement
mva (parptr),y+ _eos
;read next token
lda (parptr),y+
sty parout
;skip directly to function tokens if it's an implicit LET
cmp #TOK_ILET
beq do_function_tokens_loop
;must special case syntax errors as the string isn't in the table
;(otherwise it could be parsed)
cmp #TOK_SXERROR
beq syntax_error
;lookup and print statement name
pha
ldy #<statement_table
ldx #>statement_table
jsr ListPrintToken
;print space
jsr IoPutSpace
;check if we just printed REM, DATA or ERROR -- we must switch
;to raw printing after this
pla
lsr ;check for TOK_REM ($00) or TOK_DATA ($01)
beq print_raw
;process function tokens
do_function_tokens_loop:
;fetch next function token
jsr ListGetByte
cmp #TOK_EOL
beq do_function_tokens_done
jsr ListPrintFunctionToken
ldy parout
;IF statements will abruptly stop after the THEN, so we must
;catch that case
cpy _eos
bne do_function_tokens_loop
do_function_tokens_done:
statement_done:
ldy #2
lda (parptr),y
cmp _eos
beq line_done
;next statement
ldy _eos
bne statement_loop
line_done:
;advance to next line
ldx #parptr
jsr VarAdvancePtrX
;add a newline
jsr IoPutNewline
;next line
bpl lineloop ;!! - unconditional
syntax_error:
ldx #msg_error2-msg_base
jsr IoPrintMessage
print_raw:
jsr ListGetByte
cmp #$9b
beq statement_done
jsr putchar
bpl print_raw ;!! - unconditional
print_const_number:
pha
ldx #$fa
print_const_number_1:
jsr ListGetByte
sta fr0+6,x
inx
bne print_const_number_1
pla
;check if we are doing hex or not
cmp #TOK_EXP_CHEX
beq print_const_hex_number
jmp IoPrintNumber
print_const_hex_number:
lda #'$'
jsr putchar
sec
jsr IoConvNumToHex
ora #$80
dey
sta (inbuff),y
jmp printStringINBUFF
print_const_string:
;print starting quote
jsr print_const_string_2
;get length
jsr ListGetByte
sta fr0
beq print_const_string_2
print_const_string_1:
jsr ListGetByte
jsr putchar
dec fr0
bne print_const_string_1
print_const_string_2:
lda #'"'
jmp putchar
print_var:
and #$7f
ldy vntp
ldx vntp+1
jsr ListPrintToken
;check if we got an array var -- if so, we need to skip the open
;parens token that's coming
cmp #'('+$80
sne:inc parout
rts
.endp
;==========================================================================
; ListPrintFunctionToken
;
;--------------------------------------------------------------------------
; ListPrintToken
;
; Entry:
; A = token index
; X:Y = table start
;
; Exit:
; A = last character in token
;
; Modified:
; iterPtr
;
.proc ListPrintFunctionToken
tax
bmi stList.print_var
cmp #TOK_EXP_CSTR
bcc stList.print_const_number
beq stList.print_const_string
sbc #$12 ;!! - carry is set
ldy #<funtok_name_table_base
ldx #>funtok_name_table_base
.def :ListPrintToken
sta stScratch
sty iterPtr
stx iterPtr+1
tya
jmp print_var_entry
print_var_loop:
jsr VarAdvanceName
print_var_entry:
dec stScratch
bpl print_var_loop
print_var_done:
ldy iterPtr+1
jsr IoSetInbuffYA
jmp printStringINBUFF
.endp
;==========================================================================
.proc ListGetByte
ldy parout
inc parout
lda (parptr),y
rts
.endp
;==========================================================================
funtok_name_table_base:
;$12
dta c','+$80
dta c'$'+$80
dta c':'+$80
dta c';'+$80
dta c'?'+$80
dta c' GOTO',c' '+$80
dta c' GOSUB',c' '+$80
dta c' TO',c' '+$80
dta c' STEP',c' '+$80
dta c' THEN',c' '+$80
dta c'#'+$80
dta c'<',c'='+$80
dta c'<',c'>'+$80
dta c'>',c'='+$80
;$20
dta c'<'+$80
dta c'>'+$80
dta c'='+$80
dta c'^'+$80
dta c'*'+$80
dta c'+'+$80
dta c'-'+$80
dta c'/'+$80
dta c' NOT',c' '+$80
dta c' OR',c' '+$80
dta c' AND',c' '+$80
dta c'('+$80
dta c')'+$80
dta c'='+$80
dta c'='+$80
dta c'<',c'='+$80
;$30
dta c'<',c'>'+$80
dta c'>',c'='+$80
dta c'<'+$80
dta c'>'+$80
dta c'='+$80
dta c'+'+$80
dta c'-'+$80
dta c'('+$80
dta c'('+$80
dta c'('+$80
dta c'('+$80
dta c'('+$80
dta c','+$80
funtok_name_table:
;$3D
dta c'STR',c'$'+$80
dta c'CHR',c'$'+$80
dta c'US',c'R'+$80
;$40
dta c'AS',c'C'+$80
dta c'VA',c'L'+$80
dta c'LE',c'N'+$80
dta c'AD',c'R'+$80
dta c'AT',c'N'+$80
dta c'CO',c'S'+$80
dta c'PEE',c'K'+$80
dta c'SI',c'N'+$80
dta c'RN',c'D'+$80
dta c'FR',c'E'+$80
dta c'EX',c'P'+$80
dta c'LO',c'G'+$80
dta c'CLO',c'G'+$80
dta c'SQ',c'R'+$80
dta c'SG',c'N'+$80
dta c'AB',c'S'+$80
;$50
dta c'IN',c'T'+$80
dta c'PADDL',c'E'+$80
dta c'STIC',c'K'+$80
dta c'PTRI',c'G'+$80
dta c'STRI',c'G'+$80
dta $81
dta '%'+$80
dta '!'+$80
dta '&'+$80
dta $81
dta 'BUMP','('*
dta $81
dta c'HEX',c'$'+$80
dta $81
dta c'DPEE',c'K'+$80
dta $81
;$60
dta 'VSTIC','K'+$80
dta 'HSTIC','K'+$80
dta 'PMAD','R'*
dta 'ER','R'+$80
dta 0
_STATIC_ASSERT *-funtok_name_table<254, "Function token name table is too long."
.echo "-- Function token table length: ", *-funtok_name_table
.echo "- List module length: ",*-?list_start
|