鋭意実装中です。
topノードにCellが入っちゃう版ができたので
とりあえずこんな感じです。

    public static Sexp parse(String sExps) throws ParseException {
        return parseCell(sExps);
    }

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<span class="synType">private</span> <span class="synType">static</span> Cell parseCell(String sCell) <span class="synType">throws</span> ParseException {
    List&lt;Sexp&gt; sexpList = <span class="synStatement">new</span> ArrayList&lt;Sexp&gt;();
    StringBuilder atom = <span class="synStatement">new</span> StringBuilder();
    <span class="synStatement">for</span> (<span class="synType">int</span> i = <span class="synConstant">0</span>; i &lt; sCell.length(); i++) {
        String s = sCell.substring(i, i + <span class="synConstant">1</span>);
        <span class="synStatement">if</span> (s.equals(<span class="synConstant">&quot; &quot;</span>)) {
            sexpList.add(Atom.newAtom(atom.toString()));
            atom.setLength(<span class="synConstant">0</span>);
        }<span class="synStatement">else</span> <span class="synStatement">if</span> (!s.equals(<span class="synConstant">&quot;(&quot;</span>) &amp;&amp; !s.equals(<span class="synConstant">&quot;)&quot;</span>) &amp;&amp; !s.isEmpty()){
            atom.append(s);
        }

        <span class="synStatement">if</span> (s.equals(<span class="synConstant">&quot;(&quot;</span>)) {
            sexpList.add( parse(sCell.substring(i+<span class="synConstant">1</span>, sCell.indexOf(<span class="synConstant">&quot;)&quot;</span>, i)+<span class="synConstant">1</span>)) );
            i = sCell.indexOf(<span class="synConstant">&quot;)&quot;</span>, i);
        }

        <span class="synStatement">if</span> (s.equals(<span class="synConstant">&quot;)&quot;</span>) || i == sCell.length()-<span class="synConstant">1</span>) {
            <span class="synStatement">if</span>(atom.length() != <span class="synConstant">0</span>){
                sexpList.add(Atom.newAtom(atom.toString()));
            }
            <span class="synStatement">return</span> <span class="synStatement">new</span> Cell(sexpList.toArray(<span class="synStatement">new</span> Sexp[<span class="synConstant">0</span>]));
        }
    }

    <span class="synStatement">throw</span> <span class="synStatement">new</span> ParseException(<span class="synConstant">&quot;parse error!&quot;</span>);
}


<span class="synType">public</span> <span class="synType">static</span> <span class="synType">void</span> main(String[] argv) {
    <span class="synStatement">try</span> {
        Sexp ss = parse(<span class="synConstant">&quot;(+ 1 2 (+ 3 4))&quot;</span>);
        System.out.println(ss);
    } <span class="synStatement">catch</span> (Exception ex) {
        Logger.getLogger(MyLispPerser.<span class="synType">class</span>.getName()).log(Level.SEVERE, <span class="synConstant">null</span>, ex);
    }
}

//((+ 1 2 (+ 3 4))) ← こうなっちゃうねぇ