diff --git a/README.md b/README.md index c4b1b42..8df3cd2 100644 --- a/README.md +++ b/README.md @@ -53,16 +53,18 @@ wt :-1: :-1: wl # 9. Print the largest first field and the line that contains it (assumes some $1 is positive): # $1 > max { max = $1; line = $0 } # END { print max, line } - -0 @max +-99999999 max! "" max-line! .. -# line first -(dup wsplit :1: toFloat [(dup max! >) (@max @line)] if) -each max! w " " w line! w +( + dup line! # Store line + wsplit :0: toFloat dup first-item! # Store first item + [(@max >) (@first-item max! @line max-line!)] if +) each +@max str w " " w @max-line wl # 10. Print every line that has at least one field # NF > 0 -.. (len 0 >) filter (wl) each +.. (wsplit len 0 >) filter (wl) each # 11. Print every line longer than 80 characters # length($0) > 80 diff --git a/examples/awk.msh b/examples/awk.msh index ea7c19a..0ff27b2 100644 --- a/examples/awk.msh +++ b/examples/awk.msh @@ -1,4 +1,4 @@ -#!/usr/bin/env mshell-go +#!/usr/bin/env mshell # 1. Print the total nubmer of input lines: # END { print NR } @@ -38,16 +38,19 @@ wt :-1: :-1: wl # 9. Print the largest first field and the line that contains it (assumes some $1 is positive): # $1 > max { max = $1; line = $0 } # END { print max, line } - -0 @max +-99999999 max! "" max-line! .. -# line first -(dup " " split :1: toFloat [(dup max! >) (@max @line)] if) -each max! w " " w line! w +( + dup line! # Store line + wsplit :0: toFloat dup first-item! # Store first item + [(@max >) (@first-item max! @line max-line!)] if +) each +@max str w " " w @max-line wl + # 10. Print every line that has at least one field # NF > 0 -stdin lines (len 0 >) filter (wl) each +.. (wsplit len 0 >) filter (wl) each # 11. Print every line longer than 80 characters # length($0) > 80 diff --git a/examples/awk/10.awk b/examples/awk/10.awk new file mode 100644 index 0000000..2848366 --- /dev/null +++ b/examples/awk/10.awk @@ -0,0 +1 @@ +NF > 0 diff --git a/examples/awk/10.data b/examples/awk/10.data new file mode 100644 index 0000000..77618a0 --- /dev/null +++ b/examples/awk/10.data @@ -0,0 +1,5 @@ +1 2 + +3 4 + +5 6 diff --git a/examples/awk/10.msh b/examples/awk/10.msh new file mode 100644 index 0000000..8f2ae8b --- /dev/null +++ b/examples/awk/10.msh @@ -0,0 +1 @@ +.. (wsplit len 0 >) filter (wl) each diff --git a/examples/awk/9.awk b/examples/awk/9.awk new file mode 100644 index 0000000..4c9b50d --- /dev/null +++ b/examples/awk/9.awk @@ -0,0 +1,2 @@ +$1 > max { max = $1; line = $0 } +END { print max, line } diff --git a/examples/awk/9.data b/examples/awk/9.data new file mode 100644 index 0000000..85599c3 --- /dev/null +++ b/examples/awk/9.data @@ -0,0 +1,10 @@ +8 Line with 8 +6 Line with 6 +2 Line with 2 +1 Line with 1 +10 Line with 10 +3 Line with 3 +4 Line with 4 +7 Line with 7 +5 Line with 5 +9 Line with 9 diff --git a/examples/awk/9.msh b/examples/awk/9.msh new file mode 100644 index 0000000..a944c63 --- /dev/null +++ b/examples/awk/9.msh @@ -0,0 +1,8 @@ +-99999999 max! "" max-line! +.. +( + dup line! # Store line + wsplit :0: toFloat dup first-item! # Store first item + [(@max >) (@first-item max! @line max-line!)] if +) each +@max str w " " w @max-line wl diff --git a/examples/awk/test.sh b/examples/awk/test.sh index 847e0ef..2828dd9 100755 --- a/examples/awk/test.sh +++ b/examples/awk/test.sh @@ -9,6 +9,15 @@ emp_test() { fi } +data_test() { + if diff <(awk -f "$1".awk "$1".data) <(mshell "$1".msh < "$1".data); then + printf "%s. pass\n" "$1" + else + printf "%s. fail\n" "$1" + FAIL=1 + fi +} + FAIL=0 emp_test 1 @@ -22,17 +31,11 @@ fi emp_test 3 emp_test 4 - - -if diff <(awk -f '5.awk' < 5.data) <(mshell 5.msh < 5.data); then - printf "5. pass\n" -else - printf "5. fail\n" - FAIL=1 -fi - +data_test 5 emp_test 6 emp_test 7 emp_test 8 +data_test 9 +data_test 10 exit "$FAIL" diff --git a/mshell/Evaluator.go b/mshell/Evaluator.go index 2e95871..104c0e3 100644 --- a/mshell/Evaluator.go +++ b/mshell/Evaluator.go @@ -1019,7 +1019,7 @@ MainLoop: } stack.Push(obj2) default: - return FailWithMessage(fmt.Sprintf("%d:%d: Cannot redirect a string to a %s.\n", t.Line, t.Column, obj2.TypeName())) + return FailWithMessage(fmt.Sprintf("%d:%d: Cannot redirect a string (%s) to a %s (%s).\n", t.Line, t.Column, obj1.DebugString(), obj2.TypeName(), obj2.DebugString())) } case *MShellLiteral: switch obj2.(type) { @@ -1038,7 +1038,7 @@ MainLoop: } } default: - return FailWithMessage(fmt.Sprintf("%d:%d: Cannot redirect a %s to a %s.\n", t.Line, t.Column, obj1.TypeName(), obj2.TypeName())) + return FailWithMessage(fmt.Sprintf("%d:%d: Cannot redirect a %s (%s) to a %s (%s).\n", t.Line, t.Column, obj1.TypeName(), obj1.DebugString(), obj2.TypeName(), obj2.DebugString())) } } } else if t.Type == STDERRREDIRECT {