PageRenderTime 23ms CodeModel.GetById 8ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://ambienttalk.googlecode.com/
Java | 141 lines | 81 code | 29 blank | 31 comment | 14 complexity | 9ca936569021db51712f7a39ae4bba84 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * AGCase.java created on Jul 6, 2008 at 12:31:50 PM
  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.exceptions.InterpreterException;
 31import edu.vub.at.exceptions.XArityMismatch;
 32import edu.vub.at.exceptions.XIllegalArgument;
 33import edu.vub.at.objects.ATAbstractGrammar;
 34import edu.vub.at.objects.ATBoolean;
 35import edu.vub.at.objects.ATContext;
 36import edu.vub.at.objects.ATObject;
 37import edu.vub.at.objects.ATTable;
 38import edu.vub.at.objects.ATTypeTag;
 39import edu.vub.at.objects.grammar.ATApplication;
 40import edu.vub.at.objects.grammar.ATAssignVariable;
 41import edu.vub.at.objects.grammar.ATClosureLiteral;
 42import edu.vub.at.objects.grammar.ATSymbol;
 43import edu.vub.at.objects.natives.grammar.NATAbstractGrammar;
 44
 45/**
 46 * @author smostinc
 47 *
 48 */
 49public class AGCase extends NATAbstractGrammar {
 50
 51	private ATSymbol		bindingForm_	= null;
 52	private ATSymbol		typeName_		= null;
 53	private ATTable			arguments_		= NATTable.EMPTY;
 54	
 55	private ATAbstractGrammar	pattern_;
 56	private ATClosureLiteral	consequence_;
 57	
 58	public AGCase(ATAbstractGrammar pattern, ATClosureLiteral consequence) throws InterpreterException {
 59		if (pattern instanceof ATAssignVariable) {
 60			ATAssignVariable assignment	= (ATAssignVariable) pattern;
 61			bindingForm_ 				= assignment.base_name();
 62			pattern						= assignment.base_valueExpression();
 63		}
 64		
 65		if(pattern instanceof ATSymbol) {
 66			typeName_	= pattern.asSymbol();
 67			
 68		} else if(pattern instanceof ATApplication) {
 69			ATApplication application	= (ATApplication) pattern;
 70
 71			typeName_	= application.base_function().asSymbol();
 72			arguments_	= application.base_arguments();
 73		}
 74
 75		pattern_		= pattern;
 76		consequence_	= consequence;
 77	}
 78	
 79	public NATText meta_print() throws InterpreterException {
 80		return NATText.atValue(
 81			"case " + (bindingForm_==null?"":(bindingForm_.toString() + " := ")) +
 82			pattern_.meta_print().javaValue + " => " + consequence_.meta_print().javaValue);
 83	}
 84	
 85	public ATBoolean base_isApplicable(ATContext theContext, ATObject theObjectToBeMatched) throws InterpreterException {
 86		ATTypeTag theTypeTag = theContext.base_lexicalScope().impl_callField(typeName_).asTypeTag();
 87		
 88		return theObjectToBeMatched.meta_isTaggedAs(theTypeTag);
 89	}
 90	
 91	public ATObject base_attemptToExecute(ATObject theObjectToBeMatched, ATContext theContext) throws InterpreterException {
 92		ATTypeTag theTypeTag = theContext.base_lexicalScope().impl_callField(typeName_).asTypeTag();
 93
 94		ATTable theArguments = NATTable.EMPTY;
 95		
 96		NATCallframe bindings = new NATCallframe(theContext.base_lexicalScope());
 97
 98		if(bindingForm_ != null)
 99			bindings.meta_defineField(bindingForm_, theObjectToBeMatched);
100		
101		if(theTypeTag instanceof NATPatternType) {
102			NATPatternType thePattern = (NATPatternType) theTypeTag;
103			
104			theArguments = thePattern.base_unapply(theObjectToBeMatched).asTable();
105		}
106		
107		ATObject[] parameters = arguments_.asNativeTable().elements_;
108		ATObject[] arguments  = theArguments.asNativeTable().elements_;
109		
110		if(parameters.length != arguments.length)
111			throw new XArityMismatch(theTypeTag.base_typeName().toString(), parameters.length, arguments.length);
112		
113		for (int i = 0; i < parameters.length; i++) {
114			ATObject parameter = parameters[i];
115			ATObject argument  = arguments[i];
116			
117			if(parameter instanceof ATSymbol) {
118				ATSymbol theVariableName = parameter.asSymbol();
119				
120				if(! bindings.hasLocalField(theVariableName)) {
121					bindings.meta_defineField((ATSymbol)parameter, argument);
122					continue;
123				} else {
124					parameter = bindings.getLocalField(theVariableName);
125				}
126			}
127			
128			if(/* parameter != argument */
129			   !parameter.base__opeql__opeql_(argument).asNativeBoolean().javaValue) {
130				throw new XIllegalArgument("No valid match since " + parameter + " != " + argument);
131			}
132		}
133		
134		
135		
136		NATContext theExtendedContext = new NATContext(bindings, theContext.base_receiver());
137		
138		return consequence_.meta_eval(theExtendedContext).asClosure().base_apply(NATTable.EMPTY);
139	}
140
141}