==============
Reserved words
==============
.. Modified: 2018-10-23/05:38-0400 btiffin
.. Copyright 2016 Brian Tiffin
.. GPL 3.0+ :ref:`license`
.. This file is part of the Unicon Programming documentation
.. image:: images/unicon.png
:align: center
.. only:: html
:ref:`genindex`
:floatright:`Unicon`
Unicon reserved words
=====================
Unicon uses a fair number of reserved words. There are 38 reserved words in
Unicon version 13. That might seem like a lot, and it is, until you compare
Unicon with the likes of :ref:`COBOL `\ [1]_.
*Emphatics*\ [2]_ is used here to denote action causing reserved words. In
many other languages, they would be called *statements*, but Unicon has no
*statements*. These reserved words all count as *expressions* and return
results along with triggering control flow actions to occur. *Almost*
everything\ [3]_ in Unicon is an :ref:`expression `. Listed here
as emphatic, these action causing reserved words act, and react, as
expressions.
While most Unicon reserved words are actionable, flow control expressions,
some others take a supporting role, as either *declarative* expressions, or as
syntax markers for optional phrasing. For example, the :ref:`by` reserved word
is only usable when paired with :ref:`to`, and will not work alone as a valid
expression. :ref:`else` is only applicable within an :ref:`if` expression.
:ref:`then` is a marker word that separates |expr1| from |expr2| in an
:ref:`if` expression. Other words inform the compiler about data management,
with variables being declared :ref:`global`, :ref:`local` or :ref:`static`.
**Reserved words cannot be used as identifier names.** [4]_
All reserved words count as expressions (or assist in defining an overall
expression).
.. [1]
COBOL has over 500 reserved words, some implementations listing over
1,000.
.. [2]
This chapter was originally called "Statements", but that is a technically
incorrect and harmfully misleading naming convention. Unicon has no
statements. All expressions return results, emphatic reserved words lead
flow control and count as expressions.
.. [3]
If you try hard, you may find a Unicon syntax element that does not
count as an expression; :ref:`preprocessor` directives come to mind, as
would the :ref:`all` reserved word that is part of :ref:`invocable` in
``invocable all``.
.. [4]
Unicon can be *tricky* sometimes. Everything is an expression, and
variables do not need to be declared before use. From the Programming
Corner, Icon Newsletter 35, March 1st, 1991:
while next := read() do write(next)
That code fragment causes an endless loop. The ``next`` is not parsed
as a new variable identifier, but is valid as the |expr1| part of the
assignment operator |expr1| ``:=`` |expr2|. Unicon accepts that code,
which at runtime evaluates the ``next``, as part of the assignment
expression, immediately transferring control back to the top of the while
loop; an infinite loop.
.. index:: reserved word list
Reserved word list
------------------
.. only:: latex
================ ================= ================= ==============
:ref:`abstract` :ref:`else` :ref:`link` :ref:`return`
:ref:`all` :ref:`end` :ref:`local` :ref:`static`
:ref:`break` :ref:`every` :ref:`method` :ref:`suspend`
:ref:`by` :ref:`fail` :ref:`next` :ref:`then`
:ref:`case` :ref:`global` :ref:`not` :ref:`thread`
:ref:`class` :ref:`if` :ref:`of` :ref:`to`
:ref:`create` :ref:`import` :ref:`package` :ref:`until`
:ref:`critical` :ref:`initial` :ref:`procedure` :ref:`while`
:ref:`default` :ref:`initially` :ref:`record`
:ref:`do` :ref:`invocable` :ref:`repeat`
================ ================= ================= ==============
.. only:: not latex
.. hlist::
:columns: 4
- :ref:`abstract`
- :ref:`all`
- :ref:`break`
- :ref:`by`
- :ref:`case`
- :ref:`class`
- :ref:`create`
- :ref:`critical`
- :ref:`default`
- :ref:`do`
- :ref:`else`
- :ref:`end`
- :ref:`every`
- :ref:`fail`
- :ref:`global`
- :ref:`if`
- :ref:`import`
- :ref:`initial`
- :ref:`initially`
- :ref:`invocable`
- :ref:`link`
- :ref:`local`
- :ref:`method`
- :ref:`next`
- :ref:`not`
- :ref:`of`
- :ref:`package`
- :ref:`procedure`
- :ref:`record`
- :ref:`repeat`
- :ref:`return`
- :ref:`static`
- :ref:`suspend`
- :ref:`then`
- :ref:`thread`
- :ref:`to`
- :ref:`until`
- :ref:`while`
--------
.. index:: reserved words; action, emphatics, actions
.. _action:
.. _emphatic:
.. _emphatics:
Unicon action words
===================
Once again, all these reserved words are *expressions* and need to be
thought of that way for effective Unicon programming. *Verb* might be an
appropriate name for these action words, but the term used in this
document is *emphatic*.
.. index:: !break
.. _break:
break
-----
Exit the nearest enclosing loop and evaluate optional expression.
**break** [|expr|]
If the |expr| includes ``break``, multiple levels of nesting can be exited.
For instance; ``break break next`` will exit two inner loop levels and restart
at the top of an outer loop. The example below breaks out of one level,
jumping to the top of the outer.
.. literalinclude:: examples/break.icn
:language: unicon
:emphasize-lines: 11, 14
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/break.icn`
Sample run:
.. command-output:: unicon -s break.icn -x
:cwd: examples
--------
.. index:: !case
.. _case:
case
----
| **case** |expr| **of** {
| |expr1|: |expr2|
| ...
| |expr3|: |expr4|
| [**default**: |exprn|]
| }
A selection expression where |expr| is compared to the compound expressions
that follow. First matching |expr1| (or |expr3|, etc) will evaluate and
produce the resulting value of the paired |expr2|. If no matches are found,
the optional ``default`` |exprn| case is evaluated. With no ``default`` and
no comparison matches, the ``case`` expression will fail.
.. literalinclude:: examples/case.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/case.icn`
Sample run:
.. command-output:: unicon -s case.icn -x
:cwd: examples
See :ref:`default`, :ref:`of`.
--------
.. index:: !create
.. _create:
create
------
**create** |expr| : *co-expression*
Create a co-expression for |expr|. Not evaluated until explicitly
requested by the invocation operation or :ref:`spawn` function.
Co-expressions are a very powerful feature of Unicon. A feature that is still
missing from many other programming languages.
See :ref:`@ ` for information on co-expression activation.
.. literalinclude:: examples/create.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/create.icn`
Giving:
.. program-output:: unicon -s create.icn -x
:cwd: examples
--------
.. index:: !critical
.. _critical:
critical
--------
**critical** ``mtx : expr`` is the equivalent of :ref:`lock`\ (mtx); |expr| ;
:ref:`unlock`\ (mtx), where ``mtx`` is a mutual exclusion facility control for
structure ``x`` defined with ``mtx :=`` :ref:`mutex`\ ``(x)``.
This allows for more concise control of critical memory sections when
multi-threading.
For instance:
.. sourcecode:: unicon
x := x + 1
That code is actually at risk in threaded applications. It is a three step
process. Fetch x, increment x, assign to x. Multiple threads, without
synchronization might all evaluate the *fetch x* part of those steps at the
same time. The final result would be incorrect.
.. sourcecode:: unicon
critical mtx : x := x + 1
That code will ensure that each thread is allowed to complete all steps of the
expression before allowing another thread access to the critical section.
.. literalinclude:: examples/critical.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/critical.icn`
Giving:
.. program-output:: unicon -s critical.icn -x
:cwd: examples
--------
.. index:: !every
.. _every:
every
-----
**every** |expr1| [**do** |expr2|]
``every`` will produce all values from a generator. Internally, the ``every``
statement always fails, causing |expr1| to be resumed for more results. An
optional :ref:`do` |expr2| will be evaluated before each resumption of |expr1|.
``every`` is a one of the work horse words in Unicon.
Of note: :ref:`while` and :ref:`every` play different roles in Unicon, and
the distinction can be confusing at first, especially when dealing with
:ref:`read`.
.. sourcecode:: unicon
every read() do stuff()
while read() do stuff()
That first expression is wrong, or at least it may not do what a new Unicon
programmer expects. *No Unicon expression that compiles is ever wrong, it
just does what it is told.* The example below reads its own source, to
highlight the effect:
.. literalinclude:: examples/every.icn
:language: unicon
.. only:: html
.. rst-class:: rightalign
:download:`examples/every.icn`
Sample run:
.. command-output:: unicon -s every.icn -x
:cwd: examples
There is an alternative to ``while`` and ``read``. The *generate elements*
operator :ref:`\! `, can be used with the file type, so it becomes
``every`` friendly. No ``read`` is called (which returns a result, that
return satisfies the every). With the :ref:`\! ` operators, Unicon files
generate the elements within the resource.
.. literalinclude:: examples/every-filegen.icn
:language: unicon
.. only:: html
.. rst-class:: rightalign
:download:`examples/every-filegen.icn`
Every with a file content generator:
.. command-output:: unicon -s every-filegen.icn -x
:cwd: examples
The conceptual difference between :ref:`return` and :ref:`suspend` is an
important one for all Unicon programmers to understand. It can take a few
practice runs before it sinks in. *Traditional* programming experiences can
make this even harder, and you may find yourself slipping back without
realizing it from time to time; before an initial program run reminds you of
the difference. No harm done, *as long as alpha testing is always part and
parcel of your Unicon development cycle*. Unicon has features that set it
apart from most other programming languages. It is important to *think Unicon*
to take maximum advantage of these features.
``every`` feeds on generators by forcing the goal-directed evaluation engine
to keep trying alternatives. If the expression completes (either by returning
a result (with :ref:`return`) or failing, ``every`` terminates. ``while`` on
the other hand, is a procedural expression with a more traditional flow
control paradigm. Unicon allows for both methods of loop management.
.. seealso::
:ref:`repeat`, :ref:`suspend`, :ref:`until`, :ref:`while`,
:ref:`break`, :ref:`next`, :ref:`return`, :ref:`\! `
--------
.. index:: !fail
.. _fail:
fail
----
**fail** causes the enclosing procedure or method to terminate immediately and
produce no result, signalling failure. The invocation may not be resumed.
``fail`` is equivalent to ``return &fail``.
.. literalinclude:: examples/fail.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/fail.icn`
Sample run:
.. program-output:: unicon -s fail.icn -x
:cwd: examples
--------
.. index:: !if
.. _if:
if
--
**if** |expr1| **then** |expr2| [**else** |expr3|]
The Unicon ``if`` emphatic expression evaluates |expr1| and if it succeeds,
proceeds to evaluate |expr2|, given after the ``then`` reserved word. If the
optional ``else`` clause is present, then a failed |expr1| will proceed to
evaluate |expr3| instead. The overall result is either failure, or the result
of |expr2| (or |expr3|) depending on the outcome of |expr1|.
.. literalinclude:: examples/if.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/if.icn`
Giving:
.. command-output:: unicon -s if.icn -x
:cwd: examples
--------
.. index:: !initial
.. _initial:
initial
-------
An **initial** |expr| is only evaluated the first time a procedure is entered.
Mainly used for initializing :ref:`static` variables, when an initializing
expression is not sufficient. Initializing a :ref:`local` variable in an
initial expression is usually not desirable, ``local`` variables will lose the
value before second entry.
.. literalinclude:: examples/initial.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/initial.icn`
Giving:
.. program-output:: unicon -s initial.icn -x
:cwd: examples
See :ref:`procedure`, :ref:`static`.
--------
.. index:: !initially
.. _initially:
initially
---------
**initially** ``[(parameters)]`` creates a special method that is invoked when
an object is instantiated. If the ``initially`` section has declared
parameters, they are used as the parameters of the constructor for the objects
of that class.
.. literalinclude:: examples/initially.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/initially.icn`
Giving:
.. program-output:: unicon -s initially.icn -x
:cwd: examples
--------
.. index:: !next
.. _next:
next
----
The **next** expression forces a loop to immediately skip to its next
iteration, bypassing any subsequent code that makes up the remainder of the
loop body. Control flow returns to the beginning of the loop expression.
.. literalinclude:: examples/next.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/next.icn`
Giving:
.. command-output:: unicon -s next.icn -x
:cwd: examples
.. seealso::
:ref:`break`, :ref:`every`, :ref:`repeat`, :ref:`suspend`,
:ref:`while`, :ref:`until`
--------
.. index:: !not
.. _not:
not
---
**not** |expr| reverses the success or failure status of |expr|.
Some care must be shown when using ``not`` in Unicon. It is reversing the
sense of success and failure, not acting as a boolean operator as used with
most other programming languages. For instance:
.. sourcecode:: unicon
not 1
Does not produce a zero, as might be assumed by a C programmer, it signals
failure, reversing the successful ``1`` result to fail.
See the bitwise functions :ref:`iand`, :ref:`icom`, :ref:`ior`, :ref:`ishift`,
and :ref:`ixor` when bit twiddling is called for.
**icom(i)** would be the closest Unicon equivalent to **not** in most boolean
value control flow languages.
.. sourcecode:: unicon
not &fail
The code above will produce a *successful* :ref:`&null` result.
.. literalinclude:: examples/not.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/not.icn`
Giving:
.. command-output:: unicon -s not.icn -x
:cwd: examples
--------
.. index:: !repeat
.. _repeat:
repeat
------
**repeat** ``expr`` introduces and infinite loop of |expr|. It is up to an
element inside |expr| to exit the loop or terminate the program.
.. literalinclude:: examples/repeat.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/repeat.icn`
Sample run:
.. program-output:: unicon -s repeat.icn -x
:cwd: examples
.. seealso::
:ref:`every`, :ref:`return`, :ref:`suspend`, :ref:`until`, :ref:`while`,
:ref:`break`, :ref:`next`, :ref:`exit`, :ref:`stop`
--------
.. index:: !return
.. _return:
return
------
**return** ``[expr]`` exits a procedure or method, producing |expr| as its
result. The invocation may not be resumed. Default value for the optional
|expr| is :ref:`&null`.
.. literalinclude:: examples/return.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/return.icn`
Sample run:
.. command-output:: unicon -s return.icn ; ./return ; echo "shell status: $?"
:cwd: examples
:shell:
.. seealso:: :ref:`procedure`, :ref:`method`, :ref:`suspend`
--------
.. index:: !suspend
.. _suspend:
suspend
-------
**suspend** |expr| [ **do** |expr1| ]
Suspend a value from a procedure. When resumed, the optional do |expr1| will
be evaluated before control passes to resumption point. If |expr| is a
generator, resumption of the suspended procedure will resume the generator and
suspend again with the result produced.
.. literalinclude:: examples/suspend-sample.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/suspend-sample.icn`
Sample run
.. command-output:: unicon -s -t suspend-sample.icn -x
:cwd: examples
.. seealso::
:ref:`every`, :ref:`repeat`, :ref:`until`, :ref:`while`,
:ref:`break`, :ref:`next`
--------
.. index:: !thread
.. _thread:
thread
------
**thread** ``expr`` is equivalent to ``spawn(create expr)``. Create a thread
and then start it running. The thread handle is produced, of type
:ref:`co-expression`.
Threading in Unicon is a wide ranging topic, best described by the feature
author, :ref:`jafar`, in Unicon technical report UTR14,
http://unicon.org/utr/utr14.pdf. This document is also shipped with the
Unicon sources as ``doc/utr/utr14.docx``
A small producer/consumer example:
.. literalinclude:: examples/thread.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/thread.icn`
Sample run:
.. command-output:: unicon -s thread.icn -x
:cwd: examples
.. seealso::
:ref:`create`, :ref:`spawn`, :ref:`wait`
--------
.. index:: !to
.. _to:
to
--
|expr1| **to** |expr2| [ **by** |expr3| ] produces the integer sequence
from |expr1| to |expr2| with an optional step value given by |expr3|. The
default step is ``1``.
The initial value of |expr1| is always included in the sequence. The last
result will be less than or equal to |expr2|, depending on the step size of
|expr3|.
.. literalinclude:: examples/to.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/to.icn`
Giving:
.. command-output:: unicon -s to.icn -x
:cwd: examples
.. seealso::
:ref:`by`
--------
.. index:: !until
.. _until:
until
-----
**until** |expr1| [ **do** |expr2| ] loops until |expr1| succeeds
evaluating the optional |expr2| after each test.
.. literalinclude:: examples/until.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/until.icn`
Giving:
.. program-output:: unicon -s until.icn -x
:cwd: examples
.. seealso::
:ref:`every`, :ref:`repeat`, :ref:`while`, :ref:`break`, :ref:`next`
--------
.. index:: !while
.. _while:
while
-----
**while** |expr1| [ **do** |expr2| ] loops until |expr1| fails, evaluating
the optional |expr2| after each test.
.. literalinclude:: examples/while.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/while.icn`
Sample run:
.. program-output:: unicon -s while.icn -x
:cwd: examples
.. seealso::
:ref:`every`, :ref:`repeat`, :ref:`until`, :ref:`break`, :ref:`next`
--------
.. index:: reserved words; declarative, declaratives
.. _declarative:
.. _declaratives:
Declarative expressions
=======================
A fair number of the Unicon reserved words are of a declarative nature:
- informing the compiler how defined items are to be treated
- for the creation of program elements at compile time
- and other construction details.
.. index:: !abstract
.. _abstract:
abstract
--------
A :ref:`method` attribute. Allows a :ref:`class` hierarchy to include methods
that must be defined lower in the inheritance chain.
**abstract** methods have no implementation at the point of declaration. A
sub-class is responsible for defining implementation code.
.. blockdiag::
blockdiag {
orientation = portrait
super-class [label="super-class\nabstract declaration", style=dashed];
cat [label="cat\nabstract definition"];
dog [label="dog\nabstract definition"];
super-class -> cat;
super-class -> dog -> fido;
}
.. literalinclude:: examples/abstract.icn
:language: unicon
:start-after: ##+
:emphasize-lines: 12,20
.. only:: html
.. rst-class:: rightalign
:download:`examples/abstract.icn`
Sample run:
.. program-output:: unicon -s abstract.icn -x
:cwd: examples
See :ref:`gui` by :ref:`Parlett` for more practical examples of ``abstract``
methods when designing graphical user interface hierarchies.
--------
.. index:: !class
.. _class:
class
-----
**class** *name* [*: superclass :...*]\ ``(attributes)`` is a Unicon object
programming declarative, defining *name*. *name* can then be used to create
instances of the class.
Class hierarchies are defined with an (optional) colon separated list of
parent classes.
``class`` definitions include
- :ref:`abstract` method declarations
- :ref:`method` definitions
- :ref:`initially` attribute initialization blocks
``class`` definitions end with the :ref:`end` reserved word.
.. literalinclude:: examples/class.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/class.icn`
Sample run:
.. program-output:: unicon -s class.icn -x
:cwd: examples
See :ref:`abstract` for more details on the code sample.
In Unicon, ``class`` can be compared to :ref:`record`; declaring a name that
is used later to define storage. Object oriented programming is orders of
magnitude more powerful, encapsulating code along with the data values, all
within an inheritance hierarchy.
--------
.. index:: !global
.. _global:
global
------
**global** var [, var...] declares one or more global variables. Placed
outside of procedures, the named variables will be accessible inside all
procedures and methods of the program.
.. literalinclude:: examples/global.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/global.icn`
Giving:
.. program-output:: unicon -s global.icn -x
:cwd: examples
.. seealso:: :ref:`scope`, :ref:`local`, :ref:`static`
--------
.. index:: !import
.. _import:
import
------
**import** ``package [package...]`` imports one or more packages. This is a
programming in the large Unicon feature. Using packages relies on an external
databases, ``uniclass`` to help manage the namespace resolution.
.. literalinclude:: examples/import.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/import.icn`
Sample run:
.. command-output:: unicon import.icn -x
:cwd: examples
.. seealso:: :ref:`package`
--------
.. index:: !invocable
.. _invocable:
invocable
---------
**invocable** ``[all | name,...]`` allow string invocation of the given
name(s), or any procedure with the :ref:`all` reserved word.
.. literalinclude:: examples/invocable.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/invocable.icn`
Sample run will end in a purposeful error, "other" is not set invocable.
.. command-output:: unicon -s invocable.icn -x
:cwd: examples
:returncode: 1
--------
.. index:: !link
.. _link:
link
----
**link** ``library [, library...]`` links other ucode into this program.
Unicon ucode files are created with ``unicon -c source.icn``. This option
creates a library that can be linked into other programs.
The :ref:`ipl` provides hundreds of ready to link library procedures.
.. literalinclude:: examples/link.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/link.icn`
Sample run:
.. command-output:: unicon -s link.icn -x
:cwd: examples
.. command-output:: unicon -s -t link.icn -x
:cwd: examples
.. seealso:: :doc:`ipl`
--------
.. index:: !local
.. _local:
local
-----
**local** ``var [, var...]`` declares one or more variable as being within
local scope of a procedure or method body.
.. literalinclude:: examples/local.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/local.icn`
Sample run:
.. program-output:: unicon -s local.icn -x
:cwd: examples
--------
.. index:: !method
.. _method:
method
------
**method** ``name`` declares a method within a :ref:`class`. An object
oriented feature of Unicon.
.. literalinclude:: examples/method.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/method.icn`
Sample run:
.. command-output:: unicon -s method.icn -x
:cwd: examples
--------
.. index:: !package
.. _package:
package
-------
**package** ``name`` declares the current source unit to be part of a package,
``name``. This defines a namespace for all procedures within the package.
Package access uses the :ref:`import` reserved word.
Packages are a programming in the large feature of Unicon. Using packages
relies on an external database, ``uniclass``, that manages the namespace
resolution.
.. literalinclude:: examples/package.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/package.icn`
Giving:
.. command-output:: unicon package.icn
:cwd: examples
.. command-output:: unicon -c package.icn
:cwd: examples
.. seealso:: :ref:`import`
--------
.. index:: !procedure
.. _procedure:
procedure
---------
**procedure** ``name([parameter,...])`` defines a procedure. The body of the
procedure continues until the :ref:`end` reserved word is encountered.
User defined procedures are semantically equivalent to built in functions;
they always produce a value or fail. Results can be :ref:`suspend`\ ed, or
:ref:`return`\ ed.
.. literalinclude:: examples/procedure.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/procedure.icn`
Sample run:
.. command-output:: unicon -s procedure.icn -x Determination
:cwd: examples
.. index:: main
.. _main:
main
....
Unless compiled with ``-c`` or similar, *all* Unicon programs require a
``procedure main()``. The program will be given a list of command line
arguments, but that usage is optional.
.. sourcecode:: unicon
procedure main() ... end
procedure main(arglist) ... end
Are both valid Unicon main procedures.
For any given program there can only be one ``main``. The operating system is
given a zero result, even if :ref:`return` is used at the end of main. Other
platform dependent status codes can be set with the :ref:`exit` function.
Error status will be set when the :ref:`stop` function is used or when an
unconverted runtime error abnormal end occurs.
--------
.. index:: !record
.. _record:
record
------
**record** ``name(field,...)`` defines a record constructor for ``name`` with
one or more fields.
.. literalinclude:: examples/record.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/record.icn`
Giving:
.. program-output:: unicon -s record.icn -x
:cwd: examples
Trivia: ``record`` can be used to form the shortest known complete Unicon
program.
.. sourcecode:: unicon
procedure main()
end
That is not the shortest valid program. This is:
.. sourcecode:: unicon
record main()
The shortest known complete program.\ [#short]_ It creates a ``record``
constructor that just happens to provide the ``main`` procedure, a required
element of all valid Unicon programs. When executed the program evaluates the
constructor expression, creating a record of type ``main`` and terminates
normally.
.. [#short]
From the Icon Newsletter, Programming Corner, Issue 32 (question), and 33
(answer).
--------
.. index:: !static
.. _static:
static
------
**static** ``var [, var...]`` declares local procedure or method variables to
be persistent while still local. Values will be remembered between
invocations during the entire program execution.
.. literalinclude:: examples/static.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/static.icn`
Giving:
.. command-output:: unicon -s static.icn -x
:cwd: examples
--------
.. index:: reserved words; syntax
Syntax reserved words
=====================
Unicon also includes a few reserved words that work together with some of the
other :ref:`emphatic` and :ref:`declarative` words.
.. index:: !all
.. _all:
all
---
**all** is an optional clause for the :ref:`invocable` declarative. Used to
allow string invocation and any user defined procedure or built-in function.
See :ref:`invocable` for more details.
--------
.. index:: !by
.. _by:
by
--
**by** is an optional step value clause for the :ref:`to` expression.
See :ref:`to` for more details. The default ``by`` step is ``1``.
--------
.. index:: !default
.. _default:
default
-------
**default** specifies a default case for the :ref:`case` ``of`` selection
expression.
See :ref:`case` for more details.
--------
.. index:: !do
.. _do:
do
--
**do** is an optional clause to specify the expression to be executed for each
iteration of a loop construct.
See :ref:`every`, :ref:`suspend` :ref:`while` for more details. *And, yes,*
``suspend`` *counts as a looping construct.*
--------
.. index:: !end
.. _end:
end
---
**end** is the reserved word to signify the end of a :ref:`class`,
:ref:`method`, or :ref:`procedure` body.
--------
.. index:: !else
.. _else:
else
----
**else** evaluates an alternative |expr3| when an :ref:`if` |expr1| fails to produce
a result. An ``else`` clause is optional with an ``if`` statement.
``if`` |expr1| ``then`` |expr2| ``else`` |expr3|
See: :ref:`if`, :ref:`then` for more details.
--------
.. index:: !of
.. _of:
of
--
**of** is the reserved word that precedes a special compound expression
consisting of a sequence of :ref:`case` branches.
``case`` |expr| ``of`` ...
See: :ref:`case`, :ref:`default` for more details.
--------
.. index:: !then
.. _then:
then
----
**then** expressions are executed when (and only when) an :ref:`if` |expr|
produces a result. The ``then`` reserved word is not optional when
constructing a valid ``if`` expression.
See: :ref:`if` and :ref:`else` for more details.
.. only:: html
..
--------
:ref:`genindex` | Previous: :doc:`operators` | Next: :doc:`functions`
|