summaryrefslogtreecommitdiff
path: root/doc/0_06_scope.adoc
blob: 5025052c86a44ee2941e016d95b3bcfb3bb30d2d (plain)
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
Scope
=====

We saw in the previous paragraph that functions can be used, and that they can have parameter.
This forces us to clarify 'scope'.

[source,chapel]
./scope.lm
----
str d (where:str) {
    print( "in D ", where, "\n")
    where = "d"
    print( "in D ", where, "\n")
}

str c ( ) {
    print( "in C ", where_g, "\n")
    where_g = "c"
    print( "in C ", where_g, "\n")
}

str b ( where:str ) {
    print( "in B ", where, "\n")
    where = "b" 
    print( "in B ", where, "\n")
}

str a( where:str ) {
    print( "in A ", where, "\n")
    where = "a" 
    b( where )
    print( "in A ", where, "\n")
}

where: str =  "global"
print( "in global ", where, "\n")
a( where )
print( "in global ", where, "\n")
global where_g:str
c( )
print( "in global ", where_g, "\n")
----

We run it with
[source,bash]
----
/opt/colm/bin/colm scope.lm
./scope
----

That gives us:
----
in global global
in A global
in B a
in B b
in A a
in global global
in C NIL
in C c
in global c
----

The thesis also mentions that variables can be passed by reference instead of by value.

[source,chapel]
.nested_scope.lm
----
str a( where:str ) {
    print( "before block1 ", where, "\n" )
    while(true) {
        where = "block1"
        print( "in block1 ", where, "\n" )
        i:int = 0
        while( true ) {
            where =  where + "a"
            print( "in loop ", where, "\n" )
            break
        }
        print( "in block1 ", where, "\n" )
        break
    }
    print( "in A ", where, "\n" )
    return where
}

where: str =  "global"
print( "in global ", where, "\n" )
a( where )
print( "in global ", where, "\n" )
----

That gives us:
----
in global global
in A global
in B a
in B b
in A a
in global global
in C NIL
in C c
in global c
----

[source,bash]
----
/opt/colm/bin/colm nested_scope.lm
./nested_scope
----

It seems that this is still the case.
----
in global global
before block1 global
in block1 block1
in loop block1a
in block1 block1a
in A block1a
in global global
----