/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
- /**
- * AmbientTalk/2 Project
- * NATPatternType.java created on Jul 4, 2008 at 10:21:18 AM
- * (c) Programming Technology Lab, 2006 - 2007
- * Authors: Tom Van Cutsem & Stijn Mostinckx
- *
- * Permission is hereby granted, free of charge, to any person
- * obtaining a copy of this software and associated documentation
- * files (the "Software"), to deal in the Software without
- * restriction, including without limitation the rights to use,
- * copy, modify, merge, publish, distribute, sublicense, and/or
- * sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following
- * conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
- */
- package edu.vub.at.objects.natives;
- import edu.vub.at.eval.Evaluator;
- import edu.vub.at.exceptions.InterpreterException;
- import edu.vub.at.exceptions.XArityMismatch;
- import edu.vub.at.exceptions.XIllegalArgument;
- import edu.vub.at.objects.ATClosure;
- import edu.vub.at.objects.ATContext;
- import edu.vub.at.objects.ATMethod;
- import edu.vub.at.objects.ATObject;
- import edu.vub.at.objects.ATTable;
- import edu.vub.at.objects.ATTypeTag;
- import edu.vub.at.objects.coercion.NativeTypeTags;
- import edu.vub.at.objects.grammar.ATSymbol;
- import edu.vub.at.objects.natives.grammar.AGApplication;
- import edu.vub.at.signals.natives.NATLiftedBehavior;
- /**
- * @author smostinc
- *
- */
- public class NATPatternType extends NATTypeTag implements ATClosure {
- ATTable parameters;
-
- public NATPatternType(AGApplication pattern) throws InterpreterException {
- super(pattern.base_function().asSymbol(), NATTable.EMPTY);
-
- parameters = pattern.base_arguments();
- }
-
- public NATPatternType(ATSymbol typeName, ATTable typeArgs) {
- super(typeName, NATTable.EMPTY);
-
- parameters = typeArgs;
- }
-
- public ATObject base_apply(ATTable theArguments) throws InterpreterException {
- boolean shouldBeLifted = false;
- ATObject[] argumentsArray = theArguments.asNativeTable().elements_;
- for (int argIndex = 0; argIndex < argumentsArray.length; argIndex++) {
- ATObject currentArgument = argumentsArray[argIndex];
-
- if (currentArgument.isBehavior()) {
- shouldBeLifted = true;
- break;
- }
- }
- if(shouldBeLifted) {
- return NATLiftedBehavior.fromClosure(this, argumentsArray);
- } // else
- NATObject thePatternObject = new NATObject(new ATTypeTag[] { this, NativeTypeTags._ISOLATE_ });
-
- ATObject[] parameters = this.parameters.asNativeTable().elements_;
- ATObject[] arguments = theArguments.asNativeTable().elements_;
-
- if(parameters.length != arguments.length)
- throw new XArityMismatch(typeName_.toString(), parameters.length, arguments.length);
-
-
- for (int i = 0; i < arguments.length; i++) {
- ATObject theValue = (ATObject) arguments[i];
- ATSymbol theName = (ATSymbol) parameters[i];
-
- thePatternObject.meta_defineField(theName, theValue);
- }
-
- return thePatternObject;
-
- }
-
- public ATObject base_unapply(ATObject thePatternObject) throws InterpreterException {
- if(! thePatternObject.meta_isTaggedAs(this).asNativeBoolean().javaValue)
- throw new XIllegalArgument("Asked to unapply " + this + " on an object which does not have the proper type tag");
-
- ATObject[] parameters = this.parameters.asNativeTable().elements_;
- ATObject[] arguments = new ATObject[parameters.length];
-
- for (int i = 0; i < arguments.length; i++) {
- ATSymbol theName = (ATSymbol) parameters[i];
- ATObject theValue = thePatternObject.meta_invokeField(thePatternObject, theName);
-
- arguments[i] = theValue;
- }
-
- return NATTable.atValue(arguments);
-
- }
-
- public ATTable meta_typeTags() throws InterpreterException {
- return NATTable.of(NativeTypeTags._TYPETAG_, NativeTypeTags._ISOLATE_, NativeTypeTags._CLOSURE_);
- }
-
-
- // BOGUS METHODS
-
- public ATObject base_applyInScope(ATTable args, ATObject scope)
- throws InterpreterException {
- // TODO Auto-generated method stub
- return null;
- }
- public ATContext base_context() throws InterpreterException {
- return new NATContext(Evaluator.getGlobalLexicalScope(), this);
- }
- public ATObject base_escape() throws InterpreterException {
- // TODO Auto-generated method stub
- return null;
- }
- public ATMethod base_method() throws InterpreterException {
- // TODO Auto-generated method stub
- return null;
- }
- public ATObject base_whileTrue_(ATClosure body) throws InterpreterException {
- // TODO Auto-generated method stub
- return null;
- }
-
- public ATClosure asClosure() throws InterpreterException {
- return this;
- }
- }