Initial revision
import java.io.*; public class LispInputStream { Reader reader; StreamTokenizer st; public LispInputStream(Reader reader){ this.reader=reader; st=new StreamTokenizer(reader); st.resetSyntax(); st.whitespaceChars('\u0000',' '); st.wordChars('!','\u00ff'); st.ordinaryChar('\''); st.ordinaryChar('('); st.ordinaryChar(')'); st.quoteChar('\"'); } public int nextToken() throws IOException{ int token=st.nextToken(); switch(token){ case StreamTokenizer.TT_NUMBER: // System.out.println("Number: "+st.nval+"String:"+st.sval); break; case StreamTokenizer.TT_WORD: // System.out.println("WORD: "+st.sval); break; default: // System.out.println("CHARACTER: "+token); break; } return token; } public LispObject readList() throws IOException{ LispObject a=nextObject(); int token=nextToken(); if(token==')') return new Cons(a,Symbol.nil); else if(token=='.') return new Cons(a,nextObject()); else{ st.pushBack(); return new Cons(a,readList()); } } public LispObject nextObject() throws IOException{ // System.out.println("nextObject"); int token=nextToken(); // System.out.println("token="+token); switch(token){ case '(': { // System.out.println("readList"); return readList(); } case '\'': { // System.out.println("quote"); LispObject ret=nextObject(); return new Cons(Symbol.quote,new Cons(ret,Symbol.nil)); } case '"': { return new LispString(st.sval); } case StreamTokenizer.TT_EOF: return null; case StreamTokenizer.TT_WORD: { if(Fixnum.isFixnumString(st.sval)) return new Fixnum(st.sval); if(Flonum.isFlonumString(st.sval)) return new Flonum(st.sval); return new Symbol(st.sval); } default: System.out.println("token="+token); } return null; } public static void main(String args[]) throws IOException{ LispInputStream lis; if(args.length>0) lis=new LispInputStream(new FileReader(args[0])); else lis=new LispInputStream(new InputStreamReader(System.in)); LispObject lo=null; while((lo=lis.nextObject())!=null){ System.out.println(lo); } } }
ktanaka Powered by ViewCVS 1.0-dev |
ViewCVS and CVS Help |