You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
5.1 KiB
Go

12 years ago
package main
import (
"bufio"
12 years ago
"errors"
12 years ago
"fmt"
"io"
"os"
"reflect"
12 years ago
"strconv"
"strings"
12 years ago
)
var DEBUG = false
12 years ago
type sexp struct {
items []interface{}
quotelvl int
}
func newSexp() *sexp {
return &sexp{
items: make([]interface{}, 0, 8),
quotelvl: 0,
}
}
12 years ago
12 years ago
func (s sexp) String() string {
12 years ago
parts := make([]string, len(s.items))
for i, _ := range s.items {
parts[i] = fmt.Sprint(s.items[i])
}
return "(" + strings.Join(parts, " ") + ")"
}
12 years ago
func (s *sexp) append(item interface{}) {
s.items = append(s.items, item)
}
12 years ago
func (s sexp) len() int {
return len(s.items)
12 years ago
}
12 years ago
type symbol string
var universe = &environment{map[symbol]interface{}{
12 years ago
// predefined values
"#t": true,
"#f": false,
"null": nil,
// builtin functions
symbol(add.name): add,
symbol(sub.name): sub,
symbol(mul.name): mul,
symbol(div.name): div,
symbol(gt.name): gt,
symbol(gte.name): gte,
symbol(lt.name): lt,
symbol(lte.name): lte,
symbol(cons.name): cons,
symbol(car.name): car,
symbol(cdr.name): cdr,
symbol(length.name): length,
symbol(lst.name): lst,
symbol(islist.name): islist,
symbol(not.name): not,
symbol(isnull.name): isnull,
symbol(issymbol.name): issymbol,
// "=": builtin(equal),
// "equal?": builtin(equal),
// "eq?"
// "append"
12 years ago
// special forms
"begin": special(begin),
"define": special(define),
12 years ago
"if": special(_if),
"lambda": special(mklambda),
"quote": special(quote),
"set!": special(set),
}, nil}
12 years ago
// parses the string lexeme into a value that can be eval'd
func atom(t token) (interface{}, error) {
12 years ago
switch t.t {
case integerToken:
val, err := strconv.ParseInt(t.lexeme, 10, 64)
if err != nil {
return nil, err
}
return val, nil
case floatToken:
val, err := strconv.ParseFloat(t.lexeme, 64)
if err != nil {
return nil, err
}
return val, nil
case stringToken:
return t.lexeme, nil
case symbolToken:
return symbol(t.lexeme), nil
}
return nil, fmt.Errorf("unable to atomize token: %v", t)
12 years ago
}
// reads in tokens on the channel until a matching close paren is found.
func (s *sexp) readIn(c chan token) error {
12 years ago
for t := range c {
switch t.t {
case closeParenToken:
return nil
case openParenToken:
12 years ago
child := newSexp()
12 years ago
if err := child.readIn(c); err != nil {
return err
}
12 years ago
s.append(child)
12 years ago
default:
v, err := atom(t)
if err != nil {
return err
}
12 years ago
s.append(v)
12 years ago
}
}
return errors.New("unexpected EOF in sexp.readIn")
12 years ago
}
// parses one value that can be evaled from the channel
func parse(c chan token) (interface{}, error) {
12 years ago
for t := range c {
switch t.t {
case closeParenToken:
return nil, errors.New("unexpected EOF in read")
case openParenToken:
12 years ago
s := newSexp()
12 years ago
if err := s.readIn(c); err != nil {
return nil, err
}
return s, nil
default:
return atom(t)
}
}
return nil, io.EOF
12 years ago
}
func eval(v interface{}, env *environment) (interface{}, error) {
if v == nil {
12 years ago
return &sexp{}, nil
}
switch t := v.(type) {
case symbol:
debugPrint("eval symbol")
s, err := env.get(t)
if err != nil {
return nil, err
}
return eval(s, env)
12 years ago
case *sexp:
debugPrint("eval sexp")
12 years ago
if t.len() == 0 {
return nil, errors.New("illegal evaluation of empty sexp ()")
}
12 years ago
if t.quotelvl > 0 {
return t, nil
}
// eval the first item
12 years ago
v, err := eval(t.items[0], env)
if err != nil {
return nil, err
}
// check to see if this is a special form
if spec, ok := v.(special); ok {
12 years ago
debugPrint("special!")
if len(t.items) > 1 {
return spec(env, t.items[1:]...)
} else {
return spec(env)
}
}
// exec builtin func if one exists
if b, ok := v.(builtin); ok {
12 years ago
if len(t.items) > 1 {
return b.call(env, t.items[1:])
} else {
return b.call(env, nil)
}
}
// exec lambda if possible
if l, ok := v.(lambda); ok {
12 years ago
if len(t.items) > 1 {
return l.call(env, t.items[1:])
} else {
return l.call(env, nil)
}
}
return nil, fmt.Errorf(`expected special form or builtin procedure, received %v`, reflect.TypeOf(v))
default:
12 years ago
debugPrint("default eval")
return v, nil
}
panic("not reached")
12 years ago
}
func evalall(c chan token, env *environment) {
12 years ago
for {
v, err := parse(c)
switch err {
case io.EOF:
return
case nil:
if v, err := eval(v, env); err != nil {
fmt.Println("error:", err)
12 years ago
return
} else {
12 years ago
if v != nil {
fmt.Println(v)
}
}
12 years ago
default:
fmt.Printf("error in eval: %v\n", err)
12 years ago
}
}
12 years ago
}
func args() {
filename := os.Args[1]
f, err := os.Open(filename)
12 years ago
if err != nil {
fmt.Fprintln(os.Stderr, "unable to read file ", filename)
os.Exit(1)
}
defer f.Close()
c := make(chan token, 32)
go lex(bufio.NewReader(f), c)
evalall(c, universe)
}
func main() {
if DEBUG {
fmt.Println(universe)
}
if len(os.Args) > 1 {
args()
return
}
r := bufio.NewReader(os.Stdin)
for {
fmt.Print("> ")
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, universe)
12 years ago
}
}