Saturday, December 08, 2012

Dynamic Variables

Copyright 2012 by Shawn H Corey. Some rights reserved.
Licence under CC BY-SA 3.0

Another look at dynamics variables. And more on exceptions.

Dynamic Variables

After thinking about dynamic variables, it won't be possible to incorporate them into my language. One of the tenants is to avoid, "Out of sight, out of mind." This means that the entire interface to a subroutine must be displayed for its call. So, no dynamic variables.

The programmer must use the down-and-up technique of sending everything down to the subroutine and storing what is needed when it returns back up. The IDE will display the entire interface when the programming is coding the call to it, so it is just a matter of filling in the slots of the interface to complete the call.

Exception Processing

When an exception happens, only three actions may be taken. This is to ensure that exceptions can't be thrown inside exception handlers.

Ignore it.

An exception can simply be ignore.

Example:

    when exception
        ignore
Declare an exception.

Example:

    when exception
        declare exception
Assign a value to a Boolean variable.

Example:

    when exception
        Boolean variable ← TRUE

    when exception
        Boolean variable ← FALSE

Exceptions which do not have a handler will generate a compile-time error and the program will not run.

The catch all phrases, any and other, can be used to handled groups of exceptions.

Example:

    when any
        ignore

    when other
        declare same

The slope Function, Revisited

Here's the slope function rewritten with these new rules:

    module Trend

    function Number slope
        given
            Point 1st
            Point 2nd
        returns
            Number slope
        except
            when infinite slope
            when points too close to determine slope

    begin

        Boolean overflowed    ← FALSE
        Boolean underflowed   ← FALSE
        Boolean X underflowed ← FALSE
        Boolean Y underflowed ← FALSE

        Number Δy ← 2nd.y - 1st.y
            when overflow
                overflowed ← TRUE
            when underflow
                Y underflowed ← TRUE

        Number Δx ← 2nd.x - 1st.x
            when overflow
                overflowed ← TRUE
            when underflow
                X underflowed ← TRUE

        if X underflowed and Y underflowed
            declare points too close to determine slope

        if overflowed
            Δy ← 2nd.y ÷ 2 - 1st.y ÷ 2
                when any
                    ignore
            Δx ← 2nd.x ÷ 2 - 1st.x ÷ 2
                when any
                    ignore

        slope ← Δy ÷ Δx
            when divide by zero
                declare infinite slope
            when overflow
                declare infinite slope
            when underflow
                underflowed ← TRUE

        if underflowed
            slope ← 0

    return slope