Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlogic committed Jan 28, 2024
1 parent cf15935 commit e40b9f8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/main/java/arb/expressions/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ public Node<D, R, F> resolveFunctionInvocationOrVariableReference(int depth,
{
if ("when".equals(reference.name))
{
return When.evaluate(this, depth + 1);
return new When<>(this, depth);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public MethodVisitor prepareStackForReuse(MethodVisitor mv)
}
}

protected final Node<D, R, F> arg;
protected Node<D, R, F> arg;

public UnaryOperation(Node<D, R, F> node, Expression<D, R, F> expression, int depth)
{
Expand Down
65 changes: 21 additions & 44 deletions src/main/java/arb/expressions/nodes/unary/When.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ public MethodVisitor generate(MethodVisitor mv, Class<?> resultType)
.collect(Collectors.toList())
.toArray(new Label[cases.size()]);

cases.entrySet()
.stream()
.map(entry -> new Label())
.collect(Collectors.toList())
.toArray(new Label[cases.size()]);

cases.values().forEach(val ->
{
val.isResult = isResult;
Expand Down Expand Up @@ -113,62 +107,46 @@ public When(Node<D, R, F> node, Expression<D, R, F> expression, int depth)
depth);
}

public When(Expression<D, R, F> expression,
TreeMap<Integer, Node<D, R, F>> cases,
Node<D, R, F> defaultValue,
int depth)
public When(Expression<D, R, F> expression, int depth)
{
super(defaultValue,
super(null,
expression,
depth);
this.cases = cases;
}

@Override
public String typeset()
{
return "when";
}

@Override
public Class<?> type()
{
return expression.rangeType;
}

public static <D, R, F extends Function<D, R>> Node<D, R, F> evaluate(Expression<D, R, F> expression, int depth)
{
TreeMap<Integer, Node<D, R, F>> cases = new TreeMap<>();
Node<D, R, F> defaultValue = null;
cases = new TreeMap<>();

do
{
defaultValue = evaluateValue(expression, depth, cases);
evaluateValue(depth, cases);
}
while (expression.nextCharacterIs(depth + 1, ','));
if (!expression.nextCharacterIs(depth + 1, ')'))
{
throw new ExpressionCompilerException("Closing parenthesis expected at position=" + expression.position
+ " of expression=" + expression);
}
if (defaultValue == null)
if (arg == null)
{
throw new ExpressionCompilerException("default value of when function not specified with else keyword at position="
+ expression.position + " of expression=" + expression);
}
return new When<D, R, F>(expression,
cases,
defaultValue,
depth + 1);
}

public static <D, R, F extends Function<D, R>> Node<D, R, F> evaluateValue(Expression<D, R, F> expression,
int depth,
TreeMap<Integer, Node<D, R, F>> cases)
@Override
public String typeset()
{
return "when";
}

@Override
public Class<?> type()
{
return expression.rangeType;
}

public void evaluateValue(int depth, TreeMap<Integer, Node<D, R, F>> cases2)
{
Node<D, R, F> value = null;

Node<D, R, F> node = expression.evaluate(depth + 1);
Node<D, R, F> node = expression.evaluate(depth + 1);
if (!(node instanceof Variable))
{
throw new ExpressionCompilerException("condition of when statement must be the equality of the input variable, but got "
Expand All @@ -179,13 +157,12 @@ public static <D, R, F extends Function<D, R>> Node<D, R, F> evaluateValue(Expre

if ("else".equals(variable.reference.name))
{
value = evaluateDefaultValue(expression, depth);
arg = evaluateDefaultValue(expression, depth);
}
else
{
evaluateConditionalValue(expression, depth, cases, variable);
evaluateConditionalValue(expression, depth, cases2, variable);
}
return value;
}

private static <D, F extends Function<D, R>, R>
Expand Down

0 comments on commit e40b9f8

Please sign in to comment.