Programs in EK9

This section will discuss the program construct in a little more detail. If you've read several of the other sections you'll already be familiar with programs. The remaining constructs and languages structures are described in order of use/simplicity/sophistication. These are linked the end of each page, but they follow the same ordering as the navigation bar on the left.

The program; as you will have deduced from the examples is the only way to run software that is written in EK9. Any number of programs can be defined in an EK9 source file; any number of command line parameters can be passed to a program. In addition 'Standard Input/Output/Error' (stdin, stdout and stderr) can also be employed.

Every EK9 has to start with '#!ek9', see the structure for more details. This is so that for systems (like MacOS and most Unix/Linux) you can change the execute permission on the EK9 file and then just run it.

Assuming you have installed EK9 correctly and the ek9 command is on your PATH the program in the file will be executed. But clearly if there is more than one program in a single EK9 source file you will have to state which program you want to run.

Running a Program from the command line

Whilst there are several examples of how to write programs there has not really been any information on how to run those programs once written. The commands below give you a better idea of what you need to do to run an EK9 program.

There are a couple of different ways to run an EK9 program depending on platform and the number of programs in the source file. Just running ek9 with no parameters gives you the options available. Please note this is not the full list of options, but just the main ones. See the EK9 command line for full details.

  • $ ek9
  • Usage: ek9 <options>
  • where possible options include:
  •   -V The version of the compiler/runtime
  •   -h Help message
  •   -c Just compile; but don't run
  •   -C Force full recompilation; but don't run
  •   -e <name>=<value> set an environment variable ie. user=Steve or user=\'Steve Limb\' for spaces.
  •   filename.ek9 - the main file to work with
  •   -r Program to run (if EK9 file as more than one)

The ordering of the flags is only important after you have specified the filename.ek9. The -r Program must come after the filename. All the other compiler parameter directives must come before the filename. Any parameters you wish to pass to the EK9 program must then follow the -r Program.

The main ek9 application is quite different to many other languages and development tools in the sense it has knowledge of more than just how to read, process and compile EK9 source files. So it has the feel of an interpreted language like Python in the sense that you can just run an EK9 program as follows:

Running a Program

  • $ ek9 helloworld.ek9
  • On Linux/Unix/MacOS you can use this as well/instead (if the execute bit is set)
  • $ ./helloworld.ek9

Compiling a Program / General Code

But importantly unlike an interpreted language, EK9 compiles the source code; however once compiled it can then just run the application. So the first time you attempt to run an EK9 program the ek9 application will check to see if it needs compiling (if it does, it will compile it, if not it will just run it from what was compiled last time).

If you were looking just to trigger a full recompilation of a program but not actually run it, then you can use the following command.

  • $ ek9 -C helloworld.ek9
  • To include debugging information
  • $ ek9 -Cg helloworld.ek9

Dependencies

In general small programs or utilities can be written without the need for any other third party software components at all. But in general the real power of open source is a vibrant and generous community that writes and makes available small, medium and large packages.

In general these valuable contributions are managed outside of the language itself via tooling like maven or NPM. This typically involves some from of project or package file description in xml or json format.

EK9 does not take this approach; see the section on packaging for more details.

Hello, World Examples

See the example from hello, world and hello worlds for a simple examples of how to both write a program and run it.

The data correlation program shown in the CLI worked example (which is quite long) could be run in the following manner. This assumes that the program is stored in a file called correlation.ek9, the named file contents is in a file called namedFile.txt and the data for processing is in a file called stdin.txt.

On Windows you would use type or get-content rather than the Unix/Linux cat.

  • Windows uses type rather than cat
  • $ type stdin.txt | ek9 correlation.ek9 -v -d 2 -f namedFile.txt
  • On Linux/Unix/MacOS you can use this form
  • $ cat stdin.txt | ./correlation.ek9 -v -d 2 -f namedFile.txt
  • Here is another example capturing stderr and stdout
  • $ cat stdin.txt | ./correlation.ek9 -v -d 2 -f namedFile.txt > results.txt 2>errors.txt
  • Or the longer form if you wish.
  • $ cat stdin.txt | ek9 ./correlation.ek9 -v -d 2 -f namedFile.txt > results.txt 2>errors.txt
  • If you wanted to run in verbose, use this command.
  • $ cat stdin.txt | ek9 -v ./correlation.ek9 -v -d 2 -f namedFile.txt > results.txt 2>errors.txt
  • The first -v is for the ek9 application and the second one for the correlation program

The example above gives you some idea of how the parameters you defined in your program get passed from the command line into your application. The command lines below give a few more examples.

Running a TCP Server

The code example TCP Server is a good example of some code that can be started on the command line or some sort of init script for when a server starts up. Below is the command line that would be used to start it, but also the command line on how a client could connect to it.

This assumes the programs described in TCP Examples are stored in a file called tcp.ek9.

  • Server Code
  • Start the Server - & means run in background on Linux/Unix
  • $ ./tcp.ek9 -r TCPServer2 4445 4446 SHUTDOWN &
  • Client Code
  • Now to run the client
  • $ ./tcp.ek9 -r TCPClient2 4445 4446

Note the processing port 4445 and control port 4446 in the command lines above and the way they are passed into the applications. Because the file tcp.ek9 actually contains 4 programs it is necessary to use the -r flag so that ek9 know which of the programs to run.

Programs and Applications

The application example shows how a program can be configured with a specific application as a configuration.

In terms of the command line instructions there is no difference when a program is defined to use an application configuration.

So if the EK9 code was stored in a file called configtest.ek9 then each of the programs could be executed in the following way.

  • $ ./configtest.ek9 -r v1Program
  • $ ./configtest.ek9 -r v2Program
  • $ ./configtest.ek9 -r v3Program

Summary

This short section on programs should give you a good idea of how to write a program, but more importantly how to compile and run that program. You should now be able to understand how to pass parameters from the command line into the program in a variety of different ways.

Next Steps

The details on packaging and deploying/publishing your code to an artefact server are covered in the section on packaging.

But if you are looking for more details on the constructs, flow control, exception handling and more EK9 specific ideas continue by reading constants.