From e221064cecfad291e65609e7412f074a64a6f5e4 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Sat, 20 Feb 2016 11:52:54 -0500 Subject: Add code generation --- (limited to 'parsing') diff --git a/parsing/codegen.sh b/parsing/codegen.sh new file mode 100644 index 0000000..c28ffca --- /dev/null +++ b/parsing/codegen.sh @@ -0,0 +1,85 @@ +toktext='' + +toktext() +{ + local t="${1}" + shift 1 + local n= + + case "${t%${US}*}" in + # Operators + T_EOF) n='';; + T_NEWLINE) n="${LF}";; + T_AND) n='&';; + T_SEMI) n=';';; + T_AND_IF) n='&&';; + T_OR_IF) n='||';; + T_DSEMI) n=';;';; + T_LESS) n='<';; + T_GREAT) n='>';; + T_DLESS) n='<<';; + T_DGREAT) n='>>';; + T_LESS) n='<';; + T_LESSAND) n='<&';; + T_GREAT) n='>';; + T_GREATAND) n='>&';; + T_LESSGREAT) n='<>';; + T_DLESSDASH) n='<<-';; + T_CLOBBER) n='>|';; + T_PIPE) n='|';; + T_LPAREN) n='(';; + T_RPAREN) n=')';; + # Reserved words + T_IF) n='if';; + T_THEN) n='then';; + T_ELSE) n='else';; + T_ELIF) n='elif';; + T_FI) n='fi';; + T_DO) n='do';; + T_DONE) n='done';; + T_CASE) n='case';; + T_ESAC) n='esac';; + T_WHILE) n='while';; + T_UNTIL) n='until';; + T_FOR) n='for';; + T_LBRACE) n='{';; + T_RBRACE) n='}';; + T_BANG) n='!';; + T_IN) n='in';; + # Special symbols + T_NAME) n="${t#*${US}}";; + T_FNAME) n="${t#*${US}}";; + T_CMDNAME) n="${t#*${US}}";; + T_IO_NUMBER) n="${t#*${US}}";; + T_WORD) n="${t#*${US}}";; + T_ASSIGNMENT_WORD) n="${t#*${US}}";; + # Unknown + *) n='';; + esac + + toktext="${n}" +} + +codegen() +{ + local toks="${1}" + shift 1 + local subtoks= + local t= + + case "${toks}" in + *"${STX}"*"${ETX}"*) + subtoks="${toks#*${STX}}" + subtoks="${subtoks%${ETX}*}" + toks="${toks%%${STX}*}$(codegen \ + "${subtoks}")${toks##*${ETX}}" + ;; + esac + + IFS="${RS}" + for t in ${toks}; do + toktext "${t}" + printf '%s ' "${toktext}" + done + unset IFS +} diff --git a/parsing/parse.sh b/parsing/parse.sh index faf4637..a28aad2 100644 --- a/parsing/parse.sh +++ b/parsing/parse.sh @@ -1,7 +1,9 @@ -HT="$(printf '\t.')"; HT="${HT%.}" -LF="$(printf '\n.')"; LF="${LF%.}" -RS="$(printf '\036.')"; RS="${RS%.}" -US="$(printf '\037.')"; US="${US%.}" +STX="$(printf '\002.')"; STX="${STX%.}" +ETX="$(printf '\003.')"; ETX="${ETX%.}" + HT="$(printf '\t.')"; HT="${HT%.}" + LF="$(printf '\n.')"; LF="${LF%.}" + RS="$(printf '\036.')"; RS="${RS%.}" + US="$(printf '\037.')"; US="${US%.}" dbg() { @@ -46,6 +48,7 @@ ptrace_fail() . ./tokens.sh . ./lexer.sh +. ./codegen.sh complete_command() { @@ -626,6 +629,9 @@ try() ;; esac done + printf 'Generated code:\n' + IFS="${LF}" + printf '\t%s\n' $(codegen "${tokens}") unset IFS else printf 'FAIL\n' @@ -634,7 +640,7 @@ try() } #try '"foo bar" && $baz || qux' '${quux%uux quuux' -#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}' @@ -653,5 +659,5 @@ try() #try 'foo(){ bar; }' #try 'case foo in bar) baz;; (qux) quux;; quux);; esac' #try 'foo bar ( baz )' -try 'foo $(bar)' +#try 'foo $(bar)' #try 'foo $((1 + 1))' -- cgit v0.9.1