/interpreter/tags/at2-build060407/src/edu/vub/at/objects/natives/grammar/AGDefTable.java

http://ambienttalk.googlecode.com/ · Java · 123 lines · 54 code · 14 blank · 55 comment · 1 complexity · 8805197d662b37b9a670028ea2541ab6 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.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.ATObject;
  34. import edu.vub.at.objects.grammar.ATBegin;
  35. import edu.vub.at.objects.grammar.ATDefTable;
  36. import edu.vub.at.objects.grammar.ATExpression;
  37. import edu.vub.at.objects.grammar.ATSymbol;
  38. import edu.vub.at.objects.natives.NATCallframe;
  39. import edu.vub.at.objects.natives.NATTable;
  40. import edu.vub.at.objects.natives.NATText;
  41. /**
  42. * @author tvc
  43. *
  44. * The native implementation of a table definition AG element.
  45. */
  46. public final class AGDefTable extends NATAbstractGrammar implements ATDefTable {
  47. private final ATSymbol tblName_;
  48. private final ATExpression sizExp_;
  49. private final ATBegin initExp_;
  50. public AGDefTable(ATSymbol nam, ATExpression siz, ATBegin ini) {
  51. tblName_ = nam;
  52. sizExp_ = siz;
  53. initExp_ = ini;
  54. }
  55. public ATSymbol base_getName() { return tblName_; }
  56. public ATExpression base_getSizeExpression() { return sizExp_; }
  57. public ATBegin base_getInitializer() { return initExp_; }
  58. /**
  59. * Defining a table requires evaluating its index expression to a size s,
  60. * then allocating a new table of size s and finally initializing it
  61. * by evaluating its initializer body s times. The return value is the defined table.
  62. *
  63. * The initializer body is evaluated in a new scope, so definitions inside the initializer
  64. * are not visible to the outside (table defining) scope.
  65. *
  66. * AGDEFTABLE(nam,siz,ini).eval(ctx) =
  67. * s = siz.eval(ctx)
  68. * t[s] = nil
  69. * inictx = ctx[cur -> callframe(ctx.cur)]
  70. * i.from: 0 to: s do: {
  71. * t[i] := ini.eval(ctx)
  72. * }
  73. * ctx.scope.defineField(nam, AGTABLE(t))
  74. * AGTABLE(t)
  75. */
  76. public ATObject meta_eval(ATContext ctx) throws InterpreterException {
  77. int siz = 0;
  78. try {
  79. siz = sizExp_.meta_eval(ctx).asNativeNumber().javaValue;
  80. } catch (XTypeMismatch e) {
  81. throw new XIllegalIndex(e.getMessage());
  82. }
  83. NATCallframe initscope = new NATCallframe(ctx.base_getLexicalScope());
  84. ATContext initctx = ctx.base_withLexicalEnvironment(initscope);
  85. ATObject[] tab = new ATObject[siz];
  86. for (int i = 0; i < tab.length; i++) {
  87. tab[i] = initExp_.meta_eval(initctx);
  88. }
  89. NATTable tbl = NATTable.atValue(tab);
  90. ctx.base_getLexicalScope().meta_defineField(tblName_, tbl);
  91. return tbl;
  92. }
  93. /**
  94. * Quoting a table definition results in a new quoted table definition.
  95. *
  96. * AGDEFTABLE(nam,siz,ini).quote(ctx) = AGDEFTABLE(nam.quote(ctx), siz.quote(ctx), ini.quote(ctx))
  97. */
  98. public ATObject meta_quote(ATContext ctx) throws InterpreterException {
  99. return new AGDefTable(tblName_.meta_quote(ctx).asSymbol(),
  100. sizExp_.meta_quote(ctx).asExpression(),
  101. initExp_.meta_quote(ctx).asBegin());
  102. }
  103. public NATText meta_print() throws InterpreterException {
  104. return NATText.atValue("def " +
  105. tblName_.meta_print().javaValue + "[" +
  106. sizExp_.meta_print().javaValue + "] { " +
  107. initExp_.meta_print().javaValue + "}");
  108. }
  109. }