Programs in EK9

A program is the only way to run software written in EK9. You have met programs in the other sections; this one covers them in more detail. The remaining constructs and language structures follow in order of use, simplicity and sophistication: each page links to the next, in the same order as the navigation bar on the left.

An EK9 source file can define any number of programs. You can pass command line parameters to a program, and it can use 'Standard Input/Output/Error' (stdin, stdout and stderr).

Every EK9 source file starts with '#!ek9' (see structure). On macOS and most Unix/Linux systems that means you can set the file's execute permission and run it directly: if EK9 is installed and the ek9 command is on your PATH, the program in the file runs. If the file holds more than one program, you say which one to run.

Quality Enforcement: the compiler applies quality limits to program code as it does to functions and methods: cyclomatic complexity at most 45, cognitive complexity 35, nesting depth 6, and 150 statements (a program is budgeted like a function). Variable names must be descriptive (no temp, flag, data, value, buffer, object). A program is an entry point: delegate complex logic to functions, methods or components. See Code Quality.

Running a Program from the command line

How you run an EK9 program depends on your platform and on how many programs the source file holds. Running ek9 with no parameters lists the options. The excerpt below shows the main ones; see the EK9 command line for them all.

$ ek9
ek9: bad command line arguments (exit 2)
ek9 <options>
        where possible options include:
        	-V The version of the compiler/runtime
        	...
        	-c Incremental compile; but don't run
        	-C Force full recompilation; but don't run
        	...
        	-e <name>=<value> set environment variable i.e. user=Steve or user='Steve Limb' for spaces
        	...
        	filename.ek9 - the main file to work with
        	-r Program to run (if EK9 file has more than one)

Order matters once you reach the file name:

ek9 knows more than how to read and compile EK9 source, so it has the feel of an interpreted language like Python: you can run a program directly.

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
# On Linux/Unix/macOS if your path includes the directory where the file is located
$ helloworld.ek9

Compiling a Program / General Code

Unlike an interpreted language, EK9 compiles the source code, then runs the result. When you run a program, ek9 checks whether it needs compiling: if so it compiles it, and if not it runs what it compiled last time.

To force a full recompilation without running the program:

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

Dependencies

Small programs and utilities need no third-party components at all. Larger software benefits from the open source community, which writes and shares small, medium and large packages.

Why EK9 does it this way. Most languages manage packages outside the language, with tools like maven or NPM and a project or package file in xml or json. EK9 builds package management and publication into the language and the compiler's command line tool instead; see packaging.

Hello, World Examples

hello, world and hello worlds show how to write a program and run it.

The (long) data correlation program in the CLI worked example could be run as shown below. This assumes the program is in correlation.ek9, the named file is namedFile.txt and the data to process is in stdin.txt. On Windows 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

These show how the parameters you define in your program are passed from the command line. Here are a few more.

Running a TCP Server

The TCP Server example is code you might start on the command line, or from an init script when a server starts up. Below is the command line that starts it, and the one a client uses to connect. This assumes the programs in TCP Examples are 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

The processing port 4445 and control port 4446 are passed to each program as parameters. tcp.ek9 holds 4 programs, so the -r flag tells ek9 which one to run.

Programs and Applications

The application example configures each program with a specific application. On the command line that makes no difference. If the code is in a file called configtest.ek9, run each program like this:

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

Summary

You have seen how to write a program, how to compile and run it, and the ways to pass it parameters from the command line.

Next Steps