| 1 | /* |
|---|
| 2 | * dotimes.java |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2003-2006 Peter Graves |
|---|
| 5 | * $Id: dotimes.java 15569 2022-03-19 12:50:18Z mevenson $ |
|---|
| 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 | import static org.armedbear.lisp.Lisp.*; |
|---|
| 37 | |
|---|
| 38 | public final class dotimes extends SpecialOperator |
|---|
| 39 | { |
|---|
| 40 | private dotimes() |
|---|
| 41 | { |
|---|
| 42 | super(Symbol.DOTIMES); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | @Override |
|---|
| 46 | public LispObject execute(LispObject args, Environment env) |
|---|
| 47 | |
|---|
| 48 | { |
|---|
| 49 | LispObject bodyForm = args.cdr(); |
|---|
| 50 | args = args.car(); |
|---|
| 51 | Symbol var = checkSymbol(args.car()); |
|---|
| 52 | LispObject countForm = args.cadr(); |
|---|
| 53 | final LispThread thread = LispThread.currentThread(); |
|---|
| 54 | LispObject resultForm = args.cdr().cdr().car(); |
|---|
| 55 | final SpecialBindingsMark mark = thread.markSpecialBindings(); |
|---|
| 56 | |
|---|
| 57 | LispObject bodyAndDecls = parseBody(bodyForm, false); |
|---|
| 58 | LispObject specials = parseSpecials(bodyAndDecls.NTH(1)); |
|---|
| 59 | bodyForm = bodyAndDecls.car(); |
|---|
| 60 | |
|---|
| 61 | LispObject blockId = new LispObject(); |
|---|
| 62 | final Environment ext = new Environment(env); |
|---|
| 63 | thread.envStack.push(ext); |
|---|
| 64 | try |
|---|
| 65 | { |
|---|
| 66 | ext.addBlock(NIL, blockId); |
|---|
| 67 | |
|---|
| 68 | LispObject limit = eval(countForm, ext, thread); |
|---|
| 69 | LispObject localTags = preprocessTagBody(bodyForm, ext); |
|---|
| 70 | |
|---|
| 71 | LispObject result; |
|---|
| 72 | // Establish a reusable binding. |
|---|
| 73 | final Object binding; |
|---|
| 74 | if (specials != NIL && memq(var, specials)) |
|---|
| 75 | { |
|---|
| 76 | thread.bindSpecial(var, null); |
|---|
| 77 | binding = thread.getSpecialBinding(var); |
|---|
| 78 | ext.declareSpecial(var); |
|---|
| 79 | } |
|---|
| 80 | else if (var.isSpecialVariable()) |
|---|
| 81 | { |
|---|
| 82 | thread.bindSpecial(var, null); |
|---|
| 83 | binding = thread.getSpecialBinding(var); |
|---|
| 84 | } |
|---|
| 85 | else |
|---|
| 86 | { |
|---|
| 87 | ext.bind(var, null); |
|---|
| 88 | binding = ext.getBinding(var); |
|---|
| 89 | } |
|---|
| 90 | while (specials != NIL) |
|---|
| 91 | { |
|---|
| 92 | ext.declareSpecial(checkSymbol(specials.car())); |
|---|
| 93 | specials = specials.cdr(); |
|---|
| 94 | } |
|---|
| 95 | if (limit instanceof Fixnum) |
|---|
| 96 | { |
|---|
| 97 | int count = ((Fixnum)limit).value; |
|---|
| 98 | int i; |
|---|
| 99 | for (i = 0; i < count; i++) |
|---|
| 100 | { |
|---|
| 101 | if (binding instanceof SpecialBinding) |
|---|
| 102 | ((SpecialBinding)binding).value = Fixnum.getInstance(i); |
|---|
| 103 | else |
|---|
| 104 | ((Binding)binding).value = Fixnum.getInstance(i); |
|---|
| 105 | |
|---|
| 106 | processTagBody(bodyForm, localTags, ext); |
|---|
| 107 | |
|---|
| 108 | if (interrupted) |
|---|
| 109 | handleInterrupt(); |
|---|
| 110 | } |
|---|
| 111 | if (binding instanceof SpecialBinding) |
|---|
| 112 | ((SpecialBinding)binding).value = Fixnum.getInstance(i); |
|---|
| 113 | else |
|---|
| 114 | ((Binding)binding).value = Fixnum.getInstance(i); |
|---|
| 115 | result = eval(resultForm, ext, thread); |
|---|
| 116 | } |
|---|
| 117 | else if (limit instanceof Bignum) |
|---|
| 118 | { |
|---|
| 119 | LispObject i = Fixnum.ZERO; |
|---|
| 120 | while (i.isLessThan(limit)) |
|---|
| 121 | { |
|---|
| 122 | if (binding instanceof SpecialBinding) |
|---|
| 123 | ((SpecialBinding)binding).value = i; |
|---|
| 124 | else |
|---|
| 125 | ((Binding)binding).value = i; |
|---|
| 126 | |
|---|
| 127 | processTagBody(bodyForm, localTags, ext); |
|---|
| 128 | |
|---|
| 129 | i = i.incr(); |
|---|
| 130 | if (interrupted) |
|---|
| 131 | handleInterrupt(); |
|---|
| 132 | } |
|---|
| 133 | if (binding instanceof SpecialBinding) |
|---|
| 134 | ((SpecialBinding)binding).value = i; |
|---|
| 135 | else |
|---|
| 136 | ((Binding)binding).value = i; |
|---|
| 137 | result = eval(resultForm, ext, thread); |
|---|
| 138 | } |
|---|
| 139 | else |
|---|
| 140 | return type_error(limit, Symbol.INTEGER); |
|---|
| 141 | return result; |
|---|
| 142 | } |
|---|
| 143 | catch (Return ret) |
|---|
| 144 | { |
|---|
| 145 | if (ret.getBlock() == blockId) |
|---|
| 146 | { |
|---|
| 147 | return ret.getResult(); |
|---|
| 148 | } |
|---|
| 149 | throw ret; |
|---|
| 150 | } |
|---|
| 151 | finally |
|---|
| 152 | { |
|---|
| 153 | thread.resetSpecialBindings(mark); |
|---|
| 154 | ext.inactive = true; |
|---|
| 155 | while (thread.envStack.pop() != ext) {}; |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | private static final dotimes DOTIMES = new dotimes(); |
|---|
| 160 | } |
|---|