1. Easy Language

1.1. What is the Easy Language ?

The EZ (Easy) language is a simple yet powerful language close to C++. The source code of the language is translated to C++ and then compiled by a C++ compiler. The aim of the language is to simplify the writing of algorithms and the annoyances of C++.

1.2. Language definition

1.2.1. Types of the Language ?

Each type has built-in methods, for exemple in the case of integers :

for strings :

for vectors :

1.2.1.a  Variables definition

We use the keyword var to introduce a list of variables

  1. // definition of a simple variable
  2. var int n
  3.  
  4. // initialization
  5. var int x = 1, y, z = 2
  6.  
  7. var string s = "hello"
  8.  
  9. // ------------------------------------------------------------------  
  10. // containers
  11. // ------------------------------------------------------------------  
  12.  
  13. // definition of a vector  
  14. var vector<int> v
  15.  
  16. // definition of a vector of size 10 with first three elements
  17. // defined
  18. var vector<int> v(10) = { 1, 2, 3 }
  19.  
  20. // definition of two arrays that range from -3 to +3
  21. var array<int>[ -3 : 3 ] tab1, tab2
  22.  

1.2.2. Instructions

For the sake of simplicity, the following rules must be respected:

The main instructions are the following:

if

  1. if x < 1 then
  2.   println "x < 1"
  3. elsif x < 10 then
  4.   println "1 <= x < 10"
  5. else
  6.   println "x >= 10"
  7. end if 
  8.  

on do

  1. // on <condition> do <simple-instruction>
  2.  
  3. on x < 1 do return false
  4.  

while

  1. var int i = 1, sum = 0
  2.  
  3. // compute sum of the integers from 1 to 10
  4.  
  5. while i <= 10 do
  6.  
  7.   sum +=// or sum = sum + i
  8.   ++i     // or i = i + 1
  9.  
  10. end while
  11.  
  12.  
  13.  

repeat until

  1. var int i = 1, sum = 0
  2.  
  3. // compute sum of the integers from 1 to 10
  4.  
  5. repeat
  6.  
  7.   sum +=// or sum = sum + i
  8.   ++i     // or i = i + 1
  9.  
  10. until i > 10
  11.  
  12.  

for

  1. // i is defined as an integer by default
  2. // if i already exists a warning will be issued
  3.  
  4. var int sum = 0
  5.  
  6. // compute sum of the integers from 1 to 10
  7.  
  8. for i in range(1,10) do
  9.  
  10.   sum += i
  11.  
  12. end for
  13.  

loop

  1. var int i = 1, sum = 0
  2.  
  3. // compute sum of the integers from 1 to 10
  4.  
  5. loop 10
  6.  
  7.   sum +=// or sum = sum + i
  8.   ++i     // or i = i + 1
  9.    
  10. end loop
  11.  

switch

  1. switch x
  2.   case 1
  3.     println "x = 1"
  4.   end case
  5.  
  6.   case 2
  7.     println "x = 2"
  8.   end case
  9.  
  10.   default case
  11.     println "x is not equal to 1 or 2"
  12.   end case
  13.  
  14. end switch
  15.  

print, read, ask, dump

  1. var int i = 3
  2. var string name = "John"
  3.  
  4. // print different data
  5. print "a string ", 10, 'c', 3.1415, " ", name
  6.  
  7. // read from standard input
  8. ask name
  9.  
  10. // print variable type and value
  11. dump name, i
  12.  
  13.  

Examples

The Hello World ! program in EZ

  1. prog hello_world
  2.  
  3. // -----------------------------------------
  4. // a procedure that has the same name as the
  5. // program is the entry point
  6. // -----------------------------------------
  7. proc hello_world()
  8.  
  9.   println( "Hello World !" )
  10.  
  11. end proc
  12.  

First 20 Prime numbers and their average

