Shop OBEX P1 Docs P2 Docs Learn Events
operator overloading — Parallax Forums

operator overloading

Peter VerkaikPeter Verkaik Posts: 3,956
edited 2006-04-26 21:27 in General Discussion
Hi,
Java does not support· operator overloading natively.
The utility subjava converts a .subjava source into a .java source
that can be compiled.

For example, the file TestForFloat32.subjava
substitutions stamp.math.forFloat32;
public class TestForFloat32 {
·· public static void main() {
····· stamp.math.Float32 c1 = 1,· c2 = -5;
····· stamp.math.Float32 c3 = new stamp.math.Float32( 1 );
····· stamp.math.Float32 c4 = c1 * c2 / c3;
····· stamp.math.Float32 c6 = 7 * c2 / 8;
····· stamp.math.Float32 c7 = c3 + c4 / c1 - c2;
·· }
}

converts to file TestForFloat32.java
public class TestForFloat32 {
·· public static void main() {
····· stamp.math.Float32 c1 = new stamp.math.Float32( 1),· c2 = new stamp.math.Float32( -5);
····· stamp.math.Float32 c3 = new stamp.math.Float32( 1 );
····· stamp.math.Float32 c4 = c1.multiply( c2 ).divide( c3 );
····· stamp.math.Float32 c6 = c2.multiply( 7 ).divide( 8 );
····· stamp.math.Float32 c7 = c3.add( c4.divide( c1 ) ).subtract( c2 );
·· }
}

To test this for yourself, you need to have installed the Java 2 runtime environment JRE
which you can download here:
http://java.sun.com/j2se/1.5.0/download.jsp

install JRE

copy TestForFloat32.subjava and subjava.zip to c:\program files\parallax inc\javelin stamp ide\projects
copy forFloat32.substitutions to c:\program files\parallax inc\javelin stamp ide\lib\stamp\math

open a command prompt window
move to folder c:\program files\parallax inc\javelin stamp ide\projects

give command
java -cp subjava.zip;"c:\program files\parallax inc\javelin stamp ide\lib"; subjava.SubJava TestForFloat32.subjava

That should generate the TestForFloat32.java file in folder projects.


I like some comments on this.

regards peter

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-04-20 21:34
    To make sources more readable set import before substitutions.

    [noparse][[/noparse]TestForFloat32.subjava]
    import stamp.math.*;
    substitutions forFloat32;
    public class TestForFloat32 {
    ·· public static void main() {
    ····· Float32 c1 = 1,· c2 = -5;
    ····· Float32 c3 = new Float32( 1 );
    ····· Float32 c4 = c1 * c2 / c3;
    ····· Float32 c6 = 7 * c2 / 8;
    ····· Float32 c7 = c3 + c4 / c1 - c2;
    ·· }
    }

    [noparse][[/noparse]TestForFloat32.java]
    import stamp.math.*;
    public class TestForFloat32 {
    ·· public static void main() {
    ····· Float32 c1 = new Float32( 1),· c2 = new Float32( -5);
    ····· Float32 c3 = new Float32( 1 );
    ····· Float32 c4 = c1.multiply( c2 ).divide( c3 );
    ····· Float32 c6 = c2.multiply( 7 ).divide( 8 );
    ····· Float32 c7 = c3.add( c4.divide( c1 ) ).subtract( c2 );
    ·· }
    }

    regards peter
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-04-26 21:27
    If you look at the Float32 overloading example you notice
    that the variables used in a formula get changed.
    So I figured a way to prevent this.
    I wrote a small class that does Int32 math, but it has
    methods that accepts 2 operands and returns a temporary
    result.

    In essence,
    ··· f = (a+b)-(c+d)*e+e;
    gets converted to
    ··· I32.set( f, I32.add( I32.sub( (I32.add( a, b )), I32.mul( (I32.add( c, d )), e ) ), e ) );

    I used a four level stack to do the calculation.
    The output from the test program is:

    Program Int32_overloaded_test
    a = 0x01234567
    b = 0x11112222
    c = 0x01234567
    d = 0x01110222
    e = 0x00000001
    calculate f = (a+b)-(c+d)*e+e
    add:sp=1
    add:sp=2
    mul:sp=2
    sub:sp=1
    add:sp=1
    set:sp=0
    f = 0x10002001
    program finished

    Note that the variables a to e are not changed,
    just as if longs are natively available.

    I believe a four level stack (see I32.java) is sufficient for any expression
    (my HP 15C calculator also uitilizes a four level stack).
    The only requirement is that there must be an assignment.
    So one can not use myfunc(a+b) but must use
    c = a+b;
    myfunc(c);
    otherwise there will be stack errors.

    regards peter

    Post Edited (Peter Verkaik) : 4/27/2006 11:23:01 AM GMT
Sign In or Register to comment.