Skip to content

Commit c278f1f

Browse files
committed
test: enhance comments and clarify lexical scoping test cases in r1
1 parent 13d9a4c commit c278f1f

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

test_programs/r1/2.lox

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
---
22
expected_error_type: none
33
---
4-
// The closure should capture the globally defined function and redeclaring
5-
// a function with the same name should not affect the closure's capture.
4+
// This function is used in the function `f` below.
65
fun global() {
76
print "global";
87
}
@@ -12,9 +11,12 @@ fun global() {
1211
global();
1312
}
1413

15-
f();
14+
f(); // this should print "global"
15+
16+
// This function declaration shouldn't affect the usage in `f` above.
1617
fun global() {
1718
print "local";
1819
}
19-
f();
20+
21+
f(); // this should also print "global"
2022
}

test_programs/r1/3.lox

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
---
22
expected_error_type: none
33
---
4-
// The `inner` function should capture the variable from the closest outer
5-
// scope, which is the `outer` function's scope.
64
var x = "global";
75

86
fun outer() {
97
var x = "outer";
108

119
fun middle() {
10+
// The `inner` function should capture the variable from the closest outer
11+
// scope, which is the `outer` function's scope.
1212
fun inner() {
1313
print x; // Should capture "outer", not "global"
1414
}
1515

16-
inner();
16+
inner(); // Should print "outer"
17+
18+
// This variable declaration shouldn't affect the usage in `inner` above.
1719
var x = "middle";
20+
1821
inner(); // Should still print "outer"
1922
}
2023

test_programs/r1/4.lox

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
22
expected_error_type: none
33
---
4-
// The closure should capture the `count` variable from the global scope
5-
// and increment it. Reassigning the `count` variable in the local scope
6-
// should not affect the closure's capture.
74
var count = 0;
85

96
{
7+
// The `counter` function should use the `count` variable from the
8+
// global scope.
109
fun makeCounter() {
1110
fun counter() {
11+
// This should increment the `count` variable from the global scope.
1212
count = count + 1;
1313
print count;
1414
}
@@ -19,6 +19,8 @@ var count = 0;
1919
counter1(); // Should print 1
2020
counter1(); // Should print 2
2121

22-
var count = 0; // This local variable shouldn't affect our counter
22+
// This variable declaration shouldn't affect our counter.
23+
var count = 0;
24+
2325
counter1(); // Should print 3
2426
}

0 commit comments

Comments
 (0)