Java Q&A
by Christophe de Dinechin


Example 1: 
tone Point : CodaTerminal {
     Point(int xx, int yy): x(xx), y(yy) {}
     ref int x;
     ref int y;
};


Example 2: 
void performer(Offset) (CodaTerminal *left, CodaTerminal *right);
void perform(Offset, CodaInteger) (CodaInteger *left, CodaInteger *right) {
    left->value += right->value;
}
    void perform(Offset, Point) (Point *left, Point *right) {
      left->x += right->x;
      left->y += right->y;
    }


Listing One
// This example demonstrates the symbolic derivation "plugin"
class Test
{
    public static final double omega = 3.276;
    public static final double theta = 0.227;
    public static final double decay = 1.447E-3;
    public static int main(String args[]) {
        // Tabulate the following expression
        for (double t = 0.0; t < 50.0; t += 0.01) {
            double y = d(Math.sin(2 * omega * t + theta)
                         * Math.exp(-decay * t))/dt;
            System.out.println("t=" + t + ", y=" + y);
        }
        return 0;
    }
}

Listing Two
[~/Development/mozart/moka] ddd% ./moka tests/derivation.java
+derivation -o /tmp/derivation.java ; javac /tmp/derivation.java
[~/Development/mozart/moka] ddd% cat /tmp/derivation.java
/* Generated by Moka using moka.stylesheet */
// This example demonstrates the symbolic derivation "plugin"
class Test
{
   public static final double omega = 3.276;
   public static final double theta = 0.227;
   public static final double decay = 0.001447;
   public static int main(String[] args)
   {
      // Tabulate the following expression
      for(double t = 0; t < 50; t += 0.01)
      {
         double y = Math.cos (2 * omega * t + theta) * ((0 * omega + 2 * 0)
                                                        * t + 2 * omega * 1
                                                        + 0) * Math.exp
                    (-decay * t) + Math.sin (2 * omega * t + theta)
                    * (Math.exp (-decay * t) * (-decay * t + -decay * 1));
         System.out.println ("t=" + t + ", y=" + y);
      }
      return 0;
   }
}
/* Thank you for using Moka. */
[~/Development/mozart/moka] ddd%

Listing Three
[~/Development/mozart/moka] ddd% ./moka tests/derivation.java
+derivation +constantfold -out
/* Generated by Moka using moka.stylesheet */
// This example demonstrates the symbolic derivation "plugin"
class Test
{
   public static final double omega = 3.276;
   public static final double theta = 0.227;
   public static final double decay = 0.001447;
   public static int main(String[] args)
   {
      // Tabulate the following expression
      for(double t = 0; t < 50; t += 0.01)
      {
         double y = Math.cos (2 * omega * t + theta) * (2 * omega) * Math.exp
                    (-(decay * t)) + Math.sin (2 * omega * t + theta)
                    * -(Math.exp (-(decay * t)) * (decay + decay * t));
         System.out.println ("t=" + t + ", y=" + y);
      }
      return 0;
   }
}
/* Thank you for using Moka. */

Listing Four
class Stack
{
    public Stack()              { objects = new Object[MAX]; top = 0;
      }
    public void Push(Object o)  { objects[top] = o; top++; }
    public Object Pop()         { top--;assert(top >= 0);return
      objects[top]; }
    public int Size()           { return top; }
    private int Remaining()     { return MAX - top; }
    private Object[] objects;
    private int top;
    private static final int MAX = 10;
    preconditions()
    {
    Push: top < MAX;
    Pop:  top > 0;
    }
    postconditions()
    {
    Push: top > 0;
    }
    invariants()
    {
        top >= 0;
        top <= MAX;
    }
}

Listing Five
/* Generated by Moka using moka.stylesheet */
class Stack
{
   Stack()
   {
      objects = new Object[MAX];
      top = 0;
   }
   public void Push(Object o)
   {
      try
      {
         if(!(top >= 0))
            assertion_failed ("invariant #1 on entry in Push", 1);
         if(!(top <= MAX))
            assertion_failed ("invariant #2 on entry in Push", 2);
         if(!(top < MAX))
            assertion_failed ("Push precondition #1", 0);
         objects[top] = o;
         top++;
      }
      finally
      {
         if(!(top > 0))
            assertion_failed ("Push postcondition #1", 3);
         if(!(top >= 0))
            assertion_failed ("invariant #1 on exit from Push", 4);
         if(!(top <= MAX))
            assertion_failed ("invariant #2 on exit from Push", 5);
      }
   }
   public Object Pop()
   {
      try
      {
         if(!(top >= 0))
            assertion_failed ("invariant #1 on entry in Pop", 7);
         if(!(top <= MAX))
            assertion_failed ("invariant #2 on entry in Pop", 8);
         if(!(top > 0))
            assertion_failed ("Pop precondition #1", 6);
         top--;
         if(!(top >= 0))
            assertion_failed ("Pop assert #11", 11);
         return objects[top];
      }
      finally
      {
         if(!(top >= 0))
            assertion_failed ("invariant #1 on exit from Pop", 9);
         if(!(top <= MAX))
            assertion_failed ("invariant #2 on exit from Pop", 10);
      }
   }
   public int Size()
   {
      try
      {
         if(!(top >= 0))
            assertion_failed ("invariant #1 on entry in Size", 12);
         if(!(top <= MAX))
            assertion_failed ("invariant #2 on entry in Size", 13);
         return top;
      }
      finally
      {
         if(!(top >= 0))
            assertion_failed ("invariant #1 on exit from Size", 14);
         if(!(top <= MAX))
            assertion_failed ("invariant #2 on exit from Size", 15);
      }
   }
   private int Remaining()
   {
      return MAX - top;
   }
   private Object[] objects;
   private int top;
   private static final int MAX = 10;
}
/* Thank you for using Moka. */

