TEACH MISHAPS                                                A. Sloman Nov 2011


CONTENTS - (Use <ENTER> g to access required sections)

 -- Run-time errors

INTRODUCTION: TYPES OF PROGRAMMING ERRORS (MISHAPS)
A brief introduction to compile time errors (called 'mishaps' in pop11 error
reports) and run time errors (also called 'mishaps');

This file merely introduces the format of teach files containing
executable code,
for practice with
dealing with an error when the whole file is compiled.

The file is a mixture of compilable pop11 code and commented out tutorial text.
Because it is compilable, at any time you can edit an example or a collection
of procedure definitions and recompile. However, if you have changed something
there may be a 'mishap'. You'll need to look at the mishap line number and try
to work out how to fix the file.

An example follows: it is not legal pop11 so if you compile the whole file by
doing
    ENTER l1 RETURN

you will get an error message in an output.p file. See now the error relates to
the faulty instruction, then delete the faulty instruction. There are more
below!

*/

;;; A faulty instruction -- syntax error -- Found "]", expecting ")"

    sqrt( 99 ]) =>

/*
That is a "syntactic error" generated while the program code was being
compiled. That is sometimes called a "compile time error".

Now delete or comment out that faulty line. You can put the editor cursor on
the line and use the key F4. (LINEDELETE). Or else put the cursor at the right
hand end of the line and type CTRL-U (sometimes abbreviated as ^U). Either
will delete the line.

You can also 'comment out' the line by using three scmi-colons, as in:

;;;     sqrt( 99 ]) =>

Try to insert another line in its place that you think should produce a
syntactic error, then repeat the compilation of the file: ENTER l1 RETURN

Which of these do you think will produce a syntactic error? Why?
Try them. You can compile each line separately by putting the text cursor on
the desired line, then typing: ESC d

    sqrt[ 99 ]) =>

    sqrt([ 99 ) =>

    "the" "apple" =>

At this stage, don't try to understand everything in the error messages. They
will become clearer later.

You will meet more syntactic errors as you produce more complex programs. They
are like playing wrong notes on a musical instrument.

-- Run-time errors ----------------------------------------------------

The previous errors were compile-time errors (syntax errors), which prevented
a piece of program being fully read in and compiled.

But you can also get a run-time error (semantic error), as the next example
will demonstrate. Here the program is compiled, then run, and something goes
wrong while the program is running: an operation is attempted that the Pop11
system does not allow, such as adding a number to a word, or asking for the
square root of a list of numbers.

If you have removed all the errors further up the file you can trigger the
first error below by compiling the whole file (ENTER l1):

*/

    ;;; The first line is fine, and should print out '** 2' Why?

    length([88 99]) =>

    ;;; The next line produces a 'run time' error after the instruction is
    ;;; compiled, when the compiled code is being run.  Why is this an error?

    sqrt([99 100]) =>

/*
Fix the above command to make it print out the square root of 88 (or some other
number). Then do ENTER l1 RETURN again, to make sure it works.

Hint: the pop11 procedure sqrt operates on numbers, not on lists of numbers,
whereas length can operate on lists and other things.

Then delete both the above tests. Function Key F4 should delete the line the
cursor is on. Or mark the line (ESC m) then delete the marked range using the
command
    ENTER d
*/

/*
Most of this file is inside Pop11 comment brackets. /*  */ which look like the
comment brackets in C and some other languages.

Try compiling the whole file,
    ENTER l1

The table of contents is near the end, followed by a command to print out
'Successfully compiled'.

If necessary, read TEACH MINIVED for revision. You will need to know how
to mark and compile a range of text in Ved, how to save your file, how
to return to it later.

*/

;;; You may wish to remove this command later:

'Starting to compile TEACH CHAT1' =>

--- $usepop/pop/teach/mishaps
--- Copyright University of Birmingham 2011. All rights reserved.
