source: branches/0.15.x/abcl/src/org/armedbear/lisp/dotimes.java

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

Factor out functions to separate declarations, the body and optionally the
documentation as well as to determine which variables have been declared
special.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.1 KB
Line 
1/*
2 * dotimes.java
3 *
4 * Copyright (C) 2003-2006 Peter Graves
5 * $Id: dotimes.java 11772 2009-04-20 20:21:37Z 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 dotimes extends SpecialOperator
37{
38  private dotimes()
39  {
40    super(Symbol.DOTIMES);
41  }
42
43  @Override
44  public LispObject execute(LispObject args, Environment env)
45    throws ConditionThrowable
46  {
47    LispObject bodyForm = args.cdr();
48    args = args.car();
49    Symbol var = checkSymbol(args.car());
50    LispObject countForm = args.cadr();
51    final LispThread thread = LispThread.currentThread();
52    LispObject resultForm = args.cdr().cdr().car();
53    SpecialBinding lastSpecialBinding = thread.lastSpecialBinding;
54    final LispObject stack = thread.getStack();
55
56    LispObject bodyAndDecls = parseBody(bodyForm, false);
57    LispObject specials = parseSpecials(bodyAndDecls.NTH(1));
58    bodyForm = bodyAndDecls.car();
59
60    try
61      {
62        LispObject limit = eval(countForm, env, thread);
63        Environment ext = new Environment(env);
64        LispObject localTags = NIL; // Tags that are local to this TAGBODY.
65        // Look for tags.
66        LispObject remaining = bodyForm;
67        while (remaining != NIL)
68          {
69            LispObject current = remaining.car();
70            remaining = remaining.cdr();
71            if (current instanceof Cons)
72              continue;
73            // It's a tag.
74            ext.addTagBinding(current, remaining);
75            localTags = new Cons(current, localTags);
76          }
77        // Implicit block.
78        ext.addBlock(NIL, new LispObject());
79        LispObject result;
80        // Establish a reusable binding.
81        final Object binding;
82        if (specials != NIL && memq(var, specials))
83          {
84            thread.bindSpecial(var, null);
85            binding = thread.getSpecialBinding(var);
86            ext.declareSpecial(var);
87          }
88        else if (var.isSpecialVariable())
89          {
90            thread.bindSpecial(var, null);
91            binding = thread.getSpecialBinding(var);
92          }
93        else
94          {
95            ext.bind(var, null);
96            binding = ext.getBinding(var);
97          }
98        while (specials != NIL)
99          {
100            ext.declareSpecial(checkSymbol(specials.car()));
101            specials = specials.cdr();
102          }
103        if (limit instanceof Fixnum)
104          {
105            int count = ((Fixnum)limit).value;
106            int i;
107            for (i = 0; i < count; i++)
108              {
109                if (binding instanceof SpecialBinding)
110                  ((SpecialBinding)binding).value = Fixnum.getInstance(i);
111                else
112                  ((Binding)binding).value = Fixnum.getInstance(i);
113                LispObject body = bodyForm;
114                while (body != NIL)
115                  {
116                    LispObject current = body.car();
117                    if (current instanceof Cons)
118                      {
119                        try
120                          {
121                            // Handle GO inline if possible.
122                            if (current.car() == Symbol.GO)
123                              {
124                                LispObject tag = current.cadr();
125                                if (memql(tag, localTags))
126                                  {
127                                    Binding b = ext.getTagBinding(tag);
128                                    if (b != null && b.value != null)
129                                      {
130                                        body = b.value;
131                                        continue;
132                                      }
133                                  }
134                                throw new Go(tag);
135                              }
136                            eval(current, ext, thread);
137                          }
138                        catch (Go go)
139                          {
140                            LispObject tag = go.getTag();
141                            if (memql(tag, localTags))
142                              {
143                                Binding b = ext.getTagBinding(tag);
144                                if (b != null && b.value != null)
145                                  {
146                                    body = b.value;
147                                    thread.setStack(stack);
148                                    continue;
149                                  }
150                              }
151                            throw go;
152                          }
153                      }
154                    body = body.cdr();
155                  }
156                if (interrupted)
157                  handleInterrupt();
158              }
159            if (binding instanceof SpecialBinding)
160              ((SpecialBinding)binding).value = Fixnum.getInstance(i);
161            else
162              ((Binding)binding).value = Fixnum.getInstance(i);
163            result = eval(resultForm, ext, thread);
164          }
165        else if (limit instanceof Bignum)
166          {
167            LispObject i = Fixnum.ZERO;
168            while (i.isLessThan(limit))
169              {
170                if (binding instanceof SpecialBinding)
171                  ((SpecialBinding)binding).value = i;
172                else
173                  ((Binding)binding).value = i;
174                LispObject body = bodyForm;
175                while (body != NIL)
176                  {
177                    LispObject current = body.car();
178                    if (current instanceof Cons)
179                      {
180                        try
181                          {
182                            // Handle GO inline if possible.
183                            if (current.car() == Symbol.GO)
184                              {
185                                LispObject tag = current.cadr();
186                                if (memql(tag, localTags))
187                                  {
188                                    Binding b = ext.getTagBinding(tag);
189                                    if (b != null && b.value != null)
190                                      {
191                                        body = b.value;
192                                        continue;
193                                      }
194                                  }
195                                throw new Go(tag);
196                              }
197                            eval(current, ext, thread);
198                          }
199                        catch (Go go)
200                          {
201                            LispObject tag = go.getTag();
202                            if (memql(tag, localTags))
203                              {
204                                Binding b = ext.getTagBinding(tag);
205                                if (b != null && b.value != null)
206                                  {
207                                    body = b.value;
208                                    thread.setStack(stack);
209                                    continue;
210                                  }
211                              }
212                            throw go;
213                          }
214                      }
215                    body = body.cdr();
216                  }
217                i = i.incr();
218                if (interrupted)
219                  handleInterrupt();
220              }
221            if (binding instanceof SpecialBinding)
222              ((SpecialBinding)binding).value = i;
223            else
224              ((Binding)binding).value = i;
225            result = eval(resultForm, ext, thread);
226          }
227        else
228          return error(new TypeError(limit, Symbol.INTEGER));
229        return result;
230      }
231    catch (Return ret)
232      {
233        if (ret.getTag() == NIL)
234          {
235            thread.setStack(stack);
236            return ret.getResult();
237          }
238        throw ret;
239      }
240    finally
241      {
242        thread.lastSpecialBinding = lastSpecialBinding;
243      }
244  }
245
246  private static final dotimes DOTIMES = new dotimes();
247}
Note: See TracBrowser for help on using the repository browser.