Lisp処理系 パースを鋭意実装中
鋭意実装中です。
topノードにCellが入っちゃう版ができたので
とりあえずこんな感じです。
public static Sexp parse(String sExps) throws ParseException { return parseCell(sExps); } private static Cell parseCell(String sCell) throws ParseException { List<Sexp> sexpList = new ArrayList<Sexp>(); StringBuilder atom = new StringBuilder(); for (int i = 0; i < sCell.length(); i++) { String s = sCell.substring(i, i + 1); if (s.equals(" ")) { sexpList.add(Atom.newAtom(atom.toString())); atom.setLength(0); }else if (!s.equals("(") && !s.equals(")") && !s.isEmpty()){ atom.append(s); } if (s.equals("(")) { sexpList.add( parse(sCell.substring(i+1, sCell.indexOf(")", i)+1)) ); i = sCell.indexOf(")", i); } if (s.equals(")") || i == sCell.length()-1) { if(atom.length() != 0){ sexpList.add(Atom.newAtom(atom.toString())); } return new Cell(sexpList.toArray(new Sexp[0])); } } throw new ParseException("parse error!"); } public static void main(String[] argv) { try { Sexp ss = parse("(+ 1 2 (+ 3 4))"); System.out.println(ss); } catch (Exception ex) { Logger.getLogger(MyLispPerser.class.getName()).log(Level.SEVERE, null, ex); } } //((+ 1 2 (+ 3 4))) ← こうなっちゃうねぇ