Tutorials Point


  Euphoria Basics
  Euphoria Useful References
  Euphoria Useful Resources
  Selected Reading

Copyright © 2014 by tutorialspoint



  Home     References     Discussion Forums     About TP  

Euphoria - Quick Guide


previous next AddThis Social Bookmark Button


Advertisements

Euphoria Overview:

Euphoria is a free, simple, flexible, easy to learn, and interpreted but extremely fast 32-bit high-level programming language for DOS, Windows, Linux, FreeBSD and more.

Euphoria's first incarnation was created by Robert Craig on an Atari Mega-ST and it was first released in 1993. It is now maintained by Rapid Deployment Software.

Euphoria stands for End-User Programming with Hierarchical Objects for Robust Interpreted Applications.

Euphoria Features:

Here is the list of major features supported by Euphoria:

  • A remarkably simple, flexible, powerful language definition that is easy to learn and use.

  • Supports dynamic storage allocation which means variables grow or shrink without the programmer having to worry about allocating and freeing of memory. This means it takes care of garbage collection automatically.

  • Extremely faster than conventional interpreters such as Perl and Python.

  • Euphoria programs run under Linux, FreeBSD, 32-bit Windows, and any DOS environment, and are not subject to any 640K memory limitations.

  • Provides an optimizing Euphoria To C Translator which you can use to translate your Euphoria program into C and then compile it with a C compiler to get an executable (.exe) file. This can boost your program speed often by a factor of 2x to 5x.

  • Underlying hardware are completely hidden which means programs are not aware of word-lengths, underlying bit-level representation of values, byte-order etc.

  • Euphoria installation comes along with a full-screen source debugger, an execution profiler and a full-screen multi-file editor.

  • Supports run-time error-handling, subscript and type checking.

  • Euphoria is completely free and open source.

Euphoria Environment:

Euphoria programs run under Linux, FreeBSD, OS X, 32-bit Windows, and any DOS environment, and are not subject to any 640K memory limitations.

You can download latest version of Euphoria from the following link for your suitable environment:

Download Euphoria.

Euphoria Interpreters:

  • Depending on the platform you are using, Euphoria has multiple interpreters, the main one being eui.

  • On windows platforms you have two choices. If you run eui then a console window is created. If you run euiw then no console is created, making it suitable for GUI applications.

  • Euphoria doesn't care about your choice of file extensions. By convention, however, console-based applications have an extension of .ex.

  • GUI-based applications have an extension of .exw and include files have an extension of .e.

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!

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.

andexitoverride
asexportprocedure
breakfallthrupublic
byforretry
casefunctionreturn
constantglobalroutine
continuegotoswitch
doifthen
elseifdefto
elsedefincludetype
elsiflabeluntil
elsifdefloopwhile
endnamespacewith
entrynotwithout
enumorxor

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 notationDescription
\nNewline
\rCarriage return
\tTab
\\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!

Variable Declaration:

Euphoria variables have to be explicitly declared to reserve memory space. Thus declaration of a variable is mandatory before you assign a value to a variable.

Variable declarations have a type name followed by a list of the variables being declared. For example:

integer x, y, z

sequence a, b, x

Assigning Values:

The equal sign (=) is used to assign values to variables. The operand to the left of the = operator is the name of the variable, and the operand to the right of the = operator is the value stored in the variable. For example:

#!/home/euphoria/bin/eui

-- Here is the declaration of the variables.
integer counter
integer miles
sequence name

counter = 100          -- An integer assignment
miles   = 1000.0       -- A floating point
name    = "John"       -- A string ( sequence )

printf(1, "Value of counter %d\n", counter )
printf(1, "Value of miles %f\n", miles )
printf(1, "Value of name %s\n", {name} )

Here 100, 1000.0 and "John" are the values assigned to counter, miles and name variables, respectively. While running this program, this will produce following result:

Value of counter 100
Value of miles 1000.000000
Value of name John

