/interpreter/tags/at2dist091109/src/edu/vub/at/objects/natives/grammar/AGImport.java

http://ambienttalk.googlecode.com/ · Java · 172 lines · 88 code · 21 blank · 63 comment · 13 complexity · f1984d65afa3a5e7f616bb3186cd6d5c MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * AGImport.java created on 6-mrt-2007 at 21:34:54
  4. * (c) Programming Technology Lab, 2006 - 2007
  5. * Authors: Tom Van Cutsem & Stijn Mostinckx
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package edu.vub.at.objects.natives.grammar;
  29. import edu.vub.at.eval.Evaluator;
  30. import edu.vub.at.eval.Import;
  31. import edu.vub.at.exceptions.InterpreterException;
  32. import edu.vub.at.exceptions.XIllegalArgument;
  33. import edu.vub.at.objects.ATContext;
  34. import edu.vub.at.objects.ATObject;
  35. import edu.vub.at.objects.grammar.ATExpression;
  36. import edu.vub.at.objects.grammar.ATImport;
  37. import edu.vub.at.objects.natives.NATTable;
  38. import edu.vub.at.objects.natives.NATText;
  39. import java.util.HashSet;
  40. import java.util.Hashtable;
  41. import java.util.Set;
  42. /**
  43. * The public interface to a native import AST component, which is of the form:
  44. * 'import <expression> (alias (symbol := symbol)+ )? (exclude symbol (, symbol)* )?'
  45. *
  46. * @see edu.vub.at.objects.grammar.ATImport
  47. *
  48. * @author tvcutsem
  49. */
  50. public class AGImport extends NATAbstractGrammar implements ATImport {
  51. private final ATExpression importedObjectExp_;
  52. private final ATExpression aliasDeclarations_;
  53. private final ATExpression excludesDeclarations_;
  54. /**
  55. * For efficiency purposes, the table form of the alias mapping is preprocessed
  56. * into a hashtable format.
  57. */
  58. private Hashtable aliasedSymbols_; // maps ATSymbols to ATSymbols
  59. /**
  60. * For efficiency purposes, the table form of the excluded symbols is preprocessed
  61. * into a hashset format.
  62. */
  63. private HashSet excludedSymbols_; // contains ATSymbols
  64. /** create a new import statement. The alias and excludes declaration tables may still contain quoted expressions */
  65. public AGImport(ATExpression importedObjectExp, ATExpression aliasDeclarations, ATExpression excludesDeclarations) {
  66. importedObjectExp_ = importedObjectExp;
  67. aliasDeclarations_ = aliasDeclarations;
  68. excludesDeclarations_ = excludesDeclarations;
  69. // cannot already preprocess the import statement here, as it may contain
  70. // unquotes which need to be evaluated to symbols first
  71. }
  72. public ATExpression base_aliasedSymbols() throws InterpreterException {
  73. return aliasDeclarations_;
  74. }
  75. public ATExpression base_excludedSymbols() throws InterpreterException {
  76. return excludesDeclarations_;
  77. }
  78. public ATExpression base_importedObjectExpression() throws InterpreterException {
  79. return importedObjectExp_;
  80. }
  81. /**
  82. * AGIMPORT(exp,aliases,excludes).eval(ctx) =
  83. * import(exp.eval(ctx), ctx, preprocess(aliases), preprocess(excludes)) ;
  84. * NIL
  85. */
  86. public ATObject meta_eval(ATContext ctx) throws InterpreterException {
  87. if (aliasedSymbols_ == null) {
  88. aliasedSymbols_ = Import.preprocessAliases(aliasDeclarations_.asTable());
  89. excludedSymbols_ = Import.preprocessExcludes(excludesDeclarations_.asTable());
  90. }
  91. return Import.performImport(importedObjectExp_.meta_eval(ctx), ctx, aliasedSymbols_, excludedSymbols_);
  92. }
  93. /**
  94. * Quoting an import statement results in a new quoted import statement.
  95. *
  96. * AGIMPORT(exp,aliases,excludes).quote(ctx) = AGIMPORT(exp.quote(ctx), aliases.quote(ctx), excludes.quote(ctx))
  97. */
  98. public ATObject meta_quote(ATContext ctx) throws InterpreterException {
  99. return new AGImport(importedObjectExp_.meta_quote(ctx).asExpression(),
  100. aliasDeclarations_.meta_quote(ctx).asExpression(),
  101. excludesDeclarations_.meta_quote(ctx).asExpression());
  102. }
  103. public NATText meta_print() throws InterpreterException {
  104. StringBuffer expression = new StringBuffer("import " + importedObjectExp_.meta_print().javaValue);
  105. if (aliasDeclarations_ != NATTable.EMPTY) {
  106. expression.append(" alias ");
  107. if (aliasDeclarations_.isTable()) {
  108. ATObject[] aliases = aliasDeclarations_.asNativeTable().elements_;
  109. // append first alias
  110. printAliasBinding(expression, aliases[0].asNativeTable());
  111. for (int i = 1; i < aliases.length; i++) {
  112. // append rest of the aliases
  113. expression.append(",");
  114. printAliasBinding(expression, aliases[i].asNativeTable());
  115. }
  116. } else {
  117. // list of aliases is a quatation
  118. expression.append(aliasDeclarations_.meta_print().javaValue);
  119. }
  120. }
  121. if (excludesDeclarations_ != NATTable.EMPTY) {
  122. expression.append(" exclude ");
  123. if (excludesDeclarations_.isTable()) {
  124. expression.append(Evaluator.printElements(excludesDeclarations_.asNativeTable().elements_, "", ",", "").javaValue);
  125. } else {
  126. // list of excluded symbols is a quotation
  127. expression.append(excludesDeclarations_.meta_print().javaValue);
  128. }
  129. }
  130. return NATText.atValue(expression.toString());
  131. }
  132. private void printAliasBinding(StringBuffer buff, NATTable aliasBinding) throws InterpreterException {
  133. ATObject[] binding = aliasBinding.elements_;
  134. if (binding.length != 2) {
  135. throw new XIllegalArgument("Alias binding of import statement is not a table of size two: " + aliasBinding.meta_print().javaValue);
  136. }
  137. buff.append(binding[0].meta_print().javaValue).append(" := ").append(binding[1].meta_print().javaValue);
  138. }
  139. /**
  140. * FV(import objExp alias nam1 := nam2 exclude nam3) = FV(objExp)
  141. */
  142. public Set impl_freeVariables() throws InterpreterException {
  143. return importedObjectExp_.impl_freeVariables();
  144. }
  145. public Set impl_quotedFreeVariables() throws InterpreterException {
  146. Set qfv = importedObjectExp_.impl_quotedFreeVariables();
  147. qfv.addAll(aliasDeclarations_.impl_quotedFreeVariables());
  148. qfv.addAll(excludesDeclarations_.impl_quotedFreeVariables());
  149. return qfv;
  150. }
  151. }