PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://ambienttalk.googlecode.com/
Java | 142 lines | 72 code | 16 blank | 54 comment | 6 complexity | 3c61c4fdb6dde5cc0f218304cf781db3 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * AGBegin.java created on 26-jul-2006 at 12:26:48
  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.XTypeMismatch;
 33import edu.vub.at.objects.ATAbstractGrammar;
 34import edu.vub.at.objects.ATContext;
 35import edu.vub.at.objects.ATObject;
 36import edu.vub.at.objects.ATTable;
 37import edu.vub.at.objects.grammar.ATBegin;
 38import edu.vub.at.objects.grammar.ATStatement;
 39import edu.vub.at.objects.mirrors.NativeClosure;
 40import edu.vub.at.objects.natives.NATNumber;
 41import edu.vub.at.objects.natives.NATText;
 42import edu.vub.util.TempFieldGenerator;
 43
 44import java.util.HashMap;
 45import java.util.HashSet;
 46import java.util.Set;
 47
 48/**
 49 * AGBegin represents the abstract grammar element of a list of statements.
 50 * Examples:
 51 *  <tt>a ; b ; c</tt>
 52 *  
 53 * @author tvcutsem
 54 */
 55public final class AGBegin extends NATAbstractGrammar implements ATBegin {
 56	
 57	private final ATTable statements_;
 58	
 59	// contains a cached version of the expression's free variables
 60	private Set freeVars_;
 61	
 62	public AGBegin(ATTable statements) {
 63	  statements_ = statements;
 64	}
 65	
 66	/**
 67	 * AGBEGIN encapsulates a sequence of statements and evaluates each of these statements in turn.
 68	 * The return value is the value of the last statement.
 69	 * 
 70	 * AGBEGIN([statement | statements]).eval(ctx) = statement.eval(ctx) ; BEGIN(statements).eval(ctx)
 71	 * AGBEGIN([statement]).eval(ctx) = statement.eval(ctx)
 72	 * AGBEGIN([]).eval(ctx) = nil
 73	 * 
 74	 * Note that this implementation of begin is *not* tail-recursive.
 75	 * Tail-recursion is made impossible because Java will not allow a return
 76	 * to the caller until the last expression is also evaluated. Tail-recursion optimalisation
 77	 * requires a minimal form of explicit continuations.
 78	 */
 79	public ATObject meta_eval(ATContext ctx) throws InterpreterException {
 80		NATNumber siz = statements_.base_length().asNativeNumber();
 81		int lastIdx = siz.javaValue;
 82		if (lastIdx == 0) {
 83			return Evaluator.getNil();
 84		}
 85		for (int i = 1; i < lastIdx; i++) {
 86			statements_.base_at(NATNumber.atValue(i)).meta_eval(ctx);
 87		}
 88		return statements_.base_at(NATNumber.atValue(lastIdx)).meta_eval(ctx);
 89	}
 90
 91	/**
 92	 * AGBEGIN(statements*).quote(ctx) = AGBEGIN((statements*).quote(ctx))
 93	 */
 94	public ATObject meta_quote(ATContext ctx) throws InterpreterException {
 95		return new AGBegin(statements_.meta_quote(ctx).asTable());
 96	}
 97	
 98	public ATTable base_statements() { return statements_; }
 99	
100	public NATText meta_print() throws InterpreterException {
101		return Evaluator.printAsStatements(statements_);
102	}
103	
104	public NATText impl_asUnquotedCode(TempFieldGenerator objectMap) throws InterpreterException {
105		objectMap.incQuoteLevel();
106		NATText code = Evaluator.codeAsStatements(objectMap, statements_);
107		objectMap.decQuoteLevel();
108		return NATText.atValue("{" + code.javaValue + "}");
109	}
110	
111	public ATBegin asBegin() throws XTypeMismatch {
112		return this;
113	}
114	
115	/**
116	 * FV({ stmt1; stmt2; ... }) = FV(stmt1) U FV(stmt2) U ... \ (IV(stmt1) U IV(stmt2) U ...) }
117	 */
118	public Set impl_freeVariables() throws InterpreterException {
119		if (freeVars_ == null) {
120			freeVars_ = new HashSet();
121			final Set boundVars = new HashSet();
122			statements_.base_each_(new NativeClosure(this) {
123				public ATObject base_apply(ATTable args) throws InterpreterException {
124					ATAbstractGrammar stmt = this.get(args, 1).asAbstractGrammar();
125					freeVars_.addAll(stmt.impl_freeVariables());
126					if (stmt.isDefinition()) {
127						boundVars.addAll(stmt.asDefinition().impl_introducedVariables());					
128					}
129					return stmt;
130				}
131			});
132			freeVars_.removeAll(boundVars);
133		}
134		return freeVars_;
135	}
136	
137	
138	public Set impl_quotedFreeVariables() throws InterpreterException {
139		return statements_.impl_quotedFreeVariables();
140	}
141
142}