8000 added POSIX --sh flag · coderofsalvation/powscript@76bfd02 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 76bfd02

Browse files
added POSIX --sh flag
1 parent e2b266b commit 76bfd02

File tree

6 files changed

+129
-37
lines changed

6 files changed

+129
-37
lines changed

.tools/compile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
#!/bin/bash
2+
selfpath="$( dirname "$(readlink -f "$0")" )"
3+
cd $selfpath/..
24

35
for i in lang/*; do
4-
[[ ! "$i" =~ "bash" ]] && continue
56
lang="$(basename $i)"
7+
[[ ! "$lang" =~ "bash" ]] && continue
68
{
7-
echo "#!/bin/$lang"
9+
echo "#!/bin/bash"
810
echo "settings='$(<lang/bash/.settings)'"
11+
echo "polyfill='$(<lang/bash/.polyfill)'"
912
cat lang/$lang/.settings lang/$lang/.util lang/$lang/.transpile
1013
echo "######################### begin-of-powscript-functions"
1114
cd lang/$lang &>/dev/null
1215
echo "powfunctions=\"$(echo *)\""
1316
cd - &>/dev/null
1417
cat lang/$lang/*
1518
echo "######################### end-of-powscript-functions"
16-
cat src/powscript.$lang
19+
cat src/powscript.bash
1720
} > powscript && chmod 755 powscript
1821
done
1922

README.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@
55
## Usage
66

77
$ wget "https://raw.githubusercontent.com/coderofsalvation/powscript/master/powscript" -O /usr/local/bin/powscript && chmod 755 /usr/local/bin/powscript
8-
$ powscript myscript.pow # run directly
9-
$ powscript --compile myscript.pow > myscript # output bashscript
8+
$ powscript myscript.pow # run directly
9+
$ powscript --compile myscript.pow > myscript # output bashscript
10+
$ powscript --compile --sh myscript.pow > myscript.sh # output sh-script
11+
12+
## Wiki
13+
14+
* [Syntax reference](https://github.com/coderofsalvation/powscript/wiki/Reference)
15+
* [Modules](https://github.com/coderofsalvation/powscript/wiki/Modules)
16+
* [Developer info / Contributions](https://github.com/coderofsalvation/powscript/wiki/Contributing)
17+
* [Similar projects](https://github.com/coderofsalvation/powscript/wiki/Similar-projects)
18+
* [Why](https://github.com/coderofsalvation/powscript/wiki/Why)
1019

1120
## Example
1221

@@ -59,18 +68,20 @@ Then hitting ctrl-p in your console will enter powscript mode:
5968
line=2,foo2,bar2,flop2
6069
>
6170

71+
## POSIX /bin/sh compatibility
72+
73+
Powscript can produce 'kindof' POSIX `/bin/sh`-compatible output by removing bashisms, by introducing the `--sh` flag:
74+
75+
$ powscript --compile foo.pow > foo.bash
76+
$ powscript --sh --compile foo.pow > foo.sh
77+
78+
This however, is not 100% tested, so please remove bashisms manually using docs/tools like [bashism guide](http://mywiki.wooledge.org/Bashism) or [https://linux.die.net/man/1/checkbashisms](checkbashisms)
79+
The general rule for POSIX sh-output is: `don't write bashfeatures in powscript`
80+
6281
## Live expansion inside editor
6382

6483
> HINT: use live expansion inside vim.
6584
> Put the lines below in .vimrc and hit 'p>' in normal/visual mode to expand powscript
6685
6786
vmap p> :!PIPE=2 powscript --compile<CR>
6887
nmap p> ggVG:!PIPE=2 powscript --compile<CR>
69-
70-
## Wiki
71-
72-
* [Syntax reference](https://github.com/coderofsalvation/powscript/wiki/Reference)
73-
* [Modules](https://github.com/coderofsalvation/powscript/wiki/Modules)
74-
* [Developer info / Contributions](https://github.com/coderofsalvation/powscript/wiki/Contributing)
75-
* [Similar projects](https://github.com/coderofsalvation/powscript/wiki/Similar-projects)
76-
* [Why](https://github.com/coderofsalvation/powscript/wiki/Why)

lang/bash/.polyfill

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# here comes a bash polyfill
2+
# it tries to substitute missing bash commands
3+
# to provide somewhat compatibility in other shells like bin/sh
4+
5+
noop(){
6+
:>/dev/null
7+
}
8+
9+
shopt(){
10+
noop
11+
}
12+

lang/bash/.settings

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
set -e # halt on error
2-
set +m #
3-
shopt -s lastpipe # flexible while loops (maintain scope)
4-
shopt -s extglob # regular expressions
1+
# powscript general settings
2+
set -e # halt on error
3+
set +m #
4+
SHELL="$(echo $0)" # shellname
5+
SHELLNAME="$(basename $SHELL)" # shellname without path
6+
shopt -s lastpipe # flexible while loops (maintain scope)
7+
shopt -s extglob # regular expressions
58
path="$(pwd)"
69
selfpath="$( dirname "$(readlink -f "$0")" )"
710
tmpfile="/tmp/$(basename $0).tmp.$(whoami)"

powscript

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
#!/bin/bash
2-
settings='set -e # halt on error
3-
set +m #
4-
shopt -s lastpipe # flexible while loops (maintain scope)
5-
shopt -s extglob # regular expressions
2+
settings='# powscript general settings
3+
set -e # halt on error
4+
set +m #
5+
SHELL="$(echo $0)" # shellname
6+
SHELLNAME="$(basename $SHELL)" # shellname without path
7+
shopt -s lastpipe # flexible while loops (maintain scope)
8+
shopt -s extglob # regular expressions
69
path="$(pwd)"
710
selfpath="$( dirname "$(readlink -f "$0")" )"
811
tmpfile="/tmp/$(basename $0).tmp.$(whoami)"'
9-
set -e # halt on error
10-
set +m #
11-
shopt -s lastpipe # flexible while loops (maintain scope)
12-
shopt -s extglob # regular expressions
12+
polyfill='# here comes a bash polyfill
13+
# it tries to substitute missing bash commands
14+
# to provide somewhat compatibility in other shells like bin/sh
15+
16+
noop(){
17+
:>/dev/null
18+
}
19+
20+
shopt(){
21+
noop
22+
}'
23+
# powscript general settings
24+
set -e # halt on error
25+
set +m #
26+
SHELL="$(echo $0)" # shellname
27+
SHELLNAME="$(basename $SHELL)" # shellname without path
28+
shopt -s lastpipe # flexible while loops (maintain scope)
29+
shopt -s extglob # regular expressions
1330
path="$(pwd)"
1431
selfpath="$( dirname "$(readlink -f "$0")" )"
1532
tmpfile="/tmp/$(basename $0).tmp.$(whoami)"
@@ -296,11 +313,12 @@ rand=$(cat /dev/urandom | tr -cd [:alnum:] | head -c 4)
296313
ID=$date"_"$rand
297314
includefuncs=""
298315
requires=""
299-
tmpfile="/tmp/.$(whoami).powscript.$date_$rand"
316+
tmpfile="/tmp/.$(whoami).pow.$date_$rand"
300317
ps1="${PS1//\\u/$USER}"; p="${p//\\h/$HOSTNAME}"
301318
evalstr=""
302319
evalstr_cache=""
303320
shopt -s extglob
321+
[[ -n $runtime ]] && runtime=$runtime || runtime=bash
304322

305323
input=$1
306324
if [[ ! -n $startfunction ]]; then
@@ -309,8 +327,8 @@ fi
309327

310328
empty "$1" && {
311329
echo 'Usage:
312-
powscript <file.powscript>
313-
powscript --compile <file.pow>
330+
powscript <file.pow>
331+
powscript --compile [--sh] <file.pow>
314332
powscript --lint <file.pow>
315333
powscript --evaluate <powscript string>
316334
powscript --interactive
@@ -323,6 +341,10 @@ empty "$1" && {
323341

324342
for arg in "$@"; do
325343
case "$arg" in
344+
--sh)
345+
runtime=sh
346+
shift
347+
;;
326348
--interactive)
327349
startfunction="console process"
328350
shift
@@ -342,6 +364,18 @@ for arg in "$@"; do
342364
esac
343365
done
344366

367+
transpile_sh(){
368+
if [[ $runtime == "bash" ]]; then
369+
cat -
370+
else
371+
cat - \
372+
| sed "s/\[\[/\[/g;s/\]\]/\]/g" \
373+
| sed "s/ == / = /g" \
374+
| sed "s/\&>\(.*[^;]\)[; $]/1>\1 2>\1; /g" \
375+
| transpile_all
376+
fi
377+
}
378+
345379
transpile_sugar(){
346380
while IFS="" read -r line; do
347381
stack_update "$line"
@@ -399,11 +433,17 @@ compile(){
399433
local dir="$(dirname "$1")"; local file="$(basename "$1")"; cd "$dir" &>/dev/null
400434
{ cat_requires "$file" ; echo -e "#\n# application code\n#\n"; cat "$file"; } | lint_pipe > $tmpfile
401435
fi
402-
[[ ! $PIPE == 2 ]] && echo -e "#!/bin/bash\n$settings"
436+
[[ ! $PIPE == 2 ]] && {
437+
echo -e "#!/bin/$runtime\n"
438+
[[ ! $runtime == "bash" ]] && echo -e "$polyfill"
439+
echo -e "$settings"
440+
}
403441
transpile_sugar "$tmpfile" | grep -v "^#" > $tmpfile.code
404442
transpile_functions $tmpfile.code
405-
cat $tmpfile.code
406-
[[ ! $PIPE == 2 ]] && for i in ${!footer[@]}; do echo "${footer[$i]}"; done
443+
{
444+
cat $tmpfile.code
445+
[[ ! $PIPE == 2 ]] && for i in ${!footer[@]}; do echo "${footer[$i]}"; done
446+
} | transpile_sh
407447
rm $tmpfile
408448
}
409449

src/powscript.bash

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ rand=$(cat /dev/urandom | tr -cd [:alnum:] | head -c 4)
44
ID=$date"_"$rand
55
includefuncs=""
66
requires=""
7-
tmpfile="/tmp/.$(whoami).powscript.$date_$rand"
7+
tmpfile="/tmp/.$(whoami).pow.$date_$rand"
88
ps1="${PS1//\\u/$USER}"; p="${p//\\h/$HOSTNAME}"
99
evalstr=""
1010
evalstr_cache=""
1111
shopt -s extglob
12+
[[ -n $runtime ]] && runtime=$runtime || runtime=bash
1213

1314
input=$1
1415
if [[ ! -n $startfunction ]]; then
@@ -17,8 +18,8 @@ fi
1718

1819
empty "$1" && {
1920
echo 'Usage:
20-
powscript <file.powscript>
21-
powscript --compile <file.pow>
21+
powscript <file.pow>
22+
powscript --compile [--sh] <file.pow>
2223
powscript --lint <file.pow>
2324
powscript --evaluate <powscript string>
2425
powscript --interactive
@@ -31,6 +32,10 @@ empty "$1" && {
3132

3233
for arg in "$@"; do
3334
case "$arg" in
35+
--sh)
36+
runtime=sh
37+
shift
38+
;;
3439
--interactive)
3540
startfunction="console process"
3641
shift
@@ -50,6 +55,18 @@ for arg in "$@"; do
5055
esac
5156
done
5257

58+
transpile_sh(){
59+
if [[ $runtime == "bash" ]]; then
60+
cat -
61+
else
62+
cat - \
63+
| sed "s/\[\[/\[/g;s/\]\]/\]/g" \
64+
| sed "s/ == / = /g" \
65+
| sed "s/\&>\(.*[^;]\)[; $]/1>\1 2>\1; /g" \
66+
| transpile_all
67+
fi
68+
}
69+
5370
transpile_sugar(){
5471
while IFS="" read -r line; do
5572
stack_update "$line"
@@ -107,11 +124,17 @@ compile(){
107124
local dir="$(dirname "$1")"; local file="$(basename "$1")"; cd "$dir" &>/dev/null
108125
{ cat_requires "$file" ; echo -e "#\n# application code\n#\n"; cat "$file"; } | lint_pipe > $tmpfile
109126
fi
110-
[[ ! $PIPE == 2 ]] && echo -e "#!/bin/bash\n$settings"
127+
[[ ! $PIPE == 2 ]] && {
128+
echo -e "#!/bin/$runtime\n"
129+
[[ ! $runtime == "bash" ]] && echo -e "$polyfill"
130+
echo -e "$settings"
131+
}
111132
transpile_sugar "$tmpfile" | grep -v "^#" > $tmpfile.code
112133
transpile_functions $tmpfile.code
113-
cat $tmpfile.code
114-
[[ ! $PIPE == 2 ]] && for i in ${!footer[@]}; do echo "${footer[$i]}"; done
134+
{
135+
cat $tmpfile.code
136+
[[ ! $PIPE == 2 ]] && for i in ${!footer[@]}; do echo "${footer[$i]}"; done
137+
} | transpile_sh
115138
rm $tmpfile
116139
}
117140

0 commit comments

Comments
 (0)
0