define_alias.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package kawa.standard;
  2. import gnu.bytecode.Type;
  3. import gnu.expr.*;
  4. import gnu.lists.*;
  5. import gnu.mapping.*;
  6. import kawa.lang.*;
  7. public class define_alias extends Syntax {
  8. public static final define_alias define_alias = new define_alias();
  9. public static final define_alias define_private_alias = new define_alias();
  10. static {
  11. define_alias.setName("define-alias");
  12. define_private_alias.setName("define-private-alias");
  13. define_private_alias.makePrivate = true;
  14. }
  15. private boolean makePrivate = false;
  16. public void scanForm (Pair st, ScopeExp defs, Translator tr) {
  17. Object formCdr = st.getCdr();
  18. SyntaxForm formSyntax = null;
  19. while (formCdr instanceof SyntaxForm) {
  20. formSyntax = (SyntaxForm) formCdr;
  21. formCdr = formSyntax.getDatum();
  22. }
  23. if (formCdr instanceof Pair) {
  24. Pair p1 = (Pair) formCdr;
  25. SyntaxForm nameSyntax = formSyntax;
  26. Object name = p1.getCar();
  27. while (name instanceof SyntaxForm) {
  28. nameSyntax = (SyntaxForm) name;
  29. name = nameSyntax.getDatum();
  30. }
  31. Object f2 = p1.getCdr();
  32. while (f2 instanceof SyntaxForm) {
  33. formSyntax = (SyntaxForm) f2;
  34. f2 = formSyntax.getDatum();
  35. }
  36. if ((name instanceof String || name instanceof Symbol)
  37. && f2 instanceof Pair
  38. && ((Pair) f2).getCdr() == LList.Empty) {
  39. Declaration decl = tr.define(name, nameSyntax, defs);
  40. decl.setIndirectBinding(true);
  41. decl.setAlias(true);
  42. if (makePrivate) {
  43. decl.setFlag(Declaration.PRIVATE_SPECIFIED);
  44. decl.setPrivate(true);
  45. }
  46. if (formSyntax != null)
  47. f2 = SyntaxForms.makeForm(f2, formSyntax.getScope());
  48. tr.pushForm(Translator.makePair(st, this,
  49. Translator.makePair(p1, decl, f2)));
  50. return;
  51. }
  52. }
  53. tr.error('e', "invalid syntax for define-alias");
  54. }
  55. public Expression rewriteForm (Pair form, Translator tr) {
  56. Object f1 = form.getCdr();
  57. if (f1 instanceof Pair) {
  58. Pair p1 = (Pair) f1;
  59. Object f2 = p1.getCar();
  60. if (f2 instanceof Declaration) {
  61. Declaration decl = (Declaration) f2;
  62. Expression arg = tr.rewrite_car((Pair) p1.getCdr(), false);
  63. if (arg instanceof ReferenceExp) {
  64. ReferenceExp rarg = (ReferenceExp) arg;
  65. Declaration d = Declaration.followAliases(rarg.getBinding());
  66. Expression dval;
  67. if (d != null
  68. && ((dval = d.getValue()) instanceof ClassExp
  69. || dval instanceof ModuleExp)) {
  70. decl.setIndirectBinding(false);
  71. decl.setFlag(Declaration.IS_CONSTANT);
  72. }
  73. else
  74. rarg.setDontDereference(true);
  75. } else {
  76. if (! (arg instanceof QuoteExp))
  77. arg = location.rewrite(arg, tr);
  78. if (arg instanceof QuoteExp) {
  79. decl.setIndirectBinding(false);
  80. decl.setFlag(Declaration.IS_CONSTANT);
  81. }
  82. }
  83. decl.setFlag(Declaration.EARLY_INIT);
  84. tr.mustCompileHere(); // For simplicity.
  85. tr.push(decl);
  86. SetExp sexp = new SetExp(decl, arg);
  87. tr.setLineOf(sexp);
  88. decl.noteValueFromSet(sexp);
  89. sexp.setDefining (true);
  90. return sexp;
  91. }
  92. }
  93. return tr.syntaxError(getName()+" is only allowed in a <body>");
  94. }
  95. }