Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] 4.2.0 dev #127

Merged
merged 8 commits into from
May 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 73 additions & 67 deletions src/main/java/com/googlecode/aviator/AviatorEvaluatorInstance.java

Large diffs are not rendered by default.

33 changes: 21 additions & 12 deletions src/main/java/com/googlecode/aviator/BaseExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import com.googlecode.aviator.runtime.FunctionArgument;
import com.googlecode.aviator.utils.Env;


Expand All @@ -17,15 +18,16 @@
*/
public abstract class BaseExpression implements Expression {

private List<String> varNames;
private List<String> varFullNames;
public static final String FUNC_PARAMS_VAR = "__funcs_args__";
private final List<String> varNames;
private final List<String> varFullNames;
private String expression;
protected AviatorEvaluatorInstance instance;
private Env compileEnv;
private Map<Integer, List<FunctionArgument>> funcsArgs = Collections.emptyMap();



public BaseExpression(AviatorEvaluatorInstance instance, List<String> varNames) {
public BaseExpression(final AviatorEvaluatorInstance instance, final List<String> varNames) {
super();
this.varFullNames = varNames;
this.instance = instance;
Expand All @@ -41,19 +43,23 @@ public BaseExpression(AviatorEvaluatorInstance instance, List<String> varNames)
}


public void setFuncsArgs(final Map<Integer, List<FunctionArgument>> funcsArgs) {
if (funcsArgs != null) {
this.funcsArgs = Collections.unmodifiableMap(funcsArgs);
}
}


public Env getCompileEnv() {
return compileEnv;
return this.compileEnv;
}



public void setCompileEnv(Env compileEnv) {
public void setCompileEnv(final Env compileEnv) {
this.compileEnv = compileEnv;
}



/**
* Returns the expression string when turn on {@link Options#TRACE_EVAL} option, else returns
* null.
Expand All @@ -64,7 +70,7 @@ public String getExpression() {
return this.expression;
}

public void setExpression(String expression) {
public void setExpression(final String expression) {
this.expression = expression;
}

Expand Down Expand Up @@ -96,7 +102,7 @@ public List<String> getVariableNames() {
return this.varNames;
}

protected Env newEnv(Map<String, Object> map, boolean direct) {
protected Env newEnv(final Map<String, Object> map, final boolean direct) {
Env env;
if (direct) {
env = new Env(map, map == Collections.EMPTY_MAP ? new HashMap<String, Object>() : map);
Expand All @@ -107,16 +113,19 @@ protected Env newEnv(Map<String, Object> map, boolean direct) {
return env;
}

protected Env genTopEnv(Map<String, Object> map) {
protected Env genTopEnv(final Map<String, Object> map) {
Env env =
newEnv(map, this.instance.getOptionValue(Options.USE_USER_ENV_AS_TOP_ENV_DIRECTLY).bool);
if (this.compileEnv != null && !this.compileEnv.isEmpty()) {
env.putAll(this.compileEnv);
}
if (!this.funcsArgs.isEmpty()) {
env.put(FUNC_PARAMS_VAR, this.funcsArgs);
}
return env;
}

protected Env newEnv(Map<String, Object> map) {
protected Env newEnv(final Map<String, Object> map) {
return newEnv(map, false);
}

Expand Down
34 changes: 25 additions & 9 deletions src/main/java/com/googlecode/aviator/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public enum Options {
*/
ALWAYS_PARSE_FLOATING_POINT_NUMBER_INTO_DECIMAL,
/**
* When true, always parsing long number into BigDecial, default is false.
* When true, always parsing integral number into BigDecial, default is false.
*
* @since 4.2.0
*/
ALWAYS_PARSE_INTEGRAL_NUMBER_INTO_DECIMAL,
/**
Expand All @@ -60,6 +62,14 @@ public enum Options {
*/
PUT_CAPTURING_GROUPS_INTO_ENV,

/**
* Whether to capture the function arguments(at invocation) into env, the argument list will be
* stored in __args__ variable in env valid for function body. Default is false(disabled).
*
* @since 4.2.0
*/
CAPTURE_FUNCTION_ARGS,

/**
* Enable property access syntax sugar, use common-beantuils to access property such as "a.b.c"
* etc. Default value is true, enable this behaviour.
Expand Down Expand Up @@ -105,24 +115,25 @@ public static class Value {
public MathContext mathContext;
public int level;

public Value(boolean bool) {
public Value(final boolean bool) {
super();
this.bool = bool;
}

public Value(MathContext mathContext) {
public Value(final MathContext mathContext) {
super();
this.mathContext = mathContext;
}

public Value(int level) {
public Value(final int level) {
super();
this.level = level;
}

@Override
public String toString() {
return "Value [bool=" + bool + ", mathContext=" + mathContext + ", level=" + level + "]";
return "Value [bool=" + this.bool + ", mathContext=" + this.mathContext + ", level="
+ this.level + "]";
}


Expand All @@ -134,7 +145,7 @@ public String toString() {
* @param val
* @return
*/
public Object intoObject(Value val) {
public Object intoObject(final Value val) {
switch (this) {
case ALWAYS_USE_DOUBLE_AS_DECIMAL:
case ALWAYS_PARSE_FLOATING_POINT_NUMBER_INTO_DECIMAL:
Expand All @@ -146,6 +157,7 @@ public Object intoObject(Value val) {
case NIL_WHEN_PROPERTY_NOT_FOUND:
case USE_USER_ENV_AS_TOP_ENV_DIRECTLY:
case DISABLE_ASSIGNMENT:
case CAPTURE_FUNCTION_ARGS:
return val.bool;
case OPTIMIZE_LEVEL: {
return val.level;
Expand All @@ -162,7 +174,7 @@ public Object intoObject(Value val) {
* @param val
* @return
*/
public Value intoValue(Object val) {
public Value intoValue(final Object val) {
switch (this) {
case ALWAYS_USE_DOUBLE_AS_DECIMAL:
case ALWAYS_PARSE_FLOATING_POINT_NUMBER_INTO_DECIMAL:
Expand All @@ -173,6 +185,7 @@ public Value intoValue(Object val) {
case ENABLE_PROPERTY_SYNTAX_SUGAR:
case NIL_WHEN_PROPERTY_NOT_FOUND:
case USE_USER_ENV_AS_TOP_ENV_DIRECTLY:
case CAPTURE_FUNCTION_ARGS:
case DISABLE_ASSIGNMENT:
return ((boolean) val) ? TRUE_VALUE : FALSE_VALUE;
case OPTIMIZE_LEVEL: {
Expand All @@ -189,7 +202,7 @@ public Value intoValue(Object val) {
throw new IllegalArgumentException("Fail to cast value " + val + " for option " + this);
}

public boolean isValidValue(Object val) {
public boolean isValidValue(final Object val) {
switch (this) {
case ALWAYS_USE_DOUBLE_AS_DECIMAL:
case ALWAYS_PARSE_FLOATING_POINT_NUMBER_INTO_DECIMAL:
Expand All @@ -201,6 +214,7 @@ public boolean isValidValue(Object val) {
case NIL_WHEN_PROPERTY_NOT_FOUND:
case USE_USER_ENV_AS_TOP_ENV_DIRECTLY:
case DISABLE_ASSIGNMENT:
case CAPTURE_FUNCTION_ARGS:
return val instanceof Boolean;
case OPTIMIZE_LEVEL:
return val instanceof Integer && (((Integer) val).intValue() == AviatorEvaluator.EVAL
Expand Down Expand Up @@ -228,7 +242,7 @@ public boolean isValidValue(Object val) {
* @return
*/
public Object getDefaultValue() {
return this.intoObject(this.getDefaultValueObject());
return intoObject(getDefaultValueObject());
}


Expand Down Expand Up @@ -259,6 +273,8 @@ public Value getDefaultValueObject() {
return TRUE_VALUE;
case USE_USER_ENV_AS_TOP_ENV_DIRECTLY:
return TRUE_VALUE;
case CAPTURE_FUNCTION_ARGS:
return FALSE_VALUE;
case DISABLE_ASSIGNMENT:
return FALSE_VALUE;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/googlecode/aviator/code/CodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
**/
package com.googlecode.aviator.code;

import java.util.List;
import com.googlecode.aviator.Expression;
import com.googlecode.aviator.lexer.token.Token;
import com.googlecode.aviator.parser.Parser;
import com.googlecode.aviator.runtime.FunctionArgument;


/**
Expand Down Expand Up @@ -125,7 +127,7 @@ public interface CodeGenerator {

public void onMethodParameter(Token<?> lookhead);

public void onMethodInvoke(Token<?> lookhead);
public void onMethodInvoke(Token<?> lookhead, List<FunctionArgument> params);

public void onLambdaDefineStart(Token<?> lookhead);

Expand Down
Loading