define-smacro.red 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. %
  2. % DEFINE-SMACRO.RED - Convert SMacros to Lisp macros
  3. %
  4. % Author: Eric Benson
  5. % Symbolic Computation Group
  6. % Computer Science Dept.
  7. % University of Utah
  8. % Date: 23 October 1981
  9. % Copyright (c) 1981 University of Utah
  10. %
  11. % <PSL.KERNEL>DEFINE-SMACRO.RED.3, 21-Sep-82 10:48:10, Edit by BENSON
  12. % Flagged internal functions
  13. % The functions SafeCDR and StdError are required for run-time support
  14. % of the code generated by DS
  15. CompileTime flag('(InstantiateInForm MakeDS SetMacroReference),
  16. 'InternalFunction);
  17. lisp procedure InstantiateInForm(Formals, Form);
  18. if Atom Form then
  19. if Form memq Formals then Form else MkQuote Form
  20. else 'List . for each X in Form collect InstantiateInForm(Formals, X);
  21. lisp procedure SetMacroReference U;
  22. list('SetQ, U, '(car !#Arg));
  23. macro procedure DS Form; %. Define Smacro
  24. %
  25. % DS(FNAME:id, PARAMS:id-list, FN:any):id
  26. % ---------------------------------------
  27. % Type: MACRO
  28. % A convenient syntax for a simple macro definition, known as an SMACRO.
  29. % The syntax of DS is similar to DE, except that a MACRO is defined instead
  30. % of an EXPR, e.g.
  31. % (DS FOO (A B) (BAR A B))
  32. % is equivalent to:
  33. % (DM FOO (U) (LIST 'BAR (CADR U) (CADDR U))).
  34. % The "implicit ProgN" is allowed when using Lisp syntax. DS is invoked
  35. % with Rlisp syntax as the procedure type SMACRO, e.g.
  36. % SMACRO PROCEDURE FOO(A, B); BAR(A, B);
  37. % produces the above Lisp form.
  38. %
  39. MakeDS(cadr Form, caddr Form, cdddr Form);
  40. lisp procedure MakeDS(MacroName, Formals, Form);
  41. begin scalar NewForm, I;
  42. NewForm := list 'PROG;
  43. NewForm := Formals . NewForm;
  44. for each X in Formals do
  45. << NewForm := '(SetQ !#Arg (SafeCDR !#Arg)) . NewForm;
  46. NewForm := SetMacroReference X . NewForm >>;
  47. NewForm := '(cond ((PairP (cdr !#Arg))
  48. (StdError "Argument mismatch in SMacro expansion")))
  49. . NewForm;
  50. NewForm := list('Return, if null cdr Form then
  51. InstantiateInForm(Formals, car Form)
  52. else 'list . '(quote ProgN)
  53. . for each X in Form collect
  54. InstantiateInForm(Formals, X)) . NewForm;
  55. return 'dm . MacroName . '(!#Arg) . list ReversIP NewForm;
  56. end;
  57. %lisp procedure PutC(Name, Type, Body);
  58. % if Type eq 'SMACRO then Eval MakeDS(Name, cadr Body, cddr Body)
  59. % else
  60. % << put(Name, Type, Body);
  61. % Name >>;
  62. END;