PageRenderTime 29ms CodeModel.GetById 15ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2-build060407/src/edu/vub/at/objects/natives/grammar/AGDefTable.java

http://ambienttalk.googlecode.com/
Java | 123 lines | 54 code | 14 blank | 55 comment | 1 complexity | 8805197d662b37b9a670028ea2541ab6 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.exceptions.InterpreterException;
 31import edu.vub.at.exceptions.XIllegalIndex;
 32import edu.vub.at.exceptions.XTypeMismatch;
 33import edu.vub.at.objects.ATContext;
 34import edu.vub.at.objects.ATObject;
 35import edu.vub.at.objects.grammar.ATBegin;
 36import edu.vub.at.objects.grammar.ATDefTable;
 37import edu.vub.at.objects.grammar.ATExpression;
 38import edu.vub.at.objects.grammar.ATSymbol;
 39import edu.vub.at.objects.natives.NATCallframe;
 40import edu.vub.at.objects.natives.NATTable;
 41import edu.vub.at.objects.natives.NATText;
 42
 43/**
 44 * @author tvc
 45 *
 46 * The native implementation of a table definition AG element.
 47 */
 48public final class AGDefTable extends NATAbstractGrammar implements ATDefTable {
 49
 50	private final ATSymbol tblName_;
 51	private final ATExpression sizExp_;
 52	private final ATBegin initExp_;
 53	
 54	public AGDefTable(ATSymbol nam, ATExpression siz, ATBegin ini) {
 55		tblName_ = nam;
 56		sizExp_ = siz;
 57		initExp_ = ini;
 58	}
 59	
 60	public ATSymbol base_getName() { return tblName_; }
 61
 62	public ATExpression base_getSizeExpression() { return sizExp_; }
 63
 64	public ATBegin base_getInitializer() { return initExp_; }
 65
 66	/**
 67	 * Defining a table requires evaluating its index expression to a size s,
 68	 * then allocating a new table of size s and finally initializing it
 69	 * by evaluating its initializer body s times. The return value is the defined table.
 70	 * 
 71	 * The initializer body is evaluated in a new scope, so definitions inside the initializer
 72	 * are not visible to the outside (table defining) scope.
 73	 * 
 74	 * AGDEFTABLE(nam,siz,ini).eval(ctx) =
 75	 *   s = siz.eval(ctx)
 76	 *   t[s] = nil
 77	 *   inictx = ctx[cur -> callframe(ctx.cur)]
 78	 *   i.from: 0 to: s do: {
 79	 *     t[i] := ini.eval(ctx)
 80	 *   }
 81	 *   ctx.scope.defineField(nam, AGTABLE(t))
 82	 *   AGTABLE(t)
 83	 */
 84	public ATObject meta_eval(ATContext ctx) throws InterpreterException {
 85		int siz = 0;
 86		try {
 87			siz = sizExp_.meta_eval(ctx).asNativeNumber().javaValue;
 88		} catch (XTypeMismatch e) {
 89			throw new XIllegalIndex(e.getMessage());
 90		}
 91		
 92		NATCallframe initscope = new NATCallframe(ctx.base_getLexicalScope());
 93		ATContext initctx = ctx.base_withLexicalEnvironment(initscope);
 94		ATObject[] tab = new ATObject[siz];
 95		for (int i = 0; i < tab.length; i++) {
 96			tab[i] = initExp_.meta_eval(initctx);
 97		}
 98		
 99		NATTable tbl = NATTable.atValue(tab);
100		ctx.base_getLexicalScope().meta_defineField(tblName_, tbl);
101		
102		return tbl;
103	}
104
105	/**
106	 * Quoting a table definition results in a new quoted table definition.
107	 * 
108	 * AGDEFTABLE(nam,siz,ini).quote(ctx) = AGDEFTABLE(nam.quote(ctx), siz.quote(ctx), ini.quote(ctx))
109	 */
110	public ATObject meta_quote(ATContext ctx) throws InterpreterException {
111		return new AGDefTable(tblName_.meta_quote(ctx).asSymbol(),
112				              sizExp_.meta_quote(ctx).asExpression(),
113				              initExp_.meta_quote(ctx).asBegin());
114	}
115	
116	public NATText meta_print() throws InterpreterException {
117		return NATText.atValue("def " +
118				tblName_.meta_print().javaValue + "[" +
119				sizExp_.meta_print().javaValue + "] { " +
120				initExp_.meta_print().javaValue + "}");
121	}
122
123}