Sunday, November 18, 2012

Grammar - Part 1

Time to look at some grammar. Here's is the top few levels of it. You should consider this to be a rough draft; it is by no means final.

Expressions in square brackets, "[" and "]", may appear zero or once. Expressions in curly braces, "{" and "}", may appear zero, once, or many times.

Module

    module ::= module_declaration
               { uses_section }
               { configuration_declaration }
               subroutine_definitions
               [ local_declarations
                   { configuration_initiation }
               ]

A module is a name space which separates its contents from those of other modules.

Module Declaration

    module_declaration ::= "module" module_name

States the name of the module.

Uses Section

    uses_section ::= "use" module_name [ "from" file_spec ]
                     [ "import"
                         subroutine_name
                         { subroutine_name }
                     ]

Tells which modules this one uses. Imported subroutines can be used with just their names. If the module is not store in a file with the same name, a file spec can be added so it will be found.

Configuration Declaration

    configuration_declaration ::= type configuration_variable_name

This is used to declare configuration variables so they may be used in the subroutines. Use of them in a subroutine is read only. Their values are set in the initiation section.

Subroutine Definitions

   subroutine_definitions ::= subroutine_definition
                              { subroutine_definition }

At least one subroutine must appear in a module.

Subroutine Definition

   subroutine_definition ::= [ "private" ] "subroutine" subroutine_name
                             [ given_section ]
                             [ returns_section ]
                             [ exceptions_section ]
                             "begin"
                             block
                             "return" [ returns_list ]

A private subroutine cannot be imported by another module. A block is a list of statements (to be determined in another article).

Given Section

    given_section ::= "given"
                      type parameter_variable_name [ "←" default_value ]
                      { type parameter_variable_name [ "←" default_value ] }

This section lists the parameters needed by the subroutine. Parameters may be listed by position or set by name. A default value may be defined to make the parameter optional.

Returns Section

    returns_section ::= "returns"
                        type return_variable_name [ "←" default_value ]
                        { type return_variable_name  [ "←" default_value ] }

List the items returned with the optional of setting their default values. Items may be listed by position or by name.

Exceptions Section

    exceptions_section ::= "except"
                           "when" exception_identifier
                           { "when" exception_identifier }

This lists what exceptions the calling code must handle. Exceptions that are not handled cause fatal errors. Exceptions will be detailed in a future article.

Returns List

    returns_list ::= return_variable_name { "," return_variable_name }

A repeat of the returned variables to remind the programmer what the results are.

Local Declarations

    local_declarations ::= "local"
                           type local_variable [ "←" default_value ]
                           { type local_variable [ "←" default_value ] }

Local variables are used to hold data for the configuration initiation. This is so complex calculations need not be repeated. They may be calculated once and stored in local variables.

Note that because local variables are defined after the subroutines, they may not appear in the subroutines.

Configuration Initiation

    configuration_initiation ::= "initialize" configuration_variable_name
                                 block
                                 "set" "read" "only" configuration_variable_name

Configuration variable are used to adapt the module to its environment. Note that the last step is to set them to read only.