Name

about:lisp-reader

Description

A lisp reader is an invisible wrapper around a TextReader and a combination of a scanner and parser. The scanner recognizes tokens such as parentheses, numbers, strings and other character combinations that have a special meaning.

The parser creates S-expressions from the stream of tokens. Sometimes tokens are converted to multiple tokens.

Comments

The reader handles line comments and block comments

123 ; line comment
123 #! line comment
(+ 123 #| block comment |# 456)

Abbreviated lambda expressions

user 1 > #(+ 2 %)
it: #<Lambda Name="">
user 2 > #(* %3 %3)
it: #<Lambda Name="">
user 3 > (it 1 2 3)
it: 9

Accessor functions

user 1 > .
it: #<Function Name="Kiezel.Runtime.MemberAccessor">
user 2 > (. "to-upper")
it: #<AccessorLambda Name="to-upper" Nullable="False">
user 3 > .to-upper
it: #<AccessorLambda Name="to-upper" Nullable="False">
user 4 > (.to-upper "hello")
it: "HELLO"

Numbers

user 1 > #9r13
it: 12
user 2 > #b111
it: 7
user 3 > #o77
it: 63
user 4 > #x20
it: 32

Strings

user 1 > #q{a b c}
it: "a b c"
user 2 > #q(a b c)
it: "a b c"
user 3 > #q[a b c]
it: "a b c"
user 4 > #q<a b c>
it: "a b c"
user 5 > #q anything really
         een
         twee
         drieanything really
it: "een                          \ntwee                         \ndrie"

Regular expression literals

user 1 > #/a(a)p/i
it: #/a(a)p/i
user 2 > :d
#s( :members #s( :match-timeout -00:00:00.0010000
                 :options IgnoreCase
                 :right-to-left false )
    :type "Kiezel.RegexPlus"
    :value #/a(a)p/i )

Backquote expressions

A backquote expression is a template for a piece of lisp code that must execute to produce output. The names bq:list, bq:force, bq:quote and bq:append are used to clarify the translation of the backquote expression. They behave as their counterparts in the lisp package.

user 1 > (quote `(a b c))
it: (bq:list (bq:quote a) (bq:quote b) (bq:quote c))
user 2 > `(a b c)
it: (a b c)
user 3 > (var b '(1 2 3))
it: (1 2 3)
user 4 > (quote `(a ,b c))
it: (bq:list (bq:quote a) (bq:force b) (bq:quote c))
user 5 > `(a ,b c)
it: (a (1 2 3) c)
user 6 > (quote `(a ,@b c))
it: (bq:append (bq:list (bq:quote a)) (bq:force b) (bq:list (bq:quote c)))
user 7 > `(a ,@b c)
it: (a 1 2 3 c)

Immediate execution expressions

user 1 > '(1 (+ 2 3) 4)
it: (1 (+ 2 3) 4)
user 2 > (setq $read-eval true)
it: true
user 3 > '(1 #.(+ 2 3) 4)
it: (1 5 4)

(Un)conditional inclusion or exclusion of forms

The following examples show how to include or exclude a single form. The first argument after #+ and #- is a logical expression that refers to symbols in the $features list. The second argument is the form to include or exclude.

user 1 > $features
it: (:debug :kiezellisp :kiezellisp-con :linux :mono :repl :unix :x64)
user 2 > '(a #; b c)
it: (a c)
user 3 > '(a #ignore b c)
it: (a c)
user 4 > '(a #+x32 b c)
it: (a c)
user 5 > '(a #-x32 b c)
it: (a b c)
user 6 > '(a #+bla b c)
it: (a c)
user 7 > '(a #-bla b c)
it: (a b c)
user 8 > '(a #+(and x32 (not bla)) b c)
it: (a c)

We can also include/exclude more than one (usually top-level) form:

user 1 > '(#if unix 1 2 #elif windows 3 #else 4 5 #endif)
it: ((compile-time-branch 1 2))

Please note that this feature is form-aware and not line-aware and that the forms in the choosen branch are executed as a single top-level form.

Infix notation

user 1 > '#i(1+x)
it: (+ 1 x)
user 2 > '#i(sin(x)**(1+1)+cos(x)**2)
it: (+ (math:pow (sin x) (+ 1 1)) (math:pow (cos x) 2))

Structures

The #s(...) reader macro produces a prototype object as a literal. When the number of items in the list is odd, the first item must be the type name of a prototype which will become the parent of the constructed prototype.

user 1 > #s(aap 1 noot 2)
it: #s( :aap 1
        :noot 2 )
user 2 > (defstruct mies aap (noot 4) (teun 5))
it: mies
user 3 > #s(mies aap 1 noot 2)
it: #s(mies :aap 1
            :noot 2
            :teun 5 )
user 4 > (type? it 'mies)
it: true
user 5 > #s(mies2 aap 1 noot 2)
Undefined type name: mies2

Read tables

TO DO

See also

$features $read-eval $readtable peek-char read read-all read-all-from-string read-char read-delimited-list read-from-string set-dispatch-macro-character set-macro-character unread-char with-input-from-string