Getting Started in ABC

ABC is an object-oriented software development system which can be used to design and deliver instruction as well as build non-instructional applications. To make full use of ABC, you need to learn some programming fundamentals. In this tutorial, it is assumed that you have some programming experience in a language like Pascal or C.

To initiate an ABC session, type the command

Note to Project Vincent users

% abc

Note: In the documentation, the '%' is an indication of the system prompt and should not be typed. Your system prompt may differ.

After a few seconds, the ABC Main Window should appear.

The ABC Main Window is the primary program development window. It consists of three sections:

ABC MenuBar
Saves changes, exits ABC, edits text and runs other ABC applications
ABC ToolBar
Adds, deletes, copies etc. objects stored in the master directory
Master Directory Window
Shows the master folder and subfolders as well as the objects stored in them. The Master directory contains all the objects stored in ABC.

Central to any programming activity is the ability to execute procedures to accomplish desired tasks. In ABC these procedures are called methods. In the first exercise, you will create a method and run it.

Exercise 1: printing "hello, world"

To create a method, follow the steps below:

  1. Click on the Add button in the ToolBar. The Add Object dialog will appear as shown below.

  2. Scroll the list of types of objects you can create until Method appears. Click on it.
  3. Enter 'test' in the editor provided.
  4. Press Return or click on Add when done. The Add Object dialog should disappear and an icon with the name 'test' should appear in the Master Directory Window.
Now that you have created a method, you need to edit it so that you can enter your programming statements. Click on the test icon you just created in the Master Directory Window and click on Edit in the ToolBar. The Method Dialog should appear.

Enter the following text in the Method editor:

	newMethod
          { system printLine "hello, world".
          }
Note: For this and subsequent exercises, you can save yourself typing if you are using Mosaic to view this document. Just click and drag the text and paste it in the Method Dialog Window. Refer to your local documentation on how to cut and paste between windows on your computer.

Click on the Compile button in the Method ToolBar. If no Text Errors window appears, your method compiled correctly. If not, the Text Errors window will show which line has a problem. Correct the error and compile again. If you succeed this time, the Text Errors window will disappear.

Click on the Run button in the Method ToolBar to execute your method. Your output should appear in the Console window. Congratulations! You have just written your first ABC method.

An ABC method consists of a name (e.g. newMethod) and a sequence of statements between the curly brackets. In this case, there is only one statement: system printLine "hello, world". Each statement consists of a receiver object (e.g. 'system'), a method name and parameters (e.g. 'printLine "hello, world"') followed by a period. When this statement is executed, it will call the 'printLine' method to print the quoted string in the console window. For comparison, the equivalent PASCAL or C statements for this ABC statement are

 

    PASCAL: writeln(OUTPUT,"hello, world");
         C: printf("hello, world");

Note: In ABC documentation, references to method names with parameters are formed by appending the keywords with colons and concatenating them. Single parameter method names have the colon appended to the name as in printLine:. A double parameter method name would be from:to:. If no parameter is required just the keyword is used as in capitalize. The colons are not used when the method is used in a statement.

ABC is case-insensitive except for quoted strings like "hello, world". Thus, printLine: and printline: are equivalent method names although the former is preferred for readability. By convention, multi-word phrases have the first word in lower case and subsequent words capitalized. This helps to visually separate the words.

Exercise 2: reading a name

The next exercise demonstrates the use of variables. It prompts for input from the system window and prints what you typed. Enter the following statements in the Method:


    newMethod
      { system print "Name:".
        name -> system input.
        system printLine name.
      }

Compile and Run the activity as before. You will need to click on the console window to enter your name.

In this example, several new elements have been added:

Variables refer to objects used in a method. Variable names begin with a letter followed by zero or more letters or digits. Variables may be global variables, method parameters, part variables or local variables. In this example, 'system' is a global variable while 'name' is a local variable.

