001: /*
002: * @(#)Builtin.java 1.4 05/05/25
003: *
004: * Copyright (c) 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.lang;
010:
011: import java.io.File;
012: import java.io.FileNotFoundException;
013: import java.io.InputStream;
014: import java.io.Reader;
015: import java.net.URL;
016: import org.pnuts.util.*;
017:
018: /**
019: * Builtin functions
020: */
021: final class Builtin extends PnutsFunction {
022:
023: private final static boolean requireLatestScript = "true"
024: .equalsIgnoreCase(Runtime
025: .getProperty("pnuts.lang.requireLatestScript"));
026:
027: protected Builtin(String name) {
028: super (name);
029: }
030:
031: protected Object exec(Object[] args, Context context) {
032: if (name == _getContext) {
033: if (args.length == 0) {
034: return context;
035: }
036: } else if (name == _package) {
037: if (args.length == 1) {
038: Package pkg;
039: if (args[0] instanceof Package) {
040: pkg = (Package) args[0];
041: } else if (args[0] != null) {
042: pkg = Package.getPackage((String) args[0], context);
043: } else {
044: pkg = Package.getInstance(null,
045: context.currentPackage.root, context);
046: }
047: context.setCurrentPackage(pkg);
048: return null;
049: } else if (args.length == 0) {
050: return context.currentPackage;
051: }
052: } else if (name == _import) {
053: if (args.length == 1) {
054: if (args[0] == null) {
055: context.importEnv = new ImportEnv();
056: return null;
057: } else {
058: String s = (String) args[0];
059: if (!s.endsWith("*")) {
060: context.addClassToImport(s);
061: } else {
062: int idx = s.lastIndexOf('.');
063: if (idx > 0) {
064: context.addPackageToImport(s.substring(0,
065: idx));
066: } else {
067: context.addPackageToImport("");
068: }
069: }
070: return null;
071: }
072: } else if (args.length == 0) {
073: return context.importEnv.list();
074: }
075: } else if (name == _throw) {
076: if (args.length == 1) {
077: Object arg = args[0];
078: if (arg instanceof PnutsException) {
079: throw (PnutsException) arg;
080: } else if (arg instanceof Throwable) {
081: throw new PnutsException((Throwable) arg, context);
082: } else {
083: throw new PnutsException(arg.toString(), context);
084: }
085: }
086: } else if (name == _defined) {
087: if (args.length == 1) {
088: return Boolean.valueOf(context
089: .defined((String) args[0]));
090: }
091: } else if (name == _quit) {
092: if (args.length == 0) {
093: throw new Escape();
094: } else if (args.length == 1) {
095: throw new Escape(args[0]);
096: }
097: } else if (name == _eval) {
098: Context c = null;
099: if (args.length == 1) {
100: // c = (Context) ((Context) context).clone(false, true);
101: c = context;
102: // c.eval = true;
103: if (c.stackFrame != null) {
104: Cell prev = c.evalFrameStack;
105: Cell cell = new Cell();
106: c.evalFrameStack = cell;
107: cell.object = c.stackFrame;
108: cell.next = prev;
109: }
110: c.stackFrame = new StackFrame();
111: return Pnuts.eval((String) args[0], c);
112: } else if (args.length == 2) {
113: if (args[1] instanceof Context) {
114: c = (Context) ((Context) args[1])
115: .clone(false, true);
116: } else if (args[1] instanceof Package) {
117: c = (Context) context.clone(false, true);
118: c.currentPackage = (Package) args[1];
119: } else {
120: c = (Context) context.clone(false, true);
121: c.currentPackage = Package.getPackage(
122: (String) args[1], context);
123: }
124: // c.eval = true;
125: return c.pnutsImpl.eval((String) args[0], c);
126: }
127: } else if (name == _loadFile) {
128: try {
129: Context c = null;
130: Object arg0 = args[0];
131: if (args.length > 0) {
132: if (args.length > 1) {
133: c = (Context) ((Context) args[1]).clone(false,
134: true);
135: } else {
136: c = (Context) context.clone();
137: }
138: if (arg0 instanceof File) {
139: return Pnuts.loadFile(((File) arg0).getPath(),
140: c);
141: } else {
142: return Pnuts.loadFile((String) arg0, c);
143: }
144: }
145: } catch (FileNotFoundException e) {
146: throw new PnutsException(e, context);
147: }
148: } else if (name == _load) {
149: try {
150: Context c = null;
151: if (args.length > 0) {
152: if (args.length > 1) {
153: c = (Context) args[1];
154: c = (Context) c.clone(false, true);
155: } else {
156: c = (Context) context.clone();
157: }
158: if (args[0] instanceof InputStream) {
159: return Pnuts.load((InputStream) args[0], c);
160: } else if (args[0] instanceof Reader) {
161: return Pnuts.load((Reader) args[0], c);
162: } else if (args[0] instanceof URL) {
163: return Pnuts.load((URL) args[0], c);
164: } else {
165: return Pnuts.load((String) args[0], c);
166: }
167: }
168: } catch (FileNotFoundException e) {
169: throw new PnutsException(e, context);
170: }
171: } else if (name == _autoload) {
172: if (args.length == 2) {
173: context.currentPackage.autoload((String) args[0],
174: (String) args[1], context);
175: return null;
176: }
177: } else if (name == _require) {
178: try {
179: if (args.length == 1) {
180: Context c = (Context) context.clone();
181: Pnuts.require((String) args[0], c,
182: requireLatestScript);
183: return null;
184: }
185: } catch (FileNotFoundException e) {
186: throw new PnutsException(e, context);
187: }
188: } else if (name == _class) {
189: if (args.length == 1) {
190: Object arg = args[0];
191: if (arg instanceof Class) {
192: return arg;
193: } else {
194: try {
195: Class cls = Pnuts.loadClass((String) arg,
196: context);
197: cls.getClass(); // initialize the class
198: return cls;
199: } catch (ClassNotFoundException e) {
200: return null;
201: }
202: }
203: }
204: } else if (name == _use) {
205: if (args.length == 0) {
206: return context.usedPackages();
207: } else if (args.length == 1) {
208: Object arg0 = args[0];
209: if (arg0 == null) {
210: context.clearPackages();
211: return null;
212: } else if (arg0 instanceof Package) {
213: boolean ret = context.usePackage((Package) arg0,
214: false);
215: return (ret ? Boolean.TRUE : Boolean.FALSE);
216: } else if (arg0 instanceof String) {
217: boolean ret = context.usePackage((String) arg0,
218: false);
219: return (ret ? Boolean.TRUE : Boolean.FALSE);
220: }
221: }
222: } else if (name == _unuse) {
223: if (args.length == 1) {
224: Object arg0 = args[0];
225: Package pkg = null;
226: if (arg0 instanceof String) {
227: pkg = Package.getPackage((String) arg0, context);
228: } else if (arg0 instanceof Package) {
229: pkg = (Package) arg0;
230: }
231: if (pkg != null) {
232: boolean ret = context.unusePackage(pkg);
233: return (ret ? Boolean.TRUE : Boolean.FALSE);
234: }
235: }
236: }
237: throw new PnutsException("function.notDefined", new Object[] {
238: name, new Integer(args.length) }, context);
239: }
240:
241: public String toString() {
242: return "<builtin " + name + ">";
243: }
244:
245: public String unparse(int narg) {
246: return null;
247: }
248:
249: public String[] getImportEnv(int narg) {
250: return null;
251: }
252:
253: public boolean isBuiltin() {
254: return true;
255: }
256: }
|