cleaned up some redundancy

master
Jordan Orelli 12 years ago
parent 42c2ffd0d6
commit 926911a7c6

@ -121,7 +121,7 @@ func lexWhitespace(l *lexer) (stateFn, error) {
return nil, err return nil, err
} }
switch r { switch r {
case ' ', '\t', '\n': case ' ', '\t', '\n', '\r':
return lexWhitespace, nil return lexWhitespace, nil
case '"': case '"':
return lexString, nil return lexString, nil
@ -176,7 +176,7 @@ func lexInt(l *lexer) (stateFn, error) {
return nil, err return nil, err
} }
switch r { switch r {
case ' ', '\t', '\n': case ' ', '\t', '\n', '\r':
l.emit(int3ger) l.emit(int3ger)
return lexWhitespace, nil return lexWhitespace, nil
case '.': case '.':
@ -206,7 +206,7 @@ func lexFloat(l *lexer) (stateFn, error) {
} }
switch r { switch r {
case ' ', '\t', '\n': case ' ', '\t', '\n', '\r':
l.emit(fl0at) l.emit(fl0at)
return lexWhitespace, nil return lexWhitespace, nil
case ')': case ')':
@ -232,7 +232,7 @@ func lexSymbol(l *lexer) (stateFn, error) {
} }
switch r { switch r {
case ' ', '\t', '\n': case ' ', '\t', '\n', '\r':
debugPrint("ending lexSymbol on whitespace") debugPrint("ending lexSymbol on whitespace")
l.emit(symbol) l.emit(symbol)
return lexWhitespace, nil return lexWhitespace, nil
@ -259,7 +259,7 @@ func lexCloseParen(l *lexer) (stateFn, error) {
return nil, err return nil, err
} }
switch r { switch r {
case ' ', '\t', '\n': case ' ', '\t', '\n', '\r':
return lexWhitespace, nil return lexWhitespace, nil
case ')': case ')':
return lexCloseParen, nil return lexCloseParen, nil
@ -286,9 +286,9 @@ func lexComment(l *lexer) (stateFn, error) {
// lexes some lispy input from an io.Reader, emiting tokens on chan c. The // lexes some lispy input from an io.Reader, emiting tokens on chan c. The
// channel is closed when the input reaches EOF, signaling that there are no // channel is closed when the input reaches EOF, signaling that there are no
// new tokens. // new tokens.
func lex(input io.Reader, c chan token) { func lex(input io.RuneReader, c chan token) {
defer close(c) defer close(c)
l := &lexer{bufio.NewReader(input), nil, 0, c} l := &lexer{input, nil, 0, c}
var err error var err error
f := stateFn(lexWhitespace) f := stateFn(lexWhitespace)
@ -313,35 +313,15 @@ func args() {
defer f.Close() defer f.Close()
c := make(chan token) c := make(chan token)
go lex(f, c) go lex(bufio.NewReader(f), c)
for s := range c { for s := range c {
fmt.Printf("%11s %s\n", s.t, s.lexeme) fmt.Printf("%11s %s\n", s.t, s.lexeme)
} }
} }
func lexs(input string) { func lexs(input string, c chan token) {
c := make(chan token) lex(strings.NewReader(input), c)
go func() {
defer close(c)
l := &lexer{strings.NewReader(input), nil, 0, c}
var err error
f := stateFn(lexWhitespace)
for err == nil {
f, err = f(l)
}
if err != io.EOF {
fmt.Println(err)
}
if l.depth != 0 {
fmt.Println("error: unbalanced parenthesis")
}
}()
for t := range c {
fmt.Printf("%11s %s\n", t.t, t.lexeme)
}
} }
func main() { func main() {
@ -361,6 +341,11 @@ func main() {
fmt.Println("error: ", err) fmt.Println("error: ", err)
continue continue
} }
lexs(string(line) + "\n")
c := make(chan token)
go lexs(string(line) + "\n", c)
for s := range c {
fmt.Printf("%11s %s\n", s.t, s.lexeme)
}
} }
} }

Loading…
Cancel
Save