Eve Aspects

An aspect is the one kind of script that runs. It is an encapsulated machine: other aspects call it by its name, its process main is the only way in, and data goes in and out only through the parameters. An exclusive or a concurrent aspect is called by another aspect; a primary aspect is run by a command.

Aspects

An "aspect" is a script that solves a problem or addresses a specific concern of a project. Aspect files are executable. An aspect has declarations and one or more processes. The process main is its entry point: it is public, it receives the parameters of the aspect, and every aspect has one. The other processes of the aspect, if any, are private: only the processes of the same aspect call them, by their bare names.

An aspect is not instantiated and has no public members: the dot operator is never used with an aspect, so worker.ingest(…) is an error. Other aspects write apply worker(10); or start worker(10);, and that runs main. Data goes in by the parameters of main and comes out by its output parameters (@). Each call creates the state of the aspect, and the state is dropped when main returns. The private processes of the aspect work on that same state. An aspect is executed with the keyword apply, in serial mode, or with start in a parallel group. Errors propagate to the caller like in any other process. An error that nobody handles ends the application. The aspect that is run captures the exit code of panic, raise, expect and assert.

Aspects are independent of each other. An aspect knows nothing of the other aspects: it sees no variable of the aspect that calls it, it can't call a member of another aspect, and no other script can reach into it. Everything it needs comes through the parameters of main, and everything it produces leaves through its output parameters. This is what lets one aspect compose others freely: an aspect can be tested alone, by running it, and moved to another application without change. Shared code belongs in a module, which every aspect may import.

Calls between aspects:

  • Any aspect may apply or start another aspect, by its name: apply for an exclusive or a concurrent aspect, start only for a concurrent one. A primary aspect is never the target of either;
  • An aspect can't call itself, and no chain of calls may come back to an aspect already in it: the compiler reports the chain;
  • Calls go at most three levels deep: the aspect that is run, an aspect it calls, and an aspect called from there, which calls no other aspect; a concurrent aspect is called only from the primary aspect;
  • Only the call of an aspect makes a level; a call of a private process never does.

Kinds of aspect:

  • exclusive aspect (the default, also written aspect): runs alone, in serial mode. It is called with apply, never with start. It may use any function and any module;
  • concurrent aspect: called with apply, or started with start in a parallel group. The compiler checks that it uses only what is thread safe;
  • primary aspect: designed to be executed first, by a command (eve report.eve). It can run alone, and no aspect can apply or start it. It is serial like an exclusive aspect (its processes are exclusive), but its parallel groups can start aspects. It is the entry point of an application and of an Eve macro.

Aspect skeleton:


** purpose of the aspect
aspect aspect_name is

  ** declarations of the aspect: private, created at each call
  ** and dropped when main returns; an aspect defines no globals
  new count = 0 :Integer;

  from $path/library_name use (*);

  ** helper work is done by functions
  procedure helper(parameter_list) is
    ...
  return;

  ** a private process: only the processes of this aspect call it
  process prepare(parameter_list) is
    ...
  return;

  ** the entry point: the only public process
  process main(parameter_list) is
    ** executable region
    ...
  recover
    ** handle all errors
    ...
  finalize
    ** release locked resources
    ...
  return;
end aspect_name;

Run an aspect

A primary aspect is run with the virtual machine: eve report.eve runs the primary aspect report. It calls the other aspects, and when its main returns the application gives control back to the Eve virtual machine. If no other process is running, the machine shuts down after it cleans up the memory.

Notes:

  • A project can have several primary aspects, each one a different use case of the same aspects and modules;
  • An aspect can read configuration files at startup;
  • The process main of a primary aspect receives the command-line arguments as its parameters;
  • The state of a primary aspect is the scope of the run: the modules it imports are bound to it;
  • No aspect applies or starts a primary aspect: it can only be run by a command;
  • An aspect is never imported; only a module is;
  • When a primary aspect is run, the aspects it calls are found and compiled in memory with it, then its main is executed.

The console REPL (parse, debug and execute a script step by step) and the daemon mode are planned for a later version. They describe the tool, not the language, so they are documented in the manual of the implementation: manual/usage.md in the eve-lang repository.

Example: a primary aspect applies another aspect

The aspect print_this is in its own file, print_this.eve. The primary aspect greeting is the one that is run: it decides when the other aspect runs and what it receives.


# the aspect that is called: it receives a parameter and prints it
exclusive aspect print_this is
  process main(p: String) is
    print p;
  return;
end print_this;

# the primary aspect: run first by a command, the entry point of the application
primary aspect greeting is
  process main is
    apply print_this("Hello");        ** serial mode: greeting waits
    apply print_this("Goodbye");
  return;
end greeting;

Run eve greeting.eve: greeting applies print_this twice. Nothing can apply greeting.

A small application can keep its aspects in one file: an Eve macro. There the aspects also share the declarations at the top of the file, and exactly one of them is the primary aspect.


Read next: Macros