PageRenderTime 18ms CodeModel.GetById 11ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist110511/src/edu/vub/at/objects/natives/grammar/AGMultiDefinition.java

http://ambienttalk.googlecode.com/
Java | 133 lines | 64 code | 16 blank | 53 comment | 6 complexity | f0e63357d0ccddc92b511d15fc3b3525 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * AGMultiDefinition.java created on 18-aug-2006 at 10:13:58
  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.Evaluator;
 31import edu.vub.at.eval.PartialBinder;
 32import edu.vub.at.exceptions.InterpreterException;
 33import edu.vub.at.objects.ATContext;
 34import edu.vub.at.objects.ATObject;
 35import edu.vub.at.objects.ATTable;
 36import edu.vub.at.objects.grammar.ATDefinition;
 37import edu.vub.at.objects.grammar.ATExpression;
 38import edu.vub.at.objects.grammar.ATMultiDefinition;
 39import edu.vub.at.objects.natives.NATText;
 40import edu.vub.util.TempFieldGenerator;
 41
 42import java.util.HashSet;
 43import java.util.Set;
 44
 45/**
 46 * @author tvc
 47 *
 48 * The native implementation of a multiple definition AG element.
 49 */
 50public class AGMultiDefinition extends AGDefinition implements ATMultiDefinition {
 51
 52	private final ATTable parameters_;
 53	private final ATExpression valueExp_;
 54	
 55	private final PartialBinder binderPartialFunction_;
 56	
 57	public AGMultiDefinition(ATTable par, ATExpression val) throws InterpreterException {
 58		parameters_ = par;
 59		valueExp_ = val;
 60		binderPartialFunction_ = PartialBinder.calculateResidual("multi-definition", par);
 61	}
 62
 63	public ATTable base_parameters() { return parameters_; }
 64	
 65	public ATExpression base_valueExpression() { return valueExp_; }
 66	
 67	/**
 68	 * To evaluate a multiple definition, evaluate the right hand side to a table
 69	 * and bind the parameters on the left hand side to the 'arguments' of the right hand side,
 70	 * exactly as if they were bound during a function call.
 71	 * 
 72	 * AGMULTIDEF(par,val).eval(ctx) = bind(ctx.scope, par, val.eval(ctx))
 73	 * 
 74	 * @return the evaluated arguments table
 75	 */
 76	public ATObject meta_eval(ATContext ctx) throws InterpreterException {
 77		ATTable args = valueExp_.meta_eval(ctx).asTable();
 78		PartialBinder.defineParamsForArgs(binderPartialFunction_, ctx, args);
 79		return args;
 80	}
 81
 82	/**
 83	 * AGMULTIDEF(par,val).quote(ctx) = AGMULTIDEF(par.quote(ctx), val.quote(ctx))
 84	 */
 85	public ATObject meta_quote(ATContext ctx) throws InterpreterException {
 86		return new AGMultiDefinition(parameters_.meta_quote(ctx).asTable(), valueExp_.meta_quote(ctx).asExpression());
 87	}
 88	
 89	public NATText meta_print() throws InterpreterException {
 90		return NATText.atValue("def " + parameters_.meta_print().javaValue + " := " + valueExp_.meta_print().javaValue);
 91	}
 92	
 93	public NATText impl_asUnquotedCode(TempFieldGenerator objectMap) throws InterpreterException {
 94		return NATText.atValue("def " + parameters_.impl_asUnquotedCode(objectMap).javaValue + " := " + valueExp_.impl_asUnquotedCode(objectMap).javaValue);
 95	}
 96
 97	/**
 98	 * IV(def [v1, v2 := exp, @ rest] := exp) = { v1, v2, rest }
 99	 */
100	public Set impl_introducedVariables() throws InterpreterException {
101		Set introducedVariables = new HashSet();
102		ATObject[] params = parameters_.asNativeTable().elements_;
103		for (int i = 0; i < params.length; i++) {
104			if (params[i].isSymbol()) {
105				// Mandatory arguments, e.g. x
106				introducedVariables.add(params[i].asSymbol());
107			} else if (params[i].isVariableAssignment()) {
108				// Optional arguments, e.g. x := 5
109				introducedVariables.add(params[i].asVariableAssignment().base_name());
110			} else if (params[i].isSplice()) {
111				// Rest arguments, e.g. @x
112				introducedVariables.add(params[i].asSplice().base_expression().asSymbol());
113			}
114		}
115		return introducedVariables;
116	}
117
118	/**
119	 * FV(def [v1, v2 := exp1, @ rest] := exp2) = FV(exp1) U (FV(exp2) \ { f1,f2 })
120	 */
121	public Set impl_freeVariables() throws InterpreterException {
122		Set fvValueExp = valueExp_.impl_freeVariables();
123		Evaluator.processFreeVariables(fvValueExp, parameters_);
124		return fvValueExp;
125	}
126	
127	public Set impl_quotedFreeVariables() throws InterpreterException {
128		Set qfv = parameters_.impl_quotedFreeVariables();
129		qfv.addAll(valueExp_.impl_quotedFreeVariables());
130		return qfv;
131	}
132
133}