Skip to content

Commit

Permalink
Merge branch 'feature/refactor-capturing' into feature/refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kindlich committed Oct 4, 2024
2 parents 75ba801 + 55c5b9c commit 81799c9
Show file tree
Hide file tree
Showing 33 changed files with 256 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import org.openzen.zencode.shared.StringExpansion;
import org.openzen.zenscript.codemodel.OperatorType;
import org.openzen.zenscript.codemodel.expression.*;
import org.openzen.zenscript.codemodel.expression.captured.CapturedClosureExpression;
import org.openzen.zenscript.codemodel.expression.captured.CapturedLocalVariableExpression;
import org.openzen.zenscript.codemodel.expression.captured.CapturedParameterExpression;
import org.openzen.zenscript.codemodel.expression.captured.CapturedThisExpression;
import org.openzen.zenscript.codemodel.identifiers.MethodID;
import org.openzen.zenscript.codemodel.statement.ExpressionStatement;
import org.openzen.zenscript.codemodel.statement.ReturnStatement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import org.openzen.zenscript.codemodel.compilation.CompilingExpression;
import org.openzen.zenscript.codemodel.compilation.ExpressionCompiler;
import org.openzen.zenscript.codemodel.expression.CapturedClosureExpression;
import org.openzen.zenscript.codemodel.expression.CapturedExpression;
import org.openzen.zenscript.codemodel.expression.captured.CapturedClosureExpression;
import org.openzen.zenscript.codemodel.expression.captured.CapturedExpression;
import org.openzen.zenscript.codemodel.expression.LambdaClosure;

