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

http://ambienttalk.googlecode.com/ · Java · 121 lines · 55 code · 16 blank · 50 comment · 0 complexity · 9dbbfbec0afa6f49dce2e667fefec2a7 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * AGAssignTable.java created on 26-jul-2006 at 15:57:37
  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.exceptions.InterpreterException;
  30. import edu.vub.at.objects.ATContext;
  31. import edu.vub.at.objects.ATObject;
  32. import edu.vub.at.objects.grammar.ATAssignTable;
  33. import edu.vub.at.objects.grammar.ATExpression;
  34. import edu.vub.at.objects.natives.NATMethodInvocation;
  35. import edu.vub.at.objects.natives.NATTable;
  36. import edu.vub.at.objects.natives.NATText;
  37. import java.util.Set;
  38. /**
  39. * The native implementation of a table assignment AG element.
  40. *
  41. * @author tvc
  42. */
  43. public final class AGAssignTable extends NATAbstractGrammar implements ATAssignTable {
  44. private final ATExpression tblExp_;
  45. private final ATExpression idxExp_;
  46. private final ATExpression valExp_;
  47. public AGAssignTable(ATExpression tbl, ATExpression idx, ATExpression val) {
  48. tblExp_ = tbl;
  49. idxExp_ = idx;
  50. valExp_ = val;
  51. }
  52. public ATExpression base_tableExpression() { return tblExp_; }
  53. public ATExpression base_indexExpression() { return idxExp_; }
  54. public ATExpression base_valueExpression() { return valExp_; }
  55. /**
  56. * To evaluate a table assignment, evaluate its table expression to a valid ATTable.
  57. * Next, evaluate its index expression into a valid number.
  58. * Finally, evaluate its value expression and assign the result to the proper index slot of the table.
  59. *
  60. * AGASSTABLE(tbl,idx,val).eval(ctx) =
  61. * tbl.eval(ctx).atPut(idx.eval(ctx), val.eval(ctx))
  62. *
  63. * @return the value stored in the table
  64. */
  65. public ATObject meta_eval(ATContext ctx) throws InterpreterException {
  66. ATObject col = tblExp_.meta_eval(ctx);
  67. ATObject idx = idxExp_.meta_eval(ctx);
  68. ATObject val = valExp_.meta_eval(ctx);
  69. return col.meta_invoke(col,
  70. new NATMethodInvocation(
  71. AGSymbol.jAlloc("atPut"),
  72. NATTable.of(idx, val),
  73. NATTable.EMPTY));
  74. }
  75. /**
  76. * Quoting a table assignment results in a new quoted table assignment.
  77. *
  78. * AGASSTABLE(tbl,idx,val).quote(ctx) = AGASSTABLE(tbl.quote(ctx),idx.quote(ctx),val.quote(ctx))
  79. */
  80. public ATObject meta_quote(ATContext ctx) throws InterpreterException {
  81. return new AGAssignTable(tblExp_.meta_quote(ctx).asExpression(),
  82. idxExp_.meta_quote(ctx).asExpression(),
  83. valExp_.meta_quote(ctx).asExpression());
  84. }
  85. public NATText meta_print() throws InterpreterException {
  86. return NATText.atValue(tblExp_.meta_print().javaValue + "[" +
  87. idxExp_.meta_print().javaValue + "] := " +
  88. valExp_.meta_print().javaValue);
  89. }
  90. /**
  91. * FV(tblExp[idxExp] := valExp) = FV(tblExp) U FV(idxExp) U FV(valExp)
  92. */
  93. public Set impl_freeVariables() throws InterpreterException {
  94. Set fvTblExp = tblExp_.impl_freeVariables();
  95. fvTblExp.addAll(idxExp_.impl_freeVariables());
  96. fvTblExp.addAll(valExp_.impl_freeVariables());
  97. return fvTblExp;
  98. }
  99. public Set impl_quotedFreeVariables() throws InterpreterException {
  100. Set qfv = tblExp_.impl_quotedFreeVariables();
  101. qfv.addAll(idxExp_.impl_quotedFreeVariables());
  102. qfv.addAll(valExp_.impl_quotedFreeVariables());
  103. return qfv;
  104. }
  105. }