This is the 9th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Multiline expressions and returns

AviatorScript supports multi-line expressions, which must be separated by semicolons. Separate, support newlines, and we’ve seen many examples before:

## examples/statements.av

let a = 1;
let b = 2;
c = a + b;
Copy the code

The entire script returns the result of the last expression by default. Note, however, that adding a semicolon fixes the result of the entire expression to nil, so if you execute the script above and print the result, it must be null, not the value of C:

package com.googlecode.aviator.example; import com.googlecode.aviator.AviatorEvaluator; import com.googlecode.aviator.Expression; import com.googlecode.aviator.Options; /** * Run a script under examples folder. * * @author dennis([email protected]) * */ public class RunScriptExample { public static void main(final String[] args) throws Exception { // You can try to test every script in examples folder by changing the file name. Expression exp = AviatorEvaluator.getInstance().compileScript("examples/statements.av"); Object result = exp.execute(); System.out.println(result); }}Copy the code

Exp. Execute returns a result that prints null:

result: null
Copy the code

If you want to return the value of the expression, not nil, just leave the last expression without a semicolon

## examples/statements_result.av

let a = 1;
let b = 2;
c = a + b
Copy the code

Execute returns the value of c = a +b, and the result of the assignment statement is the rvalue, which is 3.

Any expression in AviatorScript has a value that is discarded to nil by adding a semicolon.

Instead of returning without a semicolon, you can use the return statement to specify the return:

## examples/statements_return.av

let a = 1;
let b = 2;
c = a + b;

return c;
Copy the code

Note that the return statement must be complete with a semicolon, otherwise a syntax error will be reported

Return is also used to return ahead of time. In combination with conditional statements, more complex logic can be made:

## examples/if_return.av

if a < b {
  return "a is less than b.";
}

return a - b;
Copy the code

Then we pass in variables a and b to test them:

package com.googlecode.aviator.example; import com.googlecode.aviator.AviatorEvaluator; import com.googlecode.aviator.Expression; import com.googlecode.aviator.Options; /** * Run a script under examples folder. * * @author dennis([email protected]) * */ public class RunScriptExample { public static void main(final String[] args) throws Exception { // You can try to test every script in examples folder by changing the file name. Expression exp = AviatorEvaluator.getInstance().compileScript("examples/if_return.av"); Object result = exp.execute(exp.newEnv("a", 9, "b", 1)); System.out.println("result: " + result); result = exp.execute(exp.newEnv("a", 1, "b", 9)); System.out.println("result: " + result); }}Copy the code

Execute different return statements:

result: 8
result: a is less than b.
Copy the code

Notice that here we use Expression#newEnv(key1, value1, key2, value2…) This is the recommended approach and will perform slightly better than constructing a HashMap directly.