Skip to content

Commit 5c52909

Browse files
author
Yueming Liu
committed
Add operator support for Groovy
1 parent 2b6bd2b commit 5c52909

File tree

4 files changed

+73
-7
lines changed

4 files changed

+73
-7
lines changed

.settings/org.eclipse.jdt.core.prefs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
eclipse.preferences.version=1
22
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3-
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
3+
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
4+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
45
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5-
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.compliance=1.8
67
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
78
org.eclipse.jdt.core.compiler.debug.localVariable=generate
89
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
910
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
1011
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11-
org.eclipse.jdt.core.compiler.source=1.7
12+
org.eclipse.jdt.core.compiler.source=1.8

src/symjava/bytecode/BytecodeFunc.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22

33
public interface BytecodeFunc {
44
double apply(double ...args);
5+
default double call(double ...args) {
6+
return apply(args);
7+
}
58
}

src/symjava/examples/Example0.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public static void main(String[] args) {
1313

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

src/symjava/symbolic/Expr.java

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,27 @@ public Expr add(double other) {
212212
public Expr addRev(double other) {
213213
return Add.simplifiedIns(new SymDouble(other), this);
214214
}
215-
215+
/**
216+
* Operator overload support for Groovy:
217+
* a+b
218+
* @param other
219+
* @return
220+
*/
221+
public Expr plus(Expr other) {
222+
return Add.simplifiedIns(this, other);
223+
}
224+
public Expr plus(int other) {
225+
return Add.simplifiedIns(this, new SymInteger(other));
226+
}
227+
public Expr plus(long other) {
228+
return Add.simplifiedIns(this, new SymLong(other));
229+
}
230+
public Expr plus(float other) {
231+
return Add.simplifiedIns(this, new SymFloat(other));
232+
}
233+
public Expr plus(double other) {
234+
return Add.simplifiedIns(this, new SymDouble(other));
235+
}
216236
/**
217237
* Operator overload support:
218238
* a-b
@@ -246,7 +266,27 @@ public Expr subtract(double other) {
246266
public Expr subtractRev(double other) {
247267
return Subtract.simplifiedIns(new SymDouble(other), this);
248268
}
249-
269+
/**
270+
* Operator overload support for Groovy:
271+
* a-b
272+
* @param other
273+
* @return
274+
*/
275+
public Expr minus(Expr other) {
276+
return Subtract.simplifiedIns(this, other);
277+
}
278+
public Expr minus(int other) {
279+
return Subtract.simplifiedIns(this, new SymInteger(other));
280+
}
281+
public Expr minus(long other) {
282+
return Subtract.simplifiedIns(this, new SymLong(other));
283+
}
284+
public Expr minus(float other) {
285+
return Subtract.simplifiedIns(this, new SymFloat(other));
286+
}
287+
public Expr minus(double other) {
288+
return Subtract.simplifiedIns(this, new SymDouble(other));
289+
}
250290
/**
251291
* Operator overload support:
252292
* a*b
@@ -314,7 +354,27 @@ public Expr divide(double other) {
314354
public Expr divideRev(double other) {
315355
return Divide.simplifiedIns(new SymDouble(other), this);
316356
}
317-
357+
/**
358+
* Operator overload support for Groovy:
359+
* a/b
360+
* @param other
361+
* @return
362+
*/
363+
public Expr div(Expr other) {
364+
return Divide.simplifiedIns(this, other);
365+
}
366+
public Expr divi(int other) {
367+
return Divide.simplifiedIns(this, new SymInteger(other));
368+
}
369+
public Expr div(long other) {
370+
return Divide.simplifiedIns(this, new SymLong(other));
371+
}
372+
public Expr div(float other) {
373+
return Divide.simplifiedIns(this, new SymFloat(other));
374+
}
375+
public Expr div(double other) {
376+
return Divide.simplifiedIns(this, new SymDouble(other));
377+
}
318378
/**
319379
* Operator overload support:
320380
* -a

0 commit comments

Comments
 (0)