parser.red 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. module parser; % Functions for parsing RLISP expressions.
  2. % Author: Anthony C. Hearn.
  3. % Copyright (c) 1991 The RAND Corporation. All rights reserved.
  4. fluid '(!*backtrace);
  5. global '(cursym!* letl!* nxtsym!*);
  6. %With the exception of assignment statements, which are handled by
  7. %XREAD, statements in REDUCE are introduced by a key-word, which
  8. %initiates a reading process peculiar to that statement. The key-word
  9. %is recognized (in XREAD1) by the indicator STAT on its property list.
  10. %The corresponding property is the name of the function (of no
  11. %arguments) which carries out the reading sequence.
  12. % ***** COMMENTS *****
  13. symbolic procedure comm1 u;
  14. begin scalar bool;
  15. if u eq 'end then go to b;
  16. a: if cursym!* eq '!*semicol!*
  17. or u eq 'end
  18. and cursym!* memq
  19. '(end else then until !*rpar!* !*rsqbkt!*)
  20. then return nil
  21. else if u eq 'end and null bool
  22. then progn(lprim list("END-COMMENT NO LONGER SUPPORTED"),
  23. bool := t);
  24. b: scan();
  25. go to a
  26. end;
  27. % ***** CONDITIONAL STATEMENT *****
  28. symbolic procedure ifstat;
  29. begin scalar condx,condit;
  30. a: condx := xread t;
  31. if not(cursym!* eq 'then) then symerr('if,t);
  32. condit := aconc!*(condit,list(condx,xread t));
  33. if not(cursym!* eq 'else) then nil
  34. else if scan() eq 'if then go to a
  35. else condit := aconc!*(condit,list(t,xread1 t));
  36. return ('cond . condit)
  37. end;
  38. put('if,'stat,'ifstat);
  39. flag ('(then else),'delim);
  40. % ***** FUNCTION STATEMENT *****
  41. symbolic procedure functionstat;
  42. begin scalar x;
  43. x := scan();
  44. return list('function,
  45. if x eq '!*lpar!* then xread1 t
  46. else if idp x and null(x eq 'lambda)
  47. then progn(scan(),x)
  48. else symerr("Function",t))
  49. end;
  50. put('function,'stat,'functionstat);
  51. % ***** LAMBDA STATEMENT *****
  52. symbolic procedure lamstat;
  53. begin scalar x,y;
  54. x:= xread 'lambda;
  55. % x := flagtype(if null x then nil else remcomma x,'scalar);
  56. if x then x := remcomma x;
  57. y := list('lambda,x,xread t);
  58. % remtype x;
  59. return y
  60. end;
  61. put ('lambda,'stat,'lamstat);
  62. % ***** GROUP STATEMENT *****
  63. symbolic procedure readprogn;
  64. %Expects a list of statements terminated by a >>;
  65. begin scalar lst;
  66. a: lst := aconc!*(lst,xread 'group);
  67. if null(cursym!* eq '!*rsqbkt!*) then go to a;
  68. scan();
  69. return ('progn . lst)
  70. end;
  71. put('!*lsqbkt!*,'stat,'readprogn);
  72. flag('(!*rsqbkt!*),'delim);
  73. flag('(!*rsqbkt!*),'nodel);
  74. % ***** END STATEMENT *****
  75. symbolic procedure endstat;
  76. %This procedure can also be used for any key-words which take no
  77. %arguments;
  78. begin scalar x; x := cursym!*; comm1 'end; return list x end;
  79. put('end,'stat,'endstat);
  80. put('endmodule,'stat,'endstat);
  81. put('bye,'stat,'endstat);
  82. put('quit,'stat,'endstat);
  83. flag('(bye quit),'eval);
  84. put('showtime,'stat,'endstat);
  85. endmodule;
  86. end;