diff options
-rw-r--r-- | eshtrans/main.esh | 164 |
1 files changed, 69 insertions, 95 deletions
diff --git a/eshtrans/main.esh b/eshtrans/main.esh index 0a0b929..7e0cb8c 100644 --- a/eshtrans/main.esh +++ b/eshtrans/main.esh @@ -18,106 +18,80 @@ # along with the Eggshell Compiler. If not, see # <http://www.gnu.org/licenses/>. -try() +set -u + +usage() { - local tokens= - local t= + printf 'Usage: %s [-o <output>] <input>...\n' "${0}" +} - 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' +version() +{ + cat <<EOF +eshtrans 0.1.0 +Copyright (C) 2016 Patrick "P. J." McDermott +License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. +This is free software: you are free to change and redistribute it. +There is NO WARRANTY, to the extent permitted by law. +EOF +} + +err() +{ + local fmt="${1}" + shift 1 + + printf "eshtrans: Error: ${fmt}\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' &&" -#try '$(foo bar)' -#try 'foo <<EOF' '$(bar baz)' 'EOF' -#try '$foo' -#try '${foo%bar baz}' -#try '$(($(foo bar) + 1))' -#try 'foo 2>bar' -#try '2>bar' -#try '2 foo' -dbg=true -try 'foo $(bar \' 'baz \' 'qux) &&' # Line number testing + local opt= + local input= + local output= + + output='' + unset OPTARG + while getopts 'V:o:' opt; do + case "${opt}" in + 'V') + version + exit + ;; + 'o') + output="${OPTARG}" + ;; + '?') + usage >&2 + return 1 + ;; + esac + unset OPTARG + done + shift $(($OPTIND - 1)) -# sh_codegen "$(esh_parse "${1}" <"${1}")" + if [ ${#} -eq 0 ]; then + err 'No input files' + return 1 + fi + + if [ "x${output}" = 'x' ]; then + for input in "${@}"; do + output="${input%.esh}.sh" + sh_codegen "$(esh_parse "${input}" <"${input}")" \ + >"${output}" + done + else + if [ ${#} -gt 1 ]; then + err 'Cannot specify -o with multiple input files' + return 1 + fi + input="${1}" + if [ "x${output}" = 'x-' ]; then + sh_codegen "$(esh_parse "${input}" <"${input}")" + else + sh_codegen "$(esh_parse "${input}" <"${input}")" \ + >"${output}" + fi + fi } |