From ee8a7d3ee5ebaf73ac890dd65ee9062c3e48ddca Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 21 Oct 2012 15:54:52 -0400 Subject: [PATCH] added "null" and "null?" --- proc.go | 13 +++++++++++++ skeam.go | 9 ++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/proc.go b/proc.go index aae46ef..8fa110e 100644 --- a/proc.go +++ b/proc.go @@ -119,3 +119,16 @@ func islist(vals []interface{}) (interface{}, error) { _, ok := vals[0].(sexp) return ok, nil } + +func isnull(vals []interface{}) (interface{}, error) { + if err := checkArity(1, vals, "null?"); err != nil { + return nil, err + } + + s, ok := vals[0].(sexp) + if !ok { + return false, nil + } + + return len(s) == 0, nil +} diff --git a/skeam.go b/skeam.go index 33d4fcd..dc65636 100644 --- a/skeam.go +++ b/skeam.go @@ -23,6 +23,7 @@ type symbol string var universe = &environment{map[symbol]interface{}{ "#t": true, "#f": false, + "null": nil, "+": builtin(addition), "-": builtin(subtraction), "*": builtin(multiplication), @@ -31,6 +32,7 @@ var universe = &environment{map[symbol]interface{}{ "list": builtin(list), "list?": builtin(islist), "not": builtin(not), + "null?": builtin(isnull), "begin": special(begin), "define": special(define), "if": special(_if), @@ -109,6 +111,10 @@ func parse(c chan token) (interface{}, error) { } func eval(v interface{}, env *environment) (interface{}, error) { + if v == nil { + return sexp{}, nil + } + switch t := v.(type) { case symbol: @@ -163,7 +169,8 @@ func eval(v interface{}, env *environment) (interface{}, error) { default: return v, nil } - return nil, nil + + panic("not reached") } func evalall(c chan token, env *environment) {