// file: extractFromSexp.java // author: Robert Keller // // purpose: Showing how to extract information from an S expression // // action: Repeatedly reads S expressions and "extracts" information from // them // // run: java -cs analyzeSexp // $Id: extractFromSexp.java,v 1.3 1997/02/10 04:14:05 keller Exp keller $ import Poly.*; // cs 60 package import java.io.*; class extractFromSexp { static String promptString = " > "; // default prompt static int count = 1; // line counter public static void main(String arg[]) { Tokenizer in = new Tokenizer(System.in); // S exp tokenizer PrintStream out = System.out; Object ob; while( (ob = prompt(in, out)) != Tokenizer.eof ) // read next S exp { if( ob instanceof Long ) { long value = ((Long)ob).longValue(); // handle long out.println("Long: " + value); } else if( ob instanceof Double ) { double value = ((Double)ob).doubleValue(); // handle double out.println("Double: " + value); } else if( ob instanceof String ) { String value = (String)ob; // handle string out.println("String: " + value); } else if( ob instanceof Poly.List ) { Object array[] = ((Poly.List)ob).array(); // convert to array out.print("A list of length " + array.length); out.println( array.length > 0 ? " consisting of:" : ""); for( int i = 0; i < array.length; i++ ) // traverse the array { out.println(" " + i + ": " + array[i]); } } else out.println(Poly.List.analysis(ob)); // other } out.println(); // new line } static Object prompt(Tokenizer in, PrintStream out) // prompt method { out.print(count++ + promptString); // display prompt out.flush(); // & increment count return in.nextSexp(); // get input } }