Turbozen Tiny Lisp Reference Manual
Download Turbozen Tiny Lisp - A simple lisp interpreter for integrating into Objective-C programs.
Xcode 2.4. Apache license.
Other Source Code by David Phillip Oster
Turbozen Tiny Lisp is a very small implementation Lisp designed to interface with Objective C.
control structure:
(progn {expr1} .. {exprn}) -- evaluates in order, returns value of {exprn}
(prog0 {expr1} .. {exprn}) -- evaluates in order, returns value of {expr0}
(if {expr0} {expr1}) -- if expr0 evaluates non nil, evaluate expr1 and return it, else nil.
(if {expr0} {expr1} {expr2}) -- if expr0 evaluates non nil, evaluate expr1 and return it, else value of {expr2}
(while {expr0} {expr1} .. {exprn}) -- while expr0 evaluates non nil, evaluate expr1 thruough exprn and return value of last {exprn}
(and {expr0} {expr1} .. {exprn}) -- evaluates in order, stops at first nil. returns value of {exprn}, if it gets that far.
(or {expr0} {expr1} .. {exprn}) -- evaluates in order, stops at first non-nil. returns value of {exprn}, if it gets that far.
i/o:
(print {expr1} .. {exprn}) -- prints each to standard output.
special forms:
'a is expanded by the reader to (' a), which evaluates to a. ' is the quote function.
(λ ({formal1} ... {formaln}) expr1} .. {exprn}) -- a user defined function. When applied, binds arguments to formals, evaluates expressions.
example:
(λ (x) (+ x x))
A function application is a lambda list, followed by arguments
example:
((λ (x y) (* (+ x x) y)) 5 7)
=> 70
comparisons:
(= a 70) -- any type. currently, the rest are defined for numbers.
(< x 1) --
(> x 3)
(<= x 1)
(>= x 2)
note: longer lists are allowed. (< 1 2 3) is true (read it in infix notation as 1 < 2 && 2 < 3)
This holds true for all the comparisons.
math:
(+ 1 1)
(- 1 1)
(* 1 1)
(/ 1 1)
note: longer lists are allowed for + or *. lists length 1 are allowed for (- 12) => 12 and (/ 4) => 0.25
atoms:
An atom evaluates to itself initially.
(set 'a 12) assigns 12 as the value of a.
As above, atoms also have properties:
(setPropertyForKey 'a "hello" "world")
"hello"
(propertyForKey 'a "world")
"hello"
(propertyKeys 'a)
("world")
(removePropertyForKey 'a "world")
(propertyKeys 'a)
()
lists and strings:
(list {expr1} .. {exprn}) -- returns the list by evaluating its arguments.
(count "abc") => 3 -- number of elements in the list or string.
(at "0123456" 2) => 50 -- returns the unicode char as a number at the zero-based index in the string.
(insert a '(a b c) 2) -- insert into the list or string, the item at the second argument at the index, third argument.
for strings, you can only insert numbers=characters, or strings.
(remove B 0) -- remove an item from the list or a character from the string at the zero-based index
(replace a 2 '(1 2 3)) -- replace an item at zero-based index with the new item.
for strings, you can only insert numbers=characters, or strings.
Access to Cocoa:
(class "NSColor") -- returns the Cocoa class
(perform {target} {selectorString} {expr1} .. {exprn}) - make an Obj-C call.
examples:
(perform (perform (perform (class "NSNumber") "alloc") "initWithInt:" 1000) "autorelease")
(perform (class "NSNumber") "numberWithInt:" 1000)
Currently, only objects and ints are supported. I need to beef up (perform...) to handle other argument types.
TurboZen TinyList Home.
Page last modified September 13, 2007