File: initializer.5c

package info (click to toggle)
nickle 2.47-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,112 kB
  • ctags: 3,255
  • sloc: ansic: 30,401; yacc: 1,843; sh: 865; lex: 838; makefile: 202
file content (104 lines) | stat: -rw-r--r-- 2,120 bytes parent folder | download | duplicates (2)
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
/*
 * An example of initializers and scoping rules
 *
 * Copyright © 2001 Keith Packard
 * All Rights Reserved.  See the file COPYING in this directory
 * for licensing information.
 */

continuation	c;

/*
 * Static initializer example
 */
function stat ()
{
    int	x = 1;
    function bar ()
    {
	static int qq = 37;
	/*
	 * Create an array containing two objects.
	 *
	 * During the evaluation of the first initializer,
	 * a continuation is captured.  Thus the initializer
	 * can be reentered at this point.
	 *
	 * The second continuation demonstrates creating an
	 * anonymous lambda and using a variety of values
	 * which are in scope
	 */
	static int[*] y = (int[2]) { 
	    (int l = setjmp (&c, 1)), 
	    l + (func (a) { return a + ++l + x; }) (++x) + ++qq 
	};
	return ++y[0] + ++y[1];
    }
    return bar () + bar();
}

/*
 * Some sample values from this function.  These are rather
 * hard to figure out from the code above.
 */

stat ()		/* 98 */
longjmp (c, 0)	/* 98 */
longjmp (c, 1)	/* 110 */

/*
 * Global initializer example
 */
int glob_x = 2;

function glob ()
{
    int	x = 2;
    function bar ()
    {
	int	z = 3;
	global	q = 7;
	function bletch ()
	{
	    /*
	     * This initializer is run in glob's static initializer context
	     * Again, an anonymous lambda is used to create a local name
	     * context to hold a variety of name types
	     */
	    global y = (func (a) { 
		static l_s = 7;
		global l_g = 8; 
		auto l_a = 9;
		return a + q + l_s + l_g + l_a; 
	    }) (glob_x);
	    /*
	     * This initializer is run in bletch's static initializer context
	     * Hence it can reference dynamic variables anywhere above
	     * bletch, static variables within bletch and global variables
	     * anywhere
	     */
	    static u = z + glob_x;
	    static t = u + q;

	    return ++y + u + t;
	}
	return bletch () + x;
    }
    return bar ();
}

/* 
 * Sample output from this function
 */

glob()	    /* 53 */
glob()	    /* 54 */
glob()	    /* 55 */

/*
 * Modify current glob_x value, has no effect on y value,
 * but does change u value
 */
glob_x = 47;	

glob ()	    /* 146 */