summaryrefslogtreecommitdiffstats
path: root/eshtrans/join.sh
blob: e40bf451a1817ca2e9216aa038b12f7aba7c3649 (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
#buf="$(cat "${0}")"
buf="$(cat 'backend/codegen.esh')"
#buf="$(cat 'frontend/lexer.esh')"

eval "$(printf '%s' "${buf}" | awk -v FS='' -v j=0 \
	-v squote="'" -v esc_squote="'\\\\''" '
	{
		for (i = 1; i <= NF; ++i) {
			sub(squote, esc_squote, $i);
			printf("lbufv_%d=" squote "%s" squote "\n",
				j++, $i);
		};
		printf("lbufv_%d=" squote "\n" squote "\n", j++);
	}
	')"

test_printf()
{
	foo()
	{
		i=0
		while [ ${i} -le ${#buf} ]; do
			c=$(eval "printf '%sx' \"\${lbufv_${i}}\"")
			printf '%s' "${c%x}"
			i=$((${i} + 1))
		done
	}
	out="$(foo)"
}

test_concat()
{
	foo()
	{
		i=0
		s=''
		while [ ${i} -le ${#buf} ]; do
			c=$(eval "printf '%sx' \"\${lbufv_${i}}\"")
			s="${s}${c%x}"
			i=$((${i} + 1))
		done
		printf '%s' "${s}"
	}
	out="$(foo)"
}

test_bc()
{
	foo()
	{
		eval "printf '%s' \"$(printf '${lbufv_%d}' $(printf \
			'for (i = 0; i <= %d; ++i) i\n' ${#buf} | bc))\""
	}
	out="$(foo)"
}

test_awk()
{
	foo()
	{
		eval "printf '%s' \"$(printf '${lbufv_%d}' $(awk \
			-v end=${#buf} \
			'BEGIN { for (i = 0; i <= end; ++i) print(i); }'))\""
	}
	out="$(foo)"
}

if [ ${#} -eq 1 ]; then
	test_${1}
	if ! [ "x${buf}" = "x${out}" ]; then
		printf 'Error: bad output:\n'
		printf '%s' "${out}" | sed 's/^/\t/'
	fi
else
	i=0
	while [ ${i} -lt 3 ]; do
		for f in printf concat bc awk; do
			printf '%s:\n' "${f}"
			time -p sh "${0}" "${f}" 2>&1 | sed 's/^/\t/'
		done
		i=$((${i} + 1))
	done
fi