/interpreter/tags/at2dist220411/src/edu/vub/at/objects/natives/grammar/AGDefExternalField.java

http://ambienttalk.googlecode.com/ · Java · 129 lines · 62 code · 15 blank · 52 comment · 0 complexity · 895a52ef7a8318b453f1a46190c4fa08 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * AGDefExternalField.java created on 16-nov-2006 at 8:31: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.XIllegalOperation;
  32. import edu.vub.at.objects.ATContext;
  33. import edu.vub.at.objects.ATObject;
  34. import edu.vub.at.objects.grammar.ATDefExternalField;
  35. import edu.vub.at.objects.grammar.ATDefinition;
  36. import edu.vub.at.objects.grammar.ATExpression;
  37. import edu.vub.at.objects.grammar.ATSymbol;
  38. import edu.vub.at.objects.natives.NATText;
  39. import edu.vub.util.TempFieldGenerator;
  40. import java.util.HashSet;
  41. import java.util.Set;
  42. /**
  43. * Represents the abstract grammar for defining external fields.
  44. *
  45. * @author tvcutsem
  46. */
  47. public class AGDefExternalField extends AGDefinition implements ATDefExternalField {
  48. private final ATSymbol rcvNam_;
  49. private final ATSymbol name_;
  50. private final ATExpression valueExp_;
  51. public AGDefExternalField(ATSymbol rcv, ATSymbol name, ATExpression value) {
  52. rcvNam_ = rcv;
  53. name_ = name;
  54. valueExp_ = value;
  55. }
  56. public ATSymbol base_receiver() { return rcvNam_; }
  57. public ATSymbol base_name() { return name_; }
  58. public ATExpression base_valueExpression() { return valueExp_; }
  59. /**
  60. * Defines a new field in the object denoted by the receiver symbol.
  61. * The return value is the value of the field.
  62. *
  63. * AGDEFEXTFLD(rcv,nam,val).eval(ctx) =
  64. * rcv.eval(ctx).addField(nam, val.eval(ctx))
  65. *
  66. * @throws XIllegalOperation if the receiver is an instance of a native type (whose field maps are sealed).
  67. */
  68. public ATObject meta_eval(ATContext ctx) throws InterpreterException {
  69. ATObject receiver = rcvNam_.meta_eval(ctx);
  70. ATObject val = valueExp_.meta_eval(ctx);
  71. receiver.meta_defineField(name_, val);
  72. return val;
  73. }
  74. /**
  75. * Quoting an external field definition results in a new quoted external field definition.
  76. *
  77. * AGDEFEXTFLD(rcv,nam,val).quote(ctx) = AGDEFEXTFLD(rcv.quote(ctx), nam.quote(ctx), val.quote(ctx))
  78. */
  79. public ATObject meta_quote(ATContext ctx) throws InterpreterException {
  80. return new AGDefExternalField(rcvNam_.meta_quote(ctx).asSymbol(),
  81. name_.meta_quote(ctx).asSymbol(),
  82. valueExp_.meta_quote(ctx).asExpression());
  83. }
  84. public NATText meta_print() throws InterpreterException {
  85. return NATText.atValue("def " + rcvNam_.meta_print().javaValue
  86. + "." + name_.meta_print().javaValue
  87. + " := " + valueExp_.meta_print().javaValue);
  88. }
  89. public NATText impl_asUnquotedCode(TempFieldGenerator objectMap) throws InterpreterException {
  90. return NATText.atValue("def " + rcvNam_.impl_asUnquotedCode(objectMap).javaValue
  91. + "." + name_.impl_asUnquotedCode(objectMap).javaValue
  92. + " := " + valueExp_.impl_asUnquotedCode(objectMap).javaValue);
  93. }
  94. /**
  95. * IV(def o.x := val) = { }
  96. */
  97. public Set impl_introducedVariables() throws InterpreterException {
  98. return new HashSet();
  99. }
  100. /**
  101. * FV(def o.x := val) = FV(val) U { o }
  102. */
  103. public Set impl_freeVariables() throws InterpreterException {
  104. Set fvValueExp = valueExp_.impl_freeVariables();
  105. fvValueExp.add(rcvNam_);
  106. return fvValueExp;
  107. }
  108. public Set impl_quotedFreeVariables() throws InterpreterException {
  109. Set qfv = valueExp_.impl_quotedFreeVariables();
  110. qfv.addAll(rcvNam_.impl_quotedFreeVariables());
  111. qfv.addAll(name_.impl_quotedFreeVariables());
  112. return qfv;
  113. }
  114. }