The following program computes the first 20 prime numbers using the user defined function 'is_prime'. Once these numbers are found we can compute their average.

  1. prog prime_numbers
  2. (
  3.   var int max_primes = 20 parse as "mp"
  4. )
  5.  
  6. var vector<int> v
  7.  
  8. /* =====================================
  9.  * function that determines if 'x' is
  10.  * a prime number or not
  11.  * =====================================
  12.  */
  13.  
  14. func bool is_prime( int x )
  15.   on n <= 1 do return false
  16.   on n <= 3 do return true
  17.   on (n % 2) = 0 do return false
  18.  
  19.   // no need to declare variable i it will be declared
  20.   // in C++ by "for (int i = 3; i <= x.sqrt(); i += 2)"
  21.  
  22.   for i in [3 : x.sqrt() : 2]
  23.  
  24.     on (x % i) = 0 do return false
  25.    
  26.   end for
  27.  
  28.   return true
  29.  
  30. end func
  31.  
  32. // =====================================
  33. // start program here
  34. // =====================================
  35.  
  36. proc prime_numbers()
  37.  
  38.   var int n = 1
  39.  
  40.   while (v.size() != max_primes) and (n <= 1000) do
  41.  
  42.     on is_prime(n) do v.insert_last(n)
  43.     ++n
  44.    
  45.   end while
  46.  
  47.   println "the first ", MAX_PRIMES, " numbers are ", v
  48.   println "their average is ", v.average()
  49.  
  50. end proc
  51.  
  52.  

This program will be translated in C++ as the following:

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. #include "ez.h"
  5.  
  6. const int MAX_PRIMES = 20;
  7. int n;
  8.  
  9. bool is_prime(int x) {
  10.   int i;
  11.  
  12.   if (n <= 1) return false;
  13.   if (n <= 3) return true;
  14.   for (i = 2; i <= x/2; ++i) {
  15.     if ((x % i) == 0) return false;
  16.   }
  17.  
  18.   return true;
  19. }
  20.  
  21.  
  22. int main() {
  23.   ez::vector<int> v;
  24.  
  25.   n = 1
  26.   while (v.size() < MAX_PRIMES) {
  27.     if (is_prime(n))  v.push_back(n);
  28.     n += 1;
  29.   }
  30.   cout << "the first " << MAX_PRIMES << " numbers are " << v << endl;
  31.   cout <<  "their average is " << v.average() << endl;
  32.   return EXIT_SUCCESS;
  33. }
  34.  

User-defined data structure

The following program creates a vector of persons and sort them by name, by age and computes maximum of ages

  1. prog prog_person
  2.  
  3. import math
  4.  
  5. /**
  6.  * definition of a person,
  7.  * default methods will be created for this class
  8.  * - a constructor with no arguments that initializes all data members
  9.  * - a constructor with arguments for each data members except containers
  10.  * - a print method to display all data members
  11.  */
  12. class Person  
  13.  
  14.   var string name
  15.   var int age
  16.  
  17. end class
  18.  
  19. // =====================================
  20. // start program here
  21. // =====================================
  22. proc prog_person()
  23.  
  24.   var Person p
  25.   var vector<Person> v
  26.   var int i, max
  27.  
  28.   for i in range(1,10) do
  29.     p.name = "p" + i
  30.     p.age = math.rand( 1, 100 )
  31.     v.insert(p)
  32.   end for
  33.  
  34.   // sort by name
  35.   v.sort( name );
  36.   println v
  37.  
  38.   // store to file and restore from file 
  39.   v.store( "persons.txt" )
  40.   v.empty()
  41.   v.restore( "persons.txt" )
  42.  
  43.   // sort by age 
  44.   v.sort( age )
  45.  
  46.   // compute maximum of ages (detailed version)
  47.   max = 0
  48.   for i in v.range()
  49.    
  50.     on v[i].age > max do max = v[i].age
  51.    
  52.   end for
  53.  
  54.   // or use built-in function
  55.   max = v.max(age)
  56.  
  57.   println "oldest person is ", max, " years old"
  58.  
  59. end proc
  60.  
  61.  

