|
|
1.1 ! root 1: ! 2: ! 3: ! 4: ! 5: ! 6: ! 7: ! 8: CHAPTER 7 ! 9: ! 10: ! 11: The Lisp Reader ! 12: ! 13: ! 14: ! 15: ! 16: ! 17: ! 18: 7.1. Introduction ! 19: ! 20: The _r_e_a_d function is responsible for converting a ! 21: stream of characters into a Lisp expression. _R_e_a_d is ! 22: table driven and the table it uses is called a _r_e_a_d_t_- ! 23: _a_b_l_e. The _p_r_i_n_t function does the inverse of _r_e_a_d; it ! 24: converts a Lisp expression into a stream of charac- ! 25: ters. Typically the conversion is done in such a way ! 26: that if that stream of characters were read by _r_e_a_d, ! 27: the result would be an expression equal to the one ! 28: _p_r_i_n_t was given. _P_r_i_n_t must also refer to the readt- ! 29: able in order to determine how to format its output. ! 30: The _e_x_p_l_o_d_e function, which returns a list of charac- ! 31: ters rather than printing them, must also refer to ! 32: the readtable. ! 33: ! 34: A readtable is created with the _m_a_k_e_r_e_a_d_t_a_b_l_e ! 35: function, modified with the _s_e_t_s_y_n_t_a_x function and ! 36: interrogated with the _g_e_t_s_y_n_t_a_x function. The struc- ! 37: ture of a readtable is hidden from the user - a ! 38: readtable should only be manipulated with the three ! 39: functions mentioned above. ! 40: ! 41: There is one distinguished readtable called the ! 42: _c_u_r_r_e_n_t _r_e_a_d_t_a_b_l_e whose value determines what _r_e_a_d, ! 43: _p_r_i_n_t and _e_x_p_l_o_d_e do. The current readtable is the ! 44: value of the symbol _r_e_a_d_t_a_b_l_e. Thus it is possible ! 45: to rapidly change the current syntax by lambda binding ! 46: a different readtable to the symbol _r_e_a_d_t_a_b_l_e. When ! 47: the binding is undone, the syntax reverts to its old ! 48: form. ! 49: ! 50: ! 51: ! 52: 7.2. Syntax Classes ! 53: ! 54: The readtable describes how each of the 128 ascii ! 55: characters should be treated by the reader and ! 56: printer. Each character belongs to a _s_y_n_t_a_x _c_l_a_s_s ! 57: which has three properties: ! 58: ! 59: character class - ! 60: Tells what the reader should do when it sees this ! 61: ! 62: ! 63: The Lisp Reader 7-1 ! 64: ! 65: ! 66: ! 67: ! 68: ! 69: ! 70: ! 71: The Lisp Reader 7-2 ! 72: ! 73: ! 74: character. There are a large number of character ! 75: classes. They are described below. ! 76: ! 77: separator - ! 78: Most types of tokens the reader constructs are ! 79: one character long. Four token types have an ! 80: arbitrary length: number (1234), symbol print ! 81: name (franz), escaped symbol print name ! 82: (|franz|), and string ("franz"). The reader can ! 83: easily determine when it has come to the end of ! 84: one of the last two types: it just looks for the ! 85: matching delimiter (| or "). When the reader is ! 86: reading a number or symbol print name, it stops ! 87: reading when it comes to a character with the ! 88: _s_e_p_a_r_a_t_o_r property. The separator character is ! 89: pushed back into the input stream and will be the ! 90: first character read when the reader is called ! 91: again. ! 92: ! 93: escape - ! 94: Tells the printer when to put escapes in front ! 95: of, or around, a symbol whose print name contains ! 96: this character. There are three possibilities: ! 97: always escape a symbol with this character in it, ! 98: only escape a symbol if this is the only charac- ! 99: ter in the symbol, and only escape a symbol if ! 100: this is the first character in the symbol. ! 101: [note: The printer will always escape a symbol ! 102: which, if printed out, would look like a valid ! 103: number.] ! 104: ! 105: When the Lisp system is built, Lisp code is added ! 106: to a C-coded kernel and the result becomes the stan- ! 107: dard lisp system. The readtable present in the C- ! 108: coded kernel, called the _r_a_w _r_e_a_d_t_a_b_l_e, contains the ! 109: bare necessities for reading in Lisp code. During the ! 110: construction of the complete Lisp system, a copy is ! 111: made of the raw readtable and then the copy is modi- ! 112: fied by adding macro characters. The result is what ! 113: is called the _s_t_a_n_d_a_r_d _r_e_a_d_t_a_b_l_e. When a new readt- ! 114: able is created with _m_a_k_e_r_e_a_d_t_a_b_l_e, a copy is made of ! 115: either the raw readtable or the current readtable ! 116: (which is likely to be the standard readtable). ! 117: ! 118: ! 119: ! 120: 7.3. Reader operations ! 121: ! 122: The reader has a very simple algorithm. It is ! 123: either _s_c_a_n_n_i_n_g for a token, _c_o_l_l_e_c_t_i_n_g a token, or ! 124: _p_r_o_c_e_s_s_i_n_g a token. Scanning involves reading charac- ! 125: ters and throwing away those which don't start tokens ! 126: (such as blanks and tabs). Collecting means gathering ! 127: ! 128: ! 129: Printed: July 21, 1983 ! 130: ! 131: ! 132: ! 133: ! 134: ! 135: ! 136: ! 137: The Lisp Reader 7-3 ! 138: ! 139: ! 140: the characters which make up a token into a buffer. ! 141: Processing may involve creating symbols, strings, ! 142: lists, fixnums, bignums or flonums or calling a user ! 143: written function called a character macro. ! 144: ! 145: The components of the syntax class determine when ! 146: the reader switches between the scanning, collecting ! 147: and processing states. The reader will continue scan- ! 148: ning as long as the character class of the characters ! 149: it reads is _c_s_e_p_a_r_a_t_o_r. When it reads a character ! 150: whose character class is not _c_s_e_p_a_r_a_t_o_r it stores that ! 151: character in its buffer and begins the collecting ! 152: phase. ! 153: ! 154: If the character class of that first character is ! 155: _c_c_h_a_r_a_c_t_e_r, _c_n_u_m_b_e_r, _c_p_e_r_i_o_d, or _c_s_i_g_n. then it will ! 156: continue collecting until it runs into a character ! 157: whose syntax class has the _s_e_p_a_r_a_t_o_r property. (That ! 158: last character will be pushed back into the input ! 159: buffer and will be the first character read next ! 160: time.) Now the reader goes into the processing phase, ! 161: checking to see if the token it read is a number or ! 162: symbol. It is important to note that after the first ! 163: character is collected the component of the syntax ! 164: class which tells the reader to stop collecting is ! 165: the _s_e_p_a_r_a_t_o_r property, not the character class. ! 166: ! 167: If the character class of the character which ! 168: stopped the scanning is not _c_c_h_a_r_a_c_t_e_r, _c_n_u_m_b_e_r, ! 169: _c_p_e_r_i_o_d, or _c_s_i_g_n. then the reader processes that ! 170: character immediately. The character classes ! 171: _c_s_i_n_g_l_e-_m_a_c_r_o, _c_s_i_n_g_l_e-_s_p_l_i_c_i_n_g-_m_a_c_r_o, and _c_s_i_n_g_l_e- ! 172: _i_n_f_i_x-_m_a_c_r_o will act like _c_c_h_a_r_a_c_t_e_r if the following ! 173: token is not a _s_e_p_a_r_a_t_o_r. The processing which is ! 174: done for a given character class is described in ! 175: detail in the next section. ! 176: ! 177: ! 178: ! 179: 7.4. Character classes ! 180: ! 181: ! 182: _c_c_h_a_r_a_c_t_e_r raw readtable:A-Z a-z ^H !#$%&*,/:;<=>?@^_`{}~ ! 183: standard readtable:A-Z a-z ^H !$%&*/:;<=>?@^_{}~ ! 184: A normal character. ! 185: ! 186: ! 187: _c_n_u_m_b_e_r raw readtable:0-9 ! 188: standard readtable:0-9 ! 189: This type is a digit. The syntax for an integer (fix- ! 190: num or bignum) is a string of _c_n_u_m_b_e_r characters ! 191: optionally followed by a _c_p_e_r_i_o_d. If the digits are ! 192: not followed by a _c_p_e_r_i_o_d, then they are interpreted ! 193: ! 194: ! 195: Printed: July 21, 1983 ! 196: ! 197: ! 198: ! 199: ! 200: ! 201: ! 202: ! 203: The Lisp Reader 7-4 ! 204: ! 205: ! 206: in base _i_b_a_s_e which must be eight or ten. The syntax ! 207: for a floating point number is either zero or more ! 208: _c_n_u_m_b_e_r's followed by a _c_p_e_r_i_o_d and then followed by ! 209: one or more _c_n_u_m_b_e_r's. A floating point number may ! 210: also be an integer or floating point number followed ! 211: by 'e' or 'd', an optional '+' or '-' and then zero or ! 212: more _c_n_u_m_b_e_r's. ! 213: ! 214: ! 215: _c_s_i_g_n raw readtable:+- ! 216: standard readtable:+- ! 217: A leading sign for a number. No other characters ! 218: should be given this class. ! 219: ! 220: ! 221: _c_l_e_f_t_-_p_a_r_e_n raw readtable:( ! 222: standard readtable:( ! 223: A left parenthesis. Tells the reader to begin forming ! 224: a list. ! 225: ! 226: ! 227: _c_r_i_g_h_t_-_p_a_r_e_n raw readtable:) ! 228: standard readtable:) ! 229: A right parenthesis. Tells the reader that it has ! 230: reached the end of a list. ! 231: ! 232: ! 233: _c_l_e_f_t_-_b_r_a_c_k_e_t raw readtable:[ ! 234: standard readtable:[ ! 235: A left bracket. Tells the reader that it should begin ! 236: forming a list. See the description of _c_r_i_g_h_t-_b_r_a_c_k_e_t ! 237: for the difference between cleft-bracket and cleft- ! 238: paren. ! 239: ! 240: ! 241: _c_r_i_g_h_t_-_b_r_a_c_k_e_t raw readtable:] ! 242: standard readtable:] ! 243: A right bracket. A _c_r_i_g_h_t-_b_r_a_c_k_e_t finishes the forma- ! 244: tion of the current list and all enclosing lists until ! 245: it finds one which begins with a _c_l_e_f_t-_b_r_a_c_k_e_t or ! 246: until it reaches the top level list. ! 247: ! 248: ! 249: _c_p_e_r_i_o_d raw readtable:. ! 250: standard readtable:. ! 251: The period is used to separate element of a cons cell ! 252: [e.g. (a . (b . nil)) is the same as (a b)]. _c_p_e_r_i_o_d ! 253: is also used in numbers as described above. ! 254: ! 255: ! 256: _c_s_e_p_a_r_a_t_o_r raw readtable:^I-^M esc space ! 257: standard readtable:^I-^M esc space ! 258: Separates tokens. When the reader is scanning, these ! 259: ! 260: ! 261: Printed: July 21, 1983 ! 262: ! 263: ! 264: ! 265: ! 266: ! 267: ! 268: ! 269: The Lisp Reader 7-5 ! 270: ! 271: ! 272: character are passed over. Note: there is a differ- ! 273: ence between the _c_s_e_p_a_r_a_t_o_r character class and the ! 274: _s_e_p_a_r_a_t_o_r property of a syntax class. ! 275: ! 276: ! 277: _c_s_i_n_g_l_e_-_q_u_o_t_e raw readtable:' ! 278: standard readtable:' ! 279: This causes _r_e_a_d to be called recursively and the list ! 280: (quote <value read>) to be returned. ! 281: ! 282: ! 283: _c_s_y_m_b_o_l_-_d_e_l_i_m_i_t_e_r raw readtable:| ! 284: standard readtable:| ! 285: This causes the reader to begin collecting characters ! 286: and to stop only when another identical _c_s_y_m_b_o_l- ! 287: _d_e_l_i_m_i_t_e_r is seen. The only way to escape a _c_s_y_m_b_o_l- ! 288: _d_e_l_i_m_i_t_e_r within a symbol name is with a _c_e_s_c_a_p_e char- ! 289: acter. The collected characters are converted into a ! 290: string which becomes the print name of a symbol. If a ! 291: symbol with an identical print name already exists, ! 292: then the allocation is not done, rather the existing ! 293: symbol is used. ! 294: ! 295: ! 296: _c_e_s_c_a_p_e raw readtable:\ ! 297: standard readtable:\ ! 298: This causes the next character to read in to be ! 299: treated as a vcharacter. A character whose syntax ! 300: class is vcharacter has a character class _c_c_h_a_r_a_c_t_e_r ! 301: and does not have the _s_e_p_a_r_a_t_o_r property so it will ! 302: not separate symbols. ! 303: ! 304: ! 305: _c_s_t_r_i_n_g_-_d_e_l_i_m_i_t_e_r raw readtable:" ! 306: standard readtable:" ! 307: This is the same as _c_s_y_m_b_o_l-_d_e_l_i_m_i_t_e_r except the ! 308: result is returned as a string instead of a symbol. ! 309: ! 310: ! 311: _c_s_i_n_g_l_e_-_c_h_a_r_a_c_t_e_r_-_s_y_m_b_o_l raw readtable:none ! 312: standard readtable:none ! 313: This returns a symbol whose print name is the the sin- ! 314: gle character which has been collected. ! 315: ! 316: ! 317: _c_m_a_c_r_o raw readtable:none ! 318: standard readtable:`, ! 319: The reader calls the macro function associated with ! 320: this character and the current readtable, passing it ! 321: no arguments. The result of the macro is added to the ! 322: structure the reader is building, just as if that form ! 323: were directly read by the reader. More details on ! 324: macros are provided below. ! 325: ! 326: ! 327: Printed: July 21, 1983 ! 328: ! 329: ! 330: ! 331: ! 332: ! 333: ! 334: ! 335: The Lisp Reader 7-6 ! 336: ! 337: ! 338: _c_s_p_l_i_c_i_n_g_-_m_a_c_r_o raw readtable:none ! 339: standard readtable:#; ! 340: A _c_s_p_l_i_c_i_n_g-_m_a_c_r_o differs from a _c_m_a_c_r_o in the way the ! 341: result is incorporated in the structure the reader is ! 342: building. A _c_s_p_l_i_c_i_n_g-_m_a_c_r_o must return a list of ! 343: forms (possibly empty). The reader acts as if it read ! 344: each element of the list itself without the surround- ! 345: ing parenthesis. ! 346: ! 347: ! 348: _c_s_i_n_g_l_e_-_m_a_c_r_o raw readtable:none ! 349: standard readtable:none ! 350: This causes to reader to check the next character. If ! 351: it is a _c_s_e_p_a_r_a_t_o_r then this acts like a _c_m_a_c_r_o. Oth- ! 352: erwise, it acts like a _c_c_h_a_r_a_c_t_e_r. ! 353: ! 354: ! 355: _c_s_i_n_g_l_e_-_s_p_l_i_c_i_n_g_-_m_a_c_r_o raw readtable:none ! 356: standard readtable:none ! 357: This is triggered like a _c_s_i_n_g_l_e-_m_a_c_r_o however the ! 358: result is spliced in like a _c_s_p_l_i_c_i_n_g-_m_a_c_r_o. ! 359: ! 360: ! 361: _c_i_n_f_i_x_-_m_a_c_r_o raw readtable:none ! 362: standard readtable:none ! 363: This is differs from a _c_m_a_c_r_o in that the macro func- ! 364: tion is passed a form representing what the reader has ! 365: read so far. The result of the macro replaces what the ! 366: reader had read so far. ! 367: ! 368: ! 369: _c_s_i_n_g_l_e_-_i_n_f_i_x_-_m_a_c_r_o raw readtable:none ! 370: standard readtable:none ! 371: This differs from the _c_i_n_f_i_x-_m_a_c_r_o in that the macro ! 372: will only be triggered if the character following the ! 373: _c_s_i_n_g_l_e-_i_n_f_i_x-_m_a_c_r_o character is a _c_s_e_p_a_r_a_t_o_r. ! 374: ! 375: ! 376: _c_i_l_l_e_g_a_l raw readtable:^@-^G^N-^Z^\-^_rubout ! 377: standard readtable:^@-^G^N-^Z^\-^_rubout ! 378: The characters cause the reader to signal an error if ! 379: read. ! 380: ! 381: ! 382: ! 383: 7.5. Syntax classes ! 384: ! 385: The readtable maps each character into a syntax ! 386: class. The syntax class contains three pieces of ! 387: information: the character class, whether this is a ! 388: separator, and the escape properties. The first two ! 389: properties are used by the reader, the last by the ! 390: printer (and _e_x_p_l_o_d_e). The initial lisp system has ! 391: ! 392: ! 393: Printed: July 21, 1983 ! 394: ! 395: ! 396: ! 397: ! 398: ! 399: ! 400: ! 401: The Lisp Reader 7-7 ! 402: ! 403: ! 404: the following syntax classes defined. The user may ! 405: add syntax classes with _a_d_d-_s_y_n_t_a_x-_c_l_a_s_s. For each ! 406: syntax class, we list the properties of the class and ! 407: which characters have this syntax class by default. ! 408: More information about each syntax class can be found ! 409: under the description of the syntax class's character ! 410: class. ! 411: ! 412: vcharacter raw readtable:A-Z a-z ^H !#$%&*,/:;<=>?@^_`{}~ ! 413: _c_c_h_a_r_a_c_t_e_r standard readtable:A-Z a-z ^H !$%&*/:;<=>?@^_{}~ ! 414: ! 415: ! 416: ! 417: vnumber raw readtable:0-9 ! 418: _c_n_u_m_b_e_r standard readtable:0-9 ! 419: ! 420: ! 421: ! 422: vsign raw readtable:+- ! 423: _c_s_i_g_n standard readtable:+- ! 424: ! 425: ! 426: ! 427: vleft-paren raw readtable:( ! 428: _c_l_e_f_t_-_p_a_r_e_n standard readtable:( ! 429: _e_s_c_a_p_e_-_a_l_w_a_y_s ! 430: _s_e_p_a_r_a_t_o_r ! 431: ! 432: vright-paren raw readtable:) ! 433: _c_r_i_g_h_t_-_p_a_r_e_n standard readtable:) ! 434: _e_s_c_a_p_e_-_a_l_w_a_y_s ! 435: _s_e_p_a_r_a_t_o_r ! 436: ! 437: vleft-bracket raw readtable:[ ! 438: _c_l_e_f_t_-_b_r_a_c_k_e_t standard readtable:[ ! 439: _e_s_c_a_p_e_-_a_l_w_a_y_s ! 440: _s_e_p_a_r_a_t_o_r ! 441: ! 442: vright-bracket raw readtable:] ! 443: _c_r_i_g_h_t_-_b_r_a_c_k_e_t standard readtable:] ! 444: _e_s_c_a_p_e_-_a_l_w_a_y_s ! 445: _s_e_p_a_r_a_t_o_r ! 446: ! 447: vperiod raw readtable:. ! 448: _c_p_e_r_i_o_d standard readtable:. ! 449: _e_s_c_a_p_e_-_w_h_e_n_-_u_n_i_q_u_e ! 450: ! 451: ! 452: vseparator raw readtable:^I-^M esc space ! 453: _c_s_e_p_a_r_a_t_o_r standard readtable:^I-^M esc space ! 454: _e_s_c_a_p_e_-_a_l_w_a_y_s ! 455: _s_e_p_a_r_a_t_o_r ! 456: 9 ! 457: ! 458: 9 Printed: July 21, 1983 ! 459: ! 460: ! 461: ! 462: ! 463: ! 464: ! 465: ! 466: The Lisp Reader 7-8 ! 467: ! 468: ! 469: vsingle-quote raw readtable:' ! 470: _c_s_i_n_g_l_e_-_q_u_o_t_e standard readtable:' ! 471: _e_s_c_a_p_e_-_a_l_w_a_y_s ! 472: _s_e_p_a_r_a_t_o_r ! 473: ! 474: vsymbol-delimiter raw readtable:| ! 475: _c_s_i_n_g_l_e_-_d_e_l_i_m_i_t_e_r standard readtable:| ! 476: _e_s_c_a_p_e_-_a_l_w_a_y_s ! 477: ! 478: ! 479: vescape raw readtable:\ ! 480: _c_e_s_c_a_p_e standard readtable:\ ! 481: _e_s_c_a_p_e_-_a_l_w_a_y_s ! 482: ! 483: ! 484: vstring-delimiter raw readtable:" ! 485: _c_s_t_r_i_n_g_-_d_e_l_i_m_i_t_e_r standard readtable:" ! 486: _e_s_c_a_p_e_-_a_l_w_a_y_s ! 487: ! 488: ! 489: vsingle-character-symbol raw readtable:none ! 490: _c_s_i_n_g_l_e_-_c_h_a_r_a_c_t_e_r_-_s_y_m_b_o_l standard readtable:none ! 491: _s_e_p_a_r_a_t_o_r ! 492: ! 493: ! 494: vmacro raw readtable:none ! 495: _c_m_a_c_r_o standard readtable:`, ! 496: _e_s_c_a_p_e_-_a_l_w_a_y_s ! 497: _s_e_p_a_r_a_t_o_r ! 498: ! 499: vsplicing-macro raw readtable:none ! 500: _c_s_p_l_i_c_i_n_g_-_m_a_c_r_o standard readtable:#; ! 501: _e_s_c_a_p_e_-_a_l_w_a_y_s ! 502: _s_e_p_a_r_a_t_o_r ! 503: ! 504: vsingle-macro raw readtable:none ! 505: _c_s_i_n_g_l_e_-_m_a_c_r_o standard readtable:none ! 506: _e_s_c_a_p_e_-_w_h_e_n_-_u_n_i_q_u_e ! 507: ! 508: ! 509: vsingle-splicing-macro raw readtable:none ! 510: _c_s_i_n_g_l_e_-_s_p_l_i_c_i_n_g_-_m_a_c_r_o standard readtable:none ! 511: _e_s_c_a_p_e_-_w_h_e_n_-_u_n_i_q_u_e ! 512: ! 513: ! 514: vinfix-macro raw readtable:none ! 515: _c_i_n_f_i_x_-_m_a_c_r_o standard readtable:none ! 516: _e_s_c_a_p_e_-_a_l_w_a_y_s ! 517: _s_e_p_a_r_a_t_o_r ! 518: ! 519: vsingle-infix-macro raw readtable:none ! 520: _c_s_i_n_g_l_e_-_i_n_f_i_x_-_m_a_c_r_o standard readtable:none ! 521: _e_s_c_a_p_e_-_w_h_e_n_-_u_n_i_q_u_e ! 522: ! 523: ! 524: Printed: July 21, 1983 ! 525: ! 526: ! 527: ! 528: ! 529: ! 530: ! 531: ! 532: The Lisp Reader 7-9 ! 533: ! 534: ! 535: ! 536: ! 537: villegal raw readtable:^@-^G^N-^Z^\-^_rubout ! 538: _c_i_l_l_e_g_a_l standard readtable:^@-^G^N-^Z^\-^_rubout ! 539: _e_s_c_a_p_e_-_a_l_w_a_y_s ! 540: _s_e_p_a_r_a_t_o_r ! 541: ! 542: ! 543: ! 544: 7.6. Character Macros ! 545: ! 546: Character macros are user written functions which ! 547: are executed during the reading process. The value ! 548: returned by a character macro may or may not be used ! 549: by the reader, depending on the type of macro and the ! 550: value returned. Character macros are always attached ! 551: to a single character with the _s_e_t_s_y_n_t_a_x function. ! 552: ! 553: ! 554: ! 555: 7.6.1. Types There are three types of character ! 556: macros: normal, splicing and infix. These types ! 557: differ in the arguments they are given or in what ! 558: is done with the result they return. ! 559: ! 560: ! 561: ! 562: 7.6.1.1. Normal ! 563: ! 564: A normal macro is passed no arguments. The ! 565: value returned by a normal macro is simply used ! 566: by the reader as if it had read the value ! 567: itself. Here is an example of a macro which ! 568: returns the abbreviation for a given state. ! 569: ! 570: ! 571: ____________________________________________________ ! 572: ! 573: ->(_d_e_f_u_n _s_t_a_t_e_a_b_b_r_e_v _n_i_l ! 574: (_c_d_r (_a_s_s_q (_r_e_a_d) '((_c_a_l_i_f_o_r_n_i_a . _c_a) (_p_e_n_n_s_y_l_v_a_n_i_a . _p_a))))) ! 575: stateabbrev ! 576: -> (_s_e_t_s_y_n_t_a_x '_\! '_v_m_a_c_r_o '_s_t_a_t_e_a_b_b_r_e_v) ! 577: t ! 578: -> '( ! _c_a_l_i_f_o_r_n_i_a ! _w_y_o_m_i_n_g ! _p_e_n_n_s_y_l_v_a_n_i_a) ! 579: (ca nil pa) ! 580: ____________________________________________________ ! 581: ! 582: ! 583: ! 584: Notice what happened to ! 585: ! _w_y_o_m_i_n_g. Since it wasn't in the table, the macro prob- ! 586: ably didn't want to return anything at all, but it had to ! 587: return something, and whatever it returned was put in the ! 588: ! 589: ! 590: Printed: July 21, 1983 ! 591: ! 592: ! 593: ! 594: ! 595: ! 596: ! 597: ! 598: The Lisp Reader 7-10 ! 599: ! 600: ! 601: list. The splicing macro, described next, allows a charac- ! 602: ter macro function to return a value that is ignored. ! 603: ! 604: ! 605: ! 606: 7.6.1.2. Splicing ! 607: ! 608: The value returned from a splicing macro ! 609: must be a list or nil. If the value is nil, ! 610: then the value is ignored, otherwise the reader ! 611: acts as if it read each object in the list. ! 612: Usually the list only contains one element. If ! 613: the reader is reading at the top level (i.e. not ! 614: collecting elements of list), then it is illegal ! 615: for a splicing macro to return more then one ! 616: element in the list. The major advantage of a ! 617: splicing macro over a normal macro is the abil- ! 618: ity of the splicing macro to return nothing. The ! 619: comment character (usually ;) is a splicing ! 620: macro bound to a function which reads to the end ! 621: of the line and always returns nil. Here is the ! 622: previous example written as a splicing macro ! 623: ! 624: ! 625: ____________________________________________________ ! 626: ! 627: -> (_d_e_f_u_n _s_t_a_t_e_a_b_b_r_e_v _n_i_l ! 628: ((_l_a_m_b_d_a (_v_a_l_u_e) ! 629: (_c_o_n_d (_v_a_l_u_e (_l_i_s_t _v_a_l_u_e)) ! 630: (_t _n_i_l))) ! 631: (_c_d_r (_a_s_s_q (_r_e_a_d) '((_c_a_l_i_f_o_r_n_i_a . _c_a) (_p_e_n_n_s_y_l_v_a_n_i_a . _p_a)))))) ! 632: -> (_s_e_t_s_y_n_t_a_x '! '_v_s_p_l_i_c_i_n_g-_m_a_c_r_o '_s_t_a_t_e_a_b_b_r_e_v) ! 633: -> '(!_p_e_n_n_s_y_l_v_a_n_i_a ! _f_o_o !_c_a_l_i_f_o_r_n_i_a) ! 634: (pa ca) ! 635: -> '!_f_o_o !_b_a_r !_p_e_n_n_s_y_l_v_a_n_i_a ! 636: pa ! 637: -> ! 638: ____________________________________________________ ! 639: ! 640: ! 641: ! 642: ! 643: ! 644: ! 645: 7.6.1.3. Infix ! 646: ! 647: Infix macros are passed a _c_o_n_c structure ! 648: representing what has been read so far. ! 649: Briefly, a tconc structure is a single list cell ! 650: whose car points to a list and whose cdr points ! 651: to the last list cell in that list. The ! 652: interpretation by the reader of the value ! 653: returned by an infix macro depends on whether ! 654: ! 655: ! 656: Printed: July 21, 1983 ! 657: ! 658: ! 659: ! 660: ! 661: ! 662: ! 663: ! 664: The Lisp Reader 7-11 ! 665: ! 666: ! 667: the macro is called while the reader is con- ! 668: structing a list or whether it is called at the ! 669: top level of the reader. If the macro is called ! 670: while a list is being constructed, then the ! 671: value returned should be a tconc structure. ! 672: The car of that structure replaces the list of ! 673: elements that the reader has been collecting. ! 674: If the macro is called at top level, then it ! 675: will be passed the value nil, and the value it ! 676: returns should either be nil or a tconc struc- ! 677: ture. If the macro returns nil, then the value ! 678: is ignored and the reader continues to read. If ! 679: the macro returns a tconc structure of one ele- ! 680: ment (i.e. whose car is a list of one element), ! 681: then that single element is returned as the ! 682: value of _r_e_a_d. If the macro returns a tconc ! 683: structure of more than one element, then that ! 684: list of elements is returned as the value of ! 685: read. ! 686: ! 687: ! 688: ____________________________________________________ ! 689: ! 690: -> (_d_e_f_u_n _p_l_u_s_o_p (_x) ! 691: (_c_o_n_d ((_n_u_l_l _x) (_t_c_o_n_c _n_i_l '_\+)) ! 692: (_t (_l_c_o_n_c _n_i_l (_l_i_s_t '_p_l_u_s (_c_a_a_r _x) (_r_e_a_d)))))) ! 693: ! 694: plusop ! 695: -> (_s_e_t_s_y_n_t_a_x '_\+ '_v_i_n_f_i_x-_m_a_c_r_o '_p_l_u_s_o_p) ! 696: t ! 697: -> '(_a + _b) ! 698: (plus a b) ! 699: -> '+ ! 700: |+| ! 701: -> ! 702: ____________________________________________________ ! 703: ! 704: ! 705: ! 706: ! 707: ! 708: ! 709: 7.6.2. Invocations ! 710: ! 711: There are three different circumstances in ! 712: which you would like a macro function to be trig- ! 713: gered. ! 714: ! 715: _A_l_w_a_y_s - ! 716: Whenever the macro character is seen, the ! 717: macro should be invoked. This is accomplished ! 718: by using the character classes _c_m_a_c_r_o, ! 719: _c_s_p_l_i_c_i_n_g-_m_a_c_r_o, or _c_i_n_f_i_x-_m_a_c_r_o, and by using ! 720: ! 721: ! 722: Printed: July 21, 1983 ! 723: ! 724: ! 725: ! 726: ! 727: ! 728: ! 729: ! 730: The Lisp Reader 7-12 ! 731: ! 732: ! 733: the _s_e_p_a_r_a_t_o_r property. The syntax classes ! 734: vmacro, vsplicing-macro, and vsingle-macro are ! 735: defined this way. ! 736: ! 737: _W_h_e_n _f_i_r_s_t - ! 738: The macro should only be triggered when the ! 739: macro character is the first character found ! 740: after the scanning process. A syntax class ! 741: for a _w_h_e_n _f_i_r_s_t macro would be defined using ! 742: _c_m_a_c_r_o, _c_s_p_l_i_c_i_n_g-_m_a_c_r_o, or _c_i_n_f_i_x-_m_a_c_r_o and ! 743: not including the _s_e_p_a_r_a_t_o_r property. ! 744: ! 745: _W_h_e_n _u_n_i_q_u_e - ! 746: The macro should only be triggered when the ! 747: macro character is the only character col- ! 748: lected in the token collection phase of the ! 749: reader, i.e the macro character is preceeded ! 750: by zero or more _c_s_e_p_a_r_a_t_o_rs and followed by a ! 751: _s_e_p_a_r_a_t_o_r. A syntax class for a _w_h_e_n _u_n_i_q_u_e ! 752: macro would be defined using _c_s_i_n_g_l_e-_m_a_c_r_o, ! 753: _c_s_i_n_g_l_e-_s_p_l_i_c_i_n_g-_m_a_c_r_o, or _c_s_i_n_g_l_e-_i_n_f_i_x-_m_a_c_r_o ! 754: and not including the _s_e_p_a_r_a_t_o_r property. The ! 755: syntax classes so defined are vsingle-macro, ! 756: vsingle-splicing-macro, and vsingle-infix- ! 757: macro. ! 758: ! 759: ! 760: ! 761: 7.7. Functions ! 762: ! 763: (setsyntax 's_symbol 's_synclass ['ls_func]) ! 764: ! 765: WHERE: ls_func is the name of a function or a lambda ! 766: body. ! 767: ! 768: RETURNS: t ! 769: ! 770: SIDE EFFECT: S_symbol should be a symbol whose print ! 771: name is only one character. The syntax ! 772: class for that character is set to ! 773: s_synclass in the current readtable. If ! 774: s_synclass is a class that requires a ! 775: character macro, then ls_func must be sup- ! 776: plied. ! 777: ! 778: NOTE: The symbolic syntax codes are new to Opus 38. ! 779: For compatibility, s_synclass can be one of the ! 780: fixnum syntax codes which appeared in older ver- ! 781: sions of the FRANZ LISP Manual. This compatibil- ! 782: ity is only temporary: existing code which uses ! 783: the fixnum syntax codes should be converted. ! 784: ! 785: 9 ! 786: ! 787: 9 Printed: July 21, 1983 ! 788: ! 789: ! 790: ! 791: ! 792: ! 793: ! 794: ! 795: The Lisp Reader 7-13 ! 796: ! 797: ! 798: (getsyntax 's_symbol) ! 799: ! 800: RETURNS: the syntax class of the first character of ! 801: s_symbol's print name. s_symbol's print name ! 802: must be exactly one character long. ! 803: ! 804: NOTE: This function is new to Opus 38. It supercedes ! 805: (_s_t_a_t_u_s _s_y_n_t_a_x) which no longer exists. ! 806: ! 807: (add-syntax-class 's_synclass 'l_properties) ! 808: ! 809: RETURNS: s_synclass ! 810: ! 811: SIDE EFFECT: Defines the syntax class s_synclass to ! 812: have properties l_properties. The list ! 813: l_properties should contain a character ! 814: classes mentioned above. l_properties may ! 815: contain one of the escape properties: ! 816: _e_s_c_a_p_e-_a_l_w_a_y_s, _e_s_c_a_p_e-_w_h_e_n-_u_n_i_q_u_e, or ! 817: _e_s_c_a_p_e-_w_h_e_n-_f_i_r_s_t. l_properties may con- ! 818: tain the _s_e_p_a_r_a_t_o_r property. After a syn- ! 819: tax class has been defined with _a_d_d- ! 820: _s_y_n_t_a_x-_c_l_a_s_s, the _s_e_t_s_y_n_t_a_x function can ! 821: be used to give characters that syntax ! 822: class. ! 823: ! 824: ! 825: ____________________________________________________ ! 826: ! 827: ; Define a non-separating macro character. ! 828: ; This type of macro character is used in UCI-Lisp, and ! 829: ; it corresponds to a FIRST MACRO in Interlisp ! 830: -> (_a_d_d-_s_y_n_t_a_x-_c_l_a_s_s '_v_u_c_i-_m_a_c_r_o '(_c_m_a_c_r_o _e_s_c_a_p_e-_w_h_e_n-_f_i_r_s_t)) ! 831: vuci-macro ! 832: -> ! 833: ____________________________________________________ ! 834: ! 835: ! 836: ! 837: ! 838: ! 839: ! 840: ! 841: ! 842: ! 843: ! 844: ! 845: ! 846: ! 847: ! 848: ! 849: ! 850: 9 ! 851: ! 852: 9 Printed: July 21, 1983 ! 853: ! 854: ! 855:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.