Skip to content

Commit

Permalink
Add foldl/sum, example 7 from awk
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchpaulus committed Dec 5, 2024
1 parent e617c62 commit 0d21336
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ wt :-1: :-1: wl
.. (wsplit :-1: toFloat 4 >) filter (wl) each

# 7. Print the total number of fields in all input lines
# { nf = nf + $NF }
# { nf = nf + NF }
# END { print nf }
.. (wsplit len) map sum wl

Expand Down
2 changes: 2 additions & 0 deletions examples/awk/7.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{ nf = nf + NF }
END { print nf }
1 change: 1 addition & 0 deletions examples/awk/7.msh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. (wsplit len) map sum wl
1 change: 1 addition & 0 deletions examples/awk/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ else
fi

emp_test 6
emp_test 7

exit "$FAIL"
20 changes: 20 additions & 0 deletions lib/std.msh
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ def filter
drop drop @filter-accum
end

# foldl (quote initial list -- result)
def foldl
# quote initial list
swap foldl-accum! # Accumulator,
# quote list
swap foldl-quote! # Quote
# list
(
@foldl-accum swap # Accumulator item
@foldl-quote x # Execute quote
foldl-accum! # Update accumulator
) each

@foldl-accum
end

# sum (list -- value)
def sum
(+) 0 rot foldl
end

# .. (-- list[str]), equals kinda looks like lines.
def .. stdin lines end
Expand Down
2 changes: 1 addition & 1 deletion mshell/Lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (t Token) ToJson() string {
}

func (t Token) DebugString() string {
return fmt.Sprintf("%d:%d: %s %s", t.Line, t.Column, t.Type, t.Lexeme)
return fmt.Sprintf("'%s'", t.Lexeme)
}

type Lexer struct {
Expand Down
1 change: 1 addition & 0 deletions tests/sum.msh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1 2 3 4] sum wl
1 change: 1 addition & 0 deletions tests/sum.msh.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10

0 comments on commit 0d21336

Please sign in to comment.