Monday, September 10, 2012

AdaTutor - Outside Assignment 5

Writing a Simple Line Editor

We're finally ready for the next Outside Assignment!  This assignment will give you a chance to write a program of greater complexity than the previous assignments.  By the time you've completed Outside Assignment 5, you should feel comfortable with Ada.  The full set of requirements for the line editor we want you to write are in your printed course notes, starting on page 20.  We'll discuss them briefly here.  No test driver is supplied, but after you've written the program, we'll give you some tests to perform manually on your line editor.  You've completed the assignment when your editor passes all the tests.

A line editor edits only one line of a text file at a time, and is much simpler than a screen editor that lets you move a cursor wherever you want.  The line editor you'll write, called Ledit, will take almost no effort to learn how to use.  The only commands are LIST and EXIT!

The user begins each line of text with a line number, similar to early versions of Basic.  Line numbers must be integers between 1 and 29999.  Regardless of the order in which lines are entered, Ledit maintains a linked list of lines in order by number, so that it can List the text in order, or write it to a file.

Line numbers need not be consecutive, and they may be preceded by any number of spaces.  For example, if we type

40 -- This is a comment.
   20 begin
10 with Ada.Text_IO; use Ada.Text_IO;
      30 end Add;
and then type LIST, the editor displays
   10 with Ada.Text_IO; use Ada.Text_IO;
   20 begin
   30 end Add;
   40 -- This is a comment.

To insert lines, we merely type lines with intermediate line numbers . In our example, if we now type

15 procedure Hello is
LIST
we'll see
   10 with Ada.Text_IO; use Ada.Text_IO;
   15 procedure Hello is
   20 begin
   30 end Add;
   40 -- This is a comment.

To replace a line, we simply retype the line with the same line number as the line to be replaced, and to delete a line, we type only the line number. If we now type

15 procedure Add is
40
LIST
we'll see
   10 with Ada.Text_IO; use Ada.Text_IO;
   15 procedure Add is
   20 begin
   30 end Add;

Thus the user can insert, replace, and delete lines by line numbers, without learning any commands!  If the user forgets the space after the line number, no harm is done; Ledit takes 20begin the same as 20 begin.  However, the user can indent code by adding extra spaces after the line number.  The following example has two extra spaces after each line number (three spaces total):

24   Put(2 + 2);
26   New_Line;
18   package My_Int_IO is new Integer_IO(Integer); use My_Int_IO;
LIST

   10 with Ada.Text_IO; use Ada.Text_IO;
   15 procedure Add is
   18   package My_Int_IO is new Integer_IO(Integer); use My_Int_IO;
   20 begin
   24   Put(2 + 2);
   26   New_Line;
   30 end Add;

When Listing, Ledit always allows exactly five spaces for the line number, so that the text lines up correctly:

LIST

    2 This is a sample listing,
   20 showing how the text
  200 lines up, even when
 2000 some line numbers are
20000 longer than others.

When we type EXIT, Ledit writes the output file without the line numbers. The text above would all start in column 1 of the output file.

For Ledit to be useful with files larger than a page, it must be possible to list a range of lines:

LIST 20 - 30

This is only a summary of the requirements for Ledit.  Please refer to pages 20-24 of your printed course notes for the actual requirements.  As a point of reference, our solution requires about 180 lines of Ada on four pages.

Here are the steps to follow for Outside Assignment 5.  They can also be found in your printed course notes on page 25:

  1. Carefully read the requirements starting on page 20 of your printed course notes.  Take your time.
  2. Write the Ada code, compile, and link.  Call the main program Ledit.  If you have any questions about what Ledit should do, you can compile and run our solution, which is in L_EDIT.ANS.
  3. Refer to pages 26-28 of your printed course notes for instructions on testing your line editor.  If any tests are failed, go back to step 2.
  4. When all the tests are passed, you've completed the assignment and will have a chance to compare your solution with ours.
Please stop reading AdaTutor temporarily, and try Outside Assignment 5.  It will probably take several days.  Take your time; there's no deadline.  Good luck!

Congratulations on Completing Outside Assignment 5!

If you like, you can compare your solution with ours, which is in L_EDIT.ANS.  A listing starts on page 29 of your printed course notes.  Note that a single procedure handles adding, deleting, and replacing lines.  Replacing is done by first deleting, then adding a line.

Your solution might be very different from ours, but if it passed all the tests, consider it correct.

Early in this course we used the generic package Integer_IO.  Let's now learn how to write our own generic packages, procedures, and functions.

Click to view our solution:

< prev   next >

Post a Comment