Wednesday, November 14, 2007

Semantics of Operator Overloading and Expressions 1

During the external context semantic checks, operators for a type or class are associated with the method. The information is annotated to the syntax objects so that during code synthesis (code generation, but since I'm using a different approach I use different terminology) the data is available to create the high-level language code for an expression.

A top-level call to get the type and synthesize the code for an expression is passed to the child nodes of a node, and continues down the expression tree until it reaches an identifier, literal -- a atomic element of an expression.

As the code fragments as strings are then propogated upward to the parent node of an expression, the expression is synthesized from the sub-expressions.

The parse of an expression statement creates a syntax tree of interconnected syntax objects -- the classic abstract syntax tree (AST). During the external semantic checks operator associated with method identifier information is annotated, for code synthesis an expression of operators is translated into an expression of method calls.

The symbol table of declarations is accessed to determine the type of an identifier, and for an operator, the method associated with that operator for that class type--a semantic error if the operator is not overloaded.

Process of Operator Expression to Method Expression



Consider the following (infix) expression in Mynx:

x = y + z;


During the code synthesis, the infix expression is converted to the postfix expression on the fly. The (postfix) expression in Mynx:

x y z + =

Once in postfix form, the expression can be translated to a method expression.

The process involves going left-to-right and for an operator, if there are two sub-expressions (identifier, literal, or method expression) then the type of the left sub-expression and the operator determine the method call with the right sub-expression as the parameter.

In the postfix expression, the sub-expressions and operator are bracketed to indicate the sub-expression translation:

x [ y z + ] =


For x,y,z of type Int, and + is for the method "add", the translation is

x [ y.add(z) ] =

For the next sub-expression, the type Int, and = is for the method "set" the translation is:

[ x y.add(z) = ]


Note that the sub-expression
y.add(z)
is handled as an atomic unit, in this case as a sub-expression string.

The resulting translation is then one of:

x.set(y.add(z));


Thus the infix operator expression:
x = y + z;

is translated to a method expression of:
x.set(y.add(z));


The process requires type an external context semantic data annotated within the syntax objects representing an expression. In compiler parlance a syntax-directed translation.

Since an expression statement is a sentence, and the parse, semantics, and synthesis of code are centered around sentences, the process is encapsulated within the Mynx sentence object (in compiler terms a descriptor).

Processing Dual Assignment Operator Expression



The dual assignment operator expression contains both a + operator and = assignment operator. The dual assignment operator += does not support operator overloading directly--but indirectly the operators are each individually overloadable.

Hence the dual assignment operator expression:

x += y + z;


In postfix form is:

x y z + +=


But as the += dual assignment operator is not overloadable, so the infix expression needs to be rewritten with the individual overloadable operators:

x = (y + z) + x


Converting the rewritten expression to postfix is:

x x y z + + =


The processing of the expression is then:

x x [y z +] + =
x x [y.add(z)] + =
x [ x y.add(z) + ] =
x [ x.add(y.add(z)) ] =
[ x x.add(y.add(z)) = ]

x.set(x.add(y.add(z)));


Now the dual assign operation operators can be translated from an operator expression to a method expression.

Semantics are not a separate phase of a compiler, the semantics of operator overloading directly impacts the code synthesis (what is conventionally called code generation). The two phases of semantics and synthesis are grey areas of the compiler, not black-and-white separate stages. Unraveling one from the other for which phase is the more difficult part.

Labels: , , , , , ,

Website Spy Software