export.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package kawa.standard;
  2. import kawa.lang.*;
  3. import gnu.expr.*;
  4. import gnu.lists.*;
  5. import gnu.mapping.Symbol;
  6. public class export extends Syntax {
  7. public static final export module_export = new export();
  8. static { module_export.setName("module-export"); }
  9. public static final export export = new export();
  10. static { export.setName("export"); }
  11. @Override
  12. public boolean scanForDefinitions (Pair st, ScopeExp defs, Translator tr) {
  13. Object list = st.getCdr();
  14. Object savePos = tr.pushPositionOf(st);
  15. try {
  16. if (! (defs instanceof ModuleExp)) {
  17. tr.error('e', "\'" + getName() + "\' not at module level");
  18. return false;
  19. }
  20. ModuleExp mexp = (ModuleExp) defs;
  21. if (mexp.getFlag(ModuleExp.HAS_SUB_MODULE)) {
  22. tr.error('e', "'export' used follow explicit modules");
  23. return false;
  24. }
  25. mexp.setFlag(ModuleExp.EXPORT_SPECIFIED);
  26. SyntaxForm restSyntax = null;
  27. while (list != LList.Empty) {
  28. tr.pushPositionOf(list);
  29. while (list instanceof SyntaxForm) {
  30. restSyntax = (SyntaxForm) list;
  31. list = restSyntax.getDatum();
  32. }
  33. SyntaxForm nameSyntax = restSyntax;
  34. if (list instanceof Pair) {
  35. st = (Pair) list;
  36. Object symbol = st.getCar();
  37. while (symbol instanceof SyntaxForm) {
  38. nameSyntax = (SyntaxForm) symbol;
  39. symbol = nameSyntax.getDatum();
  40. }
  41. if (symbol instanceof String) {
  42. String str = (String) symbol;
  43. if (str.startsWith("namespace:")) {
  44. tr.error('w', "'namespace:' prefix ignored");
  45. symbol = str.substring(10).intern();
  46. }
  47. }
  48. symbol = tr.namespaceResolve(symbol);
  49. if (symbol instanceof Pair) {
  50. // Match (rename name1 name2)
  51. Pair psym = (Pair) symbol;
  52. Object symcdr, symcddr; Pair psymcdr;
  53. if (tr.matches(psym.getCar(), "rename")
  54. && (symcdr = psym.getCdr()) instanceof Pair
  55. && ((symcddr = (psymcdr = (Pair) symcdr)
  56. .getCdr()) instanceof Pair)) {
  57. Pair psymcddr = (Pair) symcddr;
  58. Object symcdddr = psymcddr.getCdr();
  59. Object name1 = tr.namespaceResolve(psymcdr.getCar());
  60. Object name2 = tr.namespaceResolve(psymcddr.getCar());
  61. if (symcdddr == LList.Empty
  62. && name1 instanceof Symbol
  63. && name2 instanceof Symbol) {
  64. Declaration decl1 = defs.getNoDefine(name1);
  65. if (decl1.getFlag(Declaration.NOT_DEFINING))
  66. Translator.setLine(decl1, st);
  67. decl1.setFlag(Declaration.EXTERNAL_ACCESS);
  68. // Name of decl2 will be set to name2 later
  69. // using Declaration#patchSymbolFromSet.
  70. Declaration decl2 = tr.define(null, nameSyntax, defs);
  71. decl2.setIndirectBinding(true);
  72. decl2.setAlias(true);
  73. decl2.setFlag(Declaration.EXPORT_SPECIFIED
  74. | Declaration.EARLY_INIT);
  75. ReferenceExp ref1 = new ReferenceExp(decl1);
  76. ref1.setDontDereference(true);
  77. SetExp sexp = new SetExp(name2, ref1);
  78. sexp.setBinding(decl2);
  79. tr.setLineOf(sexp);
  80. decl2.noteValueFromSet(sexp);
  81. sexp.setDefining(true);
  82. list = st.getCdr();
  83. tr.pushForm(sexp);
  84. continue;
  85. }
  86. }
  87. }
  88. if (symbol instanceof String
  89. || symbol instanceof Symbol) {
  90. if (nameSyntax != null) {
  91. // Difficult to implement correctly. And
  92. // probably not much point in doing so. FIXME.
  93. }
  94. Declaration decl = defs.getNoDefine(symbol);
  95. if (decl.getFlag(Declaration.NOT_DEFINING))
  96. Translator.setLine(decl, st);
  97. decl.setFlag(Declaration.EXPORT_SPECIFIED);
  98. list = st.getCdr();
  99. continue;
  100. }
  101. }
  102. tr.error('e', "invalid syntax in '" + getName() + '\'');
  103. return false;
  104. }
  105. return true;
  106. } finally {
  107. tr.popPositionOf(savePos);
  108. }
  109. }
  110. public Expression rewriteForm (Pair form, Translator tr) {
  111. return tr.syntaxError(getName()+" is only allowed in a <body>");
  112. }
  113. }