Revision: 1.1 - (view) (download) (as text)
1 : | ktanaka | 1.1 | import java.io.*; |
2 : | |||
3 : | |||
4 : | public class LispInputStream { | ||
5 : | Reader reader; | ||
6 : | StreamTokenizer st; | ||
7 : | public LispInputStream(Reader reader){ | ||
8 : | this.reader=reader; | ||
9 : | st=new StreamTokenizer(reader); | ||
10 : | st.resetSyntax(); | ||
11 : | st.whitespaceChars('\u0000',' '); | ||
12 : | st.wordChars('!','\u00ff'); | ||
13 : | st.ordinaryChar('\''); | ||
14 : | st.ordinaryChar('('); | ||
15 : | st.ordinaryChar(')'); | ||
16 : | st.quoteChar('\"'); | ||
17 : | } | ||
18 : | public int nextToken() throws IOException{ | ||
19 : | int token=st.nextToken(); | ||
20 : | switch(token){ | ||
21 : | case StreamTokenizer.TT_NUMBER: | ||
22 : | // System.out.println("Number: "+st.nval+"String:"+st.sval); | ||
23 : | break; | ||
24 : | case StreamTokenizer.TT_WORD: | ||
25 : | // System.out.println("WORD: "+st.sval); | ||
26 : | break; | ||
27 : | default: | ||
28 : | // System.out.println("CHARACTER: "+token); | ||
29 : | break; | ||
30 : | } | ||
31 : | return token; | ||
32 : | } | ||
33 : | public LispObject readList() throws IOException{ | ||
34 : | LispObject a=nextObject(); | ||
35 : | int token=nextToken(); | ||
36 : | if(token==')') return new Cons(a,Symbol.nil); | ||
37 : | else if(token=='.') return new Cons(a,nextObject()); | ||
38 : | else{ | ||
39 : | st.pushBack(); | ||
40 : | return new Cons(a,readList()); | ||
41 : | } | ||
42 : | } | ||
43 : | public LispObject nextObject() throws IOException{ | ||
44 : | // System.out.println("nextObject"); | ||
45 : | int token=nextToken(); | ||
46 : | // System.out.println("token="+token); | ||
47 : | switch(token){ | ||
48 : | case '(': { | ||
49 : | // System.out.println("readList"); | ||
50 : | return readList(); | ||
51 : | } | ||
52 : | case '\'': { | ||
53 : | // System.out.println("quote"); | ||
54 : | LispObject ret=nextObject(); | ||
55 : | return new Cons(Symbol.quote,new Cons(ret,Symbol.nil)); | ||
56 : | } | ||
57 : | case '"': { | ||
58 : | return new LispString(st.sval); | ||
59 : | } | ||
60 : | case StreamTokenizer.TT_EOF: return null; | ||
61 : | case StreamTokenizer.TT_WORD: { | ||
62 : | if(Fixnum.isFixnumString(st.sval)) return new Fixnum(st.sval); | ||
63 : | if(Flonum.isFlonumString(st.sval)) return new Flonum(st.sval); | ||
64 : | return new Symbol(st.sval); | ||
65 : | } | ||
66 : | default: | ||
67 : | System.out.println("token="+token); | ||
68 : | } | ||
69 : | return null; | ||
70 : | } | ||
71 : | public static void main(String args[]) throws IOException{ | ||
72 : | LispInputStream lis; | ||
73 : | if(args.length>0) | ||
74 : | lis=new LispInputStream(new FileReader(args[0])); | ||
75 : | else | ||
76 : | lis=new LispInputStream(new InputStreamReader(System.in)); | ||
77 : | LispObject lo=null; | ||
78 : | while((lo=lis.nextObject())!=null){ | ||
79 : | System.out.println(lo); | ||
80 : | } | ||
81 : | } | ||
82 : | } |
ktanaka Powered by ViewCVS 1.0-dev |
ViewCVS and CVS Help |