INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified...
-
Upload
dangkhuong -
Category
Documents
-
view
215 -
download
1
Transcript of INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified...
![Page 1: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/1.jpg)
INF
311
0 -
20
17
INF3110 – Programming Languages
Scope and Runtime Organization
9/11/2017 1
![Page 2: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/2.jpg)
INF
311
0 -
20
17
9/11/2017 2
Outline: Runtime Organization I & II
▪ Block-structured languages and stack organization
▪ In-line Blocks
– activation records
– storage for local and global variables
▪ First-order functions
▪ Parameter passing
▪ Higher-order functions (not today)
![Page 3: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/3.jpg)
INF
311
0 -
20
17
9/11/2017 3
Simplified Reference Model of a Machine
- used to understand memory management
Registers(not in this class)
Environment Pointer
Program Counter
DataCode
Heap
Stack
Our main focus today
![Page 4: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/4.jpg)
INF
311
0 -
20
17
9/11/2017 4
Block-Structured Languages
▪ Blocks are syntactical structures
▪ Can be nested within each other
...;
{ int x = 2;
{
int y = 3;
x = y+2;
}
};
...
– Storage management – memory representation
▪ Enter block: allocate space for variables
▪ Exits block: space may be de-allocated
new variables declared in nested blocks
inner
block
outer
blockx: global variable
y: local variable
![Page 5: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/5.jpg)
INF
311
0 -
20
17
9/11/2017 5
Examples
▪ Blocks in common languages
– C/C++/Java/C# { … }
– Algol/Simula/Basic begin … end
– ML let … in … end
– Python (whitespace!)
▪ Two forms of blocks to start with
– In-line blocks
– Blocks associated with functions
or procedures
– To come: blocks associated with classes:
class Node
{Node left, right;
void insert(Node n)
{ ...
}
};
![Page 6: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/6.jpg)
INF
311
0 -
20
17
9/11/2017 6
Some basic concepts
▪ Declaration
– Specifies properties (name, type, kind) of an identifier
▪ Scope
– Region of program text where declaration is visible
▪ Lifetime
– Period of time when location is allocated
– Inner declaration of x hides outer one.
– Called “hole in scope”
– Lifetime of outer x includes time when inner
block is executed
– Lifetime scope
{ int x = … ;
{ int y = … ;
{ int x = … ;
…; x; …
};
…; x; …
};
};
![Page 7: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/7.jpg)
INF
311
0 -
20
17
9/11/2017 7
In-line blocks
▪ Activation record
– Data structure stored on run-time stack
– Contains space for local variables in a block
May also need space for intermediate results like (x+y), (x-y)
{ int x = 0;
int y = x+1;
{ int z = (x+y)*(x-y);
};
};
Push record with space for x, y
Set values of x, y
Push record for inner block
Set value of z
Pop record for inner block
Pop record for outer block
Do we need activation records for blocks? Yes, for loops (and funcs etc)
![Page 8: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/8.jpg)
INF
311
0 -
20
17
9/11/2017 8
Activation record for in-line block
▪ Environment pointer
– Pointer to current record on stack
▪ Control link (dynamic link)
– Pointer to previous record on
stack
– Why is it called dynamic?
▪ Push record on stack
– Set new control link (in new
record) to point to old env ptr
– Set env ptr to new record
▪ Pop record off stack
– Follow control link of current
record to reset environment
pointer
– (No need to actively blank
memory)
Control link
Local variables
Intermediate results
Control link
Local variables
Intermediate results
Environment Pointer
Sta
ck g
row
s d
ow
nw
ard
s
![Page 9: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/9.jpg)
INF
311
0 -
20
17
9/11/2017 9
Example
Control link
x
y
0
1
x+y
x-y
Environment Pointer
1
-1
Control link
z -1
{ int x = 0;
int y = x+1;
{ int z = (x+y)*(x-y);
// figure shows state here!
};
};
Push record with space for x, y
Set values of x, y
Push record for inner block
Set value of z
Pop record for inner block
Pop record for outer block
![Page 10: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/10.jpg)
INF
311
0 -
20
17
9/11/2017 10
Initial values – what is in the activation block?
▪ Specified initial value
▪ Default initial value
▪ Languages differ
– C/C++: no default initial value
▪ Undefined?
▪ Arbitrary value?
– Algol/Simula/Java/C#:
default initial value
{
int x = 0;
int y;
{
int z = (x+y)*(x-y);
};
};
![Page 11: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/11.jpg)
INF
311
0 -
20
17
http://stackoverflow.com/questions/31739792/is-uninitialized-local-variable-the-
fastest-random-number-generator
![Page 12: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/12.jpg)
INF
311
0 -
20
17
9/11/2017 12
Scope rules
▪ Global and local variables
– x, y are local to outer block
– z is local to inner bock
– x, y are global to inner block
▪ Static scope
– global refers to declaration in closest enclosing block
▪ Dynamic scope
– global refers to most recent activation record
These are the same until we consider function calls.
{ int x = 0;
int y = x+1;
{ int z = (x+y)*(x-y);
};
};
![Page 13: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/13.jpg)
INF
311
0 -
20
17
9/11/2017 13
Example – static/dynamic scoping
{ -- block 0
int x = 0;
{ -- block 1
int x = 1;
};
{ -- block 2
int x = 2;
};
{ -- block 3, called
int z = x;
}
}
Control link
x 0
Control link
x 2
Control link
z ??
Control link
x 1
If block 1 executes block 3
Static scope: z = 0
Dynamic scope: z = 1
If block 2 executes block 3
Static scope: z = 0
Dynamic scope: z = 2
1 2
3
If block 0 executes block 3
Static scope: z = 0
Dynamic scope: z = 0
0
![Page 14: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/14.jpg)
INF
311
0 -
20
17
9/11/2017 14
Functions and procedures
▪ Activation record must include space for
– parameters
– local variables
– return address
– return value
(an intermediate result)
– location to put return value
on function exit
int fact(int n) { … }
int i, j;
...
i = 3;
j = fact(i);
print(j)
![Page 15: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/15.jpg)
INF
311
0 -
20
17
9/11/2017 15
Activation record for function call
▪ Return address
– Location of code to execute on
function return
▪ Return-result address
– Address in activation record of
calling block to receive return
value
▪ Parameters
– Locations to contain data from
calling block
Control link
Local variables
Intermediate results
Environment Pointer
Parameters
Return address
Return-result addr
![Page 16: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/16.jpg)
INF
311
0 -
20
17
9/11/2017 16
Example
▪ Function
fact(n) = if n <= 1 then 1
else n * fact(n-1)
▪ Return result address
– location to put fact(n)
▪ Parameter
– set to value of n by calling
sequence
▪ Intermediate result
– location to contain value of
fact(n-1)
Control link
Local variables
Intermediate results
Environment Pointer
Parameters
Return address
Return result addr
![Page 17: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/17.jpg)
INF
311
0 -
20
17
9/11/2017 17
Function call
Return address omitted; would be
ptr into code segment
fact(n) = if n<= 1 then 1
else n * fact(n-1)
Control link
fact(n-1)
n
Return-result addr
k
fact(k)
Environment Pointer
Control link
fact(n-1)
n
Return-result addr
3
fact(3)
Control link
fact(n-1)
n
Return-result addr
2
fact(2)
Control link
fact(n-1)
n
Return-result addr
1
fact(1)
Function return next slide
![Page 18: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/18.jpg)
INF
311
0 -
20
17
9/11/2017 18
Function return – store result, pop record from
the stack
Control link
fact(n-1)
n
Return result addr
3
fact(3)
Control link
fact(n-1)
n
Return result addr
1
2
fact(2)
Control link
n
Return result addr
1
fact(1)
fact(n) = if n<= 1 then 1
else n * fact(n-1)
Control link
fact(n-1)
n
Return result addr
3
fact(3)
Control link
fact(n-1)
n
Return result addr
1
2
fact(2)
2
![Page 19: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/19.jpg)
INF
311
0 -
20
17
9/11/2017 19
Access to global variables
▪ Two possible scoping conventions
– Static scope: refer to closest enclosing block (syntactically)
– Dynamic scope: most recent activation record on stack
▪ Example
int x = 1;
function g(z) = x+z;
function f(y) =
{
int x = y+1;
return g(y*x)
};
f(3);
x 1
x 4
y 3
z 12
outer block
f(3)
g(12)
Which x is used for expression x+z?
![Page 20: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/20.jpg)
INF
311
0 -
20
17
9/11/2017 20
Activation record for static scope
▪ Control link (dynamic link)
– Link to activation record of
previous (calling) block
▪ Access link (static link)
– Link to activation record
corresponding to the closest
enclosing block in program
text
– Why is it called static?
▪ Difference
– Control link depends on
dynamic behavior of program
– Access link depends on static
form of program text
Control link
Local variables
Intermediate results
Environment Pointer
Parameters
Return address
Return result addr
Access link
![Page 21: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/21.jpg)
INF
311
0 -
20
17
9/11/2017 21
Static scope with access links (C-like notation)
{
int x = 1;
int function g(z) { return x+z };
int function f(y) {
int x = y+1;
return g(y*x)
};
main() {
f(3);
}
}
x 1
x 4
y 3
z 12
outer block
f(3)
g(12) control linkaccess link
control linkaccess link
access link
control link
main
Use access link to find global variable:
▪ Access link is always set to frame of closest enclosing lexical block
▪ For function body, this is the block that contains function declaration
g …
f …
![Page 22: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/22.jpg)
INF
311
0 -
20
17
9/11/2017 22
Static scope with access links (ML-like
notation)
x 1
x 4
y 3
z 12
outer block
f(3)
g(12) control linkaccess link
g …
f …
control linkaccess link
control linkaccess link
access link
control linkUse access link to find global variable:
▪ Access link is always set to frame of closest enclosing lexical block
▪ For function body, this is the block that contains function declaration
int x = 1;
function g(z) = x+z;
function f(y) =
{
int x = y+1;
return g(y*x)
};
f(3);
![Page 23: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/23.jpg)
INF
311
0 -
20
17
9/11/2017 23
Issues for first-order functions
▪ Access to global variables
▪ Parameter passing
– pass-by-value
– pass-by-reference
– pass-by-name
int a = 5;
void f(int x)
{
x = x+1;
}
void main()
{
f(a);
print(a);
}
✓
![Page 24: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/24.jpg)
INF
311
0 -
20
17
9/11/2017 24
Parameter passing
▪ Call-by-value (or “pass-by-value”)
– Caller places R-value (contents) of actual parameter in activation
record
– Function cannot change value of caller’s variable
– Reduces aliasing (alias: two names refer to same location)
▪ Call-by-reference
– Caller places L-value (address) of actual parameter in activation
record
– Function can assign to variable that is passed (must have an L-
value!)
– Aliasing
▪ Call-by-name
– Actual parameter expression is passed as such and evaluated
whenever the formal parameter is used in the function
– (Not common in most languages)
![Page 25: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/25.jpg)
INF
311
0 -
20
17
9/11/2017 25
Different variants of copying the value
▪ Call-by-value
– Local variable
assigned at call
– f(int in x){...}
▪ Call-by-result
– Local variable not
assigned at call, but
returned at exit
– f(int out x){...}
▪ Call-by-value-result
– Local variable
assigned at call, and
returned at exit
– f(int in-out x){...}
int a = 5;
void f(int x)
{
x = x+1;
}
void main()
{
f(a);
print(a);
}
f.x = a
...
...
a = f.x
f.x = a;
...
a = f.x
a
global
x
f
![Page 26: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/26.jpg)
INF
311
0 -
20
17
▪ Java?
▪ C#?
▪ Python?
▪ JavaScript?
▪ SML?
9/11/2017 26
What is the parameter passing semantics of
YOUR favorite language?
class Person {
public String name = “”;
}
public static void main(String[] args) {
Person p = new Person();
p.name = “Wonderwoman!”;
changeName(p);
println(p.name); // what happens here?
}
public static void changeName(Person p) {
p.name = “Batman!”;
}
![Page 27: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/27.jpg)
INF
311
0 -
20
17
▪ Java?
▪ C#?
▪ Python?
▪ JavaScript?
▪ SML?
9/11/2017 27
What is the parameter passing semantics of
YOUR favorite language?
class Person {
public String name = “”;
}
public static void main(String[] args) {
Person p = new Person();
p.name = “Wonderwoman!”;
changeName(p);
println(p.name); // ?
}
public static void changeName(Person p) {
p = new Person();
p.name = “Batman!”;
}
![Page 28: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/28.jpg)
INF
311
0 -
20
17
▪ Java?
▪ C#?
▪ Python?
▪ JavaScript?
▪ SML?
9/11/2017 28
What is the parameter passing semantics of
YOUR favorite language?
class Person {
public String name = “”;
}
public static void main(String[] args) {
Person p = new Person();
p.name = “Wonderwoman!”;
changeName(p);
println(p.name); // ?
}
public static void changeName(ref Person p) {
p.name = “Batman!”;
}
![Page 29: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/29.jpg)
INF
311
0 -
20
17
▪ Java?
▪ C#?
▪ Python?
▪ JavaScript?
▪ SML?
9/11/2017 29
What is the parameter passing semantics of
YOUR favorite language?
class Person {
public String name = “”;
}
public static void main(String[] args) {
Person p = new Person();
p.name = “Wonderwoman!”;
changeName(p);
println(p.name); // ?
}
public static void changeName(ref Person p) {
p = new Person();
p.name = “Batman!”;
}
![Page 30: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/30.jpg)
INF
311
0 -
20
17
▪ Java?
▪ C#?
▪ Python?
▪ JavaScript?
▪ SML?
9/11/2017 30
What is the parameter passing semantics of
YOUR favorite language?
class Person {
public String name = “”;
}
public static void main(String[] args) {
Person p = new Person();
p.name = “Wonderwoman!”;
changeName(p);
println(p.name); // ?
}
public static void changeName(ref Person p) {
p = null;
}
![Page 31: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/31.jpg)
INF
311
0 -
20
17
9/11/2017 31
Call by-reference
▪ The ’x’ (within f) is set to
the address of ’a’ (L-
value).
▪ The assignment ’x+1’
assigns the value of ’x+1’,
that is 6, to the variable
whose L-value is kept by
’x’, i.e. the global variable
’a’.
▪ ’a’ is therefore changed to
6, and 6 is printed.
int a = 5;
void f(int x)
{
x = x+1;
}
void main()
{
f(a);
print(a);
}
a
global
x
f
![Page 32: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/32.jpg)
INF
311
0 -
20
17
9/11/2017 32
Example - aliasing
void f(ref real v1, ref real v2, ref real v3) {
v1 = v1 - v3;
v2 = v2 + v3;
};
real x, y, z;
x = 4.0;
y = 6.0;
z = 1.0;
f(x, y, z); // ? x = 3.0;
y = 7.0;
x = 0.0;
y = 6.0; f(a[i], a[j], a[k]);f(x, y, x) // ?
Shouldn’t the compiler
warn me about this stuff???
![Page 33: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/33.jpg)
INF
311
0 -
20
17
9/11/2017 33
By-name parameters
begin integer i;
integer procedure sum(i, j);
integer i, j;
begin
integer sm; sm:= 0;
for i = 1 step 1 until 100 do
sm := sm + j;
sum:= sm
end;
print(sum(i,i*10)
end;
swap(int a, b) {
int temp;
temp = a;
a = b;
b = temp;
};
i=3;
a[3]=6;
swap(i, a[i]);
-- i = 6
temp = i; (3)
i = a[i]; (6)
a[i] = temp; (3)
-- a[3] = ?-- a[3] = 6
-- a[6] = 3Algol 60
![Page 34: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/34.jpg)
INF
311
0 -
20
17
9/11/2017 34
by name
“4.7.3.2. Name replacement (call by name). Any formal parameter not
quoted in the value list is replaced, throughout the procedure body, by the
corresponding actual parameter, after enclosing this latter in parentheses
wherever syntactically possible. Possible conflicts between identifiers inserted
through this process and other identifiers already present within the procedure
body will be avoided by suitable systematic changes of the formal or local
identifiers involved. “
What is the difference between this and macro expansion?
By name
-- i = 6
-- a[3] = 6
-- a[6] = 3
local i = 3
i = a(i): i=a(3): i=6
a(i)=local i:
a(6)=3
By macro expansion
i = i; -- local i = 0
i = a[i]; -- local i = a[0]
a[i] = i; -- a[0] = 0
int i; int a[];
swap(int a, b) {
int i;
i = a;
a = b;
b = i;
};
swap(i, a[i]);
i=3;
a[3]=6;
swap(i, a[i]);
![Page 35: INF3110 Programming Languages Scope and Runtime ... - · PDF file2017 9/11/2017 3 Simplified Reference Model of a Machine - used to understand memory management Registers (not in this](https://reader031.fdocuments.in/reader031/viewer/2022022005/5ab8e5427f8b9ac1058d2f72/html5/thumbnails/35.jpg)
INF
311
0 -
20
17
Going forward
9/11/2017 35
• Higher-order functions
• Closures
• Classes and objects
Next time:
• More ML with Jacopo