File: typetutorial_13.html

package info (click to toggle)
eli-doc 4.4.0-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,256 kB
  • ctags: 4,583
  • sloc: makefile: 42
file content (459 lines) | stat: -rw-r--r-- 16,384 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.29
     from ../tnf/typetutorial.tnf on 12 Febuary 2003 -->

<TITLE>Tutorial on Type Analysis - Functions</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#FF0000" BACKGROUND="gifs/bg.gif">
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0" VALIGN=BOTTOM>
<TR VALIGN=BOTTOM>
<TD WIDTH="160" VALIGN=BOTTOM><IMG SRC="gifs/elilogo.gif" BORDER=0>&nbsp;</TD>
<TD WIDTH="25" VALIGN=BOTTOM><img src="gifs/empty.gif" WIDTH=25 HEIGHT=25></TD>
<TD ALIGN=LEFT WIDTH="600" VALIGN=BOTTOM><IMG SRC="gifs/title.gif"></TD>
</TR>
</TABLE>

<HR size=1 noshade width=785 align=left>
<TABLE BORDER=0 CELLSPACING=2 CELLPADDING=0>
<TR>
<TD VALIGN=TOP WIDTH="160">
<h4>General Information</h4>

<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="index.html">Eli: Translator Construction Made Easy</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="gindex_toc.html">Global Index</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="faq_toc.html" >Frequently Asked Questions</a> </td></tr>
</table>

<h4>Tutorials</h4>

<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="EliRefCard_toc.html">Quick Reference Card</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="novice_toc.html">Guide For new Eli Users</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="news_toc.html">Release Notes of Eli</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="nametutorial_toc.html">Tutorial on Name Analysis</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="typetutorial_toc.html">Tutorial on Type Analysis</a></td></tr>
</table>

<h4>Reference Manuals</h4>

<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="ui_toc.html">User Interface</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="pp_toc.html">Eli products and parameters</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="lidoref_toc.html">LIDO Reference Manual</a></td></tr>
</table>

<h4>Libraries</h4>

<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="lib_toc.html">Eli library routines</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="modlib_toc.html">Specification Module Library</a></td></tr>
</table>

<h4>Translation Tasks</h4>

<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="lex_toc.html">Lexical analysis specification</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="syntax_toc.html">Syntactic Analysis Manual</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="comptrees_toc.html">Computation in Trees</a></td></tr>
</table>

<h4>Tools</h4>

<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="lcl_toc.html">LIGA Control Language</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="show_toc.html">Debugging Information for LIDO</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="gorto_toc.html">Graphical ORder TOol</a> </td></tr>
</table>
<p>
<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="fw_toc.html">FunnelWeb User's Manual</a> </td></tr>
</table>
<p>
<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="ptg_toc.html">Pattern-based Text Generator</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="deftbl_toc.html">Property Definition Language</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="oil_toc.html">Operator Identification Language</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="tp_toc.html">Tree Grammar Specification Language</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="clp_toc.html">Command Line Processing</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="cola_toc.html">COLA Options Reference Manual</a> </td></tr>
</table>
<p>
<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="idem_toc.html">Generating Unparsing Code</a> </td></tr>
</table>
<p>
<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="mon_toc.html">Monitoring a Processor's Execution</a> </td></tr>
</table>

<h4>Administration</h4>

<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="sysadmin_toc.html">System Administration Guide</a> </td></tr>
</table>

<HR WIDTH="100%">
<CENTER>&nbsp;<A HREF="mailto:elibugs@cs.colorado.edu"><IMG SRC="gifs/button_mail.gif" NOSAVE BORDER=0 HEIGHT=32 WIDTH=32></A><A HREF="mailto:elibugs@cs.colorado.edu">Questions, Comments, ....</A></CENTER>

</TD>
<TD VALIGN=TOP WIDTH="25"><img src="gifs/empty.gif" WIDTH=25 HEIGHT=25></TD>

<TD VALIGN=TOP WIDTH="600">
<H1>Tutorial on Type Analysis</H1>
<P>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT=""><A HREF="typetutorial_12.html"><IMG SRC="gifs/prev.gif" ALT="Previous Chapter" BORDER="0"></A>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT=""><A HREF="typetutorial_14.html"><IMG SRC="gifs/next.gif" ALT="Next Chapter" BORDER="0"></A>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT=""><A HREF="typetutorial_toc.html"><IMG SRC="gifs/up.gif" ALT="Table of Contents" BORDER="0"></A>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT="">
<HR size=1 noshade width=600 align=left>
<H1><A NAME="SEC13" HREF="typetutorial_toc.html#SEC13">Functions</A></H1>
<P>
We now introduce function declarations to our language.
A function is characterized by its signature.
Function types are an example of types that have properties which
are lists of types, the types of the parameters.
<P>
We extend the concrete grammar by productions for 
function declarations:
<P>
<B>Function.con</B>[57]==
<PRE>
<TT>
Declaration:    FunctionDecl.
FunctionDecl:   'fun' DefIdent Function ';'.
Function:       FunctionHead Block.
FunctionHead:   '(' Parameters ')' TypeDenoter.
Parameters:     [Parameter // ','].
Parameter:      TypeDenoter DefIdent.
</TT>
</PRE>
<FONT SIZE=1>
<PRE>
This macro is attached to a product file.
</PRE>
</FONT>
<P>
Here is an example program that defines some functions.
The grammar for function calls and return statements is
given below.
<B>FunctionExamp</B>[58]==
<PRE>
<TT>begin
  var   int i, int j,
        bool b, bool c,
        real r, real s;

  fun f (int x, real y) real
  begin r = x * y; return r;end;

  fun g (real z) void
  begin r = z; return; end;

  s = f (i+1, 3.4);
  g (f (j, s));
  return;
end
</TT>
</PRE>
<FONT SIZE=1>
<PRE>
This macro is attached to a product file.
</PRE>
</FONT>
<P>
A function signature consists of the result type and a
list of parameter types. Hence, we use an instance of
the <CODE>LidoList</CODE> module in order to compose such
type lists. 
(Note: If we had more than one mode of parameter passing,
the abstraction of a parameter in the function signature
would be a pair: parameter passing mode and parameter type.)
<P>
<B>Function.specs</B>[59]==
<PRE>
<TT>
$/Adt/LidoList.gnrc+instance=DefTableKey+referto=deftbl:inst
</TT>
</PRE>
<FONT SIZE=1>
<PRE>
This macro is attached to a product file.
</PRE>
</FONT>
<P>
The use of the module requires a particular name for the non-existing
list element:
<P>
<B>Function.head</B>[60]==
<PRE>
<TT>
#define NoDefTableKey NoKey
</TT>
</PRE>
<FONT SIZE=1>
<PRE>
This macro is attached to a product file.
</PRE>
</FONT>
<P>
A function type is described by two properties, the result type and
and the list of parameter types as introduced by the following
PDL specifications:
<P>
<B>Function.pdl</B>[61]==
<PRE>
<TT>
ParamTypes:     DefTableKeyList; "DefTableKeyList.h"
ResultType:     DefTableKey [KReset];
</TT>
</PRE>
<FONT SIZE=1>
<PRE>
This macro is attached to a product file.
</PRE>
</FONT>
<P>
We first consider the name analysis aspect of a function
declaration. The <CODE>Function</CODE> subtree is a range where the
parameter definitions are valid. The function <CODE>Block</CODE> is
nested in that range. Since the <CODE>DefIdent</CODE> for parameters
are already completely specified for name analysis, we need
only:
<P>
<B>FunctionScope.lido</B>[62]==
<PRE>
<TT>
SYMBOL Function INHERITS RangeScope END;
</TT>
</PRE>
<FONT SIZE=1>
<PRE>
This macro is attached to a product file.
</PRE>
</FONT>
<P>
Now we consider a function declaration as a definition
of a typed object, and apply the same specification pattern
as used for variable declarations.
Furthermore, each <CODE>Parameter</CODE> is also a <CODE>TypedDefinition</CODE>.
There is no problem in nesting definitions of a typed objects
this way.
<P>
<B>FunctionDecl.lido</B>[63]==
<PRE>
<TT>
SYMBOL FunctionDecl INHERITS TypedDefinition END;

RULE: FunctionDecl ::= 'fun' DefIdent Function ';' COMPUTE
  FunctionDecl.Type = Function.Type;
END;

RULE: Function ::= FunctionHead Block COMPUTE
  Function.Type = FunctionHead.Type;
END;

SYMBOL Parameter INHERITS TypedDefinition END;

RULE: Parameter ::= TypeDenoter DefIdent COMPUTE
  Parameter.Type = TypeDenoter.Type;
  DefIdent.IsVariable = 1;
END;
</TT>
</PRE>
<FONT SIZE=1>
<PRE>
This macro is attached to a product file.
</PRE>
</FONT>
<P>
Now we specify how the type of a function is composed.
The <CODE>FunctionHead</CODE>, which contains the signature,
is treated as a <CODE>TypeDenotation</CODE> for a function type.
Its <CODE>ResultType</CODE> property is obtained from the
<CODE>TypeDenoter</CODE> of the result type. The <CODE>ParamTypes</CODE>
property is the list of parameter types.
That list is simply composed from the <CODE>Root</CODE> and <CODE>Elem</CODE>
role of the <CODE>LidoList</CODE> module instantiated for lists
of <CODE>DefTableKey</CODE>s.
<P>
<B>FunctDeclType.lido</B>[64]==
<PRE>
<TT>
SYMBOL FunctionHead INHERITS TypeDenotation END;

RULE: FunctionHead ::= '(' Parameters ')' TypeDenoter COMPUTE
  FunctionHead.GotType = 
    ResetParamTypes
      (KResetResultType
         (KResetTypeName (FunctionHead.Type, "function..."),
          TypeDenoter.Type),
       Parameters.DefTableKeyList);
END;

SYMBOL Parameters INHERITS DefTableKeyListRoot END;
SYMBOL Parameter INHERITS DefTableKeyListElem END;

RULE: Parameter ::= TypeDenoter DefIdent COMPUTE
  Parameter.DefTableKeyElem = TypeDenoter.Type;
END;
</TT>
</PRE>
<FONT SIZE=1>
<PRE>
This macro is attached to a product file.
</PRE>
</FONT>
<P>
Function calls are integrated in the expression syntax of
our language. We chose a very general form of an <CODE>Expression</CODE>
to denote the function to be called. That allows us to later expand the
language by expressions which yield a function.
That feature does not create additional problems for type analysis.
<P>
We also introduce return statements into our language.
<P>
<B>Call.con</B>[65]==
<PRE>
<TT>
Expression:     Expression '(' Arguments ')'.
Arguments:      [Argument // ','].
Argument:       Expression.

Statement:      'return' ';'.
Statement:      'return' Expression ';'.
</TT>
</PRE>
<FONT SIZE=1>
<PRE>
This macro is attached to a product file.
</PRE>
</FONT>
<P>
Type analysis for a function call is straight-forward:
We access the <CODE>ResultType</CODE> from the type of the
function <CODE>Expression</CODE>; it is the type of the call.
The list of parameter types is passed to the <CODE>Arguments</CODE>.
There it is decomposed into its elements using the list decomposition
roles of the <CODE>LidoList</CODE> module, <CODE>DeListRoot</CODE> and
<CODE>DeListElem</CODE>. That mechanism also works if in erroneous
cases the lengths of the parameter list and the structural 
argument list are different.
<P>
<B>Call.lido</B>[66]==
<PRE>
<TT>
SYMBOL Arguments INHERITS DefTableKeyDeListRoot END;
RULE: Expression ::= Expression '(' Arguments ')' COMPUTE
  Expression[1].Type = 
        TransDefer (GetResultType (Expression[2].Type, NoKey));

  Arguments.DefTableKeyList = 
        GetParamTypes (Expression[2].Type, NULLDefTableKeyList);
END;

SYMBOL Argument INHERITS DefTableKeyDeListElem END;
RULE: Argument ::= Expression COMPUTE
  Expression.ReqType = Argument.DefTableKeyElem;
END;
</TT>
</PRE>
<FONT SIZE=1>
<PRE>
This macro is attached to a product file.
</PRE>
</FONT>
<P>
The computation of the <CODE>ReqType</CODE> attribute above
completely captures type checking for each argument.
For the function <CODE>Expression</CODE> we only have to check
whether it really yields a function, the first check below.
The other two checks issue messages if the lengths of the
lists are different.
<P>
<B>CallCheck.lido</B>[67]==
<PRE>
<TT>
RULE: Expression ::= Expression '(' Arguments ')' COMPUTE
  Expression[2].ReqType = Expression[2].Type;

  IF (EQ (Expression[1].Type, NoKey),
  message (ERROR, "call applied to non function", 0, COORDREF));

  IF (NE (Arguments.DefTableKeyListTail, NULLDefTableKeyList),
  message (ERROR, "arguments missing", 0, COORDREF));
END;

RULE: Argument ::= Expression COMPUTE
  IF (EQ (Argument.DefTableKeyElem, NoDefTableKey),
  message (ERROR, "too many arguments", 0, COORDREF));
END;
</TT>
</PRE>
<FONT SIZE=1>
<PRE>
This macro is attached to a product file.
</PRE>
</FONT>
<P>
A return statement refers to the immediately enclosing function
declaration. We have to check that a value of a type compatible 
to the result type is returned if the latter is not <CODE>void</CODE>.
A return from the outermost program level is considered as if
the program was a <CODE>void</CODE> function.
<P>
<B>Return.lido</B>[68]==
<PRE>
<TT>
SYMBOL Program COMPUTE 
  SYNT.Type = KResetResultType (NewKey(), voidType); 
END;

ATTR resType: DefTableKey;

RULE: Statement ::= 'return' ';' COMPUTE
  .resType =
     TransDefer 
       (GetResultType 
          (INCLUDING (FunctionDecl.Type, Program.Type),
           NoKey))
     &#60;- INCLUDING Program.GotType;

  IF (NOT (EqualTypes (.resType, voidType)),
  message (ERROR, "return value required", 0, COORDREF));
END;

RULE: Statement ::= 'return' Expression ';' COMPUTE
  .resType =
     TransDefer 
       (GetResultType 
          (INCLUDING (FunctionDecl.Type, Program.Type),
           NoKey))
     &#60;- INCLUDING Program.GotType;

  IF (EqualTypes (.resType, voidType),
  message (ERROR, "return value not allowed", 0, COORDREF));

  Expression.ReqType = .resType;
END;
</TT>
</PRE>
<FONT SIZE=1>
<PRE>
This macro is attached to a product file.
</PRE>
</FONT>
<P>
<HR size=1 noshade width=600 align=left>
<P>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT=""><A HREF="typetutorial_12.html"><IMG SRC="gifs/prev.gif" ALT="Previous Chapter" BORDER="0"></A>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT=""><A HREF="typetutorial_14.html"><IMG SRC="gifs/next.gif" ALT="Next Chapter" BORDER="0"></A>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT=""><A HREF="typetutorial_toc.html"><IMG SRC="gifs/up.gif" ALT="Table of Contents" BORDER="0"></A>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT="">
<HR size=1 noshade width=600 align=left>
</TD>
</TR>
</TABLE>

</BODY></HTML>