home *** CD-ROM | disk | FTP | other *** search
Java Source | 1995-08-11 | 7.6 KB | 265 lines |
- /*
- * @(#)ScriptContext.java 1.1 95/05/10
- *
- * Copyright (c) 1995 Sun Microsystems, Inc. All Rights reserved Permission to
- * use, copy, modify, and distribute this software and its documentation for
- * NON-COMMERCIAL purposes and without fee is hereby granted provided that
- * this copyright notice appears in all copies. Please refer to the file
- * copyright.html for further important copyright and licensing information.
- *
- * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
- * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
- * OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
- * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
- * ITS DERIVATIVES.
- */
-
- package java.tools.jsc;
- import java.util.Hashtable;
- import java.io.*;
-
- /**
- * A class to define an execution context for a Java Tool Command
- * Language script.
- * @author James Gosling
- */
-
- public class ScriptContext {
- private Hashtable defs = new Hashtable();
- private Hashtable vars = new Hashtable();
- private ScriptContext parent;
- ActiveFunction CurrentCommand;
- static private ScriptCommand unc;
- public boolean verbose;
- static private ScriptContext defaultParent = new ScriptContext ("java.tools.jsc.commands.cmd_");
- static {
- new java.tools.jsc.commands.StandardCommands(defaultParent);
- };
- private String ResolvePackage;
- public ScriptContext (String PackagePrefix) {
- ResolvePackage = PackagePrefix;
- parent = defaultParent;
- }
- public ScriptContext (String PackagePrefix, ScriptContext Parent) {
- ResolvePackage = PackagePrefix;
- parent = Parent != null ? Parent : defaultParent;
- }
- public Object eval(Object expr) {
- if (expr == null || !(expr instanceof ActiveFunction))
- return expr;
- Object ret = null;
- for (ActiveFunction af = (ActiveFunction) expr;
- af != null; af = af.next) {
- if (verbose)
- System.out.print("Evaluating " + af + "\n");
- ScriptCommand cmd = af.resolution; // Get command to execute
- String cmds = af.command;
- resolve:if (cmd == null) { // not resolved yet
- for (ScriptContext sc = this; sc != null; sc = sc.parent) {
- cmd = (ScriptCommand) sc.defs.get(cmds);
- if (cmd != null) { // resolve from contexts definitions
- af.resolution = cmd;
- break resolve;
- }
- }
- for (ScriptContext sc = this; sc != null; sc = sc.parent)
- if (sc.ResolvePackage != null) {
- try { // resolve by looking for class
- cmd = (ScriptCommand) new(sc.ResolvePackage + cmds);
- af.resolution = cmd;
- sc.defs.put(cmds, cmd);
- break resolve;
- } catch(Exception e) {
- }
- }
- if (unc == null)// Cant find it anywhere
- unc = new UnknownCommand();
- cmd = unc;
- defs.put(cmds, cmd);
- }
- ActiveFunction prev = CurrentCommand;
- CurrentCommand = af;
- try {
- ret = cmd.eval(this);
- } finally {
- CurrentCommand = prev;
- }
- }
- if (verbose)
- System.out.print(ret == null ? "Returned NULL\n"
- : "Returned " + ret + "\n");
- return ret; // evaluate command
- }
- static public ActiveFunction parse(InputStream is) {
- StreamTokenizer st = new StreamTokenizer(is);
- st.resetSyntax();
- st.wordChars(0, 0xFF);
- st.whitespaceChars(0, ' ');
- st.quoteChar('"');
- st.ordinaryChar(';');
- st.ordinaryChar('(');
- st.ordinaryChar(')');
- st.ordinaryChar('[');
- st.ordinaryChar(']');
- st.ordinaryChar('{');
- st.ordinaryChar('}');
- st.commentChar('#');
- st.eolIsSignificant = true;
- return parse(st);
- }
- static public ActiveFunction parse(String s) {
- return parse(new StringInputStream(s));
- }
- static ActiveFunction parse(StreamTokenizer st) {
- st.nextToken();
- switch (st.ttype) {
- case ')':
- case ']':
- case '}':
- case StreamTokenizer.TT_EOF:
- return null;
- case '\n':
- case ';':
- return parse(st);
- default:
- throw new MalformedScriptException("Word expected at " + st);
- case StreamTokenizer.TT_WORD:
- return new ActiveFunction(st, st.sval);
- }
- }
- public Object parseEval(InputStream is) {
- return parseEval(is, false);
- }
- public Object parseEval(InputStream is, boolean print) {
- StreamTokenizer st = new StreamTokenizer(is);
- st.resetSyntax();
- st.wordChars(0, 0xFF);
- st.whitespaceChars(0, ' ');
- st.quoteChar('"');
- st.ordinaryChar(';');
- st.ordinaryChar('(');
- st.ordinaryChar(')');
- st.ordinaryChar('[');
- st.ordinaryChar(']');
- st.ordinaryChar('{');
- st.ordinaryChar('}');
- st.commentChar('#');
- st.eolIsSignificant = true;
- Object ret = null;
- if (print)
- verbose = true;
- if (verbose)
- System.out.print("ParseEval " + is + " " + st + "\n");
- do {
- Object c = parse(st);
- if (c != null) {
- ret = eval(c);
- if (print) {
- System.out.print(c + ": ");
- if (ret == null)
- System.out.print("NULL");
- else
- System.out.print(ret.toString());
- System.out.print("\n");
- }
- } else if (verbose)
- System.out.print("Parse returned null\n");
- } while (st.ttype != StreamTokenizer.TT_EOF);
- return ret;
- }
- public Object parseEval(String s) {
- return parseEval(new StringInputStream(s));
- }
- public Object arg(int n) {
- return CurrentCommand.args[n];
- }
- public Object evarg(int n) {
- return eval(CurrentCommand.args[n]);
- }
- public int nargs() {
- return CurrentCommand.args.length;
- }
- public String arg(int n, String def) {
- Object oa[] = CurrentCommand.args;
- if (n >= oa.length)
- return def;
- Object o = evarg(n);
- return o == null ? null : o.toString();
- }
- public double arg(int n, double def) {
- Object oa[] = CurrentCommand.args;
- if (n >= oa.length)
- return def;
- Object o = evarg(n);
- if (o == null)
- return 0;
- else if (o instanceof Number)
- return ((Number) o).doubleValue();
- else if (o instanceof String)
- return Double.valueOf((String) o).doubleValue();
- else
- throw new MalformedScriptException("Number expected: " + o);
- }
- public int arg(int n, int def) {
- Object oa[] = CurrentCommand.args;
- if (n >= oa.length)
- return def;
- Object o = evarg(n);
- if (o == null)
- return 0;
- else if (o instanceof Number)
- return ((Number) o).intValue();
- else if (o instanceof String)
- return Integer.parseInt((String) o);
- else
- throw new MalformedScriptException("Number expected: " + o);
- }
- public String toString() {
- return (CurrentCommand == null
- ? "No current command"
- : CurrentCommand.toString());
- }
- static public void main(String argv[]) {
- if (argv == null || argv.length <= 0) {
- defaultParent.parseEval(new BufferedInputStream(new FileInputStream("test.jsc")),
- false);
- } else
- for (int i = 0; i < argv.length; i++) {
- System.out.print(argv[i]);
- System.out.print(": ");
- try {
- System.out.print(defaultParent.parseEval(argv[i]));
- } catch(Exception e) {
- // System.out.print(e+"\n");
- e.printStackTrace();
- }
- System.out.print("\n");
- }
- }
- public final void put(String key, Object value) {
- if (verbose)
- System.out.print("Set variable " + key + " to " + value + "\n");
- vars.put(key.toLowerCase(), value);
- }
- public final Object get(String key) {
- Object ret = vars.get(key.toLowerCase());
- if (verbose)
- System.out.print("Get variable " + key + " finds " + ret + "\n");
- return ret;
- }
- public final void defun(String key, Object value) {
- defs.put(key.toLowerCase(), value);
- }
- public final String command() {
- return CurrentCommand.command;
- }
- }
-
- class UnknownCommand implements ScriptCommand {
- public Object eval(ScriptContext ctx) {
- throw new MalformedScriptException("Undefined command: " +
- ctx.CurrentCommand);
- }
- }
-