chiark
/
gitweb
/
~mdw
/
fringe
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
aae6c1d
)
go-fringe.go: Remove all of the `;' statement terminators.
master
author
Mark Wooding
<mdw@distorted.org.uk>
Sat, 6 Apr 2013 17:22:11 +0000
(18:22 +0100)
committer
Mark Wooding
<mdw@distorted.org.uk>
Sat, 6 Apr 2013 17:22:11 +0000
(18:22 +0100)
go-fringe.go
patch
|
blob
|
blame
|
history
diff --git
a/go-fringe.go
b/go-fringe.go
index d79760ede6e71dde7e399d9369b0b68399ccdaf0..44aa438074b010887c708de0fdf4d95aa7baceea 100644
(file)
--- a/
go-fringe.go
+++ b/
go-fringe.go
@@
-6,19
+6,19
@@
package main
import (
package main
import (
- "fmt"
;
- "io"
;
- "os"
;
+ "fmt"
+ "io"
+ "os"
)
///--------------------------------------------------------------------------
/// Utilities.
)
///--------------------------------------------------------------------------
/// Utilities.
-var prog = os.Args[0]
;
+var prog = os.Args[0]
func bail(msg string) {
func bail(msg string) {
- fmt.Fprintf(os.Stderr, "%s: %s\n", prog, msg)
;
- os.Exit(1)
;
+ fmt.Fprintf(os.Stderr, "%s: %s\n", prog, msg)
+ os.Exit(1)
}
///--------------------------------------------------------------------------
}
///--------------------------------------------------------------------------
@@
-33,39
+33,39
@@
type any interface {}
type Iterable interface {
// Simply put the items to the channel CH one at a time.
type Iterable interface {
// Simply put the items to the channel CH one at a time.
- Iterate(ch chan<- any)
;
+ Iterate(ch chan<- any)
}
// An iterator.
}
// An iterator.
-type Iterator <-chan any
;
+type Iterator <-chan any
// Returns an iterator for a given iterable thing.
func MakeIterator(it Iterable) Iterator {
// Returns an iterator for a given iterable thing.
func MakeIterator(it Iterable) Iterator {
- ch := make(chan any)
;
+ ch := make(chan any)
go func () {
go func () {
- it.Iterate(ch)
;
- close(ch)
;
- } ()
;
- return ch
;
+ it.Iterate(ch)
+ close(ch)
+ } ()
+ return ch
}
// Returns the next item from an iterator IT. If there is indeed an item
// available, return it and true; otherwise return nil and false.
func (it Iterator) Next() (any, bool) {
}
// Returns the next item from an iterator IT. If there is indeed an item
// available, return it and true; otherwise return nil and false.
func (it Iterator) Next() (any, bool) {
- item, anyp := <-it
;
- return item, anyp
;
+ item, anyp := <-it
+ return item, anyp
}
// Answer whether the iterators return the same items in the same order.
// Equality is determined using `=='. Maybe we'll do better some day.
func SameIterators(ia, ib Iterator) bool {
for {
}
// Answer whether the iterators return the same items in the same order.
// Equality is determined using `=='. Maybe we'll do better some day.
func SameIterators(ia, ib Iterator) bool {
for {
- a, any_a := ia.Next()
;
- b, any_b := ib.Next()
;
+ a, any_a := ia.Next()
+ b, any_b := ib.Next()
if !any_a { return !any_b }
if a != b { break }
}
if !any_a { return !any_b }
if a != b { break }
}
- return false
;
+ return false
}
///--------------------------------------------------------------------------
}
///--------------------------------------------------------------------------
@@
-73,17
+73,17
@@
func SameIterators(ia, ib Iterator) bool {
// Just a simple binary tree.
type Node struct {
// Just a simple binary tree.
type Node struct {
- data byte
;
- left, right *Node
;
+ data byte
+ left, right *Node
}
// Iteration is easy. Note that recursion is fine here: this is just a
// simple function.
func (n *Node) Iterate(ch chan<- any) {
if n == nil { return }
}
// Iteration is easy. Note that recursion is fine here: this is just a
// simple function.
func (n *Node) Iterate(ch chan<- any) {
if n == nil { return }
- n.left.Iterate(ch)
;
- ch <- n.data
;
- n.right.Iterate(ch)
;
+ n.left.Iterate(ch)
+ ch <- n.data
+ n.right.Iterate(ch)
}
// Parse a tree from a textual description. The syntax is simple:
}
// Parse a tree from a textual description. The syntax is simple:
@@
-93,23
+93,23
@@
func (n *Node) Iterate(ch chan<- any) {
// where the ambiguity is resolved by declaring that a `(' is a tree if we're
// expecting a tree.
func ParseTree(s string) *Node {
// where the ambiguity is resolved by declaring that a `(' is a tree if we're
// expecting a tree.
func ParseTree(s string) *Node {
- var parse func(int) (*Node, int)
;
- n := len(s)
;
- i := 0
;
+ var parse func(int) (*Node, int)
+ n := len(s)
+ i := 0
parse = func(i int) (*Node, int) {
if i < n && s[i] == '(' {
parse = func(i int) (*Node, int) {
if i < n && s[i] == '(' {
- left, i := parse(i + 1)
;
+ left, i := parse(i + 1)
if i >= n { bail("no data") }
if i >= n { bail("no data") }
- data := s[i]
;
- right, i := parse(i + 1)
;
+ data := s[i]
+ right, i := parse(i + 1)
if i >= n || s[i] != ')' { bail("missing )") }
if i >= n || s[i] != ')' { bail("missing )") }
- return &Node { data, left, right }, i + 1
;
+ return &Node { data, left, right }, i + 1
}
}
- return nil, i
;
- }
;
- t, i := parse(0)
;
+ return nil, i
+ }
+ t, i := parse(0)
if i < n { bail("trailing junk") }
if i < n { bail("trailing junk") }
- return t
;
+ return t
}
///--------------------------------------------------------------------------
}
///--------------------------------------------------------------------------
@@
-118,24
+118,24
@@
func ParseTree(s string) *Node {
func main() {
switch len(os.Args) {
case 2:
func main() {
switch len(os.Args) {
case 2:
- t := ParseTree(os.Args[1])
;
- i := MakeIterator(t)
;
+ t := ParseTree(os.Args[1])
+ i := MakeIterator(t)
for {
for {
- item, winp := i.Next()
;
+ item, winp := i.Next()
if !winp { break }
if !winp { break }
- fmt.Printf("%c", item)
;
+ fmt.Printf("%c", item)
}
}
- fmt.Printf("\n")
;
+ fmt.Printf("\n")
case 3:
case 3:
- ia := MakeIterator(ParseTree(os.Args[1]))
;
- ib := MakeIterator(ParseTree(os.Args[2]))
;
+ ia := MakeIterator(ParseTree(os.Args[1]))
+ ib := MakeIterator(ParseTree(os.Args[2]))
if SameIterators(ia, ib) {
if SameIterators(ia, ib) {
- io.WriteString(os.Stdout, "match\n")
;
+ io.WriteString(os.Stdout, "match\n")
} else {
} else {
- io.WriteString(os.Stdout, "no match\n")
;
+ io.WriteString(os.Stdout, "no match\n")
}
default:
}
default:
- bail("bad args")
;
+ bail("bad args")
}
}
}
}