PageRenderTime 19ms CodeModel.GetById 1ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 1ms

/interpreter/tags/at2dist220411/src/edu/vub/at/objects/natives/grammar/AGDefTable.java

http://ambienttalk.googlecode.com/
Java | 164 lines | 83 code | 20 blank | 61 comment | 1 complexity | bc64bd5e0cdbacdb0686c2d41b3cd999 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * AGDefTable.java created on 26-jul-2006 at 15:50: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.XIllegalIndex;
 33import edu.vub.at.exceptions.XTypeMismatch;
 34import edu.vub.at.objects.ATContext;
 35import edu.vub.at.objects.ATObject;
 36import edu.vub.at.objects.grammar.ATBegin;
 37import edu.vub.at.objects.grammar.ATDefTable;
 38import edu.vub.at.objects.grammar.ATDefinition;
 39import edu.vub.at.objects.grammar.ATExpression;
 40import edu.vub.at.objects.grammar.ATSymbol;
 41import edu.vub.at.objects.natives.NATCallframe;
 42import edu.vub.at.objects.natives.NATTable;
 43import edu.vub.at.objects.natives.NATText;
 44import edu.vub.util.TempFieldGenerator;
 45
 46import java.util.HashMap;
 47import java.util.HashSet;
 48import java.util.Set;
 49
 50/**
 51 * @author tvc
 52 *
 53 * The native implementation of a table definition AG element.
 54 */
 55public final class AGDefTable extends AGDefinition implements ATDefTable {
 56
 57	private final ATSymbol tblName_;
 58	private final ATExpression sizExp_;
 59	private final ATBegin initExp_;
 60	
 61	public AGDefTable(ATSymbol nam, ATExpression siz, ATBegin ini) {
 62		tblName_ = nam;
 63		sizExp_ = siz;
 64		initExp_ = ini;
 65	}
 66	
 67	public ATSymbol base_name() { return tblName_; }
 68
 69	public ATExpression base_sizeExpression() { return sizExp_; }
 70
 71	public ATBegin base_initializer() { return initExp_; }
 72
 73	/**
 74	 * Defining a table requires evaluating its index expression to a size s,
 75	 * then allocating a new table of size s and finally initializing it
 76	 * by evaluating its initializer body s times. The return value is the defined table.
 77	 * 
 78	 * The initializer body is evaluated in a new scope, so definitions inside the initializer
 79	 * are not visible to the outside (table defining) scope.
 80	 * 
 81	 * AGDEFTABLE(nam,siz,ini).eval(ctx) =
 82	 *   s = siz.eval(ctx)
 83	 *   t[s] = nil
 84	 *   inictx = ctx[cur -> callframe(ctx.cur)]
 85	 *   i.from: 0 to: s do: {
 86	 *     t[i] := ini.eval(ctx)
 87	 *   }
 88	 *   ctx.scope.defineField(nam, AGTABLE(t))
 89	 *   AGTABLE(t)
 90	 */
 91	public ATObject meta_eval(ATContext ctx) throws InterpreterException {
 92		int siz = 0;
 93		try {
 94			siz = sizExp_.meta_eval(ctx).asNativeNumber().javaValue;
 95		} catch (XTypeMismatch e) {
 96			throw new XIllegalIndex(e.getMessage());
 97		}
 98		
 99		NATCallframe initscope = new NATCallframe(ctx.base_lexicalScope());
100		ATContext initctx = ctx.base_withLexicalEnvironment(initscope);
101		ATObject[] tab = new ATObject[siz];
102		for (int i = 0; i < tab.length; i++) {
103			tab[i] = initExp_.meta_eval(initctx);
104		}
105		
106		NATTable tbl = NATTable.atValue(tab);
107		ctx.base_lexicalScope().meta_defineField(tblName_, tbl);
108		
109		return tbl;
110	}
111
112	/**
113	 * Quoting a table definition results in a new quoted table definition.
114	 * 
115	 * AGDEFTABLE(nam,siz,ini).quote(ctx) = AGDEFTABLE(nam.quote(ctx), siz.quote(ctx), ini.quote(ctx))
116	 */
117	public ATObject meta_quote(ATContext ctx) throws InterpreterException {
118		return new AGDefTable(tblName_.meta_quote(ctx).asSymbol(),
119				              sizExp_.meta_quote(ctx).asExpression(),
120				              initExp_.meta_quote(ctx).asBegin());
121	}
122	
123	public NATText meta_print() throws InterpreterException {
124		return NATText.atValue("def " +
125				tblName_.meta_print().javaValue + "[" +
126				sizExp_.meta_print().javaValue + "] { " +
127				initExp_.meta_print().javaValue + "}");
128	}
129	
130	public NATText impl_asUnquotedCode(TempFieldGenerator objectMap) throws InterpreterException {
131		return NATText.atValue("def " +
132				tblName_.impl_asUnquotedCode(objectMap).javaValue + "[" +
133				sizExp_.impl_asUnquotedCode(objectMap).javaValue + "] { " +
134				initExp_.impl_asUnquotedCode(objectMap).javaValue + "}");
135	}
136	
137	/**
138	 * IV(def t[idx] { body }) = { t }
139	 */
140	public Set impl_introducedVariables() throws InterpreterException {
141		Set singleton = new HashSet();
142		singleton.add(tblName_);
143		return singleton;
144	}
145
146	/**
147	 * FV(def t[idx] { body }) = FV(idx) U (FV(body) \ { t })
148	 */
149	public Set impl_freeVariables() throws InterpreterException {
150		Set fvBody = initExp_.impl_freeVariables();
151		fvBody.remove(tblName_);
152		fvBody.addAll(sizExp_.impl_freeVariables());
153		return fvBody;
154	}
155	
156	
157	public Set impl_quotedFreeVariables() throws InterpreterException {
158		Set qfv = sizExp_.impl_quotedFreeVariables();
159		qfv.addAll(initExp_.impl_quotedFreeVariables());
160		qfv.addAll(tblName_.impl_quotedFreeVariables());
161		return qfv;
162	}
163
164}