KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > describe


1 /*
2  * describe.java
3  *
4  * Copyright (C) 2003-2004 Peter Graves
5  * $Id: describe.java,v 1.15 2004/04/24 12:37:04 piso Exp $
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
22 package org.armedbear.lisp;
23
24 public final class describe extends Lisp
25 {
26     // ### describe
27
// Need to support optional second argument specifying output stream.
28
private static final Primitive DESCRIBE =
29         new Primitive("describe", "object &optional stream")
30     {
31         public LispObject execute(LispObject[] args) throws ConditionThrowable
32         {
33             if (args.length != 1)
34                 return signal(new WrongNumberOfArgumentsException(this));
35             LispObject obj = args[0];
36             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(obj.writeToString());
37             if (obj instanceof Symbol) {
38                 Symbol symbol = (Symbol) obj;
39                 LispObject pkg = symbol.getPackage();
40                 sb.append(" is an ");
41                 if (pkg == NIL)
42                     sb.append("uninterned");
43                 else if (((Package JavaDoc)pkg).findExternalSymbol(symbol.getName()) == symbol)
44                     sb.append("external");
45                 else
46                     sb.append("internal");
47                 sb.append(" symbol");
48                 if (pkg != NIL) {
49                     sb.append(" in the ");
50                     sb.append(pkg.getName());
51                     sb.append(" package");
52                 }
53                 sb.append(".\n");
54                 LispObject value = symbol.getSymbolValue();
55                 if (symbol.isSpecialVariable()) {
56                     sb.append("It is a ");
57                     sb.append(symbol.isConstant() ? "constant" : "special variable");
58                     sb.append("; ");
59                     if (value != null) {
60                         sb.append("its value is ");
61                         sb.append(value.writeToString());
62                     } else
63                         sb.append("it is unbound");
64                     sb.append(".\n");
65                 } else if (value != null) {
66                     sb.append("It is an undefined variable; its value is ");
67                     sb.append(value.writeToString());
68                     sb.append(".\n");
69                 }
70                 LispObject function = symbol.getSymbolFunction();
71                 if (function != null) {
72                     sb.append("Its function binding is ");
73                     sb.append(function.writeToString());
74                     sb.append(".\n");
75                     if (function instanceof Function) {
76                         LispObject arglist = ((Function)function).getArglist();
77                         if (arglist != null) {
78                             LispThread thread = LispThread.currentThread();
79                             Environment oldDynEnv = thread.getDynamicEnvironment();
80                             thread.bindSpecial(_PRINT_ESCAPE_, NIL);
81                             sb.append("Function argument list:\n ");
82                             if (arglist instanceof AbstractString)
83                                 sb.append(arglist.getStringValue());
84                             else
85                                 sb.append(arglist.writeToString());
86                             sb.append('\n');
87                             thread.setDynamicEnvironment(oldDynEnv);
88                         }
89                     }
90                     LispObject documentation =
91                         symbol.getFunctionDocumentation();
92                     if (documentation instanceof AbstractString) {
93                         sb.append("Function documentation:\n ");
94                         sb.append(documentation.getStringValue());
95                         sb.append('\n');
96                     }
97                 }
98                 LispObject plist = symbol.getPropertyList();
99                 if (plist != NIL) {
100                     sb.append("Its property list has these indicator/value pairs:\n");
101                     LispObject[] array = plist.copyToArray();
102                     for (int i = 0; i < array.length; i += 2) {
103                         sb.append(" ");
104                         sb.append(array[i].writeToString());
105                         sb.append(' ');
106                         sb.append(array[i+1].writeToString());
107                         sb.append('\n');
108                     }
109                 }
110             }
111             Stream out = getStandardOutput();
112             out.freshLine();
113             out._writeString(sb.toString());
114             out.freshLine();
115             return LispThread.currentThread().nothing();
116         }
117     };
118 }
119
Popular Tags