CSE 111, Fall 2000

Great Ideas in Computer Science

Lecture Notes #29

PROGRAMMING IN PASCAL:
TEXT PROCESSING (continued)

21.  How to write the cursor:

a)    As a first approximation, consider the
        following program that repeats the letter
        'A', followed by a "cursor" that looks like
        the pound sign:

        (Note:  dowloadable/runnable versions
        can be found by linking to the program's
        name.)

    program RepeatAs1;
    var cursor : varying [20] of char;
    begin
        cursor := '#';
         while true do
              begin {while}
                   cursor := 'A' + cursor;
                   writeln(cursor)
              end   {while}
    end.

    Now, this is an infinite loop, but the important
    point is to see what it does:

    It makes a series of strings consisting of
    a number of 'A's followed by the "cursor":  #

    A#
    AA#
    AAA#, etc.

b)    2nd approximation:

    program RepeatAs2;
    var cursor : varying [100] of char;
          i, ptr : integer;
    begin
         writeln('How many chars?');
         readln(ptr);
         cursor := '#';
         i := 1;
         while i <= ptr do
              begin {while}
                   cursor := 'A' + cursor;
                   i := i+1
              end;  {while}
          writeln(cursor);
    end.

    This one asks the user how many 'A's to put
    in front of the cursor.

c)    3rd approx:

    program RepeatAs3;
    var cursor : varying [100] of char;
          i, ptr : integer;
    begin
         writeln('How many chars?');
         readln(ptr);
         cursor := '#';
         i := 1;
         while i <= ptr do
              begin {while}
                   cursor := ' ' + cursor;
                   i := i+1
              end;  {while}
          writeln(cursor);
    end.

        This one asks the user how many blanks (' ') to
        put in front of the cursor, so its output might
        look like this:

        I/P = 1
        O/P = ~#

        I/P = 5
        O/P = ~~~~~#

d)    Finally, here's a program that tests how we
        really want this cursor to work:

    program CursorTest;
    var text, cursor : varying [100] of char;
          i, ptr       : integer;
    begin
         text := 'This is merely sample text.';
         ptr  := length(text);
         writeln('Your final text is:');
         writeln(text);
         cursor := '#';
         i := 1;
         while i < ptr do
            begin {cursor}
                cursor := ' ' + cursor;
                i := i+1
            end;  {cursor}
         writeln(cursor)
    end.

    This prints out a sample text,
    sets the pointer to the end of the text,
    writes out the text,
    goes through a loop like the one above in order
        to set the cursor to physically be located where
        the pointer is set to,
    & prints out the cursor on the next line.
 

22.    The (almost final) text editor.


a)    Click here for a downloadable/runnable
        version of the final text editor, print it
        out, and look at it while reading the following
        description of it.

b)    This version has lots of "bells & whistles" to
        make it readable.

        Note that it begins with some comments that
        tell you who wrote it, when, and what it does.

        Then it says to go to "BEGIN READING HERE",
        so do that before continuing...

c)    The main body of the program:

        First, it writes out a welcoming message.
        Next, it initializes the text (to an empty string)
                 & the pointer (to 0).
        Then it prints out a prompt for the user to
            enter a command, and it stores that in
            the memory location named "command".
        Then it enters a loop that it will exit only when
            the user types in 'q' (for "quit").

        Within that loop, it has commands of the form:

        if command = 'i' then {code for insert}

        and it has these for 'i', 'd', and an
            error-handling routine.

d)    Let's look at the insert routine.

        First, it moves the pointer.

            To do that, it first asks if this is the first
                use of "insert";
            If it is, then it sets the pointer to 1,
                       else it moves the pointer

        Next, it asks the user what should be inserted.
        Then it calls an "Insert" function that does the
            details of inserting.  We'll come back to that.
        Finally, it prints the updated text and the
            cursor, using a while-loop for the cursor
            just like the one above.

e)    Delete is handled similarly, except that instead
        of having the delete routine right here, it also
        uses a "Delete" function.

f)    At the end of the program, it prints out the final
        text, with the cursor, as before.

g)    Now let's go back and look at the declarations:

    *    There's one type declaration.

    *    There are several variable declarations
          to reserve memory locations:

            several for strings, and a few for integers;
            all of these are commented.

    *    Next come the function declarations.

        The first few are for FirstPart, LastPart,
        and Remainder, which we've seen before.

    *    Now comes the new part:  Functions that
        do the work of Insert and Delete.

        If you look at these carefully, you'll see that
        they are just the old insert and delete programs
        that we wrote earlier, only now they appear as
        function declarations.

        One reason for doing this (another will be
        described later) is to make the actual insert
        routine in the main body of the program
        easier to read.  Now all it says is to

            Insert(text,new,ptr)

        i.e., insert into some old "text" some "new" text
              at the location of the "ptr"

            The details of how to do this, namely,
            by concatenating the FirstPart with new
            with LastPart, are hidden from the casual
            reader.

            But the details of what FirstPart, etc., are
            are also hidden, wrapped up in their own
            function declarations.

    *    Note that one reason for doing that is that
          FirstPart, for example, is used in both Insert
            and Delete, so there's no reason to have it
            included twice, once in each place.  We
          defined a new instruction, FirstPart, and used
            it in 2 different places.
 

23.    The final text editor.


a)    Here's another reason to have made Insert
        and Delete functions:

        I want to add a new feature to the editor:
                        Change
        Change will replace some old text with some
        new text.  It will do that by deleting the old
        text and inserting the new text.  The easiest
        way to do that is by using the Insert and Delete
        functions.

b)    To see a version of the program with the Change
        routine, click here.

c)    If you want to add your "insert space" routine
        from HW #11, you can!
 
THAT'S ALL FOR THE TEXT EDITOR!
NEXT TOPIC:  MACHINE ARCHITECTURE
AND ASSEMBLY LANGUAGE

SO:    READ BIERMANN, CHAPTER 8!!


Copyright © 2000 by William J. Rapaport (rapaport@cse.buffalo.edu)

file: 111F00/lecturenotes29.17nv00.html