PageRenderTime 20ms CodeModel.GetById 13ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist041108/src/edu/vub/at/objects/natives/grammar/AGMultiAssignment.java

http://ambienttalk.googlecode.com/
Java | 121 lines | 53 code | 16 blank | 52 comment | 6 complexity | 0c77da763fbf8b0add8442c290e5bfc4 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * AGMultiAssignment.java created on 18-aug-2006 at 9:31:38
  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.grammar;
 29
 30import edu.vub.at.eval.PartialBinder;
 31import edu.vub.at.exceptions.InterpreterException;
 32import edu.vub.at.objects.ATContext;
 33import edu.vub.at.objects.ATObject;
 34import edu.vub.at.objects.ATTable;
 35import edu.vub.at.objects.grammar.ATExpression;
 36import edu.vub.at.objects.grammar.ATMultiAssignment;
 37import edu.vub.at.objects.natives.NATText;
 38
 39import java.util.HashSet;
 40import java.util.Set;
 41
 42/**
 43 * @author tvc
 44 *
 45 * The native implementation of a multiple assignment AG element.
 46 */
 47public final class AGMultiAssignment extends NATAbstractGrammar implements ATMultiAssignment {
 48
 49	private final ATTable parameters_;
 50	private final ATExpression valueExp_;
 51	
 52	private final PartialBinder binderPartialFunction_;
 53	
 54	public AGMultiAssignment(ATTable par, ATExpression val) throws InterpreterException {
 55		parameters_ = par;
 56		valueExp_ = val;
 57		binderPartialFunction_ = PartialBinder.calculateResidual("multi-assignment", par);
 58	}
 59
 60	public ATTable base_parameters() { return parameters_; }
 61	
 62	public ATExpression base_valueExpression() { return valueExp_; }
 63	
 64	/**
 65	 * To evaluate a multiple assignment, evaluate the right hand side to a table
 66	 * and assign the parameters on the left hand side to the 'arguments' of the right hand side,
 67	 * almost as if they were bound during a function call (the parameters are assigned instead of defined).
 68	 * 
 69	 * AGMULTIASS(par,val).eval(ctx) = assign(ctx.scope, par, val.eval(ctx))
 70	 * 
 71	 * @return the evaluated arguments table
 72	 */
 73	public ATObject meta_eval(ATContext ctx) throws InterpreterException {
 74		ATTable args = valueExp_.meta_eval(ctx).asTable();
 75		PartialBinder.assignArgsToParams(binderPartialFunction_, ctx, args);
 76		return args;
 77	}
 78
 79	/**
 80	 * AGMULTIASS(par,val).quote(ctx) = AGMULTIASS(par.quote(ctx), val.quote(ctx))
 81	 */
 82	public ATObject meta_quote(ATContext ctx) throws InterpreterException {
 83		return new AGMultiAssignment(parameters_.meta_quote(ctx).asTable(), valueExp_.meta_quote(ctx).asExpression());
 84	}
 85	
 86	public NATText meta_print() throws InterpreterException {
 87		return NATText.atValue(parameters_.meta_print().javaValue + " := " + valueExp_.meta_print().javaValue);
 88	}
 89	
 90	/**
 91	 * FV([var1, var2 := exp1, @ rest] := exp2) =
 92	 *   { var1, var2, rest } U FV(exp1) U FV(exp2)
 93	 */
 94	public Set impl_freeVariables() throws InterpreterException {
 95        Set fvValExp = valueExp_.impl_freeVariables();
 96        
 97		ATObject[] params = parameters_.asNativeTable().elements_;
 98		for (int i = 0; i < params.length; i++) {
 99			if (params[i].isSymbol()) {
100				// Mandatory arguments, e.g. x
101				fvValExp.add(params[i].asSymbol());				
102			} else if (params[i].isVariableAssignment()) {
103				// Optional args or rest args
104				// Note: for optional args, both assigned var and free vars
105				// of the initialization expression are considered free vars
106				fvValExp.addAll(params[i].asExpression().impl_freeVariables());
107			} else if (params[i].isSplice()) {
108				fvValExp.add(params[i].asSplice().base_expression().asSymbol());
109			}
110		}
111        return fvValExp;
112	}
113	
114	
115	public Set impl_quotedFreeVariables() throws InterpreterException {
116		Set qfv = parameters_.impl_quotedFreeVariables();
117		qfv.addAll(valueExp_.impl_quotedFreeVariables());
118		return qfv;
119	}
120
121}