8000 added powscript lint · coderofsalvation/powscript@00fa2b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 00fa2b4

Browse files
added powscript lint
1 parent 171ea01 commit 00fa2b4

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "powscript",
3-
"version": "1.0.23",
3+
"version": "1.0.24",
44
"description": "bash dialect transpiler in bash: painless shellscript / indentbased / coffeescript for shellscript / bash for hipsters",
55
"main": "powscript",
66
"directories": {

powscript

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,10 @@ fi
313313
empty "$1" && {
314314
echo 'Usage:
315315
powscript <file.powscript>
316-
powscript --compile <file.powscript>
317-
powscript --interactive
316+
powscript --compile <file.pow>
317+
powscript --lint <file.pow>
318318
powscript --evaluate <powscript string>
319+
powscript --interactive
319320
echo <powscript string> | PIPE=1 powscript --compile
320321
echo <powscript string> | PIPE=1 powscript --evaluate
321322
@@ -333,6 +334,10 @@ for arg in "$@"; do
333334
startfunction=evaluate
334335
shift
335336
;;
337+
--lint)
338+
startfunction=lint
339+
shift
340+
;;
336341
--compile)
337342
startfunction=compile
338343
shift
@@ -392,10 +397,10 @@ transpile_functions(){
392397

393398
compile(){
394399
if [[ -n $PIPE ]]; then
395-
cat - > $tmpfile
400+
cat - | lint_pipe > $tmpfile
396401
else
397402
local dir="$(dirname "$1")"; local file="$(basename "$1")"; cd "$dir" &>/dev/null
398-
{ cat_requires "$file" ; echo -e "#\n# application code\n#\n"; cat "$file"; } > $tmpfile
403+
{ cat_requires "$file" ; echo -e "#\n# application code\n#\n"; cat "$file"; } | lint_pipe > $tmpfile
399404
fi
400405
[[ ! $PIPE == 2 ]] && echo -e "#!/bin/bash\n$settings"
401406
transpile_sugar "$tmpfile" | grep -v "^#" > $tmpfile.code
@@ -416,7 +421,7 @@ process(){
416421
}
417422

418423
evaluate(){
419-
[[ -n $PIPE ]] && cat - > $tmpfile || echo -e "$*" > $tmpfile
424+
[[ -n $PIPE ]] && cat - > $tmpfile || echo -e "$*" | lint_pipe > $tmpfile
420425
evalstr_cache="$evalstr_cache\n$*"
421426
[[ -n $DEBUG ]] && echo "$(transpile_sugar $tmpfile)"
422427
eval "$(transpile_sugar $tmpfile)"
@@ -503,6 +508,23 @@ help(){
503508
' | less
504509
}
505510

511+
512+
lint(){
513+
cat "$1" | lint_pipe
514+
}
515+
516+
lint_pipe(){
517+
code="$(cat -)"
518+
output="$(echo "$code" | awk -F"[ ]" '{ j=0; for(i=1;i<=NF && ($i=="");i++); j++; if( ((i-1)%2) != 0 ){ print "indent error: "$j" "$i; } }')"
519+
if [[ ${#output} != 0 ]]; then
520+
echo "$output" 1>&2
521+
exit 1
522+
else
523+
echo "$code"
524+
return 0
525+
fi
526+
}
527+
506528
console(){
507529
[[ ! $1 == "1" ]] && echo "hit ctrl-c to exit powscript, type 'edit' to launch editor, and 'help' for help"
508530
trap 'console 1' 0 1 2 3 13 15 # EXIT HUP INT QUIT PIPE TERM SIGTERM SIGHUP

src/powscript.bash

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ fi
1818
empty "$1" && {
1919
echo 'Usage:
2020
powscript <file.powscript>
21-
powscript --compile <file.powscript>
22-
powscript --interactive
21+
powscript --compile <file.pow>
22+
powscript --lint <file.pow>
2323
powscript --evaluate <powscript string>
24+
powscript --interactive
2425
echo <powscript string> | PIPE=1 powscript --compile
2526
echo <powscript string> | PIPE=1 powscript --evaluate
2627
@@ -38,6 +39,10 @@ for arg in "$@"; do
3839
startfunction=evaluate
3940
shift
4041
;;
42+
--lint)
43+
startfunction=lint
44+
shift
45+
;;
4146
--compile)
4247
startfunction=compile
4348
shift
@@ -97,10 +102,10 @@ transpile_functions(){
97102

98103
compile(){
99104
if [[ -n $PIPE ]]; then
100-
cat - > $tmpfile
105+
cat - | lint_pipe > $tmpfile
101106
else
102107
local dir="$(dirname "$1")"; local file="$(basename "$1")"; cd "$dir" &>/dev/null
103-
{ cat_requires "$file" ; echo -e "#\n# application code\n#\n"; cat "$file"; } > $tmpfile
108+
{ cat_requires "$file" ; echo -e "#\n# application code\n#\n"; cat "$file"; } | lint_pipe > $tmpfile
104109
fi
105110
[[ ! $PIPE == 2 ]] && echo -e "#!/bin/bash\n$settings"
106111
transpile_sugar "$tmpfile" | grep -v "^#" > $tmpfile.code
@@ -121,7 +126,7 @@ process(){
121126
}
122127

123128
evaluate(){
124-
[[ -n $PIPE ]] && cat - > $tmpfile || echo -e "$*" > $tmpfile
129+
[[ -n $PIPE ]] && cat - > $tmpfile || echo -e "$*" | lint_pipe > $tmpfile
125130
evalstr_cache="$evalstr_cache\n$*"
126131
[[ -n $DEBUG ]] && echo "$(transpile_sugar $tmpfile)"
127132
eval "$(transpile_sugar $tmpfile)"
@@ -208,6 +213,23 @@ help(){
208213
' | less
209214
}
210215

216+
217+
lint(){
218+
cat "$1" | lint_pipe
219+
}
220+
221+
lint_pipe(){
222+
code="$(cat -)"
223+
output="$(echo "$code" | awk -F"[ ]" '{ j=0; for(i=1;i<=NF && ($i=="");i++); j++; if( ((i-1)%2) != 0 ){ print "indent error: "$j" "$i; } }')"
224+
if [[ ${#output} != 0 ]]; then
225+
echo "$output" 1>&2
226+
exit 1
227+
else
228+
echo "$code"
229+
return 0
230+
fi
231+
}
232+
211233
console(){
212234
[[ ! $1 == "1" ]] && echo "hit ctrl-c to exit powscript, type 'edit' to launch editor, and 'help' for help"
213235
trap 'console 1' 0 1 2 3 13 15 # EXIT HUP INT QUIT PIPE TERM SIGTERM SIGHUP

0 commit comments

Comments
 (0)
0