newtok.red 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. module newtok; % Functions for introducing infix tokens to the system.
  2. % Author: Anthony C. Hearn.
  3. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  4. fluid '(!*msg !*redeflg!*);
  5. global '(preclis!* fixedpreclis!*);
  6. % Several operators in REDUCE are used in an infix form (e.g., +,- ).
  7. % The internal alphanumeric names associated with these operators are
  8. % introduced by the function NEWTOK defined below. This association,
  9. % and the precedence of each infix operator, is initialized in this
  10. % section. We also associate printing characters with each internal
  11. % alphanumeric name as well.
  12. fixedpreclis!* := '(where !*comma!* setq);
  13. preclis!*:= '(or and member memq equal neq eq geq greaterp leq % not
  14. lessp freeof plus difference times quotient expt cons);
  15. deflist ('(
  16. % (not not)
  17. (plus plus)
  18. (difference minus)
  19. (minus minus)
  20. (times times)
  21. (quotient recip)
  22. (recip recip)
  23. ), 'unary);
  24. flag ('(and or !*comma!* plus times),'nary);
  25. flag ('(cons setq plus times),'right);
  26. deflist ('((minus plus) (recip times)),'alt);
  27. symbolic procedure mkprec;
  28. begin scalar x,y,z;
  29. x := append(fixedpreclis!*,preclis!*);
  30. y := 1;
  31. a: if null x then return nil;
  32. put(car x,'infix,y);
  33. put(car x,'op,list list(y,y)); % for RPRINT.
  34. if z := get(car x,'unary) then put(z,'infix,y);
  35. if and(z,null flagp(z,'nary)) then put(z,'op,list(nil,y));
  36. x := cdr x;
  37. y := add1 y;
  38. go to a
  39. end;
  40. mkprec();
  41. symbolic procedure newtok u;
  42. begin scalar !*redeflg!*,x,y;
  43. if atom u or atom car u or null idp caar u
  44. then typerr(u,"NEWTOK argument");
  45. % set up SWITCH* property.
  46. put(caar u,'switch!*,
  47. cdr newtok1(car u,cadr u,get(caar u,'switch!*)));
  48. % set up PRTCH property.
  49. y := intern compress consescc car u;
  50. if !*redeflg!* then lprim list(y,"redefined");
  51. put(cadr u,'prtch,y);
  52. if x := get(cadr u,'unary) then put(x,'prtch,y)
  53. end;
  54. symbolic procedure newtok1(charlist,name,propy);
  55. if null propy then lstchr(charlist,name)
  56. else if null cdr charlist
  57. then begin
  58. if cdr propy and !*msg then !*redeflg!* := t;
  59. return list(car charlist,car propy,name)
  60. end
  61. else car charlist . newtok2(cdr charlist,name,car propy)
  62. . cdr propy;
  63. symbolic procedure newtok2(charlist,name,assoclist);
  64. if null assoclist then list lstchr(charlist,name)
  65. else if car charlist eq caar assoclist
  66. then newtok1(charlist,name,cdar assoclist) . cdr assoclist
  67. else car assoclist . newtok2(charlist,name,cdr assoclist);
  68. symbolic procedure consescc u;
  69. if null u then nil else '!! . car u . consescc cdr u;
  70. symbolic procedure lstchr(u,v);
  71. if null cdr u then list(car u,nil,v)
  72. else list(car u,list lstchr(cdr u,v));
  73. newtok '((!$) !*semicol!*);
  74. newtok '((!;) !*semicol!*);
  75. newtok '((!+) plus);
  76. newtok '((!-) difference);
  77. newtok '((!*) times);
  78. newtok '((!^) expt);
  79. newtok '((!* !*) expt);
  80. newtok '((!/) quotient);
  81. newtok '((!=) equal);
  82. newtok '((!,) !*comma!*);
  83. newtok '((!() !*lpar!*);
  84. newtok '((!)) !*rpar!*);
  85. newtok '((!:) !*colon!*);
  86. newtok '((!: !=) setq);
  87. newtok '((!.) cons);
  88. newtok '((!<) lessp);
  89. newtok '((!< !=) leq);
  90. newtok '((![) !*lsqbkt!*);
  91. newtok '((!< !<) !*lsqbkt!*);
  92. newtok '((!>) greaterp);
  93. newtok '((!> !=) geq);
  94. newtok '((!]) !*rsqbkt!*);
  95. newtok '((!> !>) !*rsqbkt!*);
  96. put('expt,'prtch,'!*!*); % To ensure that FORTRAN output is correct.
  97. flag('(difference minus plus setq),'spaced);
  98. flag('(newtok),'eval);
  99. endmodule;
  100. end;