Skip to content

Commit dcdc6ea

Browse files
committed
Straighten out the language a bit
1 parent 4974ae7 commit dcdc6ea

File tree

1 file changed

+44
-41
lines changed

1 file changed

+44
-41
lines changed

spec/05-functions.md

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,40 @@
22

33
_Functions_ are independent chunks of program code. _Calling_ or _invoking_ a
44
function causes its code to run. A function has zero or more _parameters_,
5-
which are bound to _arguments_ from a call expression. If the function body
6-
finishes normally, a value may be returned from the function; this value then
7-
becomes the value of the call expression that invoked the function.
5+
variables which become bound to _arguments_ passed from a call expression. A
6+
function's body may finish _normally_ or _abruptly_. If it finishes normally, a
7+
value is returned from the function; this is then the value of the call
8+
expression that invoked the function.
89

910
## 5.1 Function calls
1011

11-
Call expressions were described in section 2.8 "Call expressions";
12+
Call expressions are described in section 2.8 "Call expressions";
1213
syntactically, a call expression consists of a _callable expression_ followed
13-
by a (parentheses-enclosed) list of _operands_, which are also expressions.
14-
At the time of evaluating the call expression, the following steps happen:
14+
by a (parentheses-enclosed) list of _operands_, which are also expressions. At
15+
the time of evaluating the call expression, these steps happen:
1516

1617
* The callable expression is fully evaluated into a value.
1718
* This value is confirmed to be a function, or more precisely a value which
18-
implements the invocation protocol; if not, then an exception is signaled
19-
at runtime and evaluation stops.
19+
implements the invocation protocol; if not, an exception is signaled at
20+
runtime, and evaluation stops.
2021
* All of the operands are evaluated, left-to-right, into values called
2122
_arguments_.
2223
* The `call` method of the invocation protocol is invoked, with an array of
2324
the arguments.
24-
* Parameter binding happens, explained in the next section. If successful,
25+
* Parameter binding happens, explained in section 5.8. If successful,
2526
this results in an extended environment.
26-
* The function's body is run. This is explained in section 5.3 "Function
27-
body".
28-
* Eventually, control might return normally, in which case a value is
27+
* The function's body is run in the extended environment. This is explained
28+
in section 5.9 "Function body".
29+
* Eventually, control might return normally, in which case a value is also
2930
returned. This value is then the value of entire call expression. This is
30-
explained in section 5.4 "Returning from a function".
31+
explained in section 5.10 "Returning from a function".
3132

3233
This describes a "call-by-value" convention, in which only values are passed
33-
from call sites to functions; that is, the operand expressions are entirely
34-
evaluated at the call site, and then the resulting values are passed.
34+
from call sites to functions. The operand expressions are entirely evaluated at
35+
the call site, and then the resulting values are passed.
3536

36-
Operator expressions, although syntactically different, can be seen as
37-
syntactic sugar for the above call expressions. This is true both for built-in
38-
operators and user-defined ones.
37+
Operator expressions, although syntactically different, can be viewed as
38+
syntactic sugar for the above call expressions.
3939

4040
Both in the case of call expressions and in the case of operator expressions,
4141
if the callable expression (statically) resolves to a macro, then macro
@@ -72,42 +72,45 @@ xxx or `@named` and `...`
7272

7373
## 5.8 Parameter binding
7474

75-
After the arguments are passed to a function for invocation, and before we can
76-
run the function body, an environment for running the function body needs to
77-
be prepared. This happens in two steps: making sure that there is an argument
78-
for each required parameter and a parameter for each passed argument, and
75+
During function invocation, when arguments have been passed to a function for
76+
invocation, and before the function body can run, an environment is constructed in which to run the function body.
77+
78+
This happens in two steps: first, making sure that there is an argument for
79+
each required parameter and a parameter for each passed argument, and second,
7980
binding the parameters in the new environment.
8081

82+
The first step breaks down into the following smaller steps:
83+
8184
* Assert that at least as many positional arguments have been passed as there
8285
are required positional parameters. If not, signal an exception.
83-
* Assert that if more positional arguments were passed than positional
84-
arguments (required and optional both), there's a positional rest parameter
85-
present. If not, signal an exception.
86-
* Assert that the set of names of named arguments is non-strictly contained by
87-
the set of names of required named parameters. If not, signal an exception.
88-
* Assert that if the set of names of named arguments contains a name not
89-
contained in the set of names of named arguments (required and optional
90-
both), there's a named rest parameter present. If not, signal an exception.
86+
* Assert that the number of positional arguments does not exceed the number of
87+
(required and optional) positional parameters, or that there's a positional
88+
rest parameter declared in the parameter list. If not, signal an exception.
89+
* Assert that, for each required named parameter, there is a named argument of
90+
that name. If not, signal an exception.
91+
* Assert that all named arguments that were passed have a corresponding named
92+
parameter, or that a named rest parameter is declared in the parameter list.
93+
If not, signal an exception.
9194

9295
At this point, we know that parameter binding won't fail because not enough
9396
arguments were passed for the required parameters, or too many arguments were
9497
passed that rest parameters weren't present to absorb.
9598

9699
* For each required positional parameter, bind it to the corresponding
97100
positional argument (of which we just checked there are enough).
98-
* For each optional positional parameter, bind it (in decreasing order of
99-
preference) to the corresponding positional argument, the value resulting
100-
from evaluating the corresponding parameter default expression, or `none`.
101-
* Make an array of any remaining positional arguments, and bind the positional
102-
rest parameter (which at this point must exist) to it.
101+
* For each optional positional parameter, bind it left-to-right to the
102+
corresponding positional argument, the value of the parameter default
103+
expression if present, or `none` if not.
104+
* If there is a positional rest parameter, make an array of the remaining
105+
positional arguments, and bind the positional rest parameter to this array.
103106
* For each required named parameter, bind it to the corresponding named
104107
argument (which we just asserted exists).
105-
* For each optional named parameter, bind it (in decreasing order of
106-
preference) to the corresponding named argument, the value resulting from
107-
evaluating the corresponding parameter default expression, or `none`.
108-
* Make a dictionary of any remaining named arguments, keys being the names and
109-
values being the named arguments, and bind the named rest parameter (which at
110-
this point must exist) to it.
108+
* For each optional named parameter, bind it left-to-right (in the parameter
109+
list) to the corresponding named argument, the value of the parameter default
110+
expression if present, or `none` if not.
111+
* If there is a named rest parameter, make a dictionary of the remaining named
112+
arguments (name and argument), and bind the named rest parameter to this
113+
dictionary.
111114

112115
The resulting environment is the one that will be used when running the
113116
function body.

0 commit comments

Comments
 (0)