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