250 likes | 979 Views
Euphoria Programming Language. CSC 507 Kasilingam Vimalan. Euphoria. E nd- U ser P rogramming with H ierarchical O bjects for R obust I nterpreted A pplications Created by Robert Craig from Rapid Deployment Software First released in 1993 for 32-bit DOS platform.
E N D
Euphoria Programming Language CSC 507 Kasilingam Vimalan
Euphoria End-User Programming with Hierarchical Objects for Robust Interpreted Applications Created by Robert Craig from Rapid Deployment Software First released in 1993 for 32-bit DOS platform. Interpreter is available for Windows, DOS, Linux and FreeBSD can be downloaded freely form http://www.rapideuphoria.com Can develop various applications such as Windows GUI programs, and Linux X Windows programs, CGI programs etc
Key Features of Euphoria • Very simple language • Interpreted language • Automatic memory management and garbage collection. • Runtime type checking ( it can be turned off at runtime).
Two main Data Types 1. Atom is a number that can be either 31-bit integer or 64-bit floating-point. Example: atom a, b, c, d, e a = 'B' -- equivalent to the atom 66 b = 0 c = 1000 d = 98.6 e = -1e6
2. sequence • it a collection of numeric value or atoms • can have zero or more elements • each element is either an atom or a sequence • it can grow dynamically at runtime • The first element in a sequence has an index of one [1] Example: sequence a, b, c, d, e, f a = {2, 3, 5, 7, 11, 13, 17, 19} b = "abcd" –- this is actually {97, 98, 99,100} c = {1, 2, {3, 3, 3}, 4, {5, {6}}} d = {{"jon", "smith"}, 52389, 97.25} e = {} -- the 0-element sequence f = {x+6, 9, y*w+2, sin(0.5)}
Euphoria has two additional specialized data types: 1. Integeris a special form of atom, restricted to 31-bit integer values in the range -1073741824 to 1073741823. 2. Objectis a generic data type that can contain any of the above, and can be changed during run-time
Operators • Arithmetic Operators : unary-, unary+, *, /, +, - • Relational Operators: < > <= >= = != Note: for string use equal() or compare() • Logical Operators: and, or, xor, not • Other operator: & - concatenation operator Precedence: function/type calls unary- unary+ not / + - & < > <= >= = != and or xor { , , , }
Expressions In Euphoria one can perform an operation on entire sequences of data with one expression. Examples: x = {1,2,3} + 5 -- x is {6, 7, 8} x = -{1, 2, 3, {4, 5}} --x is {-1, -2, -3, {-4, -5}} x = {5, 6, 7, 8} + {10, 10, 20, 100} -- x is {15, 16, 27, 108} w = {{1, 2}, {3, 4}, {5}} * {4, 5, 6} -- w is {{4, 8}, {15, 20}, {30}}
Logical Expression Example: 5 and -4 -- 1 (true) not 6 -- 0 (false) Short-circuitevaluation of and and or takes place for if, elsif and while conditions only. It is not used in other contexts. Exampleswith logical and relational operators : x = 1 or {1,2,3,4,5} -- x is {1,1,1,1,1} w = {1, 0, 0, 1} and {1, 1, 1, 0} -- w is {1, 0, 0, 0} w = not {1, 5, -2, 0, 0} -- w is {0, 0, 0, 1, 1} w = {1, 2, 3} = {1, 2, 4} -- w is {1, 1, 0}
Subscripting/Slicing of Sequences x = {5, 7.2, 9, 0.5, 13} x[2] = {11,22,33} Then x becomes: {5, {11,22,33}, 9, 0.5, 13} y = { {{1,1}, {3,3}, {5,5}}, {{0,0}, {0,1}, {9,1}}, {{-1,9},{1,1}, {2,2}} } y[2][3][1] is 9 Array of strings: s = {"Hello", "World", "Euphoria", "", "Last One"} s[3] is "Euphoria" s[3][1] is 'E' Slicing of Sequences x = {1, 1, 2, 2, 2, 1, 1, 1} y = x[3..5] -- y is {2,2,2} x[3..5] = {9, 9, 9} -- x is {1, 1, 9, 9, 9, 1, 1, 1}
user-defined types type hour(integer x) return x >= 0 and x <= 23 end type hour h1, h2 h1 = 10 -- ok h2 = 25 -- error! program aborts with a message
Example: • if a < b then • x = 1 • end if • if a = 9 then • x = 4 • y = 5 • else • z = 8 • end if • if char = 'a' then • x = 1 • elsif char = 'b' then • x = 2 • elsif char = 'c' then • x = 3 • else • x = -1 • end if If statement 1. if <logical expression> then <statement> end if 2. if <logical expression> then <statement> else <statement> end if 3. if <logical expression> then <statement> elsif <logical expression> then <statement> elsif <logical expression> then . . end if
for statement for <variable> = <start> to <end> [by <step>] do <statement> end for Example: for i = 1 to 10 do ? i -- ? is a short form for print end for for i = 10.0 to 20.5 by 0.3 do for j = 20 to 10 by -2 do ? {i, j} end for end for
while statement while <logical_expression> do <statement> end while Example: i = 1 a = “hello” while i <= length(a) do ? a[i] i = i + 1 end while
Subprograms or routines There are two types of routines: ProcedureandFunction. Parameters: • All arguments to routines are always passed by value. • There is no pass-by-reference facility. • Sequences use copy-on-write semantics.
Procedure procedure <procedure name>([<arguments>]) <statement> end procedure Example: procedure plot(integer x, integer y) position(x, y) puts(1, '*') end procedure Note: Dose not allows to declare local variable
Functions Function is very similar to procedure, but it returns a value, and can be used in an expression. function <function name> ([<arguments>]) <statement> return <return object> end function function max(atom a, atom b) if a >= b then return a else return b end if end function
function swap(sequence list, integer x, integer y) object tmp tmp = list[x] list[x] = list[y] list[y] = tmp return list end function procedure permute(sequence list, integer start) if start < length (list) then for i = start to length(list) do permute(swap(list, start, i), start + 1) end for else ? list end if end procedure permute({1,2,3},1) Out put: {1,2,3} {1,3,2} {2,1,3} {2,3,1} {3,2,1} {3,1,2}
Short comes • No object oriented programming support. • No exception handling. • No concurrency support.
Bibliography 1. Euphoria programming language document http://www.rapideuphoria.com/manual.htm 2. Wikipedia, the free encyclopedia http://en.wikipedia.org/wiki/Euphoria_programming_language