input is now io.Reader

master
Jordan Orelli 12 years ago
parent 79c570c53e
commit 816da3fbbc

@ -1,25 +1,23 @@
package main package main
import ( import (
"bytes" "bufio"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
) )
type stateFn func(*lexer) (stateFn, error) type stateFn func(*lexer) (stateFn, error)
type lexer struct { type lexer struct {
input io.ReadCloser input *bufio.Reader
buf *bytes.Buffer
cur []rune cur []rune
depth int depth int
out chan string out chan string
} }
func (l *lexer) next() (rune, error) { func (l *lexer) next() (rune, error) {
r, _, err := l.buf.ReadRune() r, _, err := l.input.ReadRune()
return r, err return r, err
} }
@ -131,10 +129,10 @@ func lexCloseParen(l *lexer) (stateFn, error) {
return nil, fmt.Errorf("unimplemented") return nil, fmt.Errorf("unimplemented")
} }
func lex(b []byte, c chan string) { func lex(input io.Reader, c chan string) {
defer close(c) defer close(c)
l := &lexer{ l := &lexer{
buf: bytes.NewBuffer(b), input: bufio.NewReader(input),
out: c, out: c,
} }
@ -154,14 +152,14 @@ func lex(b []byte, c chan string) {
func main() { func main() {
filename := "input.lisp" filename := "input.lisp"
b, err := ioutil.ReadFile(filename) f, err := os.Open(filename)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "unable to read file ", filename) fmt.Fprintln(os.Stderr, "unable to read file ", filename)
os.Exit(1) os.Exit(1)
} }
c := make(chan string) c := make(chan string)
go lex(b, c) go lex(f, c)
for s := range c { for s := range c {
fmt.Println(s) fmt.Println(s)

Loading…
Cancel
Save