Class: Ti99BasicFile

Inherits:
Ti99File show all
Defined in:
lib/native_file_types/ti99/Ti99BasicFile.rb

Overview

Constant Summary collapse

TI_BASIC_TOKENS =
{
#0x00 => end of line marker
0x81=>'ELSE',
0x82=>'::',
0x83=>'!',
0x84=>'IF',
0x85=>'GO',
0x86=>'GOTO',
0x87=>'GOSUB',
0x88=>'RETURN',
0x89=>'DEF',
0x8A=>'DIM',
0x8B=>'END',
0x8C=>'FOR',
0x8D=>'LET',
0x8E=>'BREAK',
0x8F=>'UNBREAK',
0x90=>'TRACE',
0x91=>'UNTRACE',
0x92=>'INPUT',
0x93=>'DATA',
0x94=>'RESTORE',
0x95=>'RANDOMIZE',
0x96=>'NEXT',
0x97=>'READ',
0x98=>'STOP',
0x99=>'DELETE',
0x9A=>'REM',
0x9B=>'ON',
0x9C=>'PRINT',
0x9D=>'CALL',
0x9E=>'OPTION',
0x9F=>'OPEN',
0xA0=>'CLOSE',
0xA1=>'SUB',
0xA2=>'DISPLAY',
0xA3=>'IMAGE',
0xA4=>'ACCEPT',
0xA5=>'ERROR',
0xA6=>'WARNING',
0xA7=>'SUBEXIT',
0xA8=>'SUBEND',
0xA9=>'RUN',
0xAA=>'LINPUT',
0xB0=>'THEN',
0xB1=>'TO',
0xB2=>'STEP',
0xB3=>',',
0xB4=>';',
0xB5=>':',
0xB6=>')',
0xB7=>'(',
0xB8=>'&',
0xBA=>'OR',
0xBB=>'AND',
0xBC=>'XOR',
0xBD=>'NOT',
0xBE=>'=',
0xBF=>'<',
0xC0=>'>',
0xC1=>'+',
0xC2=>'-',
0xC3=>'*',
0xC4=>'/',
0xC5=>'^',
#0xC7=> QUOTED STRING (following byte indicates length of string)
#0xC8=>UNQUOTED STRING (following byte indicates length of string)
#0xC9=>LINE NUMBER (16 bit word)
0xCA=>'EOF',
0xCB=>'ABS',
0xCC=>'ATN',
0xCD=>'COS',
0xCE=>'EXP',
0xCF=>'INT',
0xD0=>'LOG',
0xD1=>'SGN',
0xD2=>'SIN',
0xD3=>'SQR',
0xD4=>'TAN',
0xD5=>'LEN',
0xD6=>'CHR$',
0xD7=>'RND',
0xD8=>'SEG$',
0xD9=>'POS',
0xDA=>'VAL',
0xDB=>'STR$',
0xDC=>'ASC',
0xDD=>'PI',
0xDE=>'REC',
0xDF=>'MAX',
0xE0=>'MIN',
0xE1=>'RPT$',
0xE8=>'NUMERIC',
0xE9=>'DIGIT',
0xEA=>'UALPHA',
0xEB=>'SIZE',
0xEC=>'ALL',
0xED=>'USING',
0xEE=>'BEEP',
0xEF=>'ERASE',
0xF0=>'AT',
0xF1=>'BASE',
0xF3=>'VARIABLE',
0xF4=>'RELATIVE',
0xF5=>'INTERNAL',
0xF6=>'SEQUENTIAL',
0xF7=>'OUTPUT',
0xF8=>'UPDATE',
0xF9=>'APPEND',
0xFA=>'FIXED',
0xFB=>'PERMANENT',
0xFC=>'TAB',
0xFD=>'#',
0xFE=>'VALIDATE',
#0xFF=>'end of file marker',
}

Instance Attribute Summary

Attributes inherited from NativeFileType

#aux_code, #contents, #file_system_image, #file_type, #filename, #meta_data

Instance Method Summary collapse

Methods inherited from Ti99File

file_system_file_types, #type_description

Methods inherited from NativeFileType

#<=>, #==, all_native_file_types, best_fit, code_for_tests, compatability_score, #data_without_header, file_type_matches?, #full_filename, #header_length, #initialize, is_valid_file_if, #load_address, load_address, matching_score, native_file_types_possible_on_file_system, non_matching_score, #to_hex_dump, #to_info_dump, #type_description

Methods included from SubclassTracking

extended

Constructor Details

This class inherits a constructor from NativeFileType

Instance Method Details

#to_listingObject

0xFF=>‘end of file marker’,



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
# File 'lib/native_file_types/ti99/Ti99BasicFile.rb', line 141

def to_listing
  buffer=contents
  end_of_line_number_table=(buffer[2]<<8)+buffer[3]
  start_of_line_number_table=(buffer[4]<<8)+buffer[5]
  number_of_lines=(1+end_of_line_number_table-start_of_line_number_table)/4
  relocation_offset=start_of_line_number_table-7  
#  puts "START %x END %x LINES %x RELOC OFFSET %x" % [start_of_line_number_table,end_of_line_number_table,number_of_lines,relocation_offset]
  line_number_table=buffer[8,(number_of_lines*4)]
  detokenised_lines={}
  number_of_lines.times do |i|
    line_number_data=line_number_table[i*4,4]
    line_number=(line_number_data[0]<<8)+line_number_data[1]
    location_of_line_in_memory=(line_number_data[2]<<8)+line_number_data[3]
    location_of_line_in_buffer=location_of_line_in_memory-relocation_offset
#   puts "#{line_number} MEM %x FILE %x " % [location_of_line_in_memory,location_of_line_in_buffer]
    line_length=buffer[location_of_line_in_buffer]
    detokenised_line="#{line_number} "
    p=location_of_line_in_buffer+1
    last_byte_was_token=false
    while (p<location_of_line_in_buffer+line_length) && (!buffer[p].nil?) && (buffer[p]!=0) do
      token=buffer[p]
      token_text=TI_BASIC_TOKENS[token]
      if (token_text.nil?) then
        detokenised_line<<" " if ((detokenised_line=~/\w$/) && (last_byte_was_token))
        if token==0xC7 then #quoted string
          detokenised_line<<"\"#{buffer[p+2,buffer[p+1]]}\""
          p+=buffer[p+1]+1
        elsif token==0xC8 then #unquoted string
          detokenised_line<<"#{buffer[p+2,buffer[p+1]]}"
          p+=buffer[p+1]+1
        elsif token==0xC9 then #line number
          detokenised_line<<"#{(buffer[p+1]<<8)+buffer[p+2]}"
          p+=2
        else          
          detokenised_line<<token.chr  
        end
        last_byte_was_token=false
      else
        last_byte_was_token=true
        detokenised_line<<" " if detokenised_line=~/(\w|:|")$/ && token_text=~/^(\w|:|")/
        detokenised_line<<token_text
      end
      p+=1
    end
    detokenised_lines[line_number]=detokenised_line
    
  end
  result=""
  detokenised_lines.keys.sort.each do |line_number|
    result << "#{detokenised_lines[line_number]}\n"
  end
#  puts result
  
  result
end