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