Skip to content

Commit

Permalink
Add awk 9 and 10
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchpaulus committed Dec 6, 2024
1 parent fa23918 commit f873c9b
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 24 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions examples/awk.msh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env mshell-go
#!/usr/bin/env mshell

# 1. Print the total nubmer of input lines:
# END { print NR }
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions examples/awk/10.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NF > 0
5 changes: 5 additions & 0 deletions examples/awk/10.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1 2

3 4

5 6
1 change: 1 addition & 0 deletions examples/awk/10.msh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. (wsplit len 0 >) filter (wl) each
2 changes: 2 additions & 0 deletions examples/awk/9.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$1 > max { max = $1; line = $0 }
END { print max, line }
10 changes: 10 additions & 0 deletions examples/awk/9.data
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions examples/awk/9.msh
Original file line number Diff line number Diff line change
@@ -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
21 changes: 12 additions & 9 deletions examples/awk/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
4 changes: 2 additions & 2 deletions mshell/Evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down

0 comments on commit f873c9b

Please sign in to comment.