Euphoria Programming Language Tutorial PDF Version Quick Guide Resources Job Search Discussion This tutorial gives you basic understanding of Euphoria programming language. Euphoria is simple, flexible, easy to learn, and interpreted high-level programming language for DOS, Windows, Linux, FreeBSD, and more. This tutorial describes everything a programmer needs to know such as its environment, data types, syntax and operators, file handling, and controlling the flow of program. Audience This tutorial is designed for the aspiring students who are keen to learn and understand Euphoria in detail. This tutorial would be of great help for the IT professionals working as programmers. The enthusiastic readers can access this tutorial as a source of additional reading. Prerequisites Before proceeding with this tutorial, you need to have a basic knowledge of working on Windows or Linux. You need to be familiar with any programming language such as C, C++. You need to have sound understanding of operating system, memory allocation and de-allocation, and basics of efficient programming and debugging. Print Page Previous Next Advertisements ”;
Category: euphoria
Euphoria – Overview
Euphoria – Overview ”; Previous Next Euphoria stands for End-User Programming with Hierarchical Objects for Robust Interpreted Applications. 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. It 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 is being used to develop Windows GUI programs, high-speed DOS games, and Linux/FreeBSD X Windows programs. Euphoria can also be used for CGI (Webbased) programming. Euphoria Features Here is the list of major features of Euphoria − It is a simple, flexible, powerful language definition that is easy to learn and use. It supports dynamic storage allocation which means variables grow or shrink without the programmer having to worry about allocating and freeing the memory. It takes care of garbage collection automatically. It is extremely faster than conventional interpreters such as Perl and Python. Euphoria programs run under Linux, FreeBSD, 32-bit Windows, and any DOS environment. Euphoria programs are not subject to any 640K memory limitations. It 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 by 2 to 5 times. 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. It supports run-time error-handling, subscript, and type checking. It is an open source language and comes completely free of cost. Platform Requirements Euphoria is available on Windows, Linux, FreeBSD, and OSX. Here is the bare minimum version required with the following platforms − WIN32 version − You need Windows 95 or any later version of Windows. It runs fine on XP and Vista. Linux version − You need any reasonably up-to-date Linux distribution, that has libc6 or later. For example, Red Hat 5.2 or later works fine. FreeBSD version − You need any reasonably up-to-date FreeBSD distribution. Mac OS X version − You need any reasonably up-to-date Intel based Mac. Euphoria Limitations Here are some prominent limitations of Euphoria − Even though Euphoria is simple, fast, and flexible enough for the programmers; it does not provide call support for many important functionalities. For example, network programming. Euphoria was invented in 1993, and still you would not find any book written on this language. There is also not much documentation available for the language. But these days, the language is getting popular very fast and you can hope to have nice utilities and books available for the language very soon. Euphoria Licensing This product is free and open source, and has benefited from the contributions of many people. You have complete royalty-free rights to distribute any Euphoria programs that you develop. Icon files, such as euphoria.ico and binaries available in euphoriabin, may be distributed with or without your changes. You can shroud or bind your program and distribute the resulting files royalty-free. Some additional 3rd party legal restrictions might apply when you use the Euphoria- To-C translator. The generous Open Source License allows Euphoria to use for both personal and commercial purposes. Unlike many other open source licenses, your changes do not have to be made open source. Print Page Previous Next Advertisements ”;
Euphoria – Environment
Euphoria – Environment ”; Previous Next This chapter describes about the installation of Euphoria on various platforms. You can follow the steps to install Euphoria on Linux, FreeBSD, and 32-bit Windows. So you can choose the steps based on your working environment. Linux, Free BSD Installation Official website provides .tar.gz file to install Euphoria on your Linux or BSD OS. You can download your latest version of Euphoria from its official website − Download Euphoria. Once you have .tar.gz file, here are three simple steps to be performed to install Euphoria on your Linux or Free BSD machine − Step 1 − Installing Files Untar the downloaded file euphoria-4.0b2.tar.gz in a directory where you want to install Euphoria. If you want to install it in /home directory as follows, then − $cp euphoria-4.0b2.tar.gz /home $cd /home $gunzip euphoria-4.0b2.tar.gz $tar -xvf euphoria-4.0b2.tar This creates a directory hierarchy inside /home/euphoria-4.0b2 directory as follows − $ls -l -rw-r–r– 1 1001 1001 2485 Aug 17 06:15 Jamfile -rw-r–r– 1 1001 1001 5172 Aug 20 12:37 Jamrules -rw-r–r– 1 1001 1001 1185 Aug 13 06:21 License.txt drwxr-xr-x 2 1001 1001 4096 Aug 31 10:07 bin drwxr-xr-x 7 1001 1001 4096 Aug 31 10:07 demo -rw-r–r– 1 1001 1001 366 Mar 18 09:02 file_id.diz drwxr-xr-x 4 1001 1001 4096 Aug 31 10:07 include -rw-r–r– 1 1001 1001 1161 Mar 18 09:02 installu.doc drwxr-xr-x 4 1001 1001 4096 Aug 31 10:07 source drwxr-xr-x 19 1001 1001 4096 Sep 7 12:09 tests drwxr-xr-x 2 1001 1001 4096 Aug 31 10:07 tutorial NOTE − File name euphoria-4.0b2.tar.gz depends on latest version available. We are using 4.0b2 version of the language for this tutorial. Step 2 − Setting Up the Path After installing Euphoria, you need to set proper paths so that your shell can find required Euphoria binaries and utilities. Before proceeding, there are following three important environment variables you need to set up − Set PATH environment variable to point /home/euphoria-4.0b2/bin directory. Set EUDIR environment variable to point to /home/euphoria-4.0b2. Set EUINC environment variable to point to /home/euphoria-4.0b2/include. These variables can be set as follows − $export PATH=$PATH:/home/euphoria-4.0b2/bin $export EUDIR=/home/euphoria-4.0b2 $export EUINC=/home/euphoria-4.0b2/include NOTE − The above commands used to set environment variables may differ depending on your Shell. We used bash shell for executing these commands to set the variables. Step 3 − Confirmation Installation Confirm if you installed Euphoria successfully or not. Execute the following command − $eui -version If you get following result, then it means you have installed Euphoria successfully; otherwise you have to go back and check all the steps again. $eui -version Euphoria Interpreter 4.0.0 beta 2 (r2670) for Linux Using System Memory $ That is it, Euphoria Programming Environment is ready on your UNIX machine, and you can start writing complex programs in easy steps. WIN32, and DOS Installation Official website provides .exe file to install Euphoria on your WIN32 or DOS OS. You can download your latest version of Euphoria from its official website − Download Euphoria. Once you have .exe file, here are three simple steps to follow for installing Euphoria Programming language on your WIN32 or DOS machine − Step 1 − Installing Files Double click on the downloaded .exe setup program to install all the files. We downloaded euphoria-40b2.exe file for installation. The filename euphoria-40b2.exe depends on latest version available. We use version 4 beta 2 of the language. By default Euphoria would be installed in C:euphoria-40b2 directory but you can also select a desired location. Step 2 − Rebooting the Machine Re-boot your machine to complete the installation. Step 3 − Confirmation Installation Confirm if you installed Euphoria successfully or not. Execute the following command − c:>eui -version If you get following result, then it means you have installed Euphoria successfully; otherwise you have to go back and check all the steps again. c:>eui -version Euphoria Interpreter 4.0.0 beta 2 (r2670) for Windows Using Managed Memory c:> That is it, Euphoria Programming Environment is ready on your WIN32 machine, and you can start writing complex programs in easy steps. Euphoria Interpreters Depending on the platform you are using, Euphoria has multiple interpreters − The main interpreter is 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 does not care about your choice of file extensions. By convention however; the console-based applications come with .ex extension. GUI-based applications have .exw extension and the include files have .e extension. Print Page Previous Next Advertisements ”;
Euphoria – Procedures
Euphoria – Procedures ”; Previous Next A procedure is a group of reusable code which can be called from anywhere in your program. This eliminates the need of writing same code again and again. This helps programmers to write modular code. Like any other advance programming language, Euphoria also supports all the features necessary to write modular code using procedures. You must have seen procedures like printf() and length() in previous chapters. We are using these procedure again and again but they have been written in core Euphoria only once. Euphoria allows you to write your own procedures as well. This section explains how to write your own procedure in Euphoria. Procedure Definition Before you use a procedure, you need to define it. 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 as shown below − procedure procedurename(parameter-list) statements ………. end procedure Example A simple procedure called sayHello that takes no parameters is defined here − procedure sayHello() puts(1, “Hello there”) end procedure Calling a Procedure To invoke a procedure somewhere later in the script, you simply need to write the name of that procedure as follows − #!/home/euphoria-4.0b2/bin/eui procedure sayHello() puts(1, “Hello there”) end procedure — Call above defined procedure. sayHello() This produces the following result − Hello there Procedure Parameters Till now you have seen procedure without a parameter. But there is a facility to pass different parameters while calling a procedure. These passed parameters can be captured inside the procedure and any manipulation can be done over those parameters. A procedure can take multiple parameters separated by comma. Example Let us do a bit modification in our sayHello procedure. This time it takes two parameters − #!/home/euphoria-4.0b2/bin/eui procedure sayHello(sequence name,atom age) printf(1, “%s is %d years old.”, {name, age}) end procedure — Call above defined procedure. sayHello(“zara”, 8) This produces the following result − zara is 8 years old. Print Page Previous Next Advertisements ”;
Euphoria – Functions
Euphoria – Functions ”; Previous Next Euphoria functions are just like procedures, but they return a value, and can be used in an expression. This chapter explains how to write your own functions in Euphoria. Function Definition Before we use a function we need to define it. 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 Example A simple function called sayHello that takes no parameters is defined here − function sayHello() puts(1, “Hello there”) return 1 end function Calling a Function To invoke a function somewhere later in the script, you would simple need to write the name of that function as follows − #!/home/euphoria-4.0b2/bin/eui function sayHello() puts(1, “Hello there”) return 1 end function — Call above defined function. sayHello() This produces the following result − Hello there Function Parameters Till now we have seen function without a parameters. But there is a facility to pass different parameters while calling a function. These passed parameters can be captured inside the function and any manipulation can be done over those parameters. A function can take multiple parameters separated by comma. Example Let us do a bit modification in our sayHello function. This time it takes two parameters − #!/home/euphoria-4.0b2/bin/eui function sayHello(sequence name,atom age) printf(1, “%s is %d years old.”, {name, age}) return 1 end function — Call above defined function. sayHello(“zara”, 8) This produces the following result − zara is 8 years old. The return Statement A Euphoria function must have return statement before closing statement end function. Any Euphoria object can be returned. You can, in effect, have multiple return values, by returning a sequence of objects. For example return {x_pos, y_pos} If you have nothing to return, then simply return 1 or 0. The return value 1 indicates success and 0 indicates failure Print Page Previous Next Advertisements ”;
Euphoria – Constants
Euphoria – Constants ”; Previous Next Constants are also variables that are assigned an initial value that can never change in the program’s life. 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 result of any expression can be assigned to a constant, even one involving calls to previously defined functions, but once the assignment is made, the value of the constant variable is “locked in”. Constants may not be declared inside a subroutine. The scope of a constant that does not have a scope modifier, starts at the declaration and ends and the end of the file it is declared in. Examples #!/home/euphoria-4.0b2/bin/eui constant MAX = 100 constant Upper = MAX – 10, Lower = 5 printf(1, “Value of MAX %dn”, MAX ) printf(1, “Value of Upper %dn”, Upper ) printf(1, “Value of Lower %dn”, Lower ) MAX = MAX + 1 printf(1, “Value of MAX %dn”, MAX ) This produces the following error − ./test.ex:10 <0110>:: may not change the value of a constant MAX = MAX + 1 ^ Press Enter If you delete last two lines from the example, then it produces the following result − Value of MAX 100 Value of Upper 90 Value of Lower 5 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. Enums may not be declared inside a subroutine. The scope of an enum that does not have a scope modifier, starts at the declaration and ends and the end of the file it is declared in. Examples #!/home/euphoria-4.0b2/bin/eui enum ONE, TWO, THREE, FOUR printf(1, “Value of ONE %dn”, ONE ) printf(1, “Value of TWO %dn”, TWO ) printf(1, “Value of THREE %dn”, THREE ) printf(1, “Value of FOUR %dn”, FOUR ) This will produce following result − Value of ONE 1 Value of TWO 2 Value of THREE 3 Value of FOUR 4 You can change the value of any one item by assigning it a numeric value. Subsequent values are always the previous value plus one, unless they too are assigned a default value. #!/home/euphoria-4.0b2/bin/eui enum ONE, TWO, THREE, ABC=10, XYZ printf(1, “Value of ONE %dn”, ONE ) printf(1, “Value of TWO %dn”, TWO ) printf(1, “Value of THREE %dn”, THREE ) printf(1, “Value of ABC %dn”, ABC ) printf(1, “Value of XYZ %dn”, XYZ ) This produce the following result − Value of ONE 1 Value of TWO 2 Value of THREE 3 Value of ABC 10 Value of XYZ 11 Sequences use integer indices, but with enum you may write code like this − enum X, Y sequence point = { 0,0 } point[X] = 3 point[Y] = 4 Print Page Previous Next Advertisements ”;
Euphoria – Sequences
Euphoria – Sequences ”; Previous Next A sequence is represented by a list of objects in brace brackets { }, separated by commas. A sequence can contain both atoms and other sequences. For example − {2, 3, 5, 7, 11, 13, 17, 19} {1, 2, {3, 3, 3}, 4, {5, {6}}} {{“Zara”, “Ayan”}, 52389, 97.25} {} — the 0-element sequence A single element of a sequence may be selected by giving the element number in square brackets. Element numbers start at 1. For example, if x contains {5, 7.2, 9, 0.5, 13} then x[2] is 7.2. Suppose x[2] contains {11,22,33}, Now if you ask for x[2] you get {11,22,33} and if you ask for x[2][3], you get the atom 33. Example #!/home/euphoria-4.0b2/bin/eui sequence x x = {1, 2, 3, 4} for a = 1 to length(x) do printf(1, “value of x[%d] = %dn”, {a, x[a]}) end for Here, length() is the built-in function which returns length of the sequence. The above example produces the following result − value of x[1] = 1 value of x[2] = 2 value of x[3] = 3 value of x[4] = 4 Character String A character string is just a sequence of characters. It may be entered in one of the two ways − (a) Using Double Quotes − “ABCDEFG” (b) Using Raw String Notation − — Using back-quotes `ABCDEFG` or — Using three double-quotes “””ABCDEFG””” You can try the following example to understand the concept − #!/home/euphoria-4.0b2/bin/eui sequence x x = “ABCD” for a = 1 to length(x) do printf(1, “value of x[%d] = %sn”, {a, x[a]}) end for This produces the following result − value of x[1] = A value of x[2] = B value of x[3] = C value of x[4] = D String Arrays An array of strings can be implemented using Sequences as follows − #!/home/euphoria-4.0b2/bin/eui sequence x = {“Hello”, “World”, “Euphoria”, “”, “Last One”} for a = 1 to length(x) do printf(1, “value of x[%d] = %sn”, {a, x[a]}) end for This produces the following result − value of x[1] = Hello value of x[2] = World value of x[3] = Euphoria value of x[4] = value of x[5] = Last One Euphoria Structures A structure can be implemented using Sequences as follows − #!/home/euphoria-4.0b2/bin/eui sequence employee = { {“John”,”Smith”}, 45000, 27, 185.5 } printf(1, “First Name = %s, Last Name = %sn”, {employee[1][1],employee[1][2]} ) This produces the following result − First Name = John, Last Name = Smith There are various operations which can be performed directly on sequences. Let us see them in detail − Urinary Operation When applied to a sequence, a unary operator is actually applied to each element in the sequence to yield a sequence of results of the same length. #!/home/euphoria-4.0b2/bin/eui sequence x x = -{1, 2, 3, 4} for a = 1 to length(x) do printf(1, “value of x[%d] = %dn”, {a, x[a]}) end for This produces the following result − value of x[1] = -1 value of x[2] = -2 value of x[3] = -3 value of x[4] = -4 Arithmetic Operations Almost all arithmetic operations can be performed on sequences as follows − #!/home/euphoria-4.0b2/bin/eui sequence x, y, a, b, c x = {1, 2, 3} y = {10, 20, 30} a = x + y puts(1, “Value of a = {“) for i = 1 to length(a) do printf(1, “%d,”, a[i]) end for puts(1, “}n”) b = x – y puts(1, “Value of b = {“) for i = 1 to length(a) do printf(1, “%d,”, b[i]) end for puts(1, “}n”) c = x * 3 puts(1, “Value of c = {“) for i = 1 to length(c) do printf(1, “%d,”, c[i]) end for puts(1, “}n”) This produces the following result − Value of a = {11,22,33,} Value of b = {-9,-18,-27,} Value of c = {3,6,9,} Command Line Options A user can pass command line options to a Euphoria script and it can be accessed as a sequence using command_line() function as follows − #!/home/euphoria-4.0b2/bin/eui sequence x x = command_line() printf(1, “Interpeter Name: %sn”, {x[1]} ) printf(1, “Script Name: %sn”, {x[2]} ) printf(1, “First Argument: %sn”, {x[3]}) printf(1, “Second Argument: %sn”, {x[4]}) Here printf() is Euphoria”s built-in function. Now if you run this script as follows − $eui test.ex “one” “two” This produces the following result − Interpeter Name: /home/euphoria-4.0b2/bin/eui Script Name: test.ex First Argument: one Second Argument: two Print Page Previous Next Advertisements ”;
Euphoria – Basic Syntax
Euphoria – Basic Syntax ”; Previous Next 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. Print Page Previous Next Advertisements ”;
Euphoria – Branching
Euphoria – Branching ”; Previous Next Branching is the most important aspect of any programming language. While writing your program, you may encounter a situation when you have to take a decision or you have to select one option out of the given many options. Following diagram shows a simple scenario where a program needs to take one of the two paths based on the given condition. Euphoria provides following three types of decision making (branching or conditional) statements − if statement switch statement ifdef statement Let us see the statements in detail − Print Page Previous Next Advertisements ”;
Euphoria – Loop Types
Euphoria – Loop Types ”; Previous Next Looping is yet another most important aspect of any programming language. While writing your program, you may encounter a situation when you have to execute same statement many times and sometime may be infinite number of times. There are several ways to specify for how long the process should go on, and how to stop or otherwise alter it. An iterative block may be informally called a loop, and each execution of code in a loop is called an iteration of the loop. The following diagram shows a simple logical flow of a loop − Euphoria provides following three types of loop statements − while statement loop until statement for statement All the above statements provide you flexibility and ease of use based on different situations. Let us see them in detail one by one − Print Page Previous Next Advertisements ”;