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

http://ambienttalk.googlecode.com/ · Java · 215 lines · 122 code · 26 blank · 67 comment · 24 complexity · 9ab0aae66329fb015e1d61d0fbef4581 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 edu.vub.util.TempFieldGenerator;
  40. import java.util.HashMap;
  41. import java.util.HashSet;
  42. import java.util.Hashtable;
  43. import java.util.Set;
  44. /**
  45. * The public interface to a native import AST component, which is of the form:
  46. * 'import <expression> (alias (symbol := symbol)+ )? (exclude symbol (, symbol)* )?'
  47. *
  48. * @see edu.vub.at.objects.grammar.ATImport
  49. *
  50. * @author tvcutsem
  51. */
  52. public class AGImport extends NATAbstractGrammar implements ATImport {
  53. private final ATExpression importedObjectExp_;
  54. private final ATExpression aliasDeclarations_;
  55. private final ATExpression excludesDeclarations_;
  56. /**
  57. * For efficiency purposes, the table form of the alias mapping is preprocessed
  58. * into a hashtable format.
  59. */
  60. private Hashtable aliasedSymbols_; // maps ATSymbols to ATSymbols
  61. /**
  62. * For efficiency purposes, the table form of the excluded symbols is preprocessed
  63. * into a hashset format.
  64. */
  65. private HashSet excludedSymbols_; // contains ATSymbols
  66. /** create a new import statement. The alias and excludes declaration tables may still contain quoted expressions */
  67. public AGImport(ATExpression importedObjectExp, ATExpression aliasDeclarations, ATExpression excludesDeclarations) {
  68. importedObjectExp_ = importedObjectExp;
  69. aliasDeclarations_ = aliasDeclarations;
  70. excludesDeclarations_ = excludesDeclarations;
  71. // cannot already preprocess the import statement here, as it may contain
  72. // unquotes which need to be evaluated to symbols first
  73. }
  74. public ATExpression base_aliasedSymbols() throws InterpreterException {
  75. return aliasDeclarations_;
  76. }
  77. public ATExpression base_excludedSymbols() throws InterpreterException {
  78. return excludesDeclarations_;
  79. }
  80. public ATExpression base_importedObjectExpression() throws InterpreterException {
  81. return importedObjectExp_;
  82. }
  83. /**
  84. * AGIMPORT(exp,aliases,excludes).eval(ctx) =
  85. * import(exp.eval(ctx), ctx, preprocess(aliases), preprocess(excludes)) ;
  86. * NIL
  87. */
  88. public ATObject meta_eval(ATContext ctx) throws InterpreterException {
  89. if (aliasedSymbols_ == null) {
  90. aliasedSymbols_ = Import.preprocessAliases(aliasDeclarations_.asTable());
  91. excludedSymbols_ = Import.preprocessExcludes(excludesDeclarations_.asTable());
  92. }
  93. return Import.performImport(importedObjectExp_.meta_eval(ctx), ctx, aliasedSymbols_, excludedSymbols_);
  94. }
  95. /**
  96. * Quoting an import statement results in a new quoted import statement.
  97. *
  98. * AGIMPORT(exp,aliases,excludes).quote(ctx) = AGIMPORT(exp.quote(ctx), aliases.quote(ctx), excludes.quote(ctx))
  99. */
  100. public ATObject meta_quote(ATContext ctx) throws InterpreterException {
  101. return new AGImport(importedObjectExp_.meta_quote(ctx).asExpression(),
  102. aliasDeclarations_.meta_quote(ctx).asExpression(),
  103. excludesDeclarations_.meta_quote(ctx).asExpression());
  104. }
  105. public NATText meta_print() throws InterpreterException {
  106. StringBuffer expression = new StringBuffer("import " + importedObjectExp_.meta_print().javaValue);
  107. if (aliasDeclarations_ != NATTable.EMPTY) {
  108. expression.append(" alias ");
  109. if (aliasDeclarations_.isTable()) {
  110. ATObject[] aliases = aliasDeclarations_.asNativeTable().elements_;
  111. // append first alias
  112. printAliasBinding(expression, aliases[0].asNativeTable());
  113. for (int i = 1; i < aliases.length; i++) {
  114. // append rest of the aliases
  115. expression.append(",");
  116. printAliasBinding(expression, aliases[i].asNativeTable());
  117. }
  118. } else {
  119. // list of aliases is a quatation
  120. expression.append(aliasDeclarations_.meta_print().javaValue);
  121. }
  122. }
  123. if (excludesDeclarations_ != NATTable.EMPTY) {
  124. expression.append(" exclude ");
  125. if (excludesDeclarations_.isTable()) {
  126. expression.append(Evaluator.printElements(excludesDeclarations_.asNativeTable().elements_, "", ",", "").javaValue);
  127. } else {
  128. // list of excluded symbols is a quotation
  129. expression.append(excludesDeclarations_.meta_print().javaValue);
  130. }
  131. }
  132. return NATText.atValue(expression.toString());
  133. }
  134. private void printAliasBinding(StringBuffer buff, NATTable aliasBinding) throws InterpreterException {
  135. ATObject[] binding = aliasBinding.elements_;
  136. if (binding.length != 2) {
  137. throw new XIllegalArgument("Alias binding of import statement is not a table of size two: " + aliasBinding.meta_print().javaValue);
  138. }
  139. buff.append(binding[0].meta_print().javaValue).append(" := ").append(binding[1].meta_print().javaValue);
  140. }
  141. public NATText impl_asUnquotedCode(TempFieldGenerator objectMap) throws InterpreterException {
  142. StringBuffer expression = new StringBuffer("import " + importedObjectExp_.impl_asUnquotedCode(objectMap).javaValue);
  143. if (aliasDeclarations_ != NATTable.EMPTY) {
  144. expression.append(" alias ");
  145. if (aliasDeclarations_.isTable()) {
  146. ATObject[] aliases = aliasDeclarations_.asNativeTable().elements_;
  147. // append first alias
  148. codeAliasBinding(objectMap, expression, aliases[0].asNativeTable());
  149. for (int i = 1; i < aliases.length; i++) {
  150. // append rest of the aliases
  151. expression.append(",");
  152. codeAliasBinding(objectMap, expression, aliases[i].asNativeTable());
  153. }
  154. } else {
  155. // list of aliases is a quatation
  156. expression.append(aliasDeclarations_.impl_asUnquotedCode(objectMap).javaValue);
  157. }
  158. }
  159. if (excludesDeclarations_ != NATTable.EMPTY) {
  160. expression.append(" exclude ");
  161. if (excludesDeclarations_.isTable()) {
  162. expression.append(Evaluator.codeElements(objectMap, excludesDeclarations_.asNativeTable().elements_, "", ",", "").javaValue);
  163. } else {
  164. // list of excluded symbols is a quotation
  165. expression.append(excludesDeclarations_.impl_asUnquotedCode(objectMap).javaValue);
  166. }
  167. }
  168. return NATText.atValue(expression.toString());
  169. }
  170. private void codeAliasBinding(TempFieldGenerator objectMap, StringBuffer buff, NATTable aliasBinding) throws InterpreterException {
  171. ATObject[] binding = aliasBinding.elements_;
  172. if (binding.length != 2) {
  173. throw new XIllegalArgument("Alias binding of import statement is not a table of size two: " + aliasBinding.impl_asUnquotedCode(objectMap).javaValue);
  174. }
  175. buff.append(binding[0].impl_asUnquotedCode(objectMap).javaValue).append(" := ").append(binding[1].impl_asUnquotedCode(objectMap).javaValue);
  176. }
  177. /**
  178. * FV(import objExp alias nam1 := nam2 exclude nam3) = FV(objExp)
  179. */
  180. public Set impl_freeVariables() throws InterpreterException {
  181. return importedObjectExp_.impl_freeVariables();
  182. }
  183. public Set impl_quotedFreeVariables() throws InterpreterException {
  184. Set qfv = importedObjectExp_.impl_quotedFreeVariables();
  185. qfv.addAll(aliasDeclarations_.impl_quotedFreeVariables());
  186. qfv.addAll(excludesDeclarations_.impl_quotedFreeVariables());
  187. return qfv;
  188. }
  189. }