Defining Operator Semantics

StringAddition.lf:
%{

  // define string concatenation
  
  static class StringAddition implements IBinaryEvaluator
  {
      private java.lang.reflect.Type fBaseType;
        
      public StringAddition() throws ClassNotFoundException
      {
         fBaseType = Class.forName( "LambdaF.StringValue" );
      }
        
      public java.lang.reflect.Type getBaseType()
      {
         return fBaseType;
      }
  
      public Value compute( Value aLeft, Value aRight ) throws ArgumentException 
      {
         if ( (aLeft instanceof StringValue) && (aRight instanceof StringValue) )
            {
               String lLeft = ((StringValue)aLeft).getValue();
               String lRight = ((StringValue)aRight).getValue();

               return new StringValue( lLeft.concat( lRight ) );
            }
         else
            throw new ArgumentException( "argument type mismatch" );
      }

      public int getOperator() 
      {
         return Operator.PLUS;
      }
  }

}%

let

in

  // initialize module

  %{
     try
     {
        Value.ValueEvaluator.registerEvaluator( new StringAddition() );
     }
     catch (Exception e)
     {
        System.out.println( "Warning: String addition ignored!" );
     }
         
        return EmptyForm.EMPTY;
  }% (||)

end
    
Application:
let

  load "StringAddition.lf"
  load "Services.lf"

in

  PrintLn ("Hello" + " World!")

end
    

Last modified: 2006-02-11 — Markus Lumpe