RPN in Euphoria


Euphoria is a small, practical programming language, known since 1993, and currently (2007) implemented on several operating systems.

Being small and simple, the language is easy to learn and use. Its area of applicability is known to include hobbyism, teaching programming, game programming, and mathematics and artificial intelligence programming.

Euphoria can be considered a scripting language, but it is a rather high-performing one. Like other scripting languages, its implementation is based on semi-interpretation: it parses to an intermediate language (IL) which is then executed. In order to make program distribution convenient, the IL can be bound to the interpreter, thus obtaining a single-file, stand-alone executable. It is also possible to translate IL into C and thus the program to be compiled. Even in interpreted mode, however, the execution speed of most Euphoria programs is good enough to rival a compiled language.

Any data item in Euphoria has the universal type object. An object is either an atom or a sequence. The atoms, in the current definition of the language, are in fact numbers, and a sequence is a dynamic collection of atoms and other sequences, i.e. of any kind of objects at all. As a special case of atom, the language provides integers.

User-specific datatypes can be defined by providing predicate functions of a specially designated kind. Such a function, actually ‘a type’ in the language's terminology, checks its argument and returns true to signify the presence of the corresponding value in the datatype. A type-function can be called directly or indirectly – as a type-defining word in variable or parameter declarations. Thus user-defined datatypes are in all respects equal to the predefined ones.

Other than their relation to datatypes, type-functions are ordinary functions, including they can be recursive. A possible use of this is implementing recursive and mutually recursive datatypes.

There is neither a character nor a string datatype per se in Euphoria, although syntax is provided for both character and string literals. The role of the characters is played by their ascii codes, and, correspondingly, Euphoria's ‘strings’ are just sequences of such codes.

It is up to the programmer to work with loosely or strictly typed data. If, for example, a certain parameter to a procedure is declared to be of type object, it means that in fact it has an unrestricted type. If that is not desired, a specific datatype can be used in the parameter declaration, and the value of that argument is correspondingly checked when the procedure is called.

Memory management in Euphoria is automatic, and the run-time system garbage-collects constantly in small pieces, so that it never causes random delays to the program execution.

Certain kinds of errors are handled at compile-time. It is notable of Euphoria that its implementation has very sensible compiler diagnostic – a not so common feature among languages. Violation of type or of subscript range, as well as other kinds of errors are checked at run-time.

One may observe certain similarities between Euphoria and Lisp, such as atoms and sequences (lists) being the two main kinds of data, as well as defining user datatypes as more or less arbitrary sets of values. There are, however, notable differences. Euphoria's sequence items can be efficiently and conveniently referenced by subscripts. Also, slices of sequences can be produced. A number of arithmetic and other operations can act on whole sequences item-wize, not unlike the way this is done in APL. For example, the expression

All control-defining units in Euphoria – subroutines, conditionals, loops etc. – are delineated from both sides by keywords. A while-loop, for example, is written while ... do ... end while. There are no block constructs other than the ones specific for the respective procedure, control etc. statements.

The language is accompanied by about 170 library procedures in a dozen or so groups, including:

Some of the library functions are readily accessible to the programmer, while others reside in includable modules. All function names can be redefined.

Links of Relevance

The Euphoria homepage. All related information can be found from there: online documentation, tutorials, archives, add-on libraries.