2005/5/10

     
 

CfgScript Operator Expressions

artefaktur

| Literals | Assigment | Operator | Instanceof | Delegates | Lambda |

An Operator is a special function connected to a Object or class.


Content of this chapter:

   How does CfgScript supports Operators
     Most Operators are simple class/object method
     Buildin Operator
   OperatorExpression
   PreFixOperator
   PostFixOperator
   InfixOperator
     Logical Operators && and ||
   OutfixOperator
     GroupOperator



 How does CfgScript supports Operators


CfgScript has very few really 'build in' operators.

 Most Operators are simple class/object method

Most operators are only a shortcut for a simply method call.
So for example the expression 1 + 2 is not evaluated by the CfgScript interpreter itself, but delegated to a an object:


1 + 2 
// CfgScript will interpret this as
1.operator+(2)
whereas 1 is a object instance (of  acdk::lang::dmi::DmiObject) which implements the method:

RDmiObject operator+(IN(RDmiObject) other);

You can also create your  own operator implementations in CfgScript.

 Buildin Operator

Not all operators can are implemented as operator method calls.
These operators are directly implemented by the CfgScript interpreter:
  • '=': Assignment operator
  • '&&', '||': relational operators has a special meaning (because shortcut boolean evaluation).
  • '.', '::' '->': for defining component names or object_or_class.method_or_field

 OperatorExpression


OperatorExpression
:  PreFixOperator
|  PostFixOperator
|  InfixOperator
|  OutfixOperator
;

The operator are ordered into priorities. The priority follows the syntax rules of C/C++/Java.

Operators with same priority will be evaluated from left to right:
a + b + c;
will be evaluated:
a + b => temp1; temp1 + c => result
If two operators has different priorities the expression with the higher priority will be evaluated first:
a + b * c will be evaluated:
b * c => temp1; a + temp1 => result
Most operation expression will be mapped to corresponding Class/Object methods. "A" + "B" will result in:
(new String("a")).operator+("B") or other sample:
1 * 2 will be (new DmiObject(1)).operator*(2)

 PreFixOperator

PreFixOperator
: '+' Expression // has no effect
| '-' Expression // negate the value of the expression
| '++' Expression // increment the expression with 1
| '--' Expression // decrement the expression with 1
;

The code:
-42 will be result in:
(new DmiObject(42)).operator-() The resulting expression is the result of the negation, incrementation or decrementation.

 PostFixOperator

PreFixOperator
: Expression '++' 
| Expression '--' 
;
Different to the PreFixOperation ++ and -- the resulting value is the orinal value of Expression (as in Java/C#/C++).

 InfixOperator

PreFixOperator
: Expression InfixOperator Expression
;

InfixOperator
: Expression '.' FunctionOrMember       // access operator
| Expression '->' FunctionOrMember // alias for access operator
| Expression '*' Expression         // multiply
| Expression '/' Expression         // divide
| Expression '+' Expression         // addition
| Expression '-' Expression         // substraction
| Expression '<<<' Expression // shift
| Expression '>>' Expression  // shift
| Expression '<' Expression      // less than
| Expression '>' Expression      // greater than
| Expression '>=' Expression     // greater or equal
| Expression '<=' Expression     // less or equal
| Expression '==' Expression        // equal/identical
| Expression '!=' Expression        // not equal
| Expression'&' Expression          // bitwise and
| Expression '^' Expression         // bitwise nor
| Expression '|' Expression         // bitwise or
| Expression '&&'                   // logical and, see Logical Operators
| Expression '||' Expression        // logical or, see Logical Operators
;

The infix operators '.', '->' will evaluated as namespace.class, namespace.class.method or namespace.class.member, obj.member, obj.method.

The infix operators '==' and '!=' will work on basic types different as to object types:


int a = 1;
int b = 1;
// compare with == and != on basic types is equal to 
// equals() call and compare the values.
__script.testAssert(a == b);
  
// different to Java basic types
// also support equals() method
__script.testAssert(a.equals(b) == true);


Integer i1 = new Integer(1);
Integer i2 = new Integer(1);

// compare with == and != on object types
// compares not for equality, but for identy
__script.testAssert(i1 != i2);
__script.testAssert((i1 == i2) == false);

// to test two object for equality, use the equals() method
__script.testAssert(i1.equals(i2) == true);


Beside logical operators (see below) all other infix operator will invoked as an operator call

a - b; 
// will be evaluated as
a.operator-(b)

 Logical Operators && and ||

The logical operators && and || are build in and not be delegated to the left hand argument.
The && and || can be used to implement expressional if expressions:


// second expression 'obj.toString().equals("") == false' will
// only be evaluated if first expression 'obj != Nil' is true.
if (obj != Nil && obj.toString().equals("") == false)
{
  //
}

 OutfixOperator

OutfixOperator
:  GroupOperator
|  BacktickOperator
;

 GroupOperator

GroupOperator
: '(' Expression ')'
;
The ( ... ) will be used to group expressions with lower priority the same way like in Java/C++. Cast expression like other = (MyClass)obj are not supported.