XLISP > XLISP Plus  -  Previous | Next

set-macro-character


Type:   -   defined function (closure)
Source:   -   init.lsp

Syntax

(set-macro-character char-num function [termflag])
char-num - an integer expression
function - a function definition
termflag - an expression, NIL or non-NIL
returns - always returns  T 

Description

The 'set-macro-character' function installs the code that will be executed when the specified character 'char-num' is encountered by the XLISP reader. The 'function' is placed in the *readtable* system variable which contains the reader table array. The table is 128 entries [0..127] for each of the 7-bit ASCII characters that XLISP can read. Each entry in the table must be one of NIL , :constituent , :white-space , :sescape , :mescape , a :tmacro dotted pair or a :nmacro dotted pair.

The 'set-macro-character' function only allows you to put in a terminating read-macro function :tmacro or a non-terminating read-macro-function :nmacro. If the 'termflag' is present and non-NIL , then the 'function' will be put in *readtable* as a :tmacro entry. If 'termflag' is not present or NIL , then 'function' will be put in *readtable* as a :nmacro entry. The 'function' can be a built-in read-macro function or a user defined defun symbol or a lambda expression.

The 'function' takes two parameters, an input stream specification, and an integer that is the character value. The 'function' should return NIL if the character is 'white-space' or a value consed with NIL to return the value. The function 'set-macro-character' always returns  T .

(defun set-macro-character (ch fun &optional tflag)
  (setf (aref *readtable* (char-int ch))
        (cons (if tflag :tmacro :nmacro) fun))
  t)

Note: The 'set-macro-character' function is not included in the standard Nyquist distribution. If you want to use it, copy the code to your 'init.lsp' file.

Examples

(print "hi") % comment                 ; prints "hi"  and gives
                                       ; error: unbound variable - %
                                       ; because % is viewed as a variable

(setq semi (get-macro-character #\;))  ; get semi-colon code

(SET-MACRO-CHARACTER #\% semi T)       ; set % to work as a comment

(print "hi") % comment                 ; prints  "hi" and no error because
                                       ; % is now a comment character in *READTABLE*

Note: In the normal XLISP system the following characters have code associated with them in the *readtable*:

  "  #  '  (  )  ,  ;  `

[double-quote, hash, single quote, opening parenthesis, closing parenthesis, comma, semicolon, backquote.]

Common Lisp: The 'set-macro-character function' is somewhat related to the Common Lisp 'set-dispatch-macro-character' function.

See also the XLISP Plus get-macro-character function.

  Back to Top


XLISP > XLISP Plus  -  Previous | Next