summaryrefslogtreecommitdiffstats
path: root/research/research6.sh
blob: 8025ddb0b4ee8b4653d53d7dded471a50cac131c (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
__ESC="$(printf '\033.')"; __ESC="${__ESC%.}"
 __RS="$(printf '\036.')";  __RS="${__RS%.}"
 __US="$(printf '\037.')";  __US="${__US%.}"
__sp=0
__fn_name=''
__fn_tu=''
__fn_vars=''
__fn_var_vals=''
__fn=''
__prev_tu=''
__prev_vars=''
__var=''
__val=''
__fn_frame=0
__fn_begin_hooks=''
__fn_end_hooks=''
__init_funcs=''

__sp_inc()
{
	__sp=$((${__sp} + 1))
}

__sp_dec()
{
	__sp=$((${__sp} - 1))
}

__frame_set()
{
	eval "__frame_${__sp}_name=\${__fn_name}"
	eval "__frame_${__sp}_tu=\${__fn_tu}"
	eval "__frame_${__sp}_vars=\${__fn_vars}"
	eval "__frame_${__sp}_var_vals=\${__fn_var_vals}"
}

__frame_get()
{
	eval "__fn_name=\${__frame_${__sp}_name}"
	eval "__fn_tu=\${__frame_${__sp}_tu}"
	eval "__fn_vars=\${__frame_${__sp}_vars}"
	eval "__fn_var_vals=\${__frame_${__sp}_var_vals}"
}

__fn_ctxsw()
{
	unset IFS
	case "${__prev_tu}" in
		${__fn_tu});;
		?*)
			# Unset static variables and functions from previous TU.
			eval "__val=\${__${__prev_tu}_static_vars}"
			case "${__val}" in ?*)
				eval "unset \${__${__prev_tu}_static_vars}"
			;; esac
			eval "__val=\${__${__prev_tu}_static_fns}"
			for __fn in ${__val}; do
				unset -f "${__fn%:*}"
			done
	;; esac
	case "${__fn_tu}" in
		${__prev_tu});;
		?*)
			# Set static variables and function for the current TU.
			eval "__val=\${__${__fn_tu}_static_var_vals}"
			eval "${__val}"
			eval "__val=\${__${__fn_tu}_static_fns}"
			for __fn in ${__val}; do
				eval "${__fn%:*}() { ${__fn#*:}; }"
			done
	;; esac
	case "${__prev_vars}" in ?*)
		unset ${__prev_vars}
	;; esac
	for __var in ${__fn_vars}; do
		eval "${__var}=\${__fn_vars_${__fn_name}__${__var}}"
	done
}

__fn_update_vars()
{
	unset IFS
	for __var in ${__fn_vars}; do
		eval "__fn_vars_${__fn_name}__${__var}=\${${__var}}"
	done
}

__fn_start()
{
	# Hooks.
	unset IFS
	for __fn in ${__fn_begin_hooks}; do
		${__fn} ${2}
	done

	# Old state.
	__prev_tu="${__fn_tu}"
	__prev_vars="${__fn_vars}"

	# Update local vars in stack.
	__fn_update_vars
	__frame_set

	# Set stack pointer.
	__sp_inc

	# New state.
	__fn_name="${2}"
	__fn_tu="${1}"
	__fn_vars=
	__fn_var_vals=
	__frame_set

	# Switch.
	__fn_ctxsw
}

__fn_end()
{
	# Hooks.
	unset IFS
	for __fn in ${__fn_end_hooks}; do
		${__fn} ${__fn_name}
	done

	# Old state.
	__prev_tu="${__fn_tu}"
	__prev_vars="${__fn_vars}"

	# Set stack pointer.
	__sp_dec

	# New state.
	__frame_get

	# Switch.
	__fn_ctxsw
}

__tu_end()
{
	# Save TU static vars.
	unset IFS
	for __var in $(eval "printf '%s' \"\${__${__tu}_static_vars}\""); do
		__val="$(eval "printf '%s' \"\${${__var}}\"" | \
			sed "s|'|'\\\\''|g")"
		eval "__${__tu}_static_var_vals=\"\${__${__tu}_static_var_vals}\
			${__var}='\${__val}'\""
		unset ${__var}
	done

	__tu=''
}

__stack_trace()
{
	__fn_frame=0
	while [ ${__fn_frame} -le ${__sp} ]; do
		eval "printf '%s\n' \"\${__frame_${__fn_frame}_name}\""
		__fn_frame=$((${__fn_frame} + 1))
	done
}

__local()
{
	unset IFS
	for __var in "${@}"; do
		case "${__var}" in
			*=*)
				__val="${__var#*=}"
				__var="${__var%%=*}"
				;;
			*)
				__val=''
				;;
		esac
		case "${__var}" in [!A-Za-z_]* | *[!0-9A-Za-z_]*)
			printf 'Syntax error: %s %s\n' \
				'illegal static variable name:' "${__var}" >&2
			exit 1
		;; esac
		eval "${__var}=\${__val}"
		__fn_vars="${__fn_vars} ${__var}"
	done
}

__static()
{
	case "${__tu}" in '')
		printf 'Error: Cannot declare static variables in functions' >&2
		exit 1
	;; esac
	unset IFS
	for __var in "${@}"; do
		case "${__var}" in
			*=*)
				__val="${__var#*=}"
				__var="${__var%%=*}"
				;;
			*)
				__val=''
				;;
		esac
		case "${__var}" in [!A-Za-z_]* | *[!0-9A-Za-z_]*)
			printf 'Syntax error: %s %s\n' \
				'illegal static variable name:' "${__var}" >&2
			exit 1
		;; esac
		eval "${__var}=\${__val}"
		eval "__${__tu}_static_vars=\"\${__${__tu}_static_vars} \
			${__var}\""
	done
}

__fn_begin_hook()
{
	__fn_begin_hooks="${__fn_begin_hooks} ${1}"
}

__fn_end_hook()
{
	__fn_end_hooks="${__fn_end_hooks} ${1}"
}

__init()
{
	__init_funcs="${__init_funcs} ${1}"
}

EOF(){ :; }

#: <<'EOF'
################################################################################
# TU 0
################################################################################
__tu=tu0
__tu0_static_vars=
__tu0_static_var_vals=
__tu0_static_fns=

a()
{
	__fn_start tu0 a
	__local v=1
	echo "v (should be 1): $v"
	b
	echo "v (should be 1): $v"
	__fn_end
}

b()
{
	__fn_start tu0 b
	echo "v (should be empty): $v"
	__fn_end
}

__tu_end

a
EOF