/interpreter/tags/at2-build190607/src/edu/vub/at/objects/natives/grammar/AGDefFunction.java
http://ambienttalk.googlecode.com/ · Java · 134 lines · 62 code · 14 blank · 58 comment · 4 complexity · 0b56de332e0f5a336cf3efaa872d74d0 MD5 · raw file
- /**
- * AmbientTalk/2 Project
- * AGDefFunction.java created on 26-jul-2006 at 15:44:08
- * (c) Programming Technology Lab, 2006 - 2007
- * Authors: Tom Van Cutsem & Stijn Mostinckx
- *
- * Permission is hereby granted, free of charge, to any person
- * obtaining a copy of this software and associated documentation
- * files (the "Software"), to deal in the Software without
- * restriction, including without limitation the rights to use,
- * copy, modify, merge, publish, distribute, sublicense, and/or
- * sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following
- * conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
- */
- package edu.vub.at.objects.natives.grammar;
- import edu.vub.at.eval.Evaluator;
- import edu.vub.at.exceptions.InterpreterException;
- import edu.vub.at.objects.ATContext;
- import edu.vub.at.objects.ATObject;
- import edu.vub.at.objects.ATTable;
- import edu.vub.at.objects.coercion.NativeTypeTags;
- import edu.vub.at.objects.grammar.ATBegin;
- import edu.vub.at.objects.grammar.ATDefMethod;
- import edu.vub.at.objects.grammar.ATSymbol;
- import edu.vub.at.objects.natives.NATClosure;
- import edu.vub.at.objects.natives.NATMethod;
- import edu.vub.at.objects.natives.NATTable;
- import edu.vub.at.objects.natives.NATText;
- /**
- * The native implementation of a function definition abstract grammar element.
- * This AG element covers both method and closure definition.
- *
- * @author tvc
- */
- public final class AGDefFunction extends NATAbstractGrammar implements ATDefMethod {
- private final ATSymbol selectorExp_;
- private final ATTable argumentExps_;
- private final ATBegin bodyStmts_;
-
- private NATMethod preprocessedMethod_;
-
- public AGDefFunction(ATSymbol sel, ATTable args, ATBegin bdy) throws InterpreterException {
- selectorExp_ = sel;
- argumentExps_ = args;
- bodyStmts_ = bdy;
- }
-
- public ATSymbol base_getSelector() {
- return selectorExp_;
- }
- public ATTable base_getArguments() {
- return argumentExps_;
- }
- public ATBegin base_getBodyExpression() {
- return bodyStmts_;
- }
-
- /**
- * Defines a new function (method or closure) in the current scope.
- * If a function definition is executed in the context of an object,
- * a new method is created. If, however, the function definition
- * is executed in the context of a call frame (i.e. its definition
- * is nested within a method/function) then a closure is created instead.
- *
- * The return value of a function definition is always a closure encapsulating the function.
- *
- * AGDEFFUN(nam,par,bdy).eval(ctx) =
- * if ctx.scope.isCallFrame
- * ctx.scope.defineField(nam, AGCLO(AGMTH(nam,par,bdy), ctx))
- * else
- * ctx.scope.addMethod(nam, AGMTH(nam,par,bdy))
- *
- */
- public ATObject meta_eval(ATContext ctx) throws InterpreterException {
- // the method is not yet created in the constructor because this gives problems
- // with quoted parameters: a quoted parameter would result in an illegal parameter
- // exception while actually the function was defined in the context of a quotation,
- // so at runtime the function definition would have never been evaluated (but quoted instead)
- if (preprocessedMethod_ == null) {
- preprocessedMethod_ = new NATMethod(selectorExp_, argumentExps_, bodyStmts_);
- }
-
- ATObject current = ctx.base_getLexicalScope();
- if (current.isCallFrame()) {
- NATClosure clo = new NATClosure(preprocessedMethod_, ctx);
- current.meta_defineField(selectorExp_, clo);
- return clo;
- } else {
- current.meta_addMethod(preprocessedMethod_);
- return current.meta_select(current, selectorExp_);
- }
- }
- /**
- * Quoting a function definition results in a new quoted function definition.
- *
- * AGDEFFUN(nam,par,bdy).quote(ctx) = AGDEFFUN(nam.quote(ctx), par.quote(ctx), bdy.quote(ctx))
- */
- public ATObject meta_quote(ATContext ctx) throws InterpreterException {
- return new AGDefFunction(selectorExp_.meta_quote(ctx).asSymbol(),
- argumentExps_.meta_quote(ctx).asTable(),
- bodyStmts_.meta_quote(ctx).asBegin());
- }
-
- public NATText meta_print() throws InterpreterException {
- return NATText.atValue("def " +
- selectorExp_.meta_print().javaValue +
- Evaluator.printAsList(argumentExps_).javaValue +
- " { " + bodyStmts_.meta_print().javaValue + " }");
- }
-
- public ATTable meta_getTypeTags() throws InterpreterException {
- return NATTable.of(NativeTypeTags._METHOD_DEFINITION_);
- }
- }