Skip to content

Commit

Permalink
Add operator support for Groovy
Browse files Browse the repository at this point in the history
  • Loading branch information
Yueming Liu committed May 29, 2018
1 parent 2b6bd2b commit 5c52909
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
7 changes: 4 additions & 3 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
org.eclipse.jdt.core.compiler.source=1.8
3 changes: 3 additions & 0 deletions src/symjava/bytecode/BytecodeFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

public interface BytecodeFunc {
double apply(double ...args);
default double call(double ...args) {
return apply(args);
}
}
4 changes: 3 additions & 1 deletion src/symjava/examples/Example0.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public static void main(String[] args) {

//Just-In-Time compile the symbolic expression to native code
BytecodeFunc func = JIT.compile(new Expr[]{x,y}, Rdy);
System.out.println(func.apply(0.362, 0.556));
System.out.println(func.apply(0.362, 0.556)); //Scala function call operator
System.out.println(func.call(0.362, 0.556)); //Groovy function call operator

}
}
66 changes: 63 additions & 3 deletions src/symjava/symbolic/Expr.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,27 @@ public Expr add(double other) {
public Expr addRev(double other) {
return Add.simplifiedIns(new SymDouble(other), this);
}

/**
* Operator overload support for Groovy:
* a+b
* @param other
* @return
*/
public Expr plus(Expr other) {
return Add.simplifiedIns(this, other);
}
public Expr plus(int other) {
return Add.simplifiedIns(this, new SymInteger(other));
}
public Expr plus(long other) {
return Add.simplifiedIns(this, new SymLong(other));
}
public Expr plus(float other) {
return Add.simplifiedIns(this, new SymFloat(other));
}
public Expr plus(double other) {
return Add.simplifiedIns(this, new SymDouble(other));
}
/**
* Operator overload support:
* a-b
Expand Down Expand Up @@ -246,7 +266,27 @@ public Expr subtract(double other) {
public Expr subtractRev(double other) {
return Subtract.simplifiedIns(new SymDouble(other), this);
}

/**
* Operator overload support for Groovy:
* a-b
* @param other
* @return
*/
public Expr minus(Expr other) {
return Subtract.simplifiedIns(this, other);
}
public Expr minus(int other) {
return Subtract.simplifiedIns(this, new SymInteger(other));
}
public Expr minus(long other) {
return Subtract.simplifiedIns(this, new SymLong(other));
}
public Expr minus(float other) {
return Subtract.simplifiedIns(this, new SymFloat(other));
}
public Expr minus(double other) {
return Subtract.simplifiedIns(this, new SymDouble(other));
}
/**
* Operator overload support:
* a*b
Expand Down Expand Up @@ -314,7 +354,27 @@ public Expr divide(double other) {
public Expr divideRev(double other) {
return Divide.simplifiedIns(new SymDouble(other), this);
}

/**
* Operator overload support for Groovy:
* a/b
* @param other
* @return
*/
public Expr div(Expr other) {
return Divide.simplifiedIns(this, other);
}
public Expr divi(int other) {
return Divide.simplifiedIns(this, new SymInteger(other));
}
public Expr div(long other) {
return Divide.simplifiedIns(this, new SymLong(other));
}
public Expr div(float other) {
return Divide.simplifiedIns(this, new SymFloat(other));
}
public Expr div(double other) {
return Divide.simplifiedIns(this, new SymDouble(other));
}
/**
* Operator overload support:
* -a
Expand Down

0 comments on commit 5c52909

Please sign in to comment.