Sunday, November 18, 2007

Semantics of Operator Overloading and Expressions II

In my first blog entry I explained how the Mynx compiler translates operator expressions into complete method expressions. In effect, the compiler is resolving the operator overloads with the methods that are overloaded for a given type or class and the operator.

There are other use cases of expression statements such as access operators and non-overloadable operators. While the algorithm to translate from an operator expression to method expression does not change, the type of code synthesis does -- depending upon the underlying high-level language target.

Expressions with Access Operator: Array, Attribute, and Method



The access operators access or invoke access on a language element. The access operators invoke:

  1. Array - access element within an array using [ ] brackets.

  2. Attribute - access element within a class using ` tic

  3. Method - access invoke method of a class using . dot



Note the literal integer of 1 is translated into literal object LIT_INT_1 in the code synthesis so that literals are instance objects.

Array



An example of a Mynx expression with an array access operator is:

x = y[0] + 1;
x y[0] 1 + =
x y[0].add(1) =
x.set(y[0].add(1));
x.set(y[0].add(LIT_INT_1));


Attribute



An example of a Mynx expression with an attribute access operator is:


x = y + this`z;
x y this`z + =
x y.add(this.z_) =
x.set(y.add(this.z_));


Method



An example of a Mynx expression with an method access operator is:


x = this.fact(1) + this`y;
x this.fact(1) this`y + =
x this.fact(1).add(this.y_) =
x.set(this.fact(1).add(this.y_));


Mixed



An example of a Mynx expression with a mix of access operators is:


x = y[0] + this.fact(0) * this`z - 0;
x y[0] this.fact(0) this`z 0 - * + =
x y[0] this.fact(0) [ this`z 0 - ] * + =
x y[0] [ this.fact(0) this.z_.sub(0) * ] + =
x [ y[0] this.fact(0).mul(this.z_.sub(0)) + ] =
x [ y[0] this.fact(0).mul(this.z_.sub(0)) + ]=
x [ y[0].add(this.fact(0).mul(this.z_.sub(0))) = ]
x.set(y[0].add(this.fact(0).mul(this.z_.sub(0))));


Non-Overloadable Operator Expression to Method



Not all the operators in Mynx are overloadable either directly, such as + or -, or indirectly such as += or *=. The operators of in, as, is, or to are not overloadable. (Note: The operators use an English word instead of symbol to distinguish it from the overloadable operators--a language design decision.)

The three operators in more specificity are:

  1. as - creation assignment operator

  2. in - instance of check operator either type or reference

  3. is - reference assignment operator

  4. to - instance of cast conversion operator



The non-overloadable operators are closer to code synthesis in the high-level language.

For the assignment reference assignment operator is, consider the simple expression statement:

x is y;


The reference handle to the identifier x is assigned explicitly the reference handle to the identifier y--both x and y refer or point to the same thing. Converting to postfix expression form:

x y is


The type of x and y must be the same, and the is operator is translated into direct assignment in the HLL code synthesis. Thus the expression in HLL code synthesis is:

x = y;

Reference assignment in both C# and Java is the = operator.

A more complex expression statement is:

x is y to Int;


Translated to an equivalent postfix operator expression statement is:

x y Int to is


The translation from postfix operator expression to the HLL code synthesis expression is:

x [y Int to] is
x [( (Int) y ) ] is
[x ((Int) y) is]
x = ((Int) y) ;


The cast and assignment reference have been translated into the syntax form for the HLL code synthesis--interestingly enough both C# and Java (and C/C++) have the same syntax redux for code synthesis. The following Mynx source code fragment illustrates the as operator:

var Int x to null;

x as Int("37");


The as operator expression in postfix is:

x Int("37") as;


The translation from postfix operator expression is:

x = new Int().construct("37");


The new operator creates the instance, but by Mynx code synthesis uses an explicit constructor named construct.

A more complex operator expression involves the Mynx in operator in its two uses, as a type check and reference assignment check. Consider the Mynx operator expression in an if statement header:

if(x in Int && x in y)
null;
end if;


The operator expression is a Mynx Bool type--evaluates to true or false. But the expression must still be translated to postfix form, and then to the HLL code synthesis translation:

x in Int && x in y

The postfix form of the operator expression is:

x Int in x y in &&

The translation from an operator expression to the HLL synthesis is:

x Int in x y in &&
[ x Int in ] x y in &&
(x instanceof Int) [ x y in ] &&
(x instanceof Int) (x == y) &&
(x instanceof Int).logAnd( (x == y) )

The only overloaded operator in the expression is the logical and operator of &&.

For the HLL code synthesis, the if expression must return a logical value of true or false, but in the HLL synthesized language--Java or C#. Thus the translated expression is only partially complete, the general form from Mynx to the HLL programming language is:

if(EXPRESSION)

The HLL code synthesis translation is uses the Mynx Bool type class:

if(Bool.TRUE_ == EXPRESSION)


If the Mynx boolean expression returns a BOOL.TRUE, the == operator will then create a boolean true to the HLL. Using that information, the expression translated ultimately becomes:

if(Bool.TRUE_ == (x instanceof Int).logAnd( (x == y) ) )


The HLL code synthesis involves the HLL equivalent operators and the overloaded operators with the Mynx method.

Summarizing, the underlying code synthesis drives the HLL expression that is synthesized in the native language from a Mynx expression statement. For non-overloadable operators the synthesized expression is closer to the HLL programming language.

Labels: , , , , , , ,

Website Spy Software