summaryrefslogtreecommitdiffstats
path: root/eshtrans/main.esh
blob: 8b94861ee3b52d0bc12bcb147b7512442bc8ec3f (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
# Eggshell Compiler entry point
#
# Copyright (C) 2016  Patrick "P. J." McDermott
#
# This file is part of the Eggshell Compiler.
#
# The Eggshell Compiler is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# The Eggshell Compiler is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with the Eggshell Compiler.  If not, see
# <http://www.gnu.org/licenses/>.

try()
{
	local tokens=
	local t=

	printf 'Trying script:\n'
	printf '\t%s\n' "${@}"
	if tokens="$(printf '%s\n' "${@}" | esh_parse -)"; then
		printf 'Tokens: %s\n' "${tokens}" | sed "
			s/${SOH}/<SOH>/g; s/${STX}/<STX>/g; s/${ETX}/<ETX>/g;
			s/${RS}/<RS>/g; s/${US}/<US>/g;
			"
		IFS="${RS}"
		for t in ${tokens}; do
			printf 'Token: %s\n' "$(tokname "${t}")"
			case "${t%${US}*}" in T_NAME|T_FNAME|T_CMDNAME|T_WORD)
				printf '       "%s"\n' "${t#*${US}}"
				;;
			esac
		done
		printf 'Generated code:\n'
		IFS="${LF}"
		printf '\t%s\n' $(sh_codegen "${tokens}")
		unset IFS
	else
		printf 'FAIL\n'
	fi
	printf '\n\n'
}

main()
{
#try '"foo bar" && $baz || qux' '${quux%uux  quuux'
#try '"foo bar" && $baz || qux' '${quux%uux } quuux'
#try 'foo ${bar}'
#try 'foo ${#bar}'
#try 'foo ${bar#baz}'
#try 'foo ${#bar#}'
#try 'foo ${^}'
#try 'foo `bar`'
#try 'foo &&'
#try '{ foo; }'
try '( foo )'
#try 'for i in 1 2 3; do stuff; done'
#try 'if foo; then bar; fi'
#try 'if foo; then bar; elif baz; then qux; else quux; fi'
#try 'if ; then ; fi'
#try 'while foo; do bar; done'
#try 'while ; do ; done'
try 'foo(){ bar; }'
#try 'case foo in bar) baz;; (qux) quux;; quux);; esac'
#try 'foo bar ( baz )'
#try 'foo $(bar)'
#try 'foo $(bar); baz'
#try 'foo $(bar)' 'baz'
#try 'foo $(bar) baz'
#try 'foo$(bar$(baz))qux'
#try 'foo $((1 + 1))'
#try '$((1 + 1))'
#try '$((1 + (1 + 1)))'
#try '$((1 + $(foo) + 1))'
#try '$((1'
#try 'foo <<EOF' 'bar' 'EOF'
#try 'foo <<-EOF' "${HT}bar" "${HT}EOF"
#try 'foo <<EOF' '$(bar)' 'EOF'
#try 'foo <<E"O"F' '$(bar)' 'EOF'
#try 'foo <<"EOF"' '$(bar)' 'EOF'
#try 'foo <<E\OF' '$(bar)' 'EOF'
#try 'foo <<\EOF' '$(bar)' 'EOF'
#try 'foo <<EOF1; bar <<EOF2' 'baz' 'EOF1' 'qux' 'EOF2'
#try '\foo'
#try '"foo bar" baz'
#try '"foo'
#try 'foo\" bar'
#try 'foo\'
#try "foo'"
#try 'foo\' 'bar'
#try 'v=foo'
#try 'if &&'
#try 'if true; do'
#try 'foo' '#bar'
#try '# foo' 'bar'
#try '' 'foo'
#try '\\ foo'
#try '$(($foo + 1))'
#try '$((1 + $foo))'
#try "foo '\`'"
#try 'foo "`"'
#try "\"'\""
#try "foo 'bar" "baz' &&"
}