|
|
@ -3,6 +3,7 @@ package main
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
@ -140,22 +141,23 @@ func (s *sexp) readIn(c chan token) error {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// parses one value that can be evaled from the channel
|
|
|
|
// parses one value that can be evaled from the channel
|
|
|
|
func parse(c chan token) (interface{}, error) {
|
|
|
|
func parse(c chan token) (interface{}, bool, error) {
|
|
|
|
for t := range c {
|
|
|
|
for t := range c {
|
|
|
|
switch t.t {
|
|
|
|
switch t.t {
|
|
|
|
case closeParenToken:
|
|
|
|
case closeParenToken:
|
|
|
|
return nil, errors.New("unexpected EOF in read")
|
|
|
|
return nil, false, errors.New("unexpected EOF in read")
|
|
|
|
case openParenToken:
|
|
|
|
case openParenToken:
|
|
|
|
s := newSexp()
|
|
|
|
s := newSexp()
|
|
|
|
if err := s.readIn(c); err != nil {
|
|
|
|
if err := s.readIn(c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
return s, false, nil
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
return atom(t)
|
|
|
|
v, err := atom(t)
|
|
|
|
|
|
|
|
return v, false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, io.EOF
|
|
|
|
return nil, false, io.EOF
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func eval(v interface{}, env *environment) (interface{}, error) {
|
|
|
|
func eval(v interface{}, env *environment) (interface{}, error) {
|
|
|
@ -208,7 +210,7 @@ func eval(v interface{}, env *environment) (interface{}, error) {
|
|
|
|
|
|
|
|
|
|
|
|
func evalall(c chan token, out chan interface{}, e chan error, env *environment) {
|
|
|
|
func evalall(c chan token, out chan interface{}, e chan error, env *environment) {
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
v, err := parse(c)
|
|
|
|
v, _, err := parse(c)
|
|
|
|
switch err {
|
|
|
|
switch err {
|
|
|
|
case io.EOF:
|
|
|
|
case io.EOF:
|
|
|
|
return
|
|
|
|
return
|
|
|
@ -237,37 +239,23 @@ func defaultInterpreter(out chan interface{}, errors chan error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
if DEBUG {
|
|
|
|
if DEBUG {
|
|
|
|
fmt.Println(universe)
|
|
|
|
fmt.Println(universe)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(os.Args) > 1 {
|
|
|
|
if *tcpAddr != "" {
|
|
|
|
args()
|
|
|
|
runTCPServer()
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(flag.Args()) > 0 {
|
|
|
|
|
|
|
|
runfile()
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
out, errors := make(chan interface{}), make(chan error)
|
|
|
|
out, errors := make(chan interface{}), make(chan error)
|
|
|
|
go defaultInterpreter(out, errors)
|
|
|
|
go defaultInterpreter(out, errors)
|
|
|
|
|
|
|
|
|
|
|
|
r := bufio.NewReader(os.Stdin)
|
|
|
|
c := make(chan token, 32)
|
|
|
|
for {
|
|
|
|
go lex(bufio.NewReader(os.Stdin), c)
|
|
|
|
fmt.Print("> ")
|
|
|
|
evalall(c, out, errors, universe)
|
|
|
|
line, prefix, err := r.ReadLine()
|
|
|
|
|
|
|
|
if prefix {
|
|
|
|
|
|
|
|
fmt.Println("(prefix)")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
switch err {
|
|
|
|
|
|
|
|
case nil:
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
case io.EOF:
|
|
|
|
|
|
|
|
fmt.Print("\n")
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
fmt.Println("error: ", err)
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c := make(chan token, 32)
|
|
|
|
|
|
|
|
go lexs(string(line)+"\n", c)
|
|
|
|
|
|
|
|
evalall(c, out, errors, universe)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|