This is the 14th day of my participation in the August Text Challenge.More challenges in August
Introduction to the
Prior to version 5.0, it was called Aviator and was always an expression engine that did not support if/else conditional statements (only ternary operators? :), there is no built-in support for/while loops (although you can use the SEQ library to handle collections in a functional-like manner), there is no assignment (later introduced in 4.0), there is no concept of scope (also partially implemented in 4.0 after the introduction of lambda functions), and other common language capabilities. After version 5.0, it became a scripting language called AviatorScript.
In 5.0, the following new features were added:
-
Curly braces {… } enclosing lexical scope.
-
The let statement is used to define local variables.
-
Conditional statement if/elsif/else.
-
Loop statements for and while, and corresponding break and continue statements are supported.
-
The return statement is used to return a value from a script or function.
-
fn hello() { println(“hello”); } The new FN syntax is used to define named functions.
-
## Single line comment support
-
Module system
-
The new syntax is used to create objects
-
Exception handling
-
Command line tool Aviator
use
AviatorScript can be used purely as a scripting language or in conjunction with Java.
Used purely as a scripting language
To use as a scripting language, you need to download an Aviator and use it to execute script files.
Download:
Executing the following command to download, if your computer is not installed wget tool, you can also directly open raw.githubusercontent.com/killme2008/… 支那
$ wget https://raw.githubusercontent.com/killme2008/aviator/master/bin/aviator
$ chmod u+x aviator
Copy the code
Initialization:
After downloading, you need to execute the command, which will automatically download the required dependencies in ~/. Aviatorscrip.
╰ - $aviator Downloading AviatorScript Now... % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 148 100 148 0 0 249 0 --:--:-- --:--:-- --:--:-- 248 100 159 100 159 0 0 158 0 0:00:01 0:00:01 --:--:-- 158 100 583k 100 583k 0 0 51321 0 0:00:11 0:00:11 --:--:-- 35877 Usage: java com.googlecode.aviator.Main [file] [args] : java com.googlecode.aviator.Main -e [script] : java com.googlecode.aviator.Main -vCopy the code
Execute script file:
Once you have downloaded the relevant environment, you can write a script file that usually ends in.av. This is not required, but you can use any other end if you want
Test. The av:
println("Hello World!");
Copy the code
Then execute the script file:
╰─$ aviator test.av
Hello World!
null
Copy the code
Interface output Hello World! For example, when we define return, this null will become the value of return. We make the following changes:
Test. The av:
println("Hello World!");
return "success";
Copy the code
We add a return data and execute the script file:
╰─$ aviator test.av
Hello World!
success
Copy the code
The result of the execution becomes the return data we defined.
For details, see the aviator command line
Working with Java
To work with Java, import the Aviator dependencies at [search.maven.org](search.maven.org/search?q=g:… AND a:aviator&core=gav) view available versions.
<dependency>
<groupId>com.googlecode.aviator</groupId>
<artifactId>aviator</artifactId>
<version>{version}</version>
</dependency>
Copy the code
After importing the dependency, let’s first demonstrate performing a 1+1 operation:
int result = (int) AviatorEvaluator.execute("return 1+1;");
System.out.println(result);
Copy the code
In the above code, we return the result of 1+1 directly, and then print it, but when we execute it, we return the following error:
Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
at top.mjava.demo.AviatorDemo.demo5(AviatorDemo.java:19)
at top.mjava.demo.AviatorDemo.main(AviatorDemo.java:15)
Copy the code
This is because in Aviator any Integer will be converted to Long, and Long cannot be converted to Integer, so the error will be reported. So we’ll just change int to long:
long result = (long) AviatorEvaluator.execute("return 1+1;");
System.out.println(result);
Copy the code
Output:
2
Copy the code
Mount Java methods
In Aviator, in addition to using the methods it provides to create functions, you can also mount Java custom methods and use them in Aviator scripts.
To define a custom Java method, we need to inherit the AbstractFunction abstract class and override the call and getName methods:
- Call: method concrete logic code
- GetName: Function name when used in Aviator
Define custom functions:
Here we have a custom method of adding, passing in two parameters and calculating their sum
class AddFunction extends AbstractFunction{
@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
long p1 = (long) arg1.getValue(env);
long p2 = (long) arg2.getValue(env);
long result = p1+p2;
return AviatorLong.valueOf(result);
}
@Override
public String getName(a) {
return "add"; }}Copy the code
Using custom functions:
To use this custom function in the Aviator script, you need to register the Java class and call it in the Aviator script using the method name returned by getName() as the function name:
// Register custom functions
AviatorEvaluator.addFunction(new AddFunction());
// Use custom functions
long result = (long) AviatorEvaluator.execute("Return to the add (2, 1);");
System.out.println(result);
Copy the code
Output:
3
Copy the code
The resources
- www.yuque.com/boyan-avfmj…
- Code.google.com/archive/p/a…
- Github.com/killme2008/…