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

http://ambienttalk.googlecode.com/ · Java · 93 lines · 38 code · 9 blank · 46 comment · 0 complexity · 92b7cf79f0ccd742b8c4a77026334fd2 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.exceptions.InterpreterException;
  30. import edu.vub.at.exceptions.XIllegalOperation;
  31. import edu.vub.at.objects.ATContext;
  32. import edu.vub.at.objects.ATObject;
  33. import edu.vub.at.objects.grammar.ATDefExternalField;
  34. import edu.vub.at.objects.grammar.ATExpression;
  35. import edu.vub.at.objects.grammar.ATSymbol;
  36. import edu.vub.at.objects.natives.NATText;
  37. /**
  38. * Represents the abstract grammar for defining external fields.
  39. *
  40. * @author tvcutsem
  41. */
  42. public class AGDefExternalField extends NATAbstractGrammar implements ATDefExternalField {
  43. private final ATSymbol rcvNam_;
  44. private final ATSymbol name_;
  45. private final ATExpression valueExp_;
  46. public AGDefExternalField(ATSymbol rcv, ATSymbol name, ATExpression value) {
  47. rcvNam_ = rcv;
  48. name_ = name;
  49. valueExp_ = value;
  50. }
  51. public ATSymbol base_receiver() { return rcvNam_; }
  52. public ATSymbol base_name() { return name_; }
  53. public ATExpression base_valueExpression() { return valueExp_; }
  54. /**
  55. * Defines a new field in the object denoted by the receiver symbol.
  56. * The return value is the value of the field.
  57. *
  58. * AGDEFEXTFLD(rcv,nam,val).eval(ctx) =
  59. * rcv.eval(ctx).addField(nam, val.eval(ctx))
  60. *
  61. * @throws XIllegalOperation if the receiver is an instance of a native type (whose field maps are sealed).
  62. */
  63. public ATObject meta_eval(ATContext ctx) throws InterpreterException {
  64. ATObject receiver = rcvNam_.meta_eval(ctx);
  65. ATObject val = valueExp_.meta_eval(ctx);
  66. receiver.meta_defineField(name_, val);
  67. return val;
  68. }
  69. /**
  70. * Quoting an external field definition results in a new quoted external field definition.
  71. *
  72. * AGDEFEXTFLD(rcv,nam,val).quote(ctx) = AGDEFEXTFLD(rcv.quote(ctx), nam.quote(ctx), val.quote(ctx))
  73. */
  74. public ATObject meta_quote(ATContext ctx) throws InterpreterException {
  75. return new AGDefExternalField(rcvNam_.meta_quote(ctx).asSymbol(),
  76. name_.meta_quote(ctx).asSymbol(),
  77. valueExp_.meta_quote(ctx).asExpression());
  78. }
  79. public NATText meta_print() throws InterpreterException {
  80. return NATText.atValue("def " + rcvNam_.meta_print().javaValue
  81. + "." + name_.meta_print().javaValue
  82. + " := " + valueExp_.meta_print().javaValue);
  83. }
  84. }