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

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

Avoid stack save/restore operations in routines not modifying the stack.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.3 KB
Line 
1/*
2 * Do.java
3 *
4 * Copyright (C) 2003-2006 Peter Graves
5 * $Id: Do.java 12055 2009-07-24 19:24:12Z 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        throws ConditionThrowable
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        throws ConditionThrowable
57      {
58        return _do(args, env, true);
59      }
60    };
61
62  private static final LispObject _do(LispObject args, Environment env,
63                                      boolean sequential)
64    throws ConditionThrowable
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 remaining = body;
124    while (remaining != NIL)
125      {
126        LispObject current = remaining.car();
127        remaining = remaining.cdr();
128        if (current instanceof Cons)
129          continue;
130        // It's a tag.
131        ext.addTagBinding(current, remaining);
132      }
133    try
134      {
135        // Implicit block.
136        ext.addBlock(NIL, new LispObject());
137        while (true)
138          {
139            // Execute body.
140            // Test for termination.
141            if (eval(end_test_form, ext, thread) != NIL)
142              break;
143            remaining = body;
144            while (remaining != NIL)
145              {
146                LispObject current = remaining.car();
147                if (current instanceof Cons)
148                  {
149                    try
150                      {
151                        // Handle GO inline if possible.
152                        if (current.car() == Symbol.GO)
153                          {
154                            LispObject tag = current.cadr();
155                            Binding binding = ext.getTagBinding(tag);
156                            if (binding != null && binding.value != null)
157                              {
158                                remaining = binding.value;
159                                continue;
160                              }
161                            throw new Go(tag);
162                          }
163                        eval(current, ext, thread);
164                      }
165                    catch (Go go)
166                      {
167                        LispObject tag = go.getTag();
168                        Binding binding = ext.getTagBinding(tag);
169                        if (binding != null && binding.value != null)
170                          {
171                            remaining = binding.value;
172                            continue;
173                          }
174                        throw go;
175                      }
176                  }
177                remaining = remaining.cdr();
178              }
179            // Update variables.
180            if (sequential)
181              {
182                for (int i = 0; i < numvars; i++)
183                  {
184                    LispObject step = stepforms[i];
185                    if (step != null)
186                      {
187                        Symbol symbol = vars[i];
188                        LispObject value = eval(step, ext, thread);
189                        if (symbol.isSpecialVariable()
190                            || ext.isDeclaredSpecial(symbol))
191                          thread.rebindSpecial(symbol, value);
192                        else
193                          ext.rebind(symbol, value);
194                      }
195                  }
196              }
197            else
198              {
199                // Evaluate step forms.
200                LispObject results[] = new LispObject[numvars];
201                for (int i = 0; i < numvars; i++)
202                  {
203                    LispObject step = stepforms[i];
204                    if (step != null)
205                      {
206                        LispObject result = eval(step, ext, thread);
207                        results[i] = result;
208                      }
209                  }
210                // Update variables.
211                for (int i = 0; i < numvars; i++)
212                  {
213                    if (results[i] != null)
214                      {
215                        Symbol symbol = vars[i];
216                        LispObject value = results[i];
217                        if (symbol.isSpecialVariable()
218                            || ext.isDeclaredSpecial(symbol))
219                          thread.rebindSpecial(symbol, value);
220                        else
221                          ext.rebind(symbol, value);
222                      }
223                  }
224              }
225            if (interrupted)
226              handleInterrupt();
227          }
228        LispObject result = progn(result_forms, ext, thread);
229        return result;
230      }
231    catch (Return ret)
232      {
233        if (ret.getTag() == NIL)
234          {
235            return ret.getResult();
236          }
237        throw ret;
238      }
239    finally
240      {
241        thread.lastSpecialBinding = lastSpecialBinding;
242      }
243  }
244}
Note: See TracBrowser for help on using the repository browser.