Like PASCAL and C, variables have an associated type. For example, 'name' in the example is a String because it refers to the string returned from 'system input'. Unlike PASCAL or C, the types of local variables are determined from context rather than by explicit declaration. When a variable first refers to an object with '->', its type becomes the type of its referrent. If 'name' later refers to another object, that object must be a String as well. Thus the statements


    name -> "test".
    name -> 3.

would be invalid because the first statement implicitly declares 'name' as a String. One ramification of implicit declarations is that a local variable may not be used in a method until it refers to an object. Thus


    newMethod
      { system printLine var.
      }

is invalid because 'var' does not yet refer to an object.

Exercise 3: capitalizing strings

Thus far you have seen examples using the global variable 'system' which is of type Opsystem. Each type defines the valid methods for objects which have that type. For example, all objects of type Opsystem can execute print: and printLine:, among others.

Try executing the method below which uses the capitalize method from type String:


    newMethod
      { system print "Name:".
        name -> system input.
        name -> name capitalize.
        system printLine name.
      }

Every statement returns an object which can be used in subsequent computations. In the example below, 'system input' returns the entered string which then becomes the receiver of the capitalize method. (Parentheses are used to delimit embedded statements.)


    newMethod
      { system print "Name:".
        name -> (system input) capitalize.
        system printLine name.
      }

For another example, the method _: concatenates the string 'name' to the string 'says hello!' and prints it:


    newMethod
      { system print "Name:".
        name -> (system input) capitalize.
        system printLine (name _ " says hello!").
      }

The Basic Types

String is one of the basic types in ABC; Integer, Real, and Boolean are the others. The Integer and Real types define the familiar numeric operations like addition, subtraction, multiplication and type conversion. Boolean defines the IF-THEN and WHILE control structures found in PASCAL or C. The methods defined by a type are like the procedures and structures defined in a subroutine library.

Integer constants in ABC consist of an optional sign followed by a sequence of digits. Reals consist of an optional sign, zero or more digits and a decimal point followed by one or more digits. In fact, it is the digit after the decimal point which distinguishes a Real from an Integer at the end of a statement. '4.5' is a Real object while '4.' is an Integer object followed by an end-of-statement period.

Normally, spacing between symbols is not significant other than to separate words. However, there may not be any intervening space between the sign and digits for Integers or Reals if you want to use signed numbers. For example,


    i -> 3 +4.

is incorrect because the plus is part of the Integer constant '+4'. ABC will complain because '+4' is not a valid method name for Integers.

Thus far we have seen methods with no parameters (e.g. name capitalize) or one parameter (e.g. system printLine name). Multiple parameters are also possible. For example, the method to return a substring from a string requires the location of the beginning and ending of the substring as in:


    w -> "hello, world" from 8 to 12.

The variable 'w' will point to the string 'world' after this statement is executed.

There is no precedence in ABC; rather parentheses are used. The following statement will generate an error because ABC will fail to find the Integer method +:*: (note that '+' and '*' are considered keywords.)


    i -> 3 + 4 * 5.

To express the normal sense of this statement you should write it as


    i -> 3 + (4 * 5).

Also, there is no implicit type conversion as in PASCAL. For example,


    i -> 3 + 4.0.

is an error because the Integer +: method expects an Integer parameter. The asInteger method must be called first to convert '4.0' to an Integer as in:


    i -> 3 + (4.0 asInteger).

Integers, Reals and Strings have methods like asReal, asInteger and asString to convert from one type to another.

Exercise 4: if and while

The receiver-method syntax may seem a bit strange at first, but it provides a consistent way to express all computation in ABC, even control structures like the familiar IF and WHILE statements used in other languages. For example, the PASCAL code


    write("Age:"); readln(age);
    if (age > 40) then
    begin
      writeln("You are over the hill!");
    end
    else
    begin
      writeln("You are over the hill!");
    end;

