| 1 | /* |
|---|
| 2 | * dolist.java |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2003-2006 Peter Graves |
|---|
| 5 | * $Id: dolist.java 12288 2009-11-29 22:00:12Z vvoutilainen $ |
|---|
| 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 | // ### dolist |
|---|
| 39 | public final class dolist extends SpecialOperator |
|---|
| 40 | { |
|---|
| 41 | private dolist() |
|---|
| 42 | { |
|---|
| 43 | super(Symbol.DOLIST); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | @Override |
|---|
| 47 | public LispObject execute(LispObject args, Environment env) |
|---|
| 48 | |
|---|
| 49 | { |
|---|
| 50 | LispObject bodyForm = args.cdr(); |
|---|
| 51 | args = args.car(); |
|---|
| 52 | Symbol var = checkSymbol(args.car()); |
|---|
| 53 | LispObject listForm = args.cadr(); |
|---|
| 54 | final LispThread thread = LispThread.currentThread(); |
|---|
| 55 | LispObject resultForm = args.cdr().cdr().car(); |
|---|
| 56 | final SpecialBindingsMark mark = thread.markSpecialBindings(); |
|---|
| 57 | // Process declarations. |
|---|
| 58 | LispObject bodyAndDecls = parseBody(bodyForm, false); |
|---|
| 59 | LispObject specials = parseSpecials(bodyAndDecls.NTH(1)); |
|---|
| 60 | bodyForm = bodyAndDecls.car(); |
|---|
| 61 | |
|---|
| 62 | LispObject blockId = new LispObject(); |
|---|
| 63 | final Environment ext = new Environment(env); |
|---|
| 64 | try |
|---|
| 65 | { |
|---|
| 66 | // Implicit block. |
|---|
| 67 | ext.addBlock(NIL, blockId); |
|---|
| 68 | // Evaluate the list form. |
|---|
| 69 | LispObject list = checkList(eval(listForm, ext, thread)); |
|---|
| 70 | // Look for tags. |
|---|
| 71 | LispObject remaining = bodyForm; |
|---|
| 72 | LispObject localTags = preprocessTagBody(bodyForm, ext); |
|---|
| 73 | |
|---|
| 74 | final Object binding; |
|---|
| 75 | if (specials != NIL && memq(var, specials)) |
|---|
| 76 | { |
|---|
| 77 | thread.bindSpecial(var, null); |
|---|
| 78 | binding = thread.getSpecialBinding(var); |
|---|
| 79 | ext.declareSpecial(var); |
|---|
| 80 | } |
|---|
| 81 | else if (var.isSpecialVariable()) |
|---|
| 82 | { |
|---|
| 83 | thread.bindSpecial(var, null); |
|---|
| 84 | binding = thread.getSpecialBinding(var); |
|---|
| 85 | } |
|---|
| 86 | else |
|---|
| 87 | { |
|---|
| 88 | ext.bind(var, null); |
|---|
| 89 | binding = ext.getBinding(var); |
|---|
| 90 | } |
|---|
| 91 | while (specials != NIL) |
|---|
| 92 | { |
|---|
| 93 | ext.declareSpecial(checkSymbol(specials.car())); |
|---|
| 94 | specials = specials.cdr(); |
|---|
| 95 | } |
|---|
| 96 | while (list != NIL) |
|---|
| 97 | { |
|---|
| 98 | if (binding instanceof SpecialBinding) |
|---|
| 99 | ((SpecialBinding)binding).value = list.car(); |
|---|
| 100 | else |
|---|
| 101 | ((Binding)binding).value = list.car(); |
|---|
| 102 | |
|---|
| 103 | processTagBody(bodyForm, localTags, ext); |
|---|
| 104 | |
|---|
| 105 | list = list.cdr(); |
|---|
| 106 | if (interrupted) |
|---|
| 107 | handleInterrupt(); |
|---|
| 108 | } |
|---|
| 109 | if (binding instanceof SpecialBinding) |
|---|
| 110 | ((SpecialBinding)binding).value = NIL; |
|---|
| 111 | else |
|---|
| 112 | ((Binding)binding).value = NIL; |
|---|
| 113 | LispObject result = eval(resultForm, ext, thread); |
|---|
| 114 | return result; |
|---|
| 115 | } |
|---|
| 116 | catch (Return ret) |
|---|
| 117 | { |
|---|
| 118 | if (ret.getBlock() == blockId) |
|---|
| 119 | { |
|---|
| 120 | return ret.getResult(); |
|---|
| 121 | } |
|---|
| 122 | throw ret; |
|---|
| 123 | } |
|---|
| 124 | finally |
|---|
| 125 | { |
|---|
| 126 | thread.resetSpecialBindings(mark); |
|---|
| 127 | ext.inactive = true; |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | private static final dolist DOLIST = new dolist(); |
|---|
| 132 | } |
|---|