Seed7

From Wikinfo

Jump to: navigation, search

Seed7 is a general-purpose programming language designed by Thomas Mertes. It is a higher level language compared to Ada, C / C++ and Java. The Seed7 interpreter and the example programs are open source software. An open-source Seed7 compiler is also under development.

In Seed7 new statements and operators can be declared easily. Functions with type results and type parameters are more elegant than a template or generics concept. Object orientation is used when it brings advantages and not in places when other solutions are more obvious. Although Seed7 contains several concepts of other programming languages it is generally not considered as a direct descendant of any other programming language.

Contents

Seed7 examples

Keywords are in blue, types are in red, strings in maroon, comments in green.

Echo the program arguments

This example program writes its arguments

 $ include "seed7_05.s7i";       # Standard Seed7 library

 const proc: main is func
   local
     var string: stri is "";
   begin
     for stri range argv(PROGRAM) do
       write(stri <& " ");
     end for;
     writeln;
   end func;

The standard Seed7 library "seed7_05.s7i" defines the 'main' function as entry point of a Seed7 program.

The 'argv(PROGRAM)' function returns the program arguments as array of strings. The 'for' loop iterates over all elements of this array. The 'for' statement is overloaded for various collection types.

Declare a statement

In the standard Seed7 library "seed7_05.s7i" the 'for' statement for arrays is declared as follows:

 const proc: for (inout baseType: variable) range (in arrayType: arr_obj) do
               (in proc: statements)
             end for is func
   local
     var integer: number is 0;
   begin
     for number range 1 to length(arr_obj) do
       variable := arr_obj[number];
       statements;
     end for;
   end func;

The syntax of this 'for' statement is declared as:

 $ syntax expr: .for.().range.().to.().do.().end.for is              -> 25;

Additionally everybody can overload the 'for' statement also. Because of this powerful features Seed7 does not need Iterators.

Template declaring a statement

Templates are just normal functions with types as parameters. The following template function declares 'for' statements:

 const proc: FOR_DECLS (in type: aType) is func
   begin

     const proc: for (inout aType: variable) range (in aType: low) to (in aType: high) do
         (in proc: statements) end for is func
       begin
         variable := low;
         if variable <= high then
           statements;
           while variable < high do
             incr(variable);
             statements;
           end while;
         end if;
       end func;

   end func;

 FOR_DECLS(char);
 FOR_DECLS(boolean);

The body of the 'FOR_DECLS' function contains a declaration of the 'for' statement for the type aType. Calling 'FOR_DECLS' with char and boolean as parameter creates corresponding declarations of 'for' statements. The example above is a simplified part of the standard Seed7 library "seed7_05.s7i".

Declare an operator

The standard Seed7 library "seed7_05.s7i" contains also the following declarations:

 const type: color is new struct
     var integer: red_part   is 0;
     var integer: green_part is 0;
     var integer: blue_part  is 0;
   end struct;

 const func color: (in color: col1) + (in color: col2) is func
   result
     var color: result is color.value;
   begin
     result.red_part   := (col1.red_part   + col2.red_part)   div 2;
     result.green_part := (col1.green_part + col2.green_part) div 2;
     result.blue_part  := (col1.blue_part  + col2.blue_part)  div 2;
   end func;

This declares the type color and the '+' operator to add two colors in an additive color system.

Memory for this color objects is managed automatically without garbage collection. Additionally it is not necessary to call constructors to initialize color objects.

Read a file into a string

This example program interprets the arguments as file names and replaces the string "dog" by "cat" in the corresponding files:

 $ include "seed7_05.s7i";       # Standard Seed7 library
   include "getf.s7i";           # Import the getf and putf functions

 const proc: main is func
   local
     var string: file_name is "";
   begin
     for file_name range argv(PROGRAM) do
       putf(file_name, replace(getf(file_name), "dog", "cat"));
     end for;
   end func;

The 'getf' function reads the complete file with the name 'file_name' into a string and returns it. Seed7 strings can have any length and can contain any data (even null characters). The 'replace' function replaces "dog" by "cat" and after that the 'putf' function writes the string back to the file with the name 'file_name'.

Simple clock

This example program displays a digital clock on the text console

 $ include "seed7_05.s7i";
   include "time.s7i";
   include "keybd.s7i";

 const proc: main is func
   local
     var time: next_time is time.value;
   begin
     writeln;
     next_time := truncToSecond(time(NOW));
     while not keypressed(KEYBOARD) do
       next_time +:= 1 . SECONDS;
       await(next_time);
       write(next_time <& " \r");
       flush(OUT);
     end while;
     writeln;
     writeln;
   end func;

This example uses time functions like 'time(NOW)' and 'await(next_time)'. There is an addition of a duration to a time and a time is written with 'write(next_time)'.

The program is terminated when a key is pressed. This check is done with the 'keypressed(KEYBOARD)' function.

External links