public class LocalCapturedExpression implements LocalExpression {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.openzen.zenscript.codemodel.compilation.CompilingExpression;
import org.openzen.zenscript.codemodel.compilation.ExpressionCompiler;
import org.openzen.zenscript.codemodel.compilation.expression.AbstractCompilingExpression;
import org.openzen.zenscript.codemodel.expression.CapturedParameterExpression;
import org.openzen.zenscript.codemodel.expression.captured.CapturedParameterExpression;
import org.openzen.zenscript.codemodel.expression.Expression;
import org.openzen.zenscript.codemodel.expression.LambdaClosure;
import org.openzen.zenscript.codemodel.expression.modifiable.ModifiableExpression;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.openzen.zenscript.codemodel.compilation.impl.capture;

import org.openzen.zencode.shared.CodePosition;
import org.openzen.zenscript.codemodel.FunctionParameter;
import org.openzen.zenscript.codemodel.compilation.CompilingExpression;
import org.openzen.zenscript.codemodel.compilation.ExpressionCompiler;
import org.openzen.zenscript.codemodel.compilation.expression.AbstractCompilingExpression;
import org.openzen.zenscript.codemodel.expression.*;
import org.openzen.zenscript.codemodel.expression.captured.CapturedThisExpression;
import org.openzen.zenscript.codemodel.ssa.CodeBlockStatement;
import org.openzen.zenscript.codemodel.ssa.SSAVariableCollector;
import org.openzen.zenscript.codemodel.type.TypeID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import org.openzen.zenscript.codemodel.compilation.expression.AbstractCompilingExpression;
import org.openzen.zenscript.codemodel.compilation.expression.InstanceMemberCompilingExpression;
import org.openzen.zenscript.codemodel.compilation.expression.SSACompilingVariable;
import org.openzen.zenscript.codemodel.expression.CapturedExpression;
import org.openzen.zenscript.codemodel.expression.CapturedLocalVariableExpression;
import org.openzen.zenscript.codemodel.expression.captured.CapturedLocalVariableExpression;
import org.openzen.zenscript.codemodel.expression.Expression;
import org.openzen.zenscript.codemodel.expression.LambdaClosure;
import org.openzen.zenscript.codemodel.expression.modifiable.ModifiableExpression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.openzen.zenscript.codemodel.compilation.impl.capture.LocalThisExpression;
import org.openzen.zenscript.codemodel.expression.*;
import org.openzen.zenscript.codemodel.expression.modifiable.ModifiableExpression;
import org.openzen.zenscript.codemodel.globals.IGlobal;
import org.openzen.zenscript.codemodel.identifiers.instances.FieldInstance;
import org.openzen.zenscript.codemodel.identifiers.instances.MethodInstance;
import org.openzen.zenscript.codemodel.member.ref.ImplementationMemberInstance;
Expand Down Expand Up @@ -62,7 +61,7 @@ public Optional<TypeID> getThisType() {

@Override
public Optional<CompilingExpression> getThis(CodePosition position) {
return getThisType().map(thisType -> locals.capture(position, new LocalThisExpression(position, thisType)).compile(this));
return getThisType().map(thisType -> locals.capture(new LocalThisExpression(position, thisType)).compile(this));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@ private LocalSymbols(LocalSymbols parent, FunctionHeader header, LambdaClosure c

private LocalSymbols(LocalSymbols parent, CompilingLoopStatement loop, String... loopName) {
this.parent = parent;
this.closure = parent.closure;
this.closure = null;
this.loop = loop;
this.loopName = loopName;
this.header = parent.header;
this.dollar = parent.dollar;
this.header = null;
this.dollar = null;

for (CompilingVariable loopVariable : loop.getLoopVariables())
localVariables.put(loopVariable.name, loopVariable);
}

private LocalSymbols(LocalSymbols parent, CompilingExpression dollar) {
this.parent = parent;
this.closure = parent.closure;
this.closure = null;
this.loop = null;
this.loopName = null;
this.header = parent.header;
this.header = null;
this.dollar = dollar;
}

Expand Down Expand Up @@ -118,18 +118,21 @@ public Optional<LocalExpression> findLocalVariable(CodePosition position, String
if (parent == null)
return Optional.empty();
else if (closure != null)
return parent.findLocalVariable(position, name).map(var -> var.capture(closure));
return parent.findLocalVariable(position, name).map(variable -> variable.capture(closure));
else
return parent.findLocalVariable(position, name);
}

public LocalExpression capture(CodePosition position, LocalExpression local) {
if (this.parent == null)
return local;
else if (closure != null)
return local.capture(closure);
else
return parent.capture(position, local);
public LocalExpression capture(LocalExpression local) {
if(parent != null) {
local = parent.capture(local);
}
if(closure != null) {
// watch out: if both parent and closure are present, both may add the same variable to the closure,
// which is what we want (don't try to optimize this)
local = local.capture(closure);
}
return local;
}

public Optional<CompilingExpression> getDollar() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.openzen.zenscript.codemodel.FunctionHeader;
import org.openzen.zenscript.codemodel.compilation.*;
import org.openzen.zenscript.codemodel.identifiers.TypeSymbol;
import org.openzen.zenscript.codemodel.type.TypeID;

public class MemberCompilerImpl implements MemberCompiler {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.openzen.zenscript.codemodel.expression;

import org.openzen.zenscript.codemodel.expression.captured.CapturedExpression;

public interface ExpressionVisitor<T> {
T visitAndAnd(AndAndExpression expression);

Expand All @@ -13,13 +15,7 @@ public interface ExpressionVisitor<T> {

T visitCallSuper(CallSuperExpression expression);

T visitCapturedClosure(CapturedClosureExpression expression);

T visitCapturedLocalVariable(CapturedLocalVariableExpression expression);

T visitCapturedParameter(CapturedParameterExpression expression);

T visitCapturedThis(CapturedThisExpression expression);
T visitCaptured(CapturedExpression expression);

T visitCheckNull(CheckNullExpression expression);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.openzen.zenscript.codemodel.expression;

import org.openzen.zenscript.codemodel.expression.captured.CapturedExpression;

public interface ExpressionVisitorWithContext<C, R> {
R visitAndAnd(C context, AndAndExpression expression);

Expand All @@ -13,13 +15,7 @@ public interface ExpressionVisitorWithContext<C, R> {

R visitCallSuper(C context, CallSuperExpression expression);

R visitCapturedClosure(C context, CapturedClosureExpression expression);

R visitCapturedLocalVariable(C context, CapturedLocalVariableExpression expression);

R visitCapturedParameter(C context, CapturedParameterExpression expression);

R visitCapturedThis(C context, CapturedThisExpression expression);
R visitCaptured(C context, CapturedExpression expression);

R visitCheckNull(C context, CheckNullExpression expression);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.openzen.zenscript.codemodel.expression;

import org.openzen.zenscript.codemodel.expression.captured.CapturedExpression;

import java.util.*;

public class LambdaClosure {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.openzen.zenscript.codemodel.expression;
package org.openzen.zenscript.codemodel.expression.captured;

import org.openzen.zencode.shared.CodePosition;
import org.openzen.zenscript.codemodel.expression.Expression;
import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
import org.openzen.zenscript.codemodel.expression.LambdaClosure;

import java.util.Objects;

Expand All @@ -13,21 +16,11 @@ public CapturedClosureExpression(CodePosition position, CapturedExpression value
this.value = value;
}

@Override
public <T> T accept(ExpressionVisitor<T> visitor) {
return visitor.visitCapturedClosure(this);
}

@Override
public <T> T accept(CapturedExpressionVisitor<T> visitor) {
return visitor.visitRecaptured(this);
}

@Override
public <C, R> R accept(C context, ExpressionVisitorWithContext<C, R> visitor) {
return visitor.visitCapturedClosure(context, this);
}

@Override
public Expression transform(ExpressionTransformer transformer) {
Expression tValue = transformer.transform(value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.openzen.zenscript.codemodel.expression.captured;

import org.openzen.zencode.shared.CodePosition;
import org.openzen.zenscript.codemodel.expression.Expression;
import org.openzen.zenscript.codemodel.expression.ExpressionVisitor;
import org.openzen.zenscript.codemodel.expression.ExpressionVisitorWithContext;
import org.openzen.zenscript.codemodel.expression.LambdaClosure;
import org.openzen.zenscript.codemodel.type.TypeID;

public abstract class CapturedExpression extends Expression {
public final LambdaClosure closure;

protected CapturedExpression(CodePosition position, TypeID type, LambdaClosure closure) {
super(position, type, null);

this.closure = closure;
}

@Override
public <T> T accept(ExpressionVisitor<T> visitor) {
return visitor.visitCaptured(this);
}

@Override
public <C, R> R accept(C context, ExpressionVisitorWithContext<C, R> visitor) {
return visitor.visitCaptured(context, this);
}

public abstract <T> T accept(CapturedExpressionVisitor<T> visitor);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.openzen.zenscript.codemodel.expression;
package org.openzen.zenscript.codemodel.expression.captured;

public interface CapturedExpressionVisitor<T> {
T visitCapturedThis(CapturedThisExpression expression);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.openzen.zenscript.codemodel.expression;
package org.openzen.zenscript.codemodel.expression.captured;

import org.openzen.zencode.shared.CodePosition;
import org.openzen.zenscript.codemodel.VariableDefinition;
import org.openzen.zenscript.codemodel.expression.*;

import java.util.Objects;

Expand All @@ -14,21 +15,11 @@ public CapturedLocalVariableExpression(CodePosition position, VariableDefinition
this.variable = variable;
}

@Override
public <T> T accept(ExpressionVisitor<T> visitor) {
return visitor.visitCapturedLocalVariable(this);
}

@Override
public <T> T accept(CapturedExpressionVisitor<T> visitor) {
return visitor.visitCapturedLocal(this);
}

@Override
public <C, R> R accept(C context, ExpressionVisitorWithContext<C, R> visitor) {
return visitor.visitCapturedLocalVariable(context, this);
}

@Override
public Expression transform(ExpressionTransformer transformer) {
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.openzen.zenscript.codemodel.expression;
package org.openzen.zenscript.codemodel.expression.captured;

import org.openzen.zencode.shared.CodePosition;
import org.openzen.zenscript.codemodel.FunctionParameter;
import org.openzen.zenscript.codemodel.expression.Expression;
import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
import org.openzen.zenscript.codemodel.expression.LambdaClosure;

import java.util.Objects;

Expand All @@ -14,21 +17,11 @@ public CapturedParameterExpression(CodePosition position, FunctionParameter para
this.parameter = parameter;
}

@Override
public <T> T accept(ExpressionVisitor<T> visitor) {
return visitor.visitCapturedParameter(this);
}

@Override
public <T> T accept(CapturedExpressionVisitor<T> visitor) {
return visitor.visitCapturedParameter(this);
}

@Override
public <C, R> R accept(C context, ExpressionVisitorWithContext<C, R> visitor) {
return visitor.visitCapturedParameter(context, this);
}

@Override
public Expression transform(ExpressionTransformer transformer) {
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.openzen.zenscript.codemodel.expression;
package org.openzen.zenscript.codemodel.expression.captured;

import org.openzen.zencode.shared.CodePosition;
import org.openzen.zenscript.codemodel.expression.Expression;
import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
import org.openzen.zenscript.codemodel.expression.LambdaClosure;
import org.openzen.zenscript.codemodel.type.TypeID;

import java.util.Objects;
Expand All @@ -10,21 +13,11 @@ public CapturedThisExpression(CodePosition position, TypeID type, LambdaClosure
super(position, type, closure);
}

@Override
public <T> T accept(ExpressionVisitor<T> visitor) {
return visitor.visitCapturedThis(this);
}

@Override
public <T> T accept(CapturedExpressionVisitor<T> visitor) {
return visitor.visitCapturedThis(this);
}

@Override
public <C, R> R accept(C context, ExpressionVisitorWithContext<C, R> visitor) {
return visitor.visitCapturedThis(context, this);
}

@Override
public Expression transform(ExpressionTransformer transformer) {
return this;
Expand Down
Loading

0 comments on commit 81799c9

Please sign in to comment.