From b9d2afce749539b646e9e3f668f751da4e0e39a3 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Tue, 30 Oct 2012 19:00:01 -0400 Subject: [PATCH] fixed quoting behavior previously it was impossible to do the following: (define dave (quote (x y z))) dave the interpreter would incorrectly try to evaluate the (x y z), because there was no quote level being recorded, and it was seen as being the same as a regular sexp. --- proc.go | 2 +- skeam.go | 18 ++++++++++++++++-- special.go | 12 +++++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/proc.go b/proc.go index abef4cd..bd25e8e 100644 --- a/proc.go +++ b/proc.go @@ -107,7 +107,7 @@ func length(vals []interface{}) (interface{}, error) { return len(x), nil } -func list(vals []interface{}) (interface{}, error) { +func lst(vals []interface{}) (interface{}, error) { return sexp(vals), nil } diff --git a/skeam.go b/skeam.go index 0d9267f..3126713 100644 --- a/skeam.go +++ b/skeam.go @@ -8,6 +8,7 @@ import ( "os" "reflect" "strconv" + "strings" ) var DEBUG = false @@ -15,7 +16,20 @@ var DEBUG = false type sexp []interface{} func (s sexp) String() string { - return "(" + fmt.Sprint(s...) + ")" + parts := make([]string, len(s)) + for i, _ := range s { + parts[i] = fmt.Sprint(s[i]) + } + return "(" + strings.Join(parts, " ") + ")" +} + +type list struct { + sexp + quotelevel int +} + +func (l list) String() string { + return l.sexp.String() } type symbol string @@ -39,7 +53,7 @@ var universe = &environment{map[symbol]interface{}{ "car": builtin(car), "cdr": builtin(cdr), "length": builtin(length), - "list": builtin(list), + "list": builtin(lst), "list?": builtin(islist), "not": builtin(not), "null?": builtin(isnull), diff --git a/special.go b/special.go index f88a2a7..eac1898 100644 --- a/special.go +++ b/special.go @@ -75,7 +75,17 @@ func quote(_ *environment, args ...interface{}) (interface{}, error) { return nil, err } - return args[0], nil + switch t := args[0].(type) { + case list: + fmt.Println("got a list...") + t.quotelevel++ + return t, nil + case sexp: + return list{t, 1}, nil + default: + return t, nil + } + panic("not reached") } // turns an arbitrary lisp value into a boolean. Apparently the sematics of