Skip to content

Commit

Permalink
Partial solution for multiple evaluation in dollar expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
stanhebben committed Jun 21, 2024
1 parent 935712c commit 9ecc78a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.openzen.zenscript.codemodel.compilation;

import org.openzen.zencode.shared.CodePosition;
import org.openzen.zenscript.codemodel.compilation.expression.AbstractCompilingExpression;
import org.openzen.zenscript.codemodel.expression.Expression;
import org.openzen.zenscript.codemodel.ssa.CodeBlockStatement;
import org.openzen.zenscript.codemodel.ssa.SSAVariableCollector;

public class MemoizedCompilingExpression extends AbstractCompilingExpression {
private final CompilingExpression expression;
private Expression result;

public MemoizedCompilingExpression(ExpressionCompiler compiler, CodePosition position, CompilingExpression expression) {
super(compiler, position);

this.expression = expression;
}

@Override
public Expression eval() {
if (result == null)
result = expression.eval();
return result;
}

@Override
public void collect(SSAVariableCollector collector) {
expression.collect(collector);
}

@Override
public void linkVariables(CodeBlockStatement.VariableLinker linker) {
expression.linkVariables(linker);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class ParsedExpressionIndex extends ParsedExpression {
private final CompilableExpression value;
Expand All @@ -28,11 +27,12 @@ public ParsedExpressionIndex(CodePosition position, CompilableExpression value,
@Override
public CompilingExpression compile(ExpressionCompiler compiler) {
CompilingExpression value = this.value.compile(compiler);
ExpressionCompiler indexCompiler = compiler.withDollar(value);
CompilingExpression memoizedValue = new MemoizedCompilingExpression(compiler, position, value);
ExpressionCompiler indexCompiler = compiler.withDollar(memoizedValue);
return new Compiling(
compiler,
position,
value,
memoizedValue,
indexes.stream().map(ix -> ix.compile(indexCompiler)).toArray(CompilingExpression[]::new));
}

Expand Down Expand Up @@ -138,31 +138,4 @@ public void linkVariables(CodeBlockStatement.VariableLinker linker) {
argument.linkVariables(linker);
}
}

private static class DollarExpression extends AbstractCompilingExpression {
private final InstanceCallable getter;
private final Expression array;

public DollarExpression(ExpressionCompiler compiler, CodePosition position, InstanceCallable getter, Expression array) {
super(compiler, position);

this.getter = getter;
this.array = array;
}

@Override
public Expression eval() {
return getter.call(compiler, position, array, TypeID.NONE, CompilingExpression.NONE);
}

@Override
public void collect(SSAVariableCollector collector) {

}

@Override
public void linkVariables(CodeBlockStatement.VariableLinker linker) {

}
}
}

0 comments on commit 9ecc78a

Please sign in to comment.