This program will be translated in C++ as the following:

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. #include "ez.h"
  5.  
  6. class Person {
  7. public:
  8.   string name;
  9.   int age;
  10.  
  11.   Person() {
  12.     name = "";
  13.     age = 0;
  14.   }
  15.  
  16.   ostream& print(ostream& out) {
  17.     out << name << ", " << age;
  18.   }
  19.  
  20.   ostream& _export(ostream& out) {
  21.     out << name << ", " << age;
  22.   }
  23.  
  24.   ostream& operator<<(ostream& out, Person& obj) {
  25.     return obj.print(out);
  26.   }
  27. };
  28.  
  29. int main() {
  30.   Person p;
  31.   ez::vector<Person> v;
  32.   int i, max;
  33.  
  34.  
  35.   for (i = 1; i <= 10; ++i) {
  36.     p.name = "p" + i;
  37.     p.age = 1 + rand() % 100;
  38.     v.push_back(p);
  39.   }
  40.  
  41.   // sort by name
  42.   sort(v.begin(), v.end(), [](const Person& lhs, const Person& rhs) {
  43.     return lhs.name > rhs.name;
  44.   });
  45.  
  46.   cout << v;
  47.   v.store("persons.txt")
  48.   v.clear()
  49.   v.restore("persons.txt")
  50.  
  51.   // sort by age
  52.   sort(v.begin(), v.end(), [](const Person& lhs, const Person& rhs) {
  53.     return lhs.age > rhs.age;
  54.   });
  55.  
  56.  
  57.   // compute maximum of ages (detailed version)
  58.   max = 0
  59.   for (i = 1; i <= v.size(); ++i) do
  60.     if  (v[i].age > max) max = v[i].age;
  61.   }
  62.  
  63.   // or use built-in function
  64.   max = v.max(age)
  65.  
  66.   cout << "maximum of ages = " << max << endl;
  67. end
  68.  
  69.  

Input and Output parameters

  1. prog parameters
  2. (
  3.   int x = 1 parse as "x" required
  4.   int y = 1 parse as "y" required
  5. )
  6.  
  7. /* ------------------------------------------------------------------
  8.  * by default any parameter declared is considered as 'input'
  9.  * which means non modifiable.
  10.  * Here z is an output parameter
  11.  * ------------------------------------------------------------------
  12.  */
  13. proc add( int x, y; exist int z )
  14.  
  15.   z = a + b
  16.  
  17. end proc
  18.  
  19. /* ------------------------------------------------------------------
  20.  * It all starts here !
  21.  * ------------------------------------------------------------------
  22.  */
  23. proc parameters()
  24.  
  25.   var int z
  26.  
  27.   add(x, y, z)
  28.  
  29.   dump z
  30.  
  31. end proc
  32.  

This program will be translated in C++ as the following:


Warning: file_get_contents(ez/parameters.cpp): Failed to open stream: No such file or directory in /home/jeanmichel.richer/public_html/html_business.php on line 448
  1.  

