Skip to content

Commit

Permalink
add more expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
chaokunyang committed Jan 27, 2025
1 parent d7fca40 commit 95be258
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ public String toString() {

/** A Reference is a variable/field that can be accessed in the expression's CodegenContext. */
class Reference implements Expression {
private final String name;
private String name;
private final TypeRef<?> type;
private final boolean nullable;
private final boolean fieldRef;
Expand Down Expand Up @@ -478,6 +478,10 @@ public static Reference fieldRef(String name, TypeRef<?> type) {
return new Reference(name, type, false, true);
}

public void setName(String name) {
this.name = name;
}

@Override
public TypeRef<?> type() {
return type;
Expand Down Expand Up @@ -2095,6 +2099,10 @@ public While(Expression predicate, Expression action) {
this(predicate, action, new Expression[0]);
}

/**
* Use lambda to create a new context, and by capturing variables, we can make the codegen of
* thoese variable expressions happen before while loop.
*/
public While(Expression predicate, SerializableSupplier<Expression> action) {
this(
predicate,
Expand Down Expand Up @@ -2150,8 +2158,7 @@ public String toString() {
class ForEach implements Expression {
private Expression inputObject;

@ClosureVisitable
private final SerializableBiFunction<Expression, Expression, Expression> action;
@ClosureVisitable final SerializableBiFunction<Expression, Expression, Expression> action;

private final TypeRef<?> elementType;

Expand Down Expand Up @@ -2415,8 +2422,9 @@ class ForLoop implements Expression {
public Expression start;
public Expression end;
public Expression step;

@ClosureVisitable public final SerializableFunction<Expression, Expression> action;
private final Class<?> maxType;
private final Reference iref;
public Expression loopAction;

public ForLoop(
Expression start,
Expand All @@ -2426,7 +2434,11 @@ public ForLoop(
this.start = start;
this.end = end;
this.step = step;
this.action = action;
this.maxType = maxType(getRawType(start.type()), getRawType(end.type()));
Preconditions.checkArgument(maxType.isPrimitive());
iref = new Reference(String.valueOf(System.identityHashCode(this)), TypeRef.of(maxType));
this.loopAction = action.apply(iref);
;
}

@Override
Expand All @@ -2436,12 +2448,9 @@ public TypeRef<?> type() {

@Override
public ExprCode doGenCode(CodegenContext ctx) {
Class<?> maxType = maxType(getRawType(start.type()), getRawType(end.type()));
Preconditions.checkArgument(maxType.isPrimitive());
StringBuilder codeBuilder = new StringBuilder();
String i = ctx.newName("i");
Reference iref = new Reference(i, TypeRef.of(maxType));
Expression loopAction = action.apply(iref);
iref.setName(i);
ExprCode startExprCode = start.genCode(ctx);
ExprCode endExprCode = end.genCode(ctx);
ExprCode stepExprCode = step.genCode(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.fury.codegen.Expression.BitAnd;
import org.apache.fury.codegen.Expression.BitOr;
import org.apache.fury.codegen.Expression.BitShift;
import org.apache.fury.codegen.Expression.Cast;
import org.apache.fury.codegen.Expression.ListExpression;
import org.apache.fury.codegen.Expression.LogicalAnd;
Expand Down Expand Up @@ -84,6 +87,10 @@ public static LogicalAnd and(Expression left, Expression right, String name) {
return new LogicalAnd(false, left, right);
}

public static LogicalAnd and(Expression left, Expression right) {
return new LogicalAnd(true, left, right);
}

public static LogicalOr or(Expression left, Expression right, Expression... expressions) {
LogicalOr logicalOr = new LogicalOr(left, right);
for (Expression expression : expressions) {
Expand All @@ -92,6 +99,14 @@ public static LogicalOr or(Expression left, Expression right, Expression... expr
return logicalOr;
}

public static BitOr bitor(Expression left, Expression right) {
return new BitOr(left, right);
}

public static BitAnd bitand(Expression left, Expression right) {
return new BitAnd(left, right);
}

public static Not not(Expression target) {
return new Not(target);
}
Expand Down Expand Up @@ -167,6 +182,22 @@ public static Arithmetic subtract(Expression left, Expression right, String valu
return arithmetic;
}

public static BitShift shift(String op, Expression target, int numBits) {
return new BitShift(op, target, numBits);
}

public static BitShift leftShift(Expression target, int numBits) {
return new BitShift("<<", target, numBits);
}

public static BitShift arithRightShift(Expression target, int numBits) {
return new BitShift(">>", target, numBits);
}

public static BitShift logicalRightShift(Expression target, int numBits) {
return new BitShift(">>", target, numBits);
}

public static Cast cast(Expression value, TypeRef<?> typeRef) {
return new Cast(value, typeRef);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ public static ExprHolder of(
return new ExprHolder(k1, v1, k2, v2, k3, v3, k4, v4);
}

public static ExprHolder of(
String k1,
Expression v1,
String k2,
Expression v2,
String k3,
Expression v3,
String k4,
Expression v4,
String k5,
Expression v5) {
return new ExprHolder(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5);
}

public Expression get(String key) {
return expressionsMap.get(key);
}
Expand Down

0 comments on commit 95be258

Please sign in to comment.