/*
 * TreeToIclTerm.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.*;


/**
 *  Functions are converted to gf(fun, args...), metavariables become 
 *  ICL variables, string literals become gf_str(string value) and
 *  integer literals gf_int(int value). The latter two are there to 
 *  avoid confusion between nullary functions and literals.
 */
public class TreeToIclTerm {

        public static IclTerm treeToIclTerm(Tree t) {
                return t.accept(new ToIclTermVisitor(), null);
        }

        private static class ToIclTermVisitor implements TreeVisitor<IclTerm,Object> {

                public IclTerm visit(Fun f, Object arg) {
                        // Since the OAA Java lib can't handle functors
                        // with capital and non-ascii letters, we represent
                        // GF functions as gf("fun", args...)

                        List<IclTerm> cs = new LinkedList<IclTerm>();
                        cs.add(new IclStr(f.getLabel()));
                        for (Tree c : f.getChildren())
                                cs.add(c.accept(this, arg));
                        return new IclStruct("gf", cs);
                }

                public IclTerm visit(IntLiteral l, Object arg) {
                        return new IclStruct("gf_int", new IclInt(l.getValue()));
                }

                public IclTerm visit(StringLiteral l, Object arg) {
                        return new IclStruct("gf_str", new IclStr(l.getValue()));
                }

                public IclTerm visit(MetaVariable v, Object arg) {
                        return new IclVar(); // FIXME: include type in variable name?
                }

        }

}