source: trunk/abcl/src/org/armedbear/lisp/Do.java

Last change on this file was 15552, checked in by Mark Evenson, 2 years ago

Support for accessing lexical environments for interpreted code

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.2 KB
Line 
1/*
2 * Do.java
3 *
4 * Copyright (C) 2003-2006 Peter Graves
5 * $Id: Do.java 15552 2022-02-23 14:37:01Z 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
34package org.armedbear.lisp;
35
36import static org.armedbear.lisp.Lisp.*;
37
38public final class Do {
39    // ### do
40    private static final SpecialOperator DO = new sf_do();
41    private static final class sf_do extends SpecialOperator {
42        sf_do() {
43            super(Symbol.DO, "varlist endlist &body body");
44        }
45
46        @Override
47        public LispObject execute(LispObject args, Environment env)
48
49        {
50            return _do(args, env, false);
51        }
52    };
53
54    // ### do*
55    private static final SpecialOperator DO_STAR = new sf_do_star();
56    private static final class sf_do_star extends SpecialOperator {
57        sf_do_star() {
58            super(Symbol.DO_STAR, "varlist endlist &body body");
59        }
60
61        @Override
62        public LispObject execute(LispObject args, Environment env)
63
64        {
65            return _do(args, env, true);
66        }
67    };
68
69    static final LispObject _do(LispObject args, Environment env,
70                                        boolean sequential)
71
72    {
73        LispObject varlist = args.car();
74        LispObject second = args.cadr();
75        LispObject end_test_form = second.car();
76        LispObject result_forms = second.cdr();
77        LispObject body = args.cddr();
78        // Process variable specifications.
79        final int numvars = varlist.length();
80        Symbol[] vars = new Symbol[numvars];
81        LispObject[] initforms = new LispObject[numvars];
82        LispObject[] stepforms = new LispObject[numvars];
83        for (int i = 0; i < numvars; i++) {
84            final LispObject varspec = varlist.car();
85            if (varspec instanceof Cons) {
86                vars[i] = checkSymbol(varspec.car());
87                initforms[i] = varspec.cadr();
88                // Is there a step form?
89                if (varspec.cddr() != NIL)
90                    stepforms[i] = varspec.caddr();
91            } else {
92                // Not a cons, must be a symbol.
93                vars[i] = checkSymbol(varspec);
94                initforms[i] = NIL;
95            }
96            varlist = varlist.cdr();
97        }
98        final LispThread thread = LispThread.currentThread();
99        final SpecialBindingsMark mark = thread.markSpecialBindings();
100        // Process declarations.
101
102        final LispObject bodyAndDecls = parseBody(body, false);
103        LispObject specials = parseSpecials(bodyAndDecls.NTH(1));
104        body = bodyAndDecls.car();
105
106        Environment ext = new Environment(env);
107        for (int i = 0; i < numvars; i++) {
108            Symbol var = vars[i];
109            LispObject value = eval(initforms[i], (sequential ? ext : env), thread);
110            ext = new Environment(ext);
111            if (specials != NIL && memq(var, specials))
112                thread.bindSpecial(var, value);
113            else if (var.isSpecialVariable())
114                thread.bindSpecial(var, value);
115            else
116                ext.bind(var, value);
117        }
118        LispObject list = specials;
119        while (list != NIL) {
120            ext.declareSpecial(checkSymbol(list.car()));
121            list = list.cdr();
122        }
123        // Look for tags.
124        LispObject localTags = preprocessTagBody(body, ext);
125        LispObject blockId = new LispObject();
126        try {
127            thread.envStack.push(ext);
128            // Implicit block.
129            ext.addBlock(NIL, blockId);
130            while (true) {
131                // Execute body.
132                // Test for termination.
133                if (eval(end_test_form, ext, thread) != NIL)
134                    break;
135
136                processTagBody(body, localTags, ext);
137
138                // Update variables.
139                if (sequential) {
140                    for (int i = 0; i < numvars; i++) {
141                        LispObject step = stepforms[i];
142                        if (step != null) {
143                            Symbol symbol = vars[i];
144                            LispObject value = eval(step, ext, thread);
145                            if (symbol.isSpecialVariable()
146                                    || ext.isDeclaredSpecial(symbol))
147                                thread.rebindSpecial(symbol, value);
148                            else
149                                ext.rebind(symbol, value);
150                        }
151                    }
152                } else {
153                    // Evaluate step forms.
154                    LispObject results[] = new LispObject[numvars];
155                    for (int i = 0; i < numvars; i++) {
156                        LispObject step = stepforms[i];
157                        if (step != null) {
158                            LispObject result = eval(step, ext, thread);
159                            results[i] = result;
160                        }
161                    }
162                    // Update variables.
163                    for (int i = 0; i < numvars; i++) {
164                        if (results[i] != null) {
165                            Symbol symbol = vars[i];
166                            LispObject value = results[i];
167                            if (symbol.isSpecialVariable()
168                                    || ext.isDeclaredSpecial(symbol))
169                                thread.rebindSpecial(symbol, value);
170                            else
171                                ext.rebind(symbol, value);
172                        }
173                    }
174                }
175                if (interrupted)
176                    handleInterrupt();
177            }
178            LispObject result = progn(result_forms, ext, thread);
179            return result;
180        } catch (Return ret) {
181            if (ret.getBlock() == blockId) {
182                return ret.getResult();
183            }
184            throw ret;
185        }
186        finally {
187            while (thread.envStack.pop() != ext) {}
188            thread.resetSpecialBindings(mark);
189            ext.inactive = true;
190        }
191    }
192}
Note: See TracBrowser for help on using the repository browser.