Euphoria Constants:

Constants are also variables that are assigned an initial value that can never change. Euphoria allows to define constants using constant keyword as follows:

constant MAX = 100
constant Upper = MAX - 10, Lower = 5
constant name_list = {"Fred", "George", "Larry"}

The enums:

An enumerated value is a special type of constant where the first value defaults to the number 1 and each item after that is incremented by 1. Enums can only take numeric values.

Examples:

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

enum ONE, TWO, THREE, FOUR

printf(1, "Value of ONE %d\n", ONE )
printf(1, "Value of TWO %d\n", TWO )
printf(1, "Value of THREE %d\n", THREE )
printf(1, "Value of FOUR %d\n", FOUR )

This will produce following result:

Value of ONE 1
Value of TWO 2
Value of THREE 3
Value of FOUR 4

Euphoria Data Types:

Euphoria integers:

Euphoria integer data types store numeric values. They are declared and defined as follows:

integer var1, var2

var1 = 1
var2 = 100

The variables declared with type integer must be atoms with integer values from -1073741824 to +1073741823 inclusive. You can perform exact calculations on larger integer values, up to about 15 decimal digits, but declare them as atom, rather than integer.

Euphoria atoms:

atom var1, var2, var3

var1 = 1000
var2 = 198.6121324234
var3 = 'E'       

The atoms can range from approximately -1e300 to +1e300 with 15 decimal digits of accuracy. An individual character is an atom which must may be entered using single quotes. For example, all the following are legal:

-- Following is equivalent to the atom 66 - the ASCII code for B
char = 'B'

-- Following is equivalent to the sequence {66}
sentence = "B"

Euphoria sequences:

sequence var1, var2, var3, var4

var1 = {2, 3, 5, 7, 11, 13, 17, 19}
var2 = {1, 2, {3, 3, 3}, 4, {5, {6}}}
var3 = {{"zara", "ali"}, 52389, 97.25}     
var4 = {} -- the 0 element sequence

A character string is just a sequence of characters which may be entered using double quotes. For example, all the following are legal:

word = 'word'
sentence = "ABCDEFG"

Character strings may be manipulated and operated upon just like any other sequences. For example the above string is entirely equivalent to the sequence:

sentence = {65, 66, 67, 68, 69, 70, 71}

You will learn more about sequence in Euphoria - Sequences.

Euphoria Objects:

object var1, var2, var3

var1 = {2, 3, 5, 7, 11, 13, 17, 19}
var2 = 100
var3 = 'E'     

An object may have one of the following values:

  • a sequence

  • an atom

  • an integer

  • an integer used as a file number

  • a string sequence, or single-character atom

Euphoria Operators

OperatorDescriptionExample
+Addition - Adds values on either side of the operator A + B will give 30
-Subtraction - Subtracts right hand operand from left hand operand A - B will give -10
*Multiplication - Multiplies values on either side of the operator A * B will give 200
/Division - Divides left hand operand by right hand operand B / A will give 2
+Unary plus - This has no impact on the variable value. +B gives 20
-Unary minus - This creates a negative value of the given variable. -B gives -20
= Checks if the value of two operands are equal or not, if yes then condition becomes true. (A = B) is not true.
!= Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true.
> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.
< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.
and Called Logical AND operator. If both the operands are non zero then then condition becomes true. (A and B) is false.
orCalled Logical OR Operator. If any of the two operands are non zero then then condition becomes true. (A or B) is true.
xorCalled Logical XOR Operator. Condition is true if one of them is true, if both operands are true or false then condition becomes false. (A xor B) is true.
notCalled Logical NOT Operator which negates the result. Using this operator, true becomes false and false becomes true not(B) is true.
=Simple assignment operator, Assigns values from right side operands to left side operand C = A + B will assigne value of A + B into C
+=Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A
-=Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand C -= A is equivalent to C = C - A
*=Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand C *= A is equivalent to C = C * A
/=Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand C /= A is equivalent to C = C / A
&=Concatenation operator C &= {2} is same as C = {C} & {2}

