Eli   Documents

General Information

 o Eli: Translator Construction Made Easy
 o Global Index
 o Frequently Asked Questions
 o Typical Eli Usage Errors

Tutorials

 o Quick Reference Card
 o Guide For new Eli Users
 o Release Notes of Eli
 o Tutorial on Name Analysis
 o Tutorial on Scope Graphs
 o Tutorial on Type Analysis
 o Typical Eli Usage Errors

Reference Manuals

 o User Interface
 o Eli products and parameters
 o LIDO Reference Manual
 o Typical Eli Usage Errors

Libraries

 o Eli library routines
 o Specification Module Library

Translation Tasks

 o Lexical analysis specification
 o Syntactic Analysis Manual
 o Computation in Trees

Tools

 o LIGA Control Language
 o Debugging Information for LIDO
 o Graphical ORder TOol

 o FunnelWeb User's Manual

 o Pattern-based Text Generator
 o Property Definition Language
 o Operator Identification Language
 o Tree Grammar Specification Language
 o Command Line Processing
 o COLA Options Reference Manual

 o Generating Unparsing Code

 o Monitoring a Processor's Execution

Administration

 o System Administration Guide

Mail Home

Tutorial on Type Analysis

Previous Chapter Next Chapter Table of Contents


Type Conversion

This chapter introduces type conversion to our language. We say, a value of a certain type t is converted into a corresponding value of some other type s. For example, a conversion of integral values into floating point values is defined for many languages. We consider such a conversion be executed by a conversion operator that has a signature t->s. We call a conversion coercion if the application of a conversion operator is determined implicitly, for example in the process of overloading resolution.

In order to demonstrate type conversion, we extend our language by a second arithmetic type for floating point values an call the type real.

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:

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:

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 the corresponding ones for the type int by adding them to the corresponding indication:

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 int to real into account. That means in an expression like a + 1 the operand types need not match exactly to the signature of a + operator, if coercion could convert the operand types into those required by the signature. In particular a could have type real. In that case coercion from int to real would be applied to 1 in order to use the real addition operator.

So, we define such a coercion operator iTor with the signature int->real:

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 int variable on the left-hand side and a real expression on the right, say i = 3.4; That means the result of the expression is to be converted to an int value, which is then assigned to the variable.

For that purpose we specify a conversion operator rToi with the signature real->int, and associate it to the operator indication assignOpr which has been introduced for the assignment context:

Assignment Conversion Operator[43]==

OPER
   rToi (realType):intType;
INDICATION
  assignOpr: rToi;
This macro is invoked in definition 46.

Note: The conversion operator rToi is only applicable in a context that is chacterized by the indication assignOpr, it is NOT applied as a coercion when resolving overloaded operators.

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.


Previous Chapter Next Chapter Table of Contents