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

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