Merge sort

  1. prog merge_sort_program
  2.  
  3. /*
  4.  * Command line argument of the program which must be provided as
  5.  *    -s <integer-constant>
  6.  * or
  7.  *    --array-size=<integer-constant>
  8.  */
  9. (
  10.   int array_size = 10 parse as 's' or "array-size"
  11. )
  12.  
  13. /* ------------------------------------------------------------------
  14.  *
  15.  * SUBPROGRAM
  16.  *
  17.  *  proc merge( exist array<int> t; int l, m, h )
  18.  *
  19.  * WHAT
  20.  *
  21.  *  Merge two parts consecutive parts of the array @param(t)
  22.  *
  23.  * PARAMETERS
  24.  *
  25.  *  @paramdef(t) array of integers
  26.  *  @paramdef(l) lowest index of array to merge
  27.  *  @paramdef(m) middle index of array to merge
  28.  *  @paramdef(h) highest index of array to merge
  29.  *
  30.  * ------------------------------------------------------------------
  31.  */
  32. proc merge( exist array<int> t; int l, m, h )
  33.  
  34.   var int i = l, j = m, k = l
  35.   var array<int> temp;
  36.  
  37.   while (i < m) and (j <= h) do
  38.     if t[ i ] < t[ j ] then
  39.       temp[ k ] = t[ i ]
  40.       ++k
  41.       ++i
  42.     elsif t[ j ] < t[ i ] then
  43.       temp[ k ] = t[ j ]
  44.       ++k
  45.       ++j
  46.     else
  47.       temp[ k ] = t[ j ]
  48.       temp[ k ] = t[ i ]
  49.       ++i
  50.       ++j
  51.       k += 2
  52.   end while
  53.  
  54.   while i < m do
  55.     temp[ k ] = t[ i ]
  56.       ++k
  57.       ++i
  58.   end while
  59.  
  60.   while j <= h do
  61.     temp[ k ] = t[ j ]
  62.     ++k
  63.     ++j
  64.   end while
  65.  
  66.   t = temp
  67.  
  68. end proc
  69.  
  70.  
  71. /* ------------------------------------------------------------------
  72.  *
  73.  * SUBPROGRAM
  74.  *
  75.  *  proc split( exist array<int> t, int l, h )
  76.  *
  77.  * WHAT
  78.  *
  79.  *  Split array @param(t) in two parts based on middle index
  80.  *  defined by @formula( (l+h) / 2 ).
  81.  *
  82.  * PARAMETERS
  83.  *
  84.  *  @paramdef(t) array of integers
  85.  *  @paramdef(l) lowest index of array to split
  86.  *  @paramdef(h) highest index of array to split
  87.  *
  88.  * ------------------------------------------------------------------
  89.  */
  90.  
  91. proc split( exist array<int> t, int l, h )
  92.  
  93.   // number of elements of the
  94.   var int size = h - l + 1
  95.  
  96.   on size = 1 do return
  97.  
  98.   if size = 2 then
  99.     on t[l] > t[h] do t.swap(l, h)
  100.  
  101.   else
  102.  
  103.     var int m = (l + h) / 2
  104.     split( t, l, m-1 )
  105.     split( t, m, h )
  106.    
  107.     merge( t, l, m, h )
  108.  
  109.   end if
  110.    
  111. end proc
  112.  
  113. /* ------------------------------------------------------------------
  114.  *
  115.  * SUBPROGRAM
  116.  *
  117.  *  proc merge_sort( exist array<int> t )
  118.  *
  119.  * WHAT
  120.  *
  121.  *  Perform merge-sort on array @param(t)
  122.  *
  123.  * PARAMETERS
  124.  *
  125.  *  @paramdef(t) array of integers
  126.  *
  127.  * ------------------------------------------------------------------
  128.  */
  129.  
  130. proc merge_sort( exist array<int> t )
  131.  
  132.   split( t, t.min_index(), t.max_index() )
  133.  
  134. end proc
  135.  
  136. /* ------------------------------------------------------------------
  137.  *
  138.  * SUBPROGRAM
  139.  *
  140.  *  proc merge_sort_program()
  141.  *
  142.  * WHAT
  143.  *
  144.  *  Main method
  145.  *
  146.  * NOTE
  147.  *
  148.  *  @var(array_size) is a parameter of the program provided
  149.  *  as a command line argument (@see arguments section).
  150.  *
  151.  * ------------------------------------------------------------------
  152.  */
  153. proc merge_sort_program()
  154.  
  155.   // check condition, if false then raise exception
  156.   assume array_size > 1
  157.  
  158.   // create array
  159.   var array<int> t[ 1:array_size ]
  160.  
  161.   t.random_fill( 20, 1500 )
  162.  
  163.   merge_sort( t )
  164.  
  165.   print t
  166.  
  167. end proc
  168.  

1.3. Compilation process

The EZ language source file must have the .ezl extension.

The compiler is called ezc and uses some compilation flags :

By default the compiler will try to generate the C++ file, compile it into and executable and run it.