PageRenderTime 25ms CodeModel.GetById 11ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://ambienttalk.googlecode.com/
Java | 129 lines | 62 code | 15 blank | 52 comment | 0 complexity | 895a52ef7a8318b453f1a46190c4fa08 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * AGDefExternalField.java created on 16-nov-2006 at 8:31:45
  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.exceptions.InterpreterException;
 32import edu.vub.at.exceptions.XIllegalOperation;
 33import edu.vub.at.objects.ATContext;
 34import edu.vub.at.objects.ATObject;
 35import edu.vub.at.objects.grammar.ATDefExternalField;
 36import edu.vub.at.objects.grammar.ATDefinition;
 37import edu.vub.at.objects.grammar.ATExpression;
 38import edu.vub.at.objects.grammar.ATSymbol;
 39import edu.vub.at.objects.natives.NATText;
 40import edu.vub.util.TempFieldGenerator;
 41
 42import java.util.HashSet;
 43import java.util.Set;
 44
 45/**
 46 * Represents the abstract grammar for defining external fields.
 47 *   
 48 *  @author tvcutsem
 49 */
 50public class AGDefExternalField extends AGDefinition implements ATDefExternalField {
 51
 52	private final ATSymbol rcvNam_;
 53	private final ATSymbol name_;
 54	private final ATExpression valueExp_;
 55	
 56	public AGDefExternalField(ATSymbol rcv, ATSymbol name, ATExpression value) {
 57		rcvNam_ = rcv;
 58		name_ = name;
 59		valueExp_ = value;
 60	}
 61	
 62	public ATSymbol base_receiver() { return rcvNam_; }
 63	public ATSymbol base_name() { return name_; }
 64	public ATExpression base_valueExpression() { return valueExp_; }
 65	
 66	/**
 67	 * Defines a new field in the object denoted by the receiver symbol.
 68	 * The return value is the value of the field.
 69	 * 
 70	 * AGDEFEXTFLD(rcv,nam,val).eval(ctx) =
 71	 *   rcv.eval(ctx).addField(nam, val.eval(ctx))
 72	 *   
 73	 * @throws XIllegalOperation if the receiver is an instance of a native type (whose field maps are sealed).
 74	 */
 75	public ATObject meta_eval(ATContext ctx) throws InterpreterException {
 76		ATObject receiver = rcvNam_.meta_eval(ctx);
 77		ATObject val = valueExp_.meta_eval(ctx);
 78		receiver.meta_defineField(name_, val);
 79		return val;
 80	}
 81
 82	/**
 83	 * Quoting an external field definition results in a new quoted external field definition.
 84	 * 
 85	 * AGDEFEXTFLD(rcv,nam,val).quote(ctx) = AGDEFEXTFLD(rcv.quote(ctx), nam.quote(ctx), val.quote(ctx))
 86	 */
 87	public ATObject meta_quote(ATContext ctx) throws InterpreterException {
 88		return new AGDefExternalField(rcvNam_.meta_quote(ctx).asSymbol(),
 89									name_.meta_quote(ctx).asSymbol(),
 90									valueExp_.meta_quote(ctx).asExpression());
 91	}
 92	
 93	public NATText meta_print() throws InterpreterException {
 94		return NATText.atValue("def " + rcvNam_.meta_print().javaValue
 95				+ "." + name_.meta_print().javaValue
 96				+ " := " + valueExp_.meta_print().javaValue);
 97	}
 98	
 99	public NATText impl_asUnquotedCode(TempFieldGenerator objectMap) throws InterpreterException {
100		return NATText.atValue("def " + rcvNam_.impl_asUnquotedCode(objectMap).javaValue
101				+ "." + name_.impl_asUnquotedCode(objectMap).javaValue
102				+ " := " + valueExp_.impl_asUnquotedCode(objectMap).javaValue);
103	}
104	
105	/**
106	 * IV(def o.x := val) = { }
107	 */
108	public Set impl_introducedVariables() throws InterpreterException {
109		return new HashSet();
110	}
111
112	/**
113	 * FV(def o.x := val) = FV(val) U { o }
114	 */
115	public Set impl_freeVariables() throws InterpreterException {
116		Set fvValueExp = valueExp_.impl_freeVariables();
117		fvValueExp.add(rcvNam_);
118		return fvValueExp;
119	}
120	
121	
122	public Set impl_quotedFreeVariables() throws InterpreterException {
123		Set qfv = valueExp_.impl_quotedFreeVariables();
124		qfv.addAll(rcvNam_.impl_quotedFreeVariables());
125		qfv.addAll(name_.impl_quotedFreeVariables());
126		return qfv;
127	}
128	
129}