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

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

http://ambienttalk.googlecode.com/
Java | 142 lines | 75 code | 15 blank | 52 comment | 2 complexity | 7c97377295724fe63daace5564d5f088 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * AGClosureLiteral.java created on 26-jul-2006 at 16:59:04
  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.objects.ATContext;
 33import edu.vub.at.objects.ATObject;
 34import edu.vub.at.objects.ATTable;
 35import edu.vub.at.objects.grammar.ATBegin;
 36import edu.vub.at.objects.grammar.ATClosureLiteral;
 37import edu.vub.at.objects.mirrors.NativeClosure;
 38import edu.vub.at.objects.natives.NATClosure;
 39import edu.vub.at.objects.natives.NATMethod;
 40import edu.vub.at.objects.natives.NATTable;
 41import edu.vub.at.objects.natives.NATText;
 42import edu.vub.util.TempFieldGenerator;
 43
 44import java.util.Set;
 45
 46/**
 47 * The native implementation of a literal closure AG element.
 48 * 
 49 * @author tvc
 50 */
 51public final class AGClosureLiteral extends AGExpression implements ATClosureLiteral {
 52
 53	protected final ATTable arguments_;
 54	protected final ATBegin body_;
 55	
 56	private NATMethod preprocessedMethod_;
 57	
 58	public AGClosureLiteral(ATTable args, ATBegin body) throws InterpreterException {
 59		arguments_ = args;
 60		body_ = body;
 61	}
 62	
 63	public ATTable base_arguments() { return arguments_; }
 64
 65	public ATBegin base_bodyExpression() { return body_; }
 66	
 67	/**
 68	 * To evaluate a closure literal, construct a closure pairing the literal's arguments and body with the
 69	 * current evaluation context.
 70	 * 
 71	 * AGCLOLIT(arg,bdy).eval(ctx) = AGCLO(AGMTH("lambda",arg,bdy), ctx)
 72	 * 
 73	 * @return a native closure closing over the current evaluation context.
 74	 */
 75	public ATObject meta_eval(ATContext ctx) throws InterpreterException {
 76		// the method is not yet created in the constructor because this gives problems
 77		// with quoted parameters: a quoted parameter would result in an illegal parameter
 78		// exception while actually the block was defined in the context of a quotation,
 79		// so at runtime the block would have never been evaluated (but quoted instead)
 80		if (preprocessedMethod_ == null) {
 81			preprocessedMethod_ = new NATMethod(Evaluator._LAMBDA_, arguments_, body_, NATTable.EMPTY);
 82			preprocessedMethod_.impl_setLocation(this.impl_getLocation());
 83		}
 84		return new NATClosure(preprocessedMethod_, ctx);
 85	}
 86
 87	/**
 88	 * Quoting a closure literal results in a new quoted closure literal.
 89	 * 
 90	 * AGCLOLIT(arg,bdy).quote(ctx) = AGCLOLIT(arg.quote(ctx), bdy.quote(ctx))
 91	 */
 92	public ATObject meta_quote(ATContext ctx) throws InterpreterException {
 93		return new AGClosureLiteral(arguments_.meta_quote(ctx).asTable(),
 94				                   body_.meta_quote(ctx).asBegin());
 95	}
 96	
 97	public NATText meta_print() throws InterpreterException {
 98		return arguments_.base_isEmpty().base_ifTrue_ifFalse_(
 99				new NativeClosure(this) {
100					public ATObject base_apply(ATTable args) throws InterpreterException {
101						return NATText.atValue("{ "+ body_.meta_print().javaValue + " }");
102					}
103				},
104				new NativeClosure(this) {
105					public ATObject base_apply(ATTable args) throws InterpreterException {
106						  return NATText.atValue(Evaluator.printElements(arguments_.asNativeTable(), "{ |", ",", "| ").javaValue +
107			                       body_.meta_print().javaValue+"}");
108					}
109				}).asNativeText();
110	}
111	
112	public NATText impl_asUnquotedCode(final TempFieldGenerator objectMap) throws InterpreterException {
113		return arguments_.base_isEmpty().base_ifTrue_ifFalse_(
114				new NativeClosure(this) {
115					public ATObject base_apply(ATTable args) throws InterpreterException {
116						return NATText.atValue("{ "+ body_.impl_asUnquotedCode(objectMap).javaValue + " }");
117					}
118				},
119				new NativeClosure(this) {
120					public ATObject base_apply(ATTable args) throws InterpreterException {
121						  return NATText.atValue("{" + Evaluator.codeAsParameterList(objectMap, arguments_.asNativeTable()).javaValue +
122			                       body_.impl_asUnquotedCode(objectMap).javaValue+"}");
123					}
124				}).asNativeText();
125	}
126	
127	/**
128	 * FV({ |args| body }) = FV(optionalArgExps) U (FV(body) \ { args })
129	 */
130	public Set impl_freeVariables() throws InterpreterException {
131		Set fvBody = body_.impl_freeVariables();
132		Evaluator.processFreeVariables(fvBody, arguments_);
133		return fvBody;
134	}
135	
136	public Set impl_quotedFreeVariables() throws InterpreterException {
137		Set qfv = arguments_.impl_quotedFreeVariables();
138		qfv.addAll(body_.impl_quotedFreeVariables());
139		return qfv;
140	}
141	
142}