/* * IclTermToTree.java * Copyright (C) 2004-2005, Bjorn Bringert (bringert@cs.chalmers.se) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package se.chalmers.cs.gf.oaa; import com.sri.oaa2.icl.*; import se.chalmers.cs.gf.abssyn.*; import java.util.*; /** * Does the reverse of TreeToIclTerm. */ public class IclTermToTree { /** * Makes an abstract syntax tree from a term. * * @return The syntax tree, or null if the term could not * be converted to a tree. */ public static Tree iclTermToTree(IclTerm t) { return new ToTreeVisitor().visit(t, null); } private static class ToTreeVisitor implements OaaPrologVisitor { public Tree visit(IclFloat node, Object data) { throw new RuntimeException("Cannot handle IclFloat: " + node); } public Tree visit(IclGroup node, Object data) { throw new RuntimeException("Cannot handle IclGroup: " + node); } public Tree visit(IclInt node, Object data) { throw new RuntimeException("IclInt found outside " + "of gf_int(): " + node); } public Tree visit(IclList node, Object data) { throw new RuntimeException("Cannot handle IclList: " + node); } public Tree visit(IclStr node, Object data) { throw new RuntimeException("IclStr found outside " + "of gf_str(): " + node); } public Tree visit(IclStruct node, Object data) { String f = node.getFunctor(); if (f.equals("gf")) { int n = node.getNumChildren(); if (n < 1) throw new RuntimeException("IclStruct has no children: " + node); IclTerm ft = node.getTerm(0); if (!ft.isStr()) throw new RuntimeException("The first child of gf() is not a string: " + node); String fun = ((IclStr)ft).toUnquotedString(); Tree[] cs = new Tree[n-1]; for (int i = 1; i < n; i++) cs[i-1] = visit(node.getTerm(i), data); return new Fun(fun, cs); } else if (f.equals("gf_str")) { String s = ((IclStr)node.getTerm(0)).toUnquotedString(); return new StringLiteral(s); } else if (f.equals("gf_int")) { int i = ((IclInt)node.getTerm(0)).toInt(); return new IntLiteral(i); } throw new RuntimeException("Unknown IclStruct: " + node); } public Tree visit(IclVar node, Object data) { return new MetaVariable(); // FIXME: retain name? } /* * Nasty hack to get around the fact that IclTerm.accept is protected */ public Tree visit(IclTerm node, Object data) { if (node instanceof IclFloat) { return visit((IclFloat)node, data); } else if (node instanceof IclGroup) { return visit((IclGroup)node, data); } else if (node instanceof IclInt) { return visit((IclInt)node, data); } else if (node instanceof IclList) { return visit((IclList)node, data); } else if (node instanceof IclStr) { return visit((IclStr)node, data); } else if (node instanceof IclStruct) { return visit((IclStruct)node, data); } else if (node instanceof IclVar) { return visit((IclVar)node, data); } return null; } } }