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

http://ambienttalk.googlecode.com/ · Java · 147 lines · 76 code · 18 blank · 53 comment · 6 complexity · 759ae7705c7e7a0c8d533ab2de27c3df MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * AGDefType.java created on 18-feb-2007 at 14:09:27
  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.objects.ATContext;
  32. import edu.vub.at.objects.ATObject;
  33. import edu.vub.at.objects.ATTable;
  34. import edu.vub.at.objects.ATTypeTag;
  35. import edu.vub.at.objects.grammar.ATDefType;
  36. import edu.vub.at.objects.grammar.ATDefinition;
  37. import edu.vub.at.objects.grammar.ATSymbol;
  38. import edu.vub.at.objects.mirrors.NativeClosure;
  39. import edu.vub.at.objects.natives.NATTable;
  40. import edu.vub.at.objects.natives.NATText;
  41. import edu.vub.at.objects.natives.NATTypeTag;
  42. import edu.vub.util.TempFieldGenerator;
  43. import java.util.HashSet;
  44. import java.util.Set;
  45. /**
  46. * The native AST node for the code:
  47. *
  48. * deftype a <: b, c;
  49. *
  50. * @author tvcutsem
  51. */
  52. public final class AGDefType extends AGDefinition implements ATDefType {
  53. private final ATSymbol typeName_;
  54. private final ATTable parentTypeExpressions_;
  55. public AGDefType(ATSymbol typeName, ATTable parentTypeExpressions) {
  56. typeName_ = typeName;
  57. parentTypeExpressions_ = parentTypeExpressions;
  58. }
  59. public ATTable base_parentTypeExpressions() {
  60. return parentTypeExpressions_;
  61. }
  62. public ATSymbol base_typeName() {
  63. return typeName_;
  64. }
  65. /**
  66. * Defines a new type in the current scope. The return value is
  67. * the new type.
  68. *
  69. * AGDEFTYPE(nam,parentExps).eval(ctx) =
  70. * ctx.scope.addField(nam, TYPETAG(nam, map eval(ctx) over parentExps))
  71. */
  72. public ATObject meta_eval(final ATContext ctx) throws InterpreterException {
  73. // parentTypeExpressions_.map: { |parent| (reflect: parent).eval(ctx).base.asType() }
  74. ATTable parentTypes = parentTypeExpressions_.base_map_(new NativeClosure(this) {
  75. public ATObject base_apply(ATTable args) throws InterpreterException {
  76. return get(args,1).meta_eval(ctx).asTypeTag();
  77. }
  78. });
  79. ATTypeTag type = NATTypeTag.atValue(typeName_, parentTypes);
  80. ctx.base_lexicalScope().meta_defineField(typeName_, type);
  81. return type;
  82. }
  83. /**
  84. * Quoting a type definition results in a new quoted type definition.
  85. *
  86. * AGDEFTYPE(nam,parentExps).quote(ctx) = AGDEFTYPE(nam.quote(ctx), parentExps.quote(ctx))
  87. */
  88. public ATObject meta_quote(ATContext ctx) throws InterpreterException {
  89. return new AGDefType(typeName_.meta_quote(ctx).asSymbol(), parentTypeExpressions_.meta_quote(ctx).asTable());
  90. }
  91. public NATText meta_print() throws InterpreterException {
  92. if (parentTypeExpressions_ == NATTable.EMPTY) {
  93. return NATText.atValue("deftype " + typeName_.meta_print().javaValue);
  94. } else {
  95. return NATText.atValue("deftype " + typeName_.meta_print().javaValue + " <: " +
  96. Evaluator.printElements(parentTypeExpressions_.asNativeTable(), "", ",", "").javaValue);
  97. }
  98. }
  99. public NATText impl_asUnquotedCode(TempFieldGenerator objectMap) throws InterpreterException {
  100. if (parentTypeExpressions_ == NATTable.EMPTY) {
  101. return NATText.atValue("deftype " + typeName_.impl_asUnquotedCode(objectMap).javaValue);
  102. } else {
  103. return NATText.atValue("deftype " + typeName_.impl_asUnquotedCode(objectMap).javaValue + " <: " +
  104. Evaluator.codeElements(objectMap, parentTypeExpressions_.asNativeTable(), "", ",", "").javaValue);
  105. }
  106. }
  107. public ATDefinition asDefinition() { return this; }
  108. /**
  109. * IV(deftype nam <: exps) = { T1 }
  110. */
  111. public Set impl_introducedVariables() throws InterpreterException {
  112. Set singleton = new HashSet();
  113. singleton.add(typeName_);
  114. return singleton;
  115. }
  116. /**
  117. * FV(deftype nam <: exps) = FV(exps) \ { nam }
  118. */
  119. public Set impl_freeVariables() throws InterpreterException {
  120. Set fvParentType = parentTypeExpressions_.impl_freeVariables();
  121. fvParentType.remove(typeName_);
  122. return fvParentType;
  123. }
  124. public Set impl_quotedFreeVariables() throws InterpreterException {
  125. Set qfv = parentTypeExpressions_.impl_quotedFreeVariables();
  126. qfv.addAll(typeName_.impl_quotedFreeVariables());
  127. return qfv;
  128. }
  129. }