HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Back to Basics"
Volume: 4 Issue: 8, p. 62

	

Listing 1: 

abstract public class LispTerminal extends HotSchemeInternalRep 
{ 
    public LispTerminal() 
 { 
 } 
    publicLispTerminal(LispTerminal in, 
 LispTerminal out) 
 { 
     in_term  = in; 
     out_term = out; 
 } 
    public final void print(Object obj) 
    { 
    if(out_term != null) 
 out_term.print(obj); 
         else 
 my_print(obj); 
    } 
    abstract public void my_print(Object 
 obj); 

    public final void print(int i)  { 
 print(new String((new 
 Integer(i)).toString())); } 
    public final void print(long l)    { 
 print(new String((new 
 Long(l)).toString())); } 
    public final void println(Object obj) 
    { 
         if(out_term != null) 
 out_term.println(obj); 
        else  my_println(obj); 
    } 
    abstract public void my_println(Object 
 obj); 
    public int read() 
    { 
        if(in_term != null) return 
 in_term.read(); 
        else       return my_read(); 
    } 
    abstract public int my_read(); 
    public void unread(int c) 
    { 
        if(in_term != null) 
 in_term.unread(c); 
        else        my_unread(c); 
    } 
    abstract public void my_unread(int c); 
    public boolean eof() 
    { 
        if(in_term != null) return 
 in_term.eof(); 
        else         return my_eof(); 
    } 
    abstract public boolean my_eof(); 
    public SchemeToken getToken() 
    throws SchemeException 
    { 
        if(token_stack.empty()) 
         return new SchemeToken(this); 
        else return 
 (SchemeToken)token_stack.pop(); 
    } 
    public void pushToken(SchemeToken 
 token) 
    { 
        token_stack.push(token); 
    } 
// place for clients to push tokens back to: 
  private Stack token_stack = new Stack(); 
    private LispTerminal in_term  = null; 
    private LispTerminal out_term = null; 
} 
import java.io.*; 
import java.util.Stack; 

Listing 2: 

public class CharLispTerminal extends LispTerminal 
{ 
    public CharLispTerminal(InputStream 
 in, PrintStream out) 
    { 
    input  = new PushbackInputStream(in); 
    output = out; 
    } 
    public void my_print(Object obj) 
    { 
    output.print(obj); output.flush(); 
    } 
    public void my_println(Object obj) 
    { 
    output.println(obj); 
    } 
    public int my_read() 
    { 
    int c = 0; 
    try 
    { 
    c = input.read(); 
    } 
    catch(java.io.IOException e) 
    { 
            ; 
        } 
        return c; 
    } 
    public void my_unread(int c) 
    { 
        try 
        { 
            input.unread(c); 
        } 
        catch(java.io.IOException e) 
        { 
            ; 
        } 
    } 
    public boolean my_eof() 
    { 
    return false; 
    } 
    private PushbackInputStream input; 
    private PrintStream output; 
} 
import java.util.Vector; 

Listing 3: 

class GUILispTerminal extends BufferedLispTerminal 
{ 
 private Vector  m_vOutput; 
 private boolean debug = false; 
    public GUILispTerminal() 
    { 
  m_vOutput = new Vector(5); 
 } 
 private void print(String s) 
 { 
  m_vOutput.addElement(s); 
  if(debug) 
  { 
System.out.println("GUILispTerminal::print Vector is " + m_vOutput); 
System.out.println("GUILispTerminal::print " + s); 
  } 
 } 
 public void setInput(String s) 
 { 
 setBuffer(s); 
  if(debug) System.out.println("GUILispTer- 
 minal::setInput " + s); //debug 
 } 
 public Vector getOutput() 
 { 
 if(debug) System.out.println("GUILispTer- 
 minal::getOutput " + m_vOutput); //debug 

  Vector vTemp = (Vector)m_vOutput.clone(); 
  m_vOutput.setSize(0); 
  return(vTemp); 
 } 
    public void my_print(Object obj) 
    { 
  print(obj.toString()); 
    } 
    public void my_println(Object obj) 
    { 
  print(obj.toString() + "\n"); 
 } 
} 

Listing 4: 

abstract public class BufferedLispTerminal extends LispTerminal 
{ 
    public BufferedLispTerminal() 
 { 
     super(); 
        CommandBuf = new StringBuffer(""); 
 } 
    public BufferedLispTerminal(LispTerminal in, LispTerminal out) 
 { 
     super(in, out); 
        CommandBuf = new StringBuffer(""); 
 } 
    public int my_read() 
    { 
  if(pos < CommandBuf.length()) 
   return((int)CommandBuf.charAt(pos++)); 
  else 
      return(0); 
    } 
    public void my_unread(int c) 
    { 
  if(pos > 0) pos--; 
    } 
    public boolean my_eof() 
    { 
        if(pos < CommandBuf.length()) 
        { 
// if everything else is spaces, return true, else false 
            for(int i = pos; i < CommandBuf.length(); i++) 
            { 
              if(!Character.isWhitespace(CommandBuf.charAt(i))) 
                    return false; 
            } 
            CommandBuf.setLength(pos);  // we won't look at those spaces again 
        } 
        return true; 
    } 
    protected void setBuffer(String s) 
    { 
        pos = 0; 
        CommandBuf.setLength(0); 
        CommandBuf.append(s); 
    } 
    private StringBuffer CommandBuf; 
   private int pos = 0; 
} 


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.