source: branches/0.17.x/abcl/src/org/armedbear/lisp/Do.java

Last change on this file was 12254, checked in by ehuelsmann, 16 years ago

Remove 'throws ConditionThrowable?' method annotations:

it's an unchecked exception now, so no need to declare it thrown.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.7 KB
Line 
1/*
2 * Do.java
3 *
4 * Copyright (C) 2003-2006 Peter Graves
5 * $Id: Do.java 12254 2009-11-06 20:07:54Z ehuelsmann $
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 *
21 * As a special exception, the copyright holders of this library give you
22 * permission to link this library with independent modules to produce an
23 * executable, regardless of the license terms of these independent
24 * modules, and to copy and distribute the resulting executable under
25 * terms of your choice, provided that you also meet, for each linked
26 * independent module, the terms and conditions of the license of that
27 * module.  An independent module is a module which is not derived from
28 * or based on this library.  If you modify this library, you may extend
29 * this exception to your version of the library, but you are not
30 * obligated to do so.  If you do not wish to do so, delete this
31 * exception statement from your version.
32 */
33
34package org.armedbear.lisp;
35
36public final class Do extends Lisp
37{
38  // ### do
39  private static final SpecialOperator DO =
40    new SpecialOperator(Symbol.DO, "varlist endlist &body body")
41    {
42      @Override
43      public LispObject execute(LispObject args, Environment env)
44
45      {
46        return _do(args, env, false);
47      }
48    };
49
50  // ### do*
51  private static final SpecialOperator DO_STAR =
52    new SpecialOperator(Symbol.DO_STAR, "varlist endlist &body body")
53    {
54      @Override
55      public LispObject execute(LispObject args, Environment env)
56
57      {
58        return _do(args, env, true);
59      }
60    };
61
62  private static final LispObject _do(LispObject args, Environment env,
63                                      boolean sequential)
64
65  {
66    LispObject varlist = args.car();
67    LispObject second = args.cadr();
68    LispObject end_test_form = second.car();
69    LispObject result_forms = second.cdr();
70    LispObject body = args.cddr();
71    // Process variable specifications.
72    final int numvars = varlist.length();
73    Symbol[] vars = new Symbol[numvars];
74    LispObject[] initforms = new LispObject[numvars];
75    LispObject[] stepforms = new LispObject[numvars];
76    for (int i = 0; i < numvars; i++)
77      {
78        final LispObject varspec = varlist.car();
79        if (varspec instanceof Cons)
80          {
81            vars[i] = checkSymbol(varspec.car());
82            initforms[i] = varspec.cadr();
83            // Is there a step form?
84            if (varspec.cddr() != NIL)
85              stepforms[i] = varspec.caddr();
86          }
87        else
88          {
89            // Not a cons, must be a symbol.
90            vars[i] = checkSymbol(varspec);
91            initforms[i] = NIL;
92          }
93        varlist = varlist.cdr();
94      }
95    final LispThread thread = LispThread.currentThread();
96    final SpecialBinding lastSpecialBinding = thread.lastSpecialBinding;
97    // Process declarations.
98
99    final LispObject bodyAndDecls = parseBody(body, false);
100    LispObject specials = parseSpecials(bodyAndDecls.NTH(1));
101    body = bodyAndDecls.car();
102
103    Environment ext = new Environment(env);
104    for (int i = 0; i < numvars; i++)
105      {
106        Symbol var = vars[i];
107        LispObject value = eval(initforms[i], (sequential ? ext : env), thread);
108  ext = new Environment(ext);
109        if (specials != NIL && memq(var, specials))
110            thread.bindSpecial(var, value);
111        else if (var.isSpecialVariable())
112          thread.bindSpecial(var, value);
113        else
114          ext.bind(var, value);
115      }
116    LispObject list = specials;
117    while (list != NIL)
118      {
119        ext.declareSpecial(checkSymbol(list.car()));
120        list = list.cdr();
121      }
122    // Look for tags.
123    LispObject localTags = preprocessTagBody(body, ext);
124    LispObject blockId = new LispObject();
125    try
126      {
127        // Implicit block.
128        ext.addBlock(NIL, blockId);
129        while (true)
130          {
131            // Execute body.
132            // Test for termination.
133            if (eval(end_test_form, ext, thread) != NIL)
134              break;
135
136            processTagBody(body, localTags, ext);
137
138            // Update variables.
139            if (sequential)
140              {
141                for (int i = 0; i < numvars; i++)
142                  {
143                    LispObject step = stepforms[i];
144                    if (step != null)
145                      {
146                        Symbol symbol = vars[i];
147                        LispObject value = eval(step, ext, thread);
148                        if (symbol.isSpecialVariable()
149                            || ext.isDeclaredSpecial(symbol))
150                          thread.rebindSpecial(symbol, value);
151                        else
152                          ext.rebind(symbol, value);
153                      }
154                  }
155              }
156            else
157              {
158                // Evaluate step forms.
159                LispObject results[] = new LispObject[numvars];
160                for (int i = 0; i < numvars; i++)
161                  {
162                    LispObject step = stepforms[i];
163                    if (step != null)
164                      {
165                        LispObject result = eval(step, ext, thread);
166                        results[i] = result;
167                      }
168                  }
169                // Update variables.
170                for (int i = 0; i < numvars; i++)
171                  {
172                    if (results[i] != null)
173                      {
174                        Symbol symbol = vars[i];
175                        LispObject value = results[i];
176                        if (symbol.isSpecialVariable()
177                            || ext.isDeclaredSpecial(symbol))
178                          thread.rebindSpecial(symbol, value);
179                        else
180                          ext.rebind(symbol, value);
181                      }
182                  }
183              }
184            if (interrupted)
185              handleInterrupt();
186          }
187        LispObject result = progn(result_forms, ext, thread);
188        return result;
189      }
190    catch (Return ret)
191      {
192        if (ret.getBlock() == blockId)
193          {
194            return ret.getResult();
195          }
196        throw ret;
197      }
198    finally
199      {
200        thread.lastSpecialBinding = lastSpecialBinding;
201        ext.inactive = true;
202      }
203  }
204}
Note: See TracBrowser for help on using the repository browser.