Listing Six
/* Generated by Moka using moka.stylesheet */
class Stack
{
   Stack()
   {
      objects = new Object[MAX];
      top = 0;
   }
   public void Push(Object o)
   {
      objects[top] = o;
      top++;
   }
   public Object Pop()
   {
      top--;
      // Nop;
      return objects[top];
   }
   public int Size()
   {
      return top;
   }
   private int Remaining()
   {
      return MAX - top;
   }
   private Object[] objects;
   private int top;
   private static final int MAX = 10;
}
/* Thank you for using Moka. */


Listing Seven
class Point {
        int x, y;
        Point() { System.out.println("default"); }
        Point(int x, int y) { this.x = x; this.y = y; }
        // Point instance is explicitly created at class initialization time:
        static Point origin = new Point(0,0);
        // A String can be implicitly created by a + operator:
        public String toString() {
                return "(" + x + "," + y + ")";
        }
}

Listing Eight
/* Generated by Moka using moka.stylesheet */
class Point
{
   int x, y;
   Point()
   {
      try
      {
         BeenThere ("Point", 0);
         System.out.println ("default");
      }
      finally
      {
         DoneThat ("Point", 0);
      }
   }
   Point(int x, int y)
   {
      try
      {
         BeenThere ("Point", 1);
         this .x = x;
         this .y = y;
      }
      finally
      {
         DoneThat ("Point", 1);
      }
   }
   // A Point instance is explicitly created at class initialization time:
   static Point origin = new Point (0, 0);
   // A String can be implicitly created by a + operator:
   public String toString()
   {
      try
      {
         BeenThere ("toString", 2);
         return "(" + x + "," + y + ")";
      }
      finally
      {
         DoneThat ("toString", 2);
      }
   }
}
/* Thank you for using Moka. */

Listing Nine
#include "mozart.h"
ulong uid = 0;
int main(int argc, char **argv)
{
    if (argc < 2)
    {
        fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
        exit(1);
    }
    // Initialize, parse parameters and read input
    Tool::Init();
    GCRoot<Note> top = Melody::Read(argv[1]);
    // Loop on all notes, and identify declarations of interest
    NoteIterator it;
    for (Note **n = it.First(&top.ptr); n; n = it.Next())
    {
        CodaDeclaration *dcl = (CodaDeclaration *) *n;
        if (dcl
            && dcl->IsA(CodaDeclaration_T)
            && dcl->initializer
            && dcl->decl_type
            && dcl->decl_type->IsA(CodaFunctionType_T)
            && dcl->initializer->IsA(CodaBlock_T)
            && dcl->name
            && dcl->name->IsA(CodaName_T))
        {
            CodaBlock *block = (CodaBlock *) dcl->initializer;
            String name = ((CodaName *) dcl->name)->value;
            Tune<CodaExpression *> args;
            // Create the "BeenThere" and "DoneThat" calls
            args.Append(new CodaString(name)).Append(new CodaInteger(uid++));
            CodaCall *pre = new CodaCall(new CodaName("BeenThere"),args);
            CodaCall *post = new CodaCall(new CodaName("DoneThat"),args);
            // Insert "BeenThere" at beginning
            block->instructions.Insert(0, pre);
            // Create finally block for "DoneThat"
            CodaBlock *final = new CodaBlock;
            final->instructions.Append(post);
            Tune<CodaCatch *> finally_clauses;
            finally_clauses.Append(new CodaFinally(final));
            CodaTry *only_statement = new CodaTry(block,finally_clauses);
            block = new CodaBlock();
            block->instructions.Append(only_statement);
            dcl->initializer = block;
        }
    }
    GC();
    Melody::Write(argv[1], top);
    return 0;
}




1

