source: branches/0.17.x/abcl/src/org/armedbear/lisp/dotimes.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: 5.1 KB
Line 
1/*
2 * dotimes.java
3 *
4 * Copyright (C) 2003-2006 Peter Graves
5 * $Id: dotimes.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 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
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
55    LispObject bodyAndDecls = parseBody(bodyForm, false);
56    LispObject specials = parseSpecials(bodyAndDecls.NTH(1));
57    bodyForm = bodyAndDecls.car();
58
59    LispObject blockId = new LispObject();
60    final Environment ext = new Environment(env);
61    try
62      {
63        ext.addBlock(NIL, blockId);
64
65        LispObject limit = eval(countForm, ext, thread);
66        LispObject localTags = preprocessTagBody(bodyForm, ext);
67
68        LispObject result;
69        // Establish a reusable binding.
70        final Object binding;
71        if (specials != NIL && memq(var, specials))
72          {
73            thread.bindSpecial(var, null);
74            binding = thread.getSpecialBinding(var);
75            ext.declareSpecial(var);
76          }
77        else if (var.isSpecialVariable())
78          {
79            thread.bindSpecial(var, null);
80            binding = thread.getSpecialBinding(var);
81          }
82        else
83          {
84            ext.bind(var, null);
85            binding = ext.getBinding(var);
86          }
87        while (specials != NIL)
88          {
89            ext.declareSpecial(checkSymbol(specials.car()));
90            specials = specials.cdr();
91          }
92        if (limit instanceof Fixnum)
93          {
94            int count = ((Fixnum)limit).value;
95            int i;
96            for (i = 0; i < count; i++)
97              {
98                if (binding instanceof SpecialBinding)
99                  ((SpecialBinding)binding).value = Fixnum.getInstance(i);
100                else
101                  ((Binding)binding).value = Fixnum.getInstance(i);
102
103                processTagBody(bodyForm, localTags, ext);
104
105                if (interrupted)
106                  handleInterrupt();
107              }
108            if (binding instanceof SpecialBinding)
109              ((SpecialBinding)binding).value = Fixnum.getInstance(i);
110            else
111              ((Binding)binding).value = Fixnum.getInstance(i);
112            result = eval(resultForm, ext, thread);
113          }
114        else if (limit instanceof Bignum)
115          {
116            LispObject i = Fixnum.ZERO;
117            while (i.isLessThan(limit))
118              {
119                if (binding instanceof SpecialBinding)
120                  ((SpecialBinding)binding).value = i;
121                else
122                  ((Binding)binding).value = i;
123
124                processTagBody(bodyForm, localTags, ext);
125
126                i = i.incr();
127                if (interrupted)
128                  handleInterrupt();
129              }
130            if (binding instanceof SpecialBinding)
131              ((SpecialBinding)binding).value = i;
132            else
133              ((Binding)binding).value = i;
134            result = eval(resultForm, ext, thread);
135          }
136        else
137          return error(new TypeError(limit, Symbol.INTEGER));
138        return result;
139      }
140    catch (Return ret)
141      {
142        if (ret.getBlock() == blockId)
143          {
144            return ret.getResult();
145          }
146        throw ret;
147      }
148    finally
149      {
150        thread.lastSpecialBinding = lastSpecialBinding;
151        ext.inactive = true;
152      }
153  }
154
155  private static final dotimes DOTIMES = new dotimes();
156}
Note: See TracBrowser for help on using the repository browser.