General Overview
Algorithms & expressions are treated differently.
Most Jasmine users will be interested in simple expression evaluation so speed of evaluation was considered to be a priority. Algorithms have all the features of expressions but also supports variables, conditional and loop statements. It means that an algorithm compiler is more complex than one needed to compile simple expressions. This why Jasmine differentiates between algorithms and expressions.
The syntax for compiling and evaluating expressions and algorithms is slightly different but the process is
the same.
From the users perspective there is little difference between evaluating an expression or algorithm, in both
cases it is a simple 3 stage process.
Stage 1 : Initialising the compiler
The compiler has to create and initialize its internal data before it can start compiling expressions / algorithms. Unless we tell it otherwise it will do this on the first attempt to compile something. Since this takes a small but significant amount of time it is worth initializing the compiler as your application launches. It can be done with this statement
Compile.init();
This statement is omitted from the code snippets in these guides only for the sake of clarity. It is recommended that it is used in all your applications to prevent unnecessary delay on the first compilation.
For modern processors the initialization time will be well under 100ms.
Stage 2 : Compiling
In this stage the textual representation of the expression is parsed and compiled into byte code that can be executed by the JVM (Java Virtual Machine).
Jasmine parses, tokenises and creates an abstract syntax tree (AST) from the textual representation of the expression. The AST is then converted into Java byte code using ASM (a Java byte code manipulation library).
The output of the compiler is either an Expression or Algorithm object which is used to evaluate the expression as many times as we like.
Stage 3 : Evaluation
Using the object created by the compiler we can evaluate the expression using the eval(params) method of the Expression or Algorithm object. The parameters are optional but allows for different starting values. The order and data type of the parameters depends on whether we are evaluating an expression or algorithm.