Precedence of Euphoria Operators:

Category  Operator Associativity 
Postfix function/type calls  
Unary  + - ! not Right to left 
Multiplicative   * / Left to right 
Additive  + -  Left to right 
Concatenation  &   Left to right 
Relational  > >= < <=   Left to right 
Equality   = !=  Left to right 
Logical AND and  Left to right 
Logical OR  or  Left to right 
Logical XOR  xor  Left to right 
Comma  Left to right 

The if Statement:

An if statement consists of a boolean expression followed by one or more statements.

Syntax:

The syntax of an if statement is:

if expression then
   -- Statements will execute if the expression is true
end if

The switch Statement:

The switch statement is used to run a specific set of statements, depending on the value of an expression. It often replaces a set of if-elsif statements giving you more control and readability of your program.

Syntax:

The syntax of simple switch statement is:

switch expression do
   case <val> [, <val-1>....] then
      -- Executes when the expression matches one of the values  
   case <val> [, <val-1>....] then
      -- Executes when the expression matches one of the values  
   .....................
   case else
      -- Executes when the expression does not matches any case.  
end if

The ifdef Statement:

The ifdef statement is executed at parse time not runtime. This allows you to change the way your program operates in a very efficient manner.

Syntax:

The syntax of ifdef statement is:

ifdef macro then
   -- Statements will execute if the macro is defined.
end if

The while statement:

A while loop is a control structure that allows you to repeat a task a certain number of times.

Syntax:

The syntax of a while loop is:

while expression do
   -- Statements executed if expression returns true
end while

The loop until statement:

A loop...until loop is similar to a while loop, except that a loop...until loop is guaranteed to execute at least one time.

Syntax:

The syntax of a loop...until is:

loop do
   -- Statements to be executed.
until expression

The for statement:

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Syntax:

The syntax of a for loop is:

for "initial value" to "last value" by "inremental value" do
   -- Statements to be executed.
end for

The exit statement:

Exiting a loop is done with the keyword exit. This causes flow to immediately leave the current loop and recommence with the first statement after the end of the loop.

Syntax:

The syntax of an exit statement is:

exit [ "Label Name" ] [Number]

The break statement:

The break statement works exactly like the exit statement, but applies to if statements or switch statements rather than to loop statements of any kind.

Syntax:

The syntax of an break statement is:

break [ "Label Name" ] [Number]

The continue statement:

The continue statement continues execution of the loop it applies to by going to the next iteration and skipping the rest of an iteration.

Going to the next iteration means testing a condition variable index and checking whether it is still within bounds.

Syntax:

The syntax of an continue statement is:

continue [ "Label Name" ] [Number]

The retry statement:

The retry statement continues execution of the loop it applies to by going to the next iteration and skipping the rest of an iteration.

Syntax:

The syntax of an retry statement is:

retry [ "Label Name" ] [Number]

The goto statement:

The goto statement instructs the computer to resume code execution at a labeled place.

The place to resume execution is called the target of the statement. It is restricted to lie in the current routine, or the current file if outside any routine.

Syntax:

The syntax of an goto statement is:

goto "Label Name"

Preocedure Definition:

Before we use a procedure we need to define that procedure. The most common way to define a procedure in Euphoria is by using the procedure keyword, followed by a unique procedure name, a list of parameters (that might be empty), and a statement block which ends with end procedure statement. The basic syntax is shown here:

procedure procedurename(parameter-list)

  statements
  ..........

end procedure

Function Definition:

Before we use a function we need to define that function. The most common way to define a function in Euphoria is by using the function keyword, followed by a unique function name, a list of parameters (that might be empty), and a statement block which ends with end function statement. The basic syntax is shown here:

function functionname(parameter-list)

  statements
  ..........
  return [Euphoria Object]

end function


previous next Printer Friendly

Advertisements


  

Advertisements