/interpreter/tags/at2-build060407/src/edu/vub/at/objects/natives/grammar/AGDefFunction.java
Java | 118 lines | 53 code | 11 blank | 54 comment | 2 complexity | 401c9c090cf851bb3b0bfa36ab08522c MD5 | raw file
1/** 2 * AmbientTalk/2 Project 3 * AGDefFunction.java created on 26-jul-2006 at 15:44:08 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.ATDefMethod; 37import edu.vub.at.objects.grammar.ATSymbol; 38import edu.vub.at.objects.natives.NATClosure; 39import edu.vub.at.objects.natives.NATMethod; 40import edu.vub.at.objects.natives.NATText; 41 42/** 43 * The native implementation of a function definition abstract grammar element. 44 * This AG element covers both method and closure definition. 45 * 46 * @author tvc 47 */ 48public final class AGDefFunction extends NATAbstractGrammar implements ATDefMethod { 49 50 private final ATSymbol selectorExp_; 51 private final ATTable argumentExps_; 52 private final ATBegin bodyStmts_; 53 54 public AGDefFunction(ATSymbol sel, ATTable args, ATBegin bdy) { 55 selectorExp_ = sel; 56 argumentExps_ = args; 57 bodyStmts_ = bdy; 58 } 59 60 public ATSymbol base_getSelector() { 61 return selectorExp_; 62 } 63 64 public ATTable base_getArguments() { 65 return argumentExps_; 66 } 67 68 public ATBegin base_getBodyExpression() { 69 return bodyStmts_; 70 } 71 72 /** 73 * Defines a new function (method or closure) in the current scope. 74 * If a function definition is executed in the context of an object, 75 * a new method is created. If, however, the function definition 76 * is executed in the context of a call frame (i.e. its definition 77 * is nested within a method/function) then a closure is created instead. 78 * 79 * The return value of a function definition is always a closure encapsulating the function. 80 * 81 * AGDEFFUN(nam,par,bdy).eval(ctx) = 82 * if ctx.scope.isCallFrame 83 * ctx.scope.defineField(nam, AGCLO(AGMTH(nam,par,bdy), ctx)) 84 * else 85 * ctx.scope.addMethod(nam, AGMTH(nam,par,bdy)) 86 * 87 */ 88 public ATObject meta_eval(ATContext ctx) throws InterpreterException { 89 ATObject current = ctx.base_getLexicalScope(); 90 if (current.isCallFrame()) { 91 NATClosure clo = new NATClosure(new NATMethod(selectorExp_, argumentExps_, bodyStmts_), ctx); 92 current.meta_defineField(selectorExp_, clo); 93 return clo; 94 } else { 95 current.meta_addMethod(new NATMethod(selectorExp_, argumentExps_, bodyStmts_)); 96 return current.meta_select(current, selectorExp_); 97 } 98 } 99 100 /** 101 * Quoting a function definition results in a new quoted function definition. 102 * 103 * AGDEFFUN(nam,par,bdy).quote(ctx) = AGDEFFUN(nam.quote(ctx), par.quote(ctx), bdy.quote(ctx)) 104 */ 105 public ATObject meta_quote(ATContext ctx) throws InterpreterException { 106 return new AGDefFunction(selectorExp_.meta_quote(ctx).asSymbol(), 107 argumentExps_.meta_quote(ctx).asTable(), 108 bodyStmts_.meta_quote(ctx).asBegin()); 109 } 110 111 public NATText meta_print() throws InterpreterException { 112 return NATText.atValue("def " + 113 selectorExp_.meta_print().javaValue + 114 Evaluator.printAsList(argumentExps_).javaValue + 115 " { " + bodyStmts_.meta_print().javaValue + " }"); 116 } 117 118}