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

http://ambienttalk.googlecode.com/ · Java · 156 lines · 85 code · 35 blank · 36 comment · 8 complexity · ce6d7796bb42db689a5b5570d1d2bd37 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * NATPatternType.java created on Jul 4, 2008 at 10:21:18 AM
  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;
  29. import edu.vub.at.eval.Evaluator;
  30. import edu.vub.at.exceptions.InterpreterException;
  31. import edu.vub.at.exceptions.XArityMismatch;
  32. import edu.vub.at.exceptions.XIllegalArgument;
  33. import edu.vub.at.objects.ATClosure;
  34. import edu.vub.at.objects.ATContext;
  35. import edu.vub.at.objects.ATMethod;
  36. import edu.vub.at.objects.ATObject;
  37. import edu.vub.at.objects.ATTable;
  38. import edu.vub.at.objects.ATTypeTag;
  39. import edu.vub.at.objects.coercion.NativeTypeTags;
  40. import edu.vub.at.objects.grammar.ATSymbol;
  41. import edu.vub.at.objects.natives.grammar.AGApplication;
  42. import edu.vub.at.signals.natives.NATLiftedBehavior;
  43. /**
  44. * @author smostinc
  45. *
  46. */
  47. public class NATPatternType extends NATTypeTag implements ATClosure {
  48. ATTable parameters;
  49. public NATPatternType(AGApplication pattern) throws InterpreterException {
  50. super(pattern.base_function().asSymbol(), NATTable.EMPTY);
  51. parameters = pattern.base_arguments();
  52. }
  53. public NATPatternType(ATSymbol typeName, ATTable typeArgs) {
  54. super(typeName, NATTable.EMPTY);
  55. parameters = typeArgs;
  56. }
  57. public ATObject base_apply(ATTable theArguments) throws InterpreterException {
  58. boolean shouldBeLifted = false;
  59. ATObject[] argumentsArray = theArguments.asNativeTable().elements_;
  60. for (int argIndex = 0; argIndex < argumentsArray.length; argIndex++) {
  61. ATObject currentArgument = argumentsArray[argIndex];
  62. if (currentArgument.isBehavior()) {
  63. shouldBeLifted = true;
  64. break;
  65. }
  66. }
  67. if(shouldBeLifted) {
  68. return NATLiftedBehavior.fromClosure(this, argumentsArray);
  69. } // else
  70. NATObject thePatternObject = new NATObject(new ATTypeTag[] { this, NativeTypeTags._ISOLATE_ });
  71. ATObject[] parameters = this.parameters.asNativeTable().elements_;
  72. ATObject[] arguments = theArguments.asNativeTable().elements_;
  73. if(parameters.length != arguments.length)
  74. throw new XArityMismatch(typeName_.toString(), parameters.length, arguments.length);
  75. for (int i = 0; i < arguments.length; i++) {
  76. ATObject theValue = (ATObject) arguments[i];
  77. ATSymbol theName = (ATSymbol) parameters[i];
  78. thePatternObject.meta_defineField(theName, theValue);
  79. }
  80. return thePatternObject;
  81. }
  82. public ATObject base_unapply(ATObject thePatternObject) throws InterpreterException {
  83. if(! thePatternObject.meta_isTaggedAs(this).asNativeBoolean().javaValue)
  84. throw new XIllegalArgument("Asked to unapply " + this + " on an object which does not have the proper type tag");
  85. ATObject[] parameters = this.parameters.asNativeTable().elements_;
  86. ATObject[] arguments = new ATObject[parameters.length];
  87. for (int i = 0; i < arguments.length; i++) {
  88. ATSymbol theName = (ATSymbol) parameters[i];
  89. ATObject theValue = thePatternObject.meta_invokeField(thePatternObject, theName);
  90. arguments[i] = theValue;
  91. }
  92. return NATTable.atValue(arguments);
  93. }
  94. public ATTable meta_typeTags() throws InterpreterException {
  95. return NATTable.of(NativeTypeTags._TYPETAG_, NativeTypeTags._ISOLATE_, NativeTypeTags._CLOSURE_);
  96. }
  97. // BOGUS METHODS
  98. public ATObject base_applyInScope(ATTable args, ATObject scope)
  99. throws InterpreterException {
  100. // TODO Auto-generated method stub
  101. return null;
  102. }
  103. public ATContext base_context() throws InterpreterException {
  104. return new NATContext(Evaluator.getGlobalLexicalScope(), this);
  105. }
  106. public ATObject base_escape() throws InterpreterException {
  107. // TODO Auto-generated method stub
  108. return null;
  109. }
  110. public ATMethod base_method() throws InterpreterException {
  111. // TODO Auto-generated method stub
  112. return null;
  113. }
  114. public ATObject base_whileTrue_(ATClosure body) throws InterpreterException {
  115. // TODO Auto-generated method stub
  116. return null;
  117. }
  118. public ATClosure asClosure() throws InterpreterException {
  119. return this;
  120. }
  121. }