General Information
Tutorials
Reference Manuals
Libraries
Translation Tasks
Tools
Administration
|
Tutorial on Type AnalysisType Conversion
This chapter introduces type conversion to our language. We say, a value
of a certain type
In order to demonstrate type conversion, we extend our language
by a second arithmetic type for floating point values an call the
type The type representation is extended by: Real type representation[37]== realType -> TypeName = {"real"}; This macro is invoked in definition 44. We add a new type denoter to the language Real type denoter[38]== RULE: TypeDenoter ::= 'real' COMPUTE TypeDenoter.Type = realType; END; This macro is invoked in definition 45.
and introduce literals of type Real literals[39]== RULE: Expression ::= RealNumber COMPUTE PrimaryContext (Expression, realType); END; This macro is invoked in definition 45.
Now we extend the set of operator specifications by
operators for the type Real operators[40]== OPER rAdd (realType,realType):realType; rSub (realType,realType):realType; rMul (realType,realType):realType; rDiv (realType,realType):realType; rPlus (realType):realType; rNeg (realType):realType; This macro is invoked in definition 46.
We specify that the Real operators overload[41]== INDICATION AddOp: rAdd; SubOp: rSub; MulOp: rMul; DivOp: rDiv; PlusOp: rPlus; NegOp: rNeg; This macro is invoked in definition 46.
Now we want to allow that overloading resolution takes conversion
from
So, we define such a coercion operator Predefined Coercion Operator[42]== COERCION iTor (intType):realType; This macro is invoked in definition 46.
Finally we reconsider the type rules for assignments. We want
to allow to have an
For that purpose we specify a conversion operator Assignment Conversion Operator[43]== OPER rToi (realType):intType; INDICATION assignOpr: rToi; This macro is invoked in definition 46.
Note: The conversion operator RealType.pdl[44]== Real type representation[37] This macro is attached to a product file. RealType.lido[45]== Real type denoter[38] Real literals[39] This macro is attached to a product file. OperatorExtensions.oil[46]== Real operators[40] Real operators overload[41] Assignment Conversion Operator[43] Predefined Coercion Operator[42] This macro is attached to a product file.
|