Eve Macros
One file, several aspects
A project spreads its work over many files: the aspects at the root and in asp/, the modules in lib/ (see Scripts). This is the right shape for a large application, but it is heavy for a small one. A job that runs two workers in parallel, a demo or a test should fit in one file that you can copy, mail or run with one command. A file with one aspect is already the shortest program (Running an aspect); when the program needs more aspects, they go in the same file and the file is a macro:
| Aspect file | Eve macro | |
|---|---|---|
| Purpose | one aspect, the unit of a project | a small application, a demo or a test: several aspects and shared code |
| Line 1 | a # title or the code | a # title or the code (no #!) |
| Top level | one aspect | declarations only: shared declarations and aspects |
| Subprograms, classes | inside the aspect | shared by the whole file |
| Aspects | applies the aspects of a project | declares its own aspects; may also apply those of a project |
| Kind word | aspect, exclusive or concurrent | the same, and exactly one primary aspect |
| Run | eve report.eve | eve pipeline.eve: runs process main of its primary aspect |
| Imported by others | no | no: a macro is self-contained |
Both are self-contained: nothing in them can be imported by another script, so they never become a hidden dependency. Code that several programs need belongs in a module.
Eve macro
An Eve macro is one file that holds shared declarations and any number of aspects, exactly one of them a primary aspect. The primary aspect is the entry point of the macro: primary is written in front of aspect, in the place of exclusive or concurrent; it is the same kind of aspect as in a project. The declarations are written at column 1, in any order; by custom the shared declarations come first, then the other aspects, and the primary aspect last, so the file reads from the tools to the work.
# score: a primary aspect and its aspect in one file
** shared declarations: seen by all the aspects
set bonus = 5 :Integer;
new total := 0;
function twice(x: Integer) => (@result: Integer) is
let result := x * 2;
return;
** an exclusive aspect runs alone, so it may change a shared variable
exclusive aspect collect is
process main(points: ()Integer) is
for p in points do
let total += p + bonus;
done;
return;
end collect;
** the primary aspect: process main is the entry point of the macro
primary aspect score is
process main is
apply collect((10, 20));
apply collect((30, 40));
print "total: {total}"; ** total: 120
print "double: {twice(total)}"; ** double: 240
return;
end score;
Shared declarations
Every top-level declaration that is not an aspect is shared by the whole file: classes, functions, procedures, methods, constants (set), variables (new) and imports (from … use). This is the difference from a project, where an aspect sees nothing of the aspect that calls it: in a macro the aspects share what the file declares at the top. Each aspect still keeps its own scope and state, and it sees no variable of another aspect; only the shared declarations are common.
- The primary aspect and the exclusive aspects use every shared declaration, the variables too: they run alone, so they change a shared variable safely.
- A concurrent aspect uses only what is thread safe: constants, classes, atomic variables (
Atomic(:T)), channels, and the functions and procedures that the compiler proves thread safe. Anything else is a compile error that names it (below).
A parallel job in one file
A macro is the natural home of a small parallel job. The primary aspect is serial, but its parallel groups start the concurrent aspects of the file (see Concurrency); a shared class carries the results back. An output is declared before the group, so it is still there after done.
# pipeline: two workers in parallel, one file
class Result = {name :String, count :Integer} <: Object is
constructor(name = "" :String, count = 0 :Integer) => (@self) is
let self := Object();
let self.name := name;
let self.count := count;
return;
end Result;
concurrent aspect job_worker_1 is
process main(@res: Result) is
** internal sequential job logic
let res := Result("worker 1", 10);
return;
end job_worker_1;
concurrent aspect job_worker_2 is
process main(@res: Result) is
** internal sequential job logic
let res := Result("worker 2", 20);
return;
end job_worker_2;
# the primary aspect: it starts the parallel tasks
primary aspect pipeline is
process main is
new r1 := Result();
new r2 := Result();
p_exec: parallel
do
start job_worker_1(@r1);
start job_worker_2(@r2);
done p_exec;
print "{r1.name}: {r1.count}, {r2.name}: {r2.count}";
return;
end pipeline;
This example is the demo pipeline_macro; every demo of the tutorial that needs aspects is written as a macro (Demos & Examples).
Concurrent aspects that must add to the same total use a shared atomic variable. Reading it gives a value that depends on the timing of the tasks, so the function that reads it is stochastic and its name ends with !:
# counter: eight tasks add to one shared atomic total
new count = 0 :Atomic(:Integer);
procedure add(n: Integer) is
let count += n;
return;
function total!() => (@result: Integer) is
let result := count;
return;
concurrent aspect adder is
process main(times: Integer) is
for i in (1..times) do
add(1);
done;
return;
end adder;
primary aspect counter is
process main is
parallel
do
for i in (1..8) do
start adder(1000);
done;
done;
print "total: {total!()}"; ** total: 8000, at every run
return;
end counter;
Rules
- A macro has exactly one primary aspect;
eve file.everuns itsprocess main, which takes the command-line arguments. It is the root of the run, so no aspect applies or starts it. - The primary aspect is serial: its processes are exclusive, applied and never started. Its parallel groups start the concurrent aspects of the macro.
primary,exclusiveandconcurrentare reserved words of the same design. A file with one aspect can beprimarytoo: it is then the shortest program, not a macro.- Line 1 is a
#title or the code. There is no#!line: Eve is a language for projects, not a shell language. - Only declarations at the top level: a statement outside a declaration is an error.
- The aspects of a macro are private: no other script can apply or start them. A macro exports nothing and can not be imported.
- An aspect named in
applyorstartis searched first in the macro, then inasp/and at the project root. An aspect of the macro with the name of an aspect file of the project is an error, so the reader never wonders which one runs. - The calls between the aspects of a macro follow the rules of Aspects: no cycle, and at most three levels deep, counted from the primary aspect.
Compile errors
| Mistake | Message |
|---|---|
| no primary aspect | a macro needs a primary aspect |
| two primary aspects | a macro has only one primary aspect |
| an aspect applies or starts the primary aspect | a primary aspect is run by a command: it can't be applied or started |
| the primary aspect starts a private process | a primary aspect is serial: apply 'name' |
| a statement at the top level | a statement outside a declaration in a macro |
| a concurrent aspect uses a plain shared variable | the concurrent aspect 'adder' uses 'count', which is not thread safe |
| a concurrent aspect calls a subprogram that changes one | … calls 'bump', which is not thread safe |
Which one to choose
A program usually grows in three steps, and each step keeps the code of the one before:
- One aspect: a few statements in
process mainto try an idea or to do a small job. - Eve macro: when the program needs aspects of its own, shared functions or classes, or parallel work, write them in the same file and mark the entry point
primary. - Project: when the file grows too long, or other programs need its aspects or its code, move the aspects to
asp/and the shared code to a module inlib/; the aspect that you run stays as it is.
Read next: Modules