From c40e6e23966b9fd51ffa700903667cb552a52923 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Sun, 21 Feb 2016 21:47:59 -0500 Subject: eshtrans: Add proper user interface --- 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 # . -try() +set -u + +usage() { - local tokens= - local t= + printf 'Usage: %s [-o ] ...\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}//g; s/${STX}//g; s/${ETX}//g; - s/${RS}//g; s/${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 <. +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 <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 } -- cgit v0.9.1