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

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