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

http://ambienttalk.googlecode.com/ · Java · 164 lines · 83 code · 20 blank · 61 comment · 1 complexity · bc64bd5e0cdbacdb0686c2d41b3cd999 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * AGDefTable.java created on 26-jul-2006 at 15:50:45
  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.exceptions.InterpreterException;
  31. import edu.vub.at.exceptions.XIllegalIndex;
  32. import edu.vub.at.exceptions.XTypeMismatch;
  33. import edu.vub.at.objects.ATContext;
  34. import edu.vub.at.objects.ATObject;
  35. import edu.vub.at.objects.grammar.ATBegin;
  36. import edu.vub.at.objects.grammar.ATDefTable;
  37. import edu.vub.at.objects.grammar.ATDefinition;
  38. import edu.vub.at.objects.grammar.ATExpression;
  39. import edu.vub.at.objects.grammar.ATSymbol;
  40. import edu.vub.at.objects.natives.NATCallframe;
  41. import edu.vub.at.objects.natives.NATTable;
  42. import edu.vub.at.objects.natives.NATText;
  43. import edu.vub.util.TempFieldGenerator;
  44. import java.util.HashMap;
  45. import java.util.HashSet;
  46. import java.util.Set;
  47. /**
  48. * @author tvc
  49. *
  50. * The native implementation of a table definition AG element.
  51. */
  52. public final class AGDefTable extends AGDefinition implements ATDefTable {
  53. private final ATSymbol tblName_;
  54. private final ATExpression sizExp_;
  55. private final ATBegin initExp_;
  56. public AGDefTable(ATSymbol nam, ATExpression siz, ATBegin ini) {
  57. tblName_ = nam;
  58. sizExp_ = siz;
  59. initExp_ = ini;
  60. }
  61. public ATSymbol base_name() { return tblName_; }
  62. public ATExpression base_sizeExpression() { return sizExp_; }
  63. public ATBegin base_initializer() { return initExp_; }
  64. /**
  65. * Defining a table requires evaluating its index expression to a size s,
  66. * then allocating a new table of size s and finally initializing it
  67. * by evaluating its initializer body s times. The return value is the defined table.
  68. *
  69. * The initializer body is evaluated in a new scope, so definitions inside the initializer
  70. * are not visible to the outside (table defining) scope.
  71. *
  72. * AGDEFTABLE(nam,siz,ini).eval(ctx) =
  73. * s = siz.eval(ctx)
  74. * t[s] = nil
  75. * inictx = ctx[cur -> callframe(ctx.cur)]
  76. * i.from: 0 to: s do: {
  77. * t[i] := ini.eval(ctx)
  78. * }
  79. * ctx.scope.defineField(nam, AGTABLE(t))
  80. * AGTABLE(t)
  81. */
  82. public ATObject meta_eval(ATContext ctx) throws InterpreterException {
  83. int siz = 0;
  84. try {
  85. siz = sizExp_.meta_eval(ctx).asNativeNumber().javaValue;
  86. } catch (XTypeMismatch e) {
  87. throw new XIllegalIndex(e.getMessage());
  88. }
  89. NATCallframe initscope = new NATCallframe(ctx.base_lexicalScope());
  90. ATContext initctx = ctx.base_withLexicalEnvironment(initscope);
  91. ATObject[] tab = new ATObject[siz];
  92. for (int i = 0; i < tab.length; i++) {
  93. tab[i] = initExp_.meta_eval(initctx);
  94. }
  95. NATTable tbl = NATTable.atValue(tab);
  96. ctx.base_lexicalScope().meta_defineField(tblName_, tbl);
  97. return tbl;
  98. }
  99. /**
  100. * Quoting a table definition results in a new quoted table definition.
  101. *
  102. * AGDEFTABLE(nam,siz,ini).quote(ctx) = AGDEFTABLE(nam.quote(ctx), siz.quote(ctx), ini.quote(ctx))
  103. */
  104. public ATObject meta_quote(ATContext ctx) throws InterpreterException {
  105. return new AGDefTable(tblName_.meta_quote(ctx).asSymbol(),
  106. sizExp_.meta_quote(ctx).asExpression(),
  107. initExp_.meta_quote(ctx).asBegin());
  108. }
  109. public NATText meta_print() throws InterpreterException {
  110. return NATText.atValue("def " +
  111. tblName_.meta_print().javaValue + "[" +
  112. sizExp_.meta_print().javaValue + "] { " +
  113. initExp_.meta_print().javaValue + "}");
  114. }
  115. public NATText impl_asUnquotedCode(TempFieldGenerator objectMap) throws InterpreterException {
  116. return NATText.atValue("def " +
  117. tblName_.impl_asUnquotedCode(objectMap).javaValue + "[" +
  118. sizExp_.impl_asUnquotedCode(objectMap).javaValue + "] { " +
  119. initExp_.impl_asUnquotedCode(objectMap).javaValue + "}");
  120. }
  121. /**
  122. * IV(def t[idx] { body }) = { t }
  123. */
  124. public Set impl_introducedVariables() throws InterpreterException {
  125. Set singleton = new HashSet();
  126. singleton.add(tblName_);
  127. return singleton;
  128. }
  129. /**
  130. * FV(def t[idx] { body }) = FV(idx) U (FV(body) \ { t })
  131. */
  132. public Set impl_freeVariables() throws InterpreterException {
  133. Set fvBody = initExp_.impl_freeVariables();
  134. fvBody.remove(tblName_);
  135. fvBody.addAll(sizExp_.impl_freeVariables());
  136. return fvBody;
  137. }
  138. public Set impl_quotedFreeVariables() throws InterpreterException {
  139. Set qfv = sizExp_.impl_quotedFreeVariables();
  140. qfv.addAll(initExp_.impl_quotedFreeVariables());
  141. qfv.addAll(tblName_.impl_quotedFreeVariables());
  142. return qfv;
  143. }
  144. }