/interpreter/tags/reactive-pattern-matching/src/edu/vub/at/objects/natives/grammar/AGDefType.java

http://ambienttalk.googlecode.com/ · Java · 127 lines · 65 code · 15 blank · 47 comment · 6 complexity · 8c0f62a63a8590072adf4fe300a88ec6 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.ATApplication;
  36. import edu.vub.at.objects.grammar.ATDefType;
  37. import edu.vub.at.objects.grammar.ATSymbol;
  38. import edu.vub.at.objects.mirrors.NativeClosure;
  39. import edu.vub.at.objects.natives.NATPatternType;
  40. import edu.vub.at.objects.natives.NATTable;
  41. import edu.vub.at.objects.natives.NATText;
  42. import edu.vub.at.objects.natives.NATTypeTag;
  43. /**
  44. * The native AST node for the code:
  45. *
  46. * deftype a <: b, c;
  47. *
  48. * @author tvcutsem
  49. */
  50. public final class AGDefType extends NATAbstractGrammar implements ATDefType {
  51. private final ATSymbol typeName_;
  52. private final ATTable typeArgs_;
  53. private final ATTable parentTypeExpressions_;
  54. public AGDefType(ATSymbol typeName, ATTable parentTypeExpressions) {
  55. typeName_ = typeName;
  56. typeArgs_ = NATTable.EMPTY;
  57. parentTypeExpressions_ = parentTypeExpressions;
  58. }
  59. public AGDefType(ATApplication typePattern, ATTable parentTypeExpressions) throws InterpreterException {
  60. typeName_ = typePattern.base_function().asSymbol();
  61. typeArgs_ = typePattern.base_arguments();
  62. parentTypeExpressions_ = parentTypeExpressions;
  63. }
  64. public ATTable base_parentTypeExpressions() {
  65. return parentTypeExpressions_;
  66. }
  67. public ATSymbol base_typeName() {
  68. return typeName_;
  69. }
  70. /**
  71. * Defines a new type in the current scope. The return value is
  72. * the new type.
  73. *
  74. * AGDEFTYPE(nam,parentExps).eval(ctx) =
  75. * ctx.scope.addField(nam, TYPETAG(nam, map eval(ctx) over parentExps))
  76. */
  77. public ATObject meta_eval(final ATContext ctx) throws InterpreterException {
  78. ATTypeTag type;
  79. // parentTypeExpressions_.map: { |parent| (reflect: parent).eval(ctx).base.asType() }
  80. ATTable parentTypes = parentTypeExpressions_.base_map_(new NativeClosure(this) {
  81. public ATObject base_apply(ATTable args) throws InterpreterException {
  82. return get(args,1).meta_eval(ctx).asTypeTag();
  83. }
  84. });
  85. if(typeArgs_ != NATTable.EMPTY) {
  86. type = new NATPatternType(typeName_, typeArgs_);
  87. } else {
  88. type = NATTypeTag.atValue(typeName_, parentTypes);
  89. }
  90. ctx.base_lexicalScope().meta_defineField(typeName_, type);
  91. return type;
  92. }
  93. /**
  94. * Quoting a type definition results in a new quoted type definition.
  95. *
  96. * AGDEFTYPE(nam,parentExps).quote(ctx) = AGDEFTYPE(nam.quote(ctx), parentExps.quote(ctx))
  97. */
  98. public ATObject meta_quote(ATContext ctx) throws InterpreterException {
  99. return new AGDefType(typeName_.meta_quote(ctx).asSymbol(), parentTypeExpressions_.meta_quote(ctx).asTable());
  100. }
  101. public NATText meta_print() throws InterpreterException {
  102. String thisType =
  103. "deftype " + typeName_.meta_print().javaValue +
  104. (typeArgs_==NATTable.EMPTY?"":(Evaluator.printElements(typeArgs_.asNativeTable(), "(", ", ", ")")).javaValue);
  105. if (parentTypeExpressions_ == NATTable.EMPTY) {
  106. return NATText.atValue(thisType);
  107. } else {
  108. return NATText.atValue(thisType + " <: " +
  109. Evaluator.printElements(parentTypeExpressions_.asNativeTable(), "", ",", "").javaValue);
  110. }
  111. }
  112. }