![]() |
![]() |
![]() |
General Information
Tutorials
Reference Manuals
Libraries
Translation Tasks
Tools
Administration
![]() |
![]() |
Tutorial on Type AnalysisOperator Overloading
We here extend our language by binary and unary operators in
order to demonstrate type analysis for expressions with
overloaded operators. The following grammar fragment introduces
binary operators such that Opr.con[27]== Expression: Expression AddOpr Factor. Factor: Factor MulOpr Operand. Operand: MonOpr Operand. Operand: '(' Expression ')'. This macro is attached to a product file.
Type analysis for operators is supported by the library module
OprType.specs[28]== $/Type/Operator.gnrc+referto=(Operator.descr):inst This macro is attached to a product file.
The Operator.descr[29]== Source Operators[30] Target Operators[31] This macro is attached to a product file.
Operators are overloaded in our language,
i.e. a source operator symbol like Each of the following lines describes one source operator by its token, the concrete grammar symbol it is derived from, the abstract grammar symbol for its representation in the tree, and a unique name: Source Operators[30]== SrcOpr ('+', AddOpr, BinOpr, AddOp) SrcOpr ('-', AddOpr, BinOpr, SubOp) SrcOpr ('*', MulOpr, BinOpr, MulOp) SrcOpr ('/', MulOpr, BinOpr, DivOp) SrcOpr ('+', MonOpr, UnOpr, PlusOp) SrcOpr ('-', MonOpr, UnOpr, NegOp) SrcOpr ('!', MonOpr, UnOpr, NotOp) This macro is invoked in definition 29.
For each of the source operators at least one meaning is specified
by one of the following target operator descriptions.
The first component of a target operator description relates it
to a source operator, the second component is a unique name.
Here, for example the The third component describes the signature of the target operator expressed in terms of keys for predefined types. Target Operators[31]== TgtOpr (AddOp, iAdd, (intType,intType):intType, TgtStr={"+"}) TgtOpr (SubOp, iSub, (intType,intType):intType, TgtStr={"-"}) TgtOpr (MulOp, iMul, (intType,intType):intType, TgtStr={"*"}) TgtOpr (DivOp, iDiv, (intType,intType):intType, TgtStr={"/"}) TgtOpr (AddOp, rAdd, (realType,realType):realType, TgtStr={"+"}) TgtOpr (SubOp, rSub, (realType,realType):realType, TgtStr={"-"}) TgtOpr (MulOp, rMul, (realType,realType):realType, TgtStr={"*"}) TgtOpr (DivOp, rDiv, (realType,realType):realType, TgtStr={"/"}) TgtOpr (PlusOp, iPlus, (intType):intType, TgtStr={"+"}) TgtOpr (NegOp, iNeg, (intType):intType, TgtStr={"-"}) TgtOpr (PlusOp, rPlus, (realType):realType, TgtStr={"+"}) TgtOpr (NegOp, rNeg, (realType):realType, TgtStr={"-"}) TgtOpr (AddOp, bAdd, (boolType,boolType):boolType, TgtStr={"||"}) TgtOpr (MulOp, bMul, (boolType,boolType):boolType, TgtStr={"&&"}) TgtOpr (NotOp, bNot, (boolType):boolType, TgtStr={"!"}) This macro is invoked in definition 29.
The last component associates a value to a property named
OprType.pdl[32]== TgtStr: CharPtr; "Strings.h" This macro is attached to a product file.
The target operator signatures as given above would require
operands to have exactly those types. E. g. OprType.oil[33]== COERCION cFloat (intType): realType; This macro is attached to a product file.
Type analysis for binary and unary expressions needs to compute
the type attribute of the whole expression (the result type
of the operation) and the required operand type (the corresponding
type of the signature of the identified target operator).
The latter may differ from the operand type if coercion is
applied. We can obtain these computations from the module
roles Opr.lido[34]== SYMBOL BinOpr INHERITS BinTgtOpr, ChkOpr END; RULE: Expression ::= Expression BinOpr Expression COMPUTE BinOpr.LType = Expression[2].Type; BinOpr.RType = Expression[3].Type; Expression[1].Type = BinOpr.ResType; Expression[2].ReqType = BinOpr.LTType; Expression[3].ReqType = BinOpr.RTType; END; SYMBOL UnOpr INHERITS UnTgtOpr, ChkOpr END; RULE: Expression ::= UnOpr Expression COMPUTE UnOpr.RType = Expression[2].Type; Expression[1].Type = UnOpr.ResType; Expression[2].ReqType = UnOpr.RTType; END; This macro is attached to a product file.
|