looks like the following in ABC:


    system print "Age:"; age -> (system input) asInteger.
    (age > 40) then
    { system printLine "You are over the hill!".
    } else 
    { system printLine "Just a spring chicken!".
    }.

In this example, age refers to the Integer which is entered from the system window. The semicolon is used to separate statements on the same line. The >: method is then executed and replies with TRUE if age is greater than 40 or FALSE if not. The Boolean result becomes the receiver of then:else:.

The sequence of statements delimited by curly brackets is a Block object. Since a block is an object like everything else in ABC, it can be passed as a parameter to a method and executed. In this case, the Boolean then:else: will execute the then-block if the receiver is TRUE and the else-block if it is FALSE.

In the same way, repetition can be accomplished with the whileDo: method for a Boolean object:

    newMethod
      { i -> 1.
        (i <= 5) whileDo 
        { system printLine (i asString).
          i++.
        }.
      }

This will print the numbers from 1 to 5. the Integer ++ increments the receiver by 1. Another way to express the same function is:

  newMethod
   { 1 to 5 do 
     { |i:Integer| 
       system printLine (i asString).  
     }.
   }

The variable 'i' is a block parameter and is set to the appropriate value before each iteration. Since designing new types of objects will be one of your primary activities, the ability to provide control structures for a type becomes a very powerful mechanism.

Handling Errors

asString was used because printLine: requires a String as a parameter. But what happens if you specify an object of the wrong type? Change the example to:

  newMethod
   { 1 to 5 do 
      { |i:Integer|
        system printLine i.
      }.
   }

When you attempt to run the example, a Text Errors window will appear. A line will appear in the listbox indicating the error, "Expecting String rather than Object". If you click on Locate, the Method cursor will move to the offending source line and you can correct the error. You may also click on any line in the listbox and the cursor will move to the appropriate source line.

Exercise 5: Running Frequently used Methods

The Run button on the ABC MenuBar displays a list of frequently accessed methods. By default, only the Type Manager is available, which lets you create and modify object types.

A method is run when it is selected from this list. To add a method to the list, create a method, edit and compile it and then drag and drop it in the folder labeled Run Items. It will be available when you start the next ABC session.

For example, create a method called Time and give it the code shown below:

Time { system printLine ((Time now) asString). }

'Time now' returns a Time object which represents the current time. When it is converted to a String it will look like '13:17:48'.

Compile this method and click on the Exit button of the Method ToolBar. Drag and drop it in the Run Itemsfolder. Then select the ABC/Exit option from the ABC Menubar. When it asks if you want to save your changes, click on OK.

Start up ABC again and select the Run/Time option from the ABC MenuBar. Your clock is now working.

The ABC ToolBar

The buttons on the ABC ToolBar have the following functions:
File
saves a selected object to a file. The format of the file (ASM) is a text description of the object suitable for loading into another ABC program.
Load
reads a file in ASM format and save it in the current folder displayed under the name given.
Edit
edits the selected object. The editor will differ depending on the type of object. If you edit a folder, it will be displayed in the Directory Window, nested within the enclosing folder. You can click on a tab to redisplay the enclosing folder.
Add
adds a new object.
Delete
deletes an object. The function of delete is to move the selected object to the clipboard folder. If you accidentally delete the object, click on the clipboard tab and drag and drop the object on the desired folder tab. Objects in the clipboard are deleted when you exit ABC.
Copy
makes a copy of an object and store it under the name indicated.
Rename
renames an object.
Align
lines up all the object in alphabetical order.
Info
lists all the objects in the folder including their types, and dates of modification.

Exiting

The changes you make during an ABC session will not be permanently saved until you click on the Save button or indicate that you want to save changes when you exit. It is wise to save periodically in case a system or ABC failure occurs.

Your changes are stored in a file called 'abc.chk'. The next time you begin an ABC session, these changes will be read and you can resume where you left off.

If you abort a session via CTRL/C, all the changes since you began the session or saved will be lost.