Euphoria – Basic Syntax


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 it is observed that there is no any prominent difference in program syntax on Linux and WIN32. Hence you can follow the same steps on WIN32.

First Euphoria Program

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

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

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

Let us say, Euphoria interpreter is available in /home/euphoria-4.0b2/bin/ directory. Now run this program as follows −

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

This produces the following result −

Hello, Euphoria!

This script used a 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 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. For example, the valid identifiers are −

  • 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. Euphoria 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 handle a single number. It can be copied, passed to a subroutine, or calculated upon as a unit. For example −

{1,2,3} + 5

This 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.

The following example shows 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, allow 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 −

The 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 −

  • Comments start by two dashes and extend to the end of the current line.

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

  • You can use a special comment beginning with the two character sequence “#!” only on the first line of the program.

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 produces the following result −

Hello, Euphoria!

Note − You can use a special comment beginning with “#!”. This informs the Linux shell that your file should be executed by the Euphoria interpreter.

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *