Copyright © tutorialspoint.com

# Euphoria - Basic Syntax

The Euphoria language has many similarities to Perl, C, and Java. However, there are some definite differences between the languages. This chapter is designed to quickly get you up to speed on the syntax that is expected in Euphoria.

This tutorial assumes you are working with Linux and all the examples have been written on Linux platform. But I did not observe any difference in program syntax on Linux and WIN32, so you can follow the same steps on WIN32.

# First Euphoria Program:

Let us write a simple Euphoria program in a script. So put the following source code in a test.ex file and save it.

    #!/home/euphoria-4.0b2/bin/eui

    puts(1, "Hello, Euphoria!\n")

Assuming, Euphoria interpreter is available in /home/euphoria-4.0b2/bin/ directory. Now try to run this program as follows:

    $ chmod +x test.ex    # This is to make file executable
    $ ./test.ex

This will produce following result:

    Hello, Euphoria!

This script used a Euphoria built-in function puts() which takes two arguments. First argument indicates file name or device number and second argument indicates a string which you want to print. Here 1 indicates STDOUT device.

# Euphoria Identifiers:

A Euphoria identifier is a name used to identify a variable, function, class, module, or other object. An identifier starts with a letter A to Z or a to z and then be followed by letters, digits or underscores.

Euphoria does not allow punctuation characters such as @, $, and % within identifiers.

Euphoria is a case sensitive programming language. Thus Manpower and manpower are two different identifiers in Euphoria. Examples of valid identifiers:

- n
- color26
- ShellSort
- quick_sort
- a_very_long_indentifier

# Reserved Words:

The following list shows the reserved words in Euphoria. These reserved words may not be used as constant or variable or any other identifier names.

Keywords contain lowercase letters only.

and | exit | override
as | export | procedure
break | fallthru | public
by | for | retry
case | function | return
constant | global | routine
continue | goto | switch
do | if | then
else | ifdef | to
elsedef | include | type
elsif | label | until
elsifdef | loop | while
end | namespace | with
entry | not | without
enum | or | xor

# Expressions:

Euphoria lets you calculate results by forming expressions. However, in Euphoria you can perform calculations on entire sequences of data with one expression.

You can handle a sequence much as you would a single number. It can be copied, passed to a subroutine, or calculated upon as a unit. For example:

    {1,2,3} + 5

is an expression that adds the sequence {1,2,3} and the atom 5 to get the resulting sequence {6,7,8}. You would learn sequences in subsequent chapters.

# Blocks of code:

One of the first caveats programmers encounter when learning Euphoria is the fact that there are no braces to indicate blocks of code for procedure and function definitions or flow control. Blocks of code are denoted by associated keywords.

Following is the example of if...then...end if block:

    if condition then
       code block comes here
    end if

# Multi-Line Statements:

Statements in Euphoria typically end with a new line. Euphoria does, however, allows to write a single statement in multiple lines. For example:

    total = item_one +
            item_two +
            item_three

# Escape Characters:

Escape characters may be entered using a back-slash. For example:

Following table is a list of escape or non-printable characters that can be represented with backslash notation.

Backslash notation | Description
--- | ---
\n | Newline
\r | Carriage return
\t | Tab
\\ | Backslash
\" | Double quote
\' | Single quote

# Comments in Euphoria:

Any comments are ignored by the compiler and have no effect on execution speed. It is advisable to use more comments in your program to make it more readable.

There are three forms of comment text:

1. Euphoria comments are started by two dashes and extend to the end of the current line.

2. The multi-line format comment is kept inside /*...*/, even if that occurs on a different line.

3. On the first line only of your program, you can use a special comment beginning with the two character sequence #!.

## Examples:

    #!/home/euphoria-4.0b2/bin/eui

    -- First comment
    puts(1, "Hello, Euphoria!\n") -- second comment

    /* This is a comment which extends over a number
    of text lines and has no impact on the program
    */

This will produce following result:

    Hello, Euphoria!

Note: On the first line (only) of your program, you can use a special comment beginning with #!. This informs the Linux shell that your file should be executed by the Euphoria interpreter.

Copyright © tutorialspoint.com