added "null" and "null?"

master
Jordan Orelli 12 years ago
parent 86c288d116
commit ee8a7d3ee5

@ -119,3 +119,16 @@ func islist(vals []interface{}) (interface{}, error) {
_, ok := vals[0].(sexp) _, ok := vals[0].(sexp)
return ok, nil 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
}

@ -23,6 +23,7 @@ type symbol string
var universe = &environment{map[symbol]interface{}{ var universe = &environment{map[symbol]interface{}{
"#t": true, "#t": true,
"#f": false, "#f": false,
"null": nil,
"+": builtin(addition), "+": builtin(addition),
"-": builtin(subtraction), "-": builtin(subtraction),
"*": builtin(multiplication), "*": builtin(multiplication),
@ -31,6 +32,7 @@ var universe = &environment{map[symbol]interface{}{
"list": builtin(list), "list": builtin(list),
"list?": builtin(islist), "list?": builtin(islist),
"not": builtin(not), "not": builtin(not),
"null?": builtin(isnull),
"begin": special(begin), "begin": special(begin),
"define": special(define), "define": special(define),
"if": special(_if), "if": special(_if),
@ -109,6 +111,10 @@ func parse(c chan token) (interface{}, error) {
} }
func eval(v interface{}, env *environment) (interface{}, error) { func eval(v interface{}, env *environment) (interface{}, error) {
if v == nil {
return sexp{}, nil
}
switch t := v.(type) { switch t := v.(type) {
case symbol: case symbol:
@ -163,7 +169,8 @@ func eval(v interface{}, env *environment) (interface{}, error) {
default: default:
return v, nil return v, nil
} }
return nil, nil
panic("not reached")
} }
func evalall(c chan token, env *environment) { func evalall(c chan token, env *environment) {

Loading…
Cancel
Save