PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://ambienttalk.googlecode.com/
Java | 93 lines | 38 code | 9 blank | 46 comment | 0 complexity | 92b7cf79f0ccd742b8c4a77026334fd2 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.exceptions.InterpreterException;
31import edu.vub.at.exceptions.XIllegalOperation;
32import edu.vub.at.objects.ATContext;
33import edu.vub.at.objects.ATObject;
34import edu.vub.at.objects.grammar.ATDefExternalField;
35import edu.vub.at.objects.grammar.ATExpression;
36import edu.vub.at.objects.grammar.ATSymbol;
37import edu.vub.at.objects.natives.NATText;
38
39/**
40 * Represents the abstract grammar for defining external fields.
41 *   
42 *  @author tvcutsem
43 */
44public class AGDefExternalField extends NATAbstractGrammar implements ATDefExternalField {
45
46	private final ATSymbol rcvNam_;
47	private final ATSymbol name_;
48	private final ATExpression valueExp_;
49	
50	public AGDefExternalField(ATSymbol rcv, ATSymbol name, ATExpression value) {
51		rcvNam_ = rcv;
52		name_ = name;
53		valueExp_ = value;
54	}
55	
56	public ATSymbol base_receiver() { return rcvNam_; }
57	public ATSymbol base_name() { return name_; }
58	public ATExpression base_valueExpression() { return valueExp_; }
59	
60	/**
61	 * Defines a new field in the object denoted by the receiver symbol.
62	 * The return value is the value of the field.
63	 * 
64	 * AGDEFEXTFLD(rcv,nam,val).eval(ctx) =
65	 *   rcv.eval(ctx).addField(nam, val.eval(ctx))
66	 *   
67	 * @throws XIllegalOperation if the receiver is an instance of a native type (whose field maps are sealed).
68	 */
69	public ATObject meta_eval(ATContext ctx) throws InterpreterException {
70		ATObject receiver = rcvNam_.meta_eval(ctx);
71		ATObject val = valueExp_.meta_eval(ctx);
72		receiver.meta_defineField(name_, val);
73		return val;
74	}
75
76	/**
77	 * Quoting an external field definition results in a new quoted external field definition.
78	 * 
79	 * AGDEFEXTFLD(rcv,nam,val).quote(ctx) = AGDEFEXTFLD(rcv.quote(ctx), nam.quote(ctx), val.quote(ctx))
80	 */
81	public ATObject meta_quote(ATContext ctx) throws InterpreterException {
82		return new AGDefExternalField(rcvNam_.meta_quote(ctx).asSymbol(),
83									name_.meta_quote(ctx).asSymbol(),
84									valueExp_.meta_quote(ctx).asExpression());
85	}
86	
87	public NATText meta_print() throws InterpreterException {
88		return NATText.atValue("def " + rcvNam_.meta_print().javaValue
89				+ "." + name_.meta_print().javaValue
90				+ " := " + valueExp_.meta_print().javaValue);
91	}
92	
93}