1 | /* |
---|
2 | * LispStackFrame.java |
---|
3 | * |
---|
4 | * Copyright (C) 2009 Mark Evenson |
---|
5 | * $Id: LispStackFrame.java 13440 2011-08-05 21:25:10Z 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 | import static org.armedbear.lisp.Lisp.*; |
---|
37 | |
---|
38 | public class LispStackFrame |
---|
39 | extends StackFrame |
---|
40 | { |
---|
41 | public final LispObject operator; |
---|
42 | private final LispObject first; |
---|
43 | private final LispObject second; |
---|
44 | private final LispObject third; |
---|
45 | private final LispObject[] args; |
---|
46 | |
---|
47 | private final class UnavailableArgument extends LispObject |
---|
48 | { |
---|
49 | public UnavailableArgument () { } |
---|
50 | @Override |
---|
51 | public String printObject() { |
---|
52 | return unreadableString("unavailable arg", false); |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | private final LispObject UNAVAILABLE_ARG = new UnavailableArgument(); |
---|
57 | |
---|
58 | public LispStackFrame(LispObject operator) |
---|
59 | { |
---|
60 | this.operator = operator; |
---|
61 | first = null; |
---|
62 | second = null; |
---|
63 | third = null; |
---|
64 | args = null; |
---|
65 | } |
---|
66 | |
---|
67 | public LispStackFrame(LispObject operator, LispObject arg) |
---|
68 | { |
---|
69 | this.operator = operator; |
---|
70 | first = arg; |
---|
71 | second = null; |
---|
72 | third = null; |
---|
73 | args = null; |
---|
74 | } |
---|
75 | |
---|
76 | public LispStackFrame(LispObject operator, LispObject first, |
---|
77 | LispObject second) |
---|
78 | { |
---|
79 | this.operator = operator; |
---|
80 | this.first = first; |
---|
81 | this.second = second; |
---|
82 | third = null; |
---|
83 | args = null; |
---|
84 | } |
---|
85 | |
---|
86 | public LispStackFrame(LispObject operator, LispObject first, |
---|
87 | LispObject second, LispObject third) |
---|
88 | |
---|
89 | { |
---|
90 | this.operator = operator; |
---|
91 | this.first = first; |
---|
92 | this.second = second; |
---|
93 | this.third = third; |
---|
94 | args = null; |
---|
95 | } |
---|
96 | |
---|
97 | public LispStackFrame(LispObject operator, LispObject... args) |
---|
98 | { |
---|
99 | this.operator = operator; |
---|
100 | first = null; |
---|
101 | second = null; |
---|
102 | third = null; |
---|
103 | this.args = args; |
---|
104 | } |
---|
105 | |
---|
106 | @Override |
---|
107 | public LispObject typeOf() { |
---|
108 | return Symbol.LISP_STACK_FRAME; |
---|
109 | } |
---|
110 | |
---|
111 | @Override |
---|
112 | public LispObject classOf() { |
---|
113 | return BuiltInClass.LISP_STACK_FRAME; |
---|
114 | } |
---|
115 | |
---|
116 | @Override |
---|
117 | public String printObject() |
---|
118 | { |
---|
119 | String result = ""; |
---|
120 | final String LISP_STACK_FRAME = "LISP-STACK-FRAME"; |
---|
121 | try { |
---|
122 | result = Symbol.PRIN1_TO_STRING.execute(this.toLispList()).printObject(); |
---|
123 | } catch (Throwable t) { // error while printing stack |
---|
124 | Debug.trace("Serious printing error: "); |
---|
125 | Debug.trace(t); |
---|
126 | result = unreadableString(LISP_STACK_FRAME); |
---|
127 | } |
---|
128 | return result; |
---|
129 | } |
---|
130 | |
---|
131 | @Override |
---|
132 | public LispObject typep(LispObject typeSpecifier) |
---|
133 | |
---|
134 | { |
---|
135 | if (typeSpecifier == Symbol.LISP_STACK_FRAME) |
---|
136 | return T; |
---|
137 | if (typeSpecifier == BuiltInClass.LISP_STACK_FRAME) |
---|
138 | return T; |
---|
139 | return super.typep(typeSpecifier); |
---|
140 | } |
---|
141 | |
---|
142 | public LispObject toLispList() |
---|
143 | |
---|
144 | { |
---|
145 | LispObject result = argsToLispList(); |
---|
146 | if (operator instanceof Operator) { |
---|
147 | LispObject lambdaName = ((Operator)operator).getLambdaName(); |
---|
148 | if (lambdaName != null && lambdaName != Lisp.NIL) |
---|
149 | return result.push(lambdaName); |
---|
150 | } |
---|
151 | return result.push(operator); |
---|
152 | } |
---|
153 | |
---|
154 | private LispObject argsToLispList() |
---|
155 | |
---|
156 | { |
---|
157 | LispObject result = Lisp.NIL; |
---|
158 | if (args != null) { |
---|
159 | for (int i = 0; i < args.length; i++) |
---|
160 | // `args' come here from LispThread.execute. I don't know |
---|
161 | // how it comes that some callers pass NULL ptrs around but |
---|
162 | // we better do not create conses with their CAR being NULL; |
---|
163 | // it'll horribly break printing such a cons; and probably |
---|
164 | // other bad things may happen, too. --TCR, 2009-09-17. |
---|
165 | if (args[i] == null) |
---|
166 | result = result.push(UNAVAILABLE_ARG); |
---|
167 | else |
---|
168 | result = result.push(args[i]); |
---|
169 | } else { |
---|
170 | do { |
---|
171 | if (first != null) |
---|
172 | result = result.push(first); |
---|
173 | else |
---|
174 | break; |
---|
175 | if (second != null) |
---|
176 | result = result.push(second); |
---|
177 | else |
---|
178 | break; |
---|
179 | if (third != null) |
---|
180 | result = result.push(third); |
---|
181 | else |
---|
182 | break; |
---|
183 | } while (false); |
---|
184 | } |
---|
185 | return result.nreverse(); |
---|
186 | } |
---|
187 | |
---|
188 | public SimpleString toLispString() |
---|
189 | |
---|
190 | { |
---|
191 | String result; |
---|
192 | try { |
---|
193 | result = this.toLispList().printObject(); |
---|
194 | } catch (Throwable t) { // error while printing stack |
---|
195 | Debug.trace("Serious printing error: "); |
---|
196 | Debug.trace(t); |
---|
197 | result = unreadableString("LISP-STACK-FRAME"); |
---|
198 | } |
---|
199 | return new SimpleString(result); |
---|
200 | } |
---|
201 | |
---|
202 | public LispObject getOperator() { |
---|
203 | return operator; |
---|
204 | } |
---|
205 | |
---|
206 | @Override |
---|
207 | public LispObject getParts() |
---|
208 | |
---|
209 | { |
---|
210 | LispObject result = NIL; |
---|
211 | result = result.push(new Cons("OPERATOR", getOperator())); |
---|
212 | LispObject args = argsToLispList(); |
---|
213 | if (args != NIL) { |
---|
214 | result = result.push(new Cons("ARGS", args)); |
---|
215 | } |
---|
216 | |
---|
217 | return result.nreverse(); |
---|
218 | } |
---|
219 | } |
---|