PageRenderTime 30ms CodeModel.GetById 13ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

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