From 41b37ca430a62570bcdd30358a6a7fc0ebd2699b Mon Sep 17 00:00:00 2001 From: Schmouk Date: Thu, 22 Nov 2018 00:34:14 +0100 Subject: [PATCH] #19-Create Typee language documentation Function call subsection added. Lead to corrections in formerly written text. --- .../documentation-3-10-functions.html | 60 ++++++++++++++++--- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/html-content/documentation-3-10-functions.html b/html-content/documentation-3-10-functions.html index 9348b90..e7a8392 100644 --- a/html-content/documentation-3-10-functions.html +++ b/html-content/documentation-3-10-functions.html @@ -11,10 +11,12 @@

3.10 - Functions

Finally, functions (and methods as well) may be overloaded and overridden. We will explain what does this mean, later in this section. The overall formal EBNF specification of functions (and methods) rules in Typee are: -
<function call>             ::= <identifier>
+
<function call statement>   ::= <function call> ';'
+
+<function call>             ::= <dotted name>
                                     [<template args>] '(' <function call args> ')' 
 
-<function declaration>      ::= <forward> <function decl>
+<function declaration>      ::= <forward> <function decl> ';'
 
 <function definition>       ::= <function decl> <statements block>
 
@@ -23,14 +25,11 @@ 

3.10 - Functions

<call operator> ::= '(' ')' -<forward> ::= 'ret' | 'return' +<forward> ::= 'forward' | 'fwd' <function args declaration> ::= '(' [<typed args list>] ')' -<function call args> ::= [ <expression> (<function call>)* - [ ',' ( <expression> (<function call>)* | - '...' <identifier> ) | - <for comprehension>] ] +<function call args> ::= [ <expression> (',' <expression>)* ] <function decl> ::= [<access protection qualifier>] ['final'] <type> <identifier> @@ -114,7 +113,7 @@

3.10.1 Function Definition

As a reminder, visibility qualifiers in Typee are:
<access protection qualifier> ::= 'hidden' | 'local' | 'private'
-                                     'protected' | 'public'
+                                  'protected' | 'public'
 
  @@ -180,10 +179,55 @@

3.10.1 Function Definition

 

3.10.2 Function Declaration

+In Typee, functions may only be declared in a forward statement. The only need to declare a function is when it is to be defined in a module after it is called in this same module. Unnamed functions have not to be declared also. They are always defined at the place they are called. + +The formal EBNF sepcification of a function declaration is then: +
<function declaration> ::= <forward> <function decl> ';'
+<forward>              ::= 'forward' | 'fwd' 
+
+ +Since the funcion is only declared there, no statements block (i.e. function body) is allowed after the declaration of the signature of the function. This declaration bieng a statement, it has to be ended with a semi-colon. Very simple. + +Such declarations help Typee translators to check types and functions signatures when they are called with no delay (this is a not-lazy evaluation or checking of types and signatures). +  

3.10.3 Function Call

+Functions are called with values or references as arguments. The types of the arguments define part of the signature of the function to be called. The other part of the signature is the type of its returned value. In case of possible ambiguities about returned type, Typee translators will set an explicit warning or even erro acording to the contextr. This is discussed in a next paragraph. + +Let's first describe the formal EBNF specification of functions calls: +
<function call>       ::= <identifier>
+                              [<template args>] '(' <function call args> ')' 
+
+<function call args>  ::= [ <expression> (',' <expression>)* ]
+
+ +The identifier is the identifier of the function. +It may be followed by template arguments if declared or defined so. Notice: templates arguments are enclosed within angle brackets < and > and are lists of types or constant values - more about templates later in this documentation. +Next are the values, variables or references passed as arguments to the function at call-time. These arguments are seperated by commas and are enclosed between parenthesis: ( and ). Their number and the ordered list of their types are part of the signature of the called function. Should any ambiguity be found at translation time, Typee translators will set an explicit error. + +Finally, to fully evaluate the signature of the called function, its returned type is evaluated at translation time. + +A function that returns no value can only be called in a function call statement: +
<function call statement> ::= <function call> ';'
+
+When a no-value returning function will be called in any arithmetic expression, assignment statement and conditional expression, Typee translators will set a type error. + +The type of the value returned by a called function will be inferred at translation time, according to the context of the call. Most of the time, this will be straightforward to evaluate. For instance, when assigning a variable with the result of a function the type of the assigned variable will be used for the type inferrence. Or, when calling a function in an arithmetic or conditional expression, the inferred type for returned type of the called function will be the type of the other terms in the evaluated expression. + +Meanwhile, it may happen that according to a same function identifier and types of arguments list, a couple of returned types could be inferred for the called function - see simple example below. Typee translators will then: +
    +
  • either set a warning if all the possibly inferred returned types are all compatible together and with the expected type in the expression or assignment (e.g. integers and floats), informing that the largest type will be used: +
      +
    1. floats first, 64-bits before 32-bits
    2. ; +
    3. integers then, in this order: 64-, 32-, 16- and then 8- bits; signed types before unsigned types if any of the other types inferred in the expression cannot be clearly inferred as unsigned
    4. ; +
        +
      1. or set an error if possibly inferred returned types are not compatible with the expectedtype
      2. . +
+ +Programmers are informed here that they should avoid as much as possible any ambiguity with signatures of called functions. Programming languages such as C++ or Java do not allow functions with same identifier to return different types of values. Typee allows this. It is simply processed at translation time when translating Typee code to programming languages that do not allow it. Then, programmers have to be careful when using this Typee goody with such not permissive programming languages. +  

3.10.5 Unnamed Functions