/interpreter/tags/at2-build190607/src/edu/vub/at/objects/natives/grammar/AGAssignTable.java

http://ambienttalk.googlecode.com/ · Java · 104 lines · 46 code · 11 blank · 47 comment · 0 complexity · f0d0e98968f15d9d7dbef39d937d20d0 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.exceptions.XIllegalIndex;
  31. import edu.vub.at.exceptions.XTypeMismatch;
  32. import edu.vub.at.objects.ATContext;
  33. import edu.vub.at.objects.ATNumber;
  34. import edu.vub.at.objects.ATObject;
  35. import edu.vub.at.objects.ATTable;
  36. import edu.vub.at.objects.grammar.ATAssignTable;
  37. import edu.vub.at.objects.grammar.ATExpression;
  38. import edu.vub.at.objects.natives.NATText;
  39. /**
  40. * The native implementation of a table assignment AG element.
  41. *
  42. * @author tvc
  43. */
  44. public final class AGAssignTable extends NATAbstractGrammar implements ATAssignTable {
  45. private final ATExpression tblExp_;
  46. private final ATExpression idxExp_;
  47. private final ATExpression valExp_;
  48. public AGAssignTable(ATExpression tbl, ATExpression idx, ATExpression val) {
  49. tblExp_ = tbl;
  50. idxExp_ = idx;
  51. valExp_ = val;
  52. }
  53. public ATExpression base_getTableExpression() { return tblExp_; }
  54. public ATExpression base_getIndexExpression() { return idxExp_; }
  55. public ATExpression base_getValueExpression() { return valExp_; }
  56. /**
  57. * To evaluate a table assignment, evaluate its table expression to a valid ATTable.
  58. * Next, evaluate its index expression into a valid number.
  59. * Finally, evaluate its value expression and assign the result to the proper index slot of the table.
  60. *
  61. * AGASSTABLE(tbl,idx,val).eval(ctx) =
  62. * tbl.eval(ctx).atPut(idx.eval(ctx), val.eval(ctx))
  63. *
  64. * @return the value stored in the table
  65. */
  66. public ATObject meta_eval(ATContext ctx) throws InterpreterException {
  67. ATTable tab = tblExp_.meta_eval(ctx).asTable();
  68. ATNumber idx = null;
  69. try {
  70. idx = idxExp_.meta_eval(ctx).asNumber();
  71. } catch (XTypeMismatch e) {
  72. throw new XIllegalIndex(e.getMessage());
  73. }
  74. ATObject val = valExp_.meta_eval(ctx);
  75. tab.base_atPut(idx, val);
  76. return val;
  77. }
  78. /**
  79. * Quoting a table assignment results in a new quoted table assignment.
  80. *
  81. * AGASSTABLE(tbl,idx,val).quote(ctx) = AGASSTABLE(tbl.quote(ctx),idx.quote(ctx),val.quote(ctx))
  82. */
  83. public ATObject meta_quote(ATContext ctx) throws InterpreterException {
  84. return new AGAssignTable(tblExp_.meta_quote(ctx).asExpression(),
  85. idxExp_.meta_quote(ctx).asExpression(),
  86. valExp_.meta_quote(ctx).asExpression());
  87. }
  88. public NATText meta_print() throws InterpreterException {
  89. return NATText.atValue(tblExp_.meta_print().javaValue + "[" +
  90. idxExp_.meta_print().javaValue + "] := " +
  91. valExp_.meta_print().javaValue);
  92. }
  93. }