/interpreter/tags/at2-build060407/src/edu/vub/at/objects/natives/grammar/AGBegin.java
Java | 93 lines | 34 code | 10 blank | 49 comment | 1 complexity | ee7695f2fb5fa6443b6d06ac734f7429 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.ATContext; 34import edu.vub.at.objects.ATObject; 35import edu.vub.at.objects.ATTable; 36import edu.vub.at.objects.grammar.ATBegin; 37import edu.vub.at.objects.natives.NATNumber; 38import edu.vub.at.objects.natives.NATText; 39 40/** 41 * AGBegin represents the abstract grammar element of a list of statements. 42 * Examples: 43 * <tt>a ; b ; c</tt> 44 * 45 * @author tvcutsem 46 */ 47public final class AGBegin extends NATAbstractGrammar implements ATBegin { 48 49 private final ATTable statements_; 50 51 public AGBegin(ATTable statements) { 52 statements_ = statements; 53 } 54 55 /** 56 * AGBEGIN encapsulates a sequence of statements and evaluates each of these statements in turn. 57 * The return value is the value of the last statement. 58 * 59 * AGBEGIN([statement | statements]).eval(ctx) = statement.eval(ctx) ; BEGIN(statements).eval(ctx) 60 * AGBEGIN([statement]).eval(ctx) = statement.eval(ctx) 61 * 62 * Note that this implementation of begin is *not* tail-recursive. 63 * Tail-recursion is made impossible because Java will not allow a return 64 * to the caller until the last expression is also evaluated. Tail-recursion optimalisation 65 * requires a minimal form of explicit continuations. 66 */ 67 public ATObject meta_eval(ATContext ctx) throws InterpreterException { 68 NATNumber siz = statements_.base_getLength().asNativeNumber(); 69 int lastIdx = siz.javaValue; 70 for (int i = 1; i < lastIdx; i++) { 71 statements_.base_at(NATNumber.atValue(i)).meta_eval(ctx); 72 } 73 return statements_.base_at(NATNumber.atValue(lastIdx)).meta_eval(ctx); 74 } 75 76 /** 77 * AGBEGIN(statements*).quote(ctx) = AGBEGIN((statements*).quote(ctx)) 78 */ 79 public ATObject meta_quote(ATContext ctx) throws InterpreterException { 80 return new AGBegin(statements_.meta_quote(ctx).asTable()); 81 } 82 83 public ATTable base_getStatements() { return statements_; } 84 85 public NATText meta_print() throws InterpreterException { 86 return Evaluator.printAsStatements(statements_); 87 } 88 89 public ATBegin asBegin() throws XTypeMismatch { 90 return this; 91 } 92 93}