(self, console, new_line, start_line, step)
return old_to_new
Renumber stored program.
Renumber stored program.
Renumber stored program .
def renum(self, console, new_line, start_line, step):
        """Renumber stored program."""
        new_line = 10 if new_line is None else new_line
        start_line = 0 if start_line is None else start_line
        step = 10 if step is None else step
        # ensure we're not about to overwrite anything
        remaining = [_k for _k in self.line_numbers.keys() if _k < start_line]
        if remaining and new_line <= max(remaining):
            raise error.BASICError(error.IFC)
        # get a sorted list of line numbers
        # assign the new numbers
        old_to_new = {}
        for old_line in sorted(_k for _k in self.line_numbers.keys() if _k >= start_line):
            if old_line < 65535 and new_line > 65529:
                raise error.BASICError(error.IFC)
            if old_line == 65536:
                break
            old_to_new[old_line] = new_line
            self.last_stored = new_line
            new_line += step
        # write the new numbers
        for old_line in old_to_new:
            self.bytecode.seek(self.line_numbers[old_line])
            # skip the \x00\xC0\xDE & overwrite line number
            self.bytecode.read(3)
            self.bytecode.write(struct.pack('<H', old_to_new[old_line]))
        # write the indirect line numbers
        ins = self.bytecode
        ins.seek(0)
        while ins.skip_to_read((tk.T_UINT,)) == tk.T_UINT:
            # get the old g number
            token = ins.read(2)
            jumpnum, = struct.unpack('<H', token)
            # handle exception for ERROR GOTO
            if jumpnum == 0:
                pos = ins.tell()
                # skip line number token
                ins.seek(-3, 1)
                if ins.backskip_blank() == tk.GOTO and ins.backskip_blank() == tk.ERROR:
                    ins.seek(pos)
                    continue
                ins.seek(pos)
            try:
                newjump = old_to_new[jumpnum]
            except KeyError:
                # not redefined, exists in program?
                if jumpnum not in self.line_numbers:
                    linum = self.get_line_number(ins.tell()-1)
                    console.write_line(b'Undefined line %d in %d' % (jumpnum, linum))
                newjump = jumpnum
            ins.seek(-2, 1)
            ins.write(struct.pack('<H', newjump))
        # rebuild the line number dictionary
        new_lines = {}
        for old_line in old_to_new:
            new_lines[old_to_new[old_line]] = self.line_numbers[old_line]
            del self.line_numbers[old_line]
        self.line_numbers.update(new_lines)
        return old_to_new
def renum ( self , console , new_line , start_line , step ) : new_line = 10 if new_line is None else new_line start_line = 0 if start_line is None else start_line step = 10 if step is None else step # ensure we're not about to overwrite anything remaining = [ _k for _k in self . line_numbers . keys ( ) if _k < start_line ] if remaining and new_line <= max ( remaining ) : raise error . BASICError ( error . IFC ) # get a sorted list of line numbers # assign the new numbers old_to_new = { } for old_line in sorted ( _k for _k in self . line_numbers . keys ( ) if _k >= start_line ) : if old_line < 65535 and new_line > 65529 : raise error . BASICError ( error . IFC ) if old_line == 65536 : break old_to_new [ old_line ] = new_line self . last_stored = new_line new_line += step # write the new numbers for old_line in old_to_new : self . bytecode . seek ( self . line_numbers [ old_line ] ) # skip the \x00\xC0\xDE & overwrite line number self . bytecode . read ( 3 ) self . bytecode . write ( struct . pack ( '<H' , old_to_new [ old_line ] ) ) # write the indirect line numbers ins = self . bytecode ins . seek ( 0 ) while ins . skip_to_read ( ( tk . T_UINT , ) ) == tk . T_UINT : # get the old g number token = ins . read ( 2 ) jumpnum , = struct . unpack ( '<H' , token ) # handle exception for ERROR GOTO if jumpnum == 0 : pos = ins . tell ( ) # skip line number token ins . seek ( - 3 , 1 ) if ins . backskip_blank ( ) == tk . GOTO and ins . backskip_blank ( ) == tk . ERROR : ins . seek ( pos ) continue ins . seek ( pos ) try : newjump = old_to_new [ jumpnum ] except KeyError : # not redefined, exists in program? if jumpnum not in self . line_numbers : linum = self . get_line_number ( ins . tell ( ) - 1 ) console . write_line ( b'Undefined line %d in %d' % ( jumpnum , linum ) ) newjump = jumpnum ins . seek ( - 2 , 1 ) ins . write ( struct . pack ( '<H' , newjump ) ) # rebuild the line number dictionary new_lines = { } for old_line in old_to_new : new_lines [ old_to_new [ old_line ] ] = self . line_numbers [ old_line ] del self . line_numbers [ old_line ] self . line_numbers . update ( new_lines ) return old_to_new