From ec5cbdcaab20d57ef7ec7157dd0e8d7b200de311 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 20 Oct 2012 21:24:28 -0400 Subject: [PATCH] added *if* --- input.scm | 8 ++++++++ skeam.go | 4 ++++ special.go | 14 ++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/input.scm b/input.scm index fddaea4..7b09965 100644 --- a/input.scm +++ b/input.scm @@ -11,6 +11,10 @@ ; string "jordan" +; booleans. I don't like the look of #t and #f. They're dumb. +true +false + ; ------------------------------------------------------------------------------ ; basic math ; ------------------------------------------------------------------------------ @@ -28,3 +32,7 @@ x (quote 1 2 3) + +(if 1 2 3) +(if false 2 3) +(if true 2 3) diff --git a/skeam.go b/skeam.go index b3cc39a..1b4ec4b 100644 --- a/skeam.go +++ b/skeam.go @@ -21,12 +21,15 @@ func (s sexp) String() string { type symbol string var universe = &environment{ + "true": true, + "false": false, "+": proc(addition), "-": proc(subtraction), "*": proc(multiplication), "/": proc(division), "define": special(define), "quote": special(quote), + "if": special(_if), } // parses the string lexeme into a value that can be eval'd @@ -178,6 +181,7 @@ func evalall(c chan token, env *environment) { case nil: if v, err := eval(v, env); err != nil { fmt.Println("error:", err) + return } else { fmt.Println(v) } diff --git a/special.go b/special.go index 42e14fc..2458e01 100644 --- a/special.go +++ b/special.go @@ -22,3 +22,17 @@ func define(env *environment, args ...interface{}) (interface{}, error) { func quote(_ *environment, args ...interface{}) (interface{}, error) { return sexp(args), nil } + +func _if(env *environment, args ...interface{}) (interface{}, error) { + if len(args) != 3 { + return nil, fmt.Errorf(`received %d arguments in *if*, expected exactly 3`, len(args)) + } + v, err := eval(args[0], env) + if err != nil { + return nil, err + } + if b, ok := v.(bool); ok && !b { + return eval(args[2], env) + } + return eval(args[1], env) +}