001: /*
002: * xtc - The eXTensible Compiler
003: * Copyright (C) 2007 Robert Grimm
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * version 2 as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
017: * USA.
018: */
019: package xtc.lang;
020:
021: import xtc.tree.GNode;
022: import xtc.tree.Printer;
023: import xtc.tree.Visitor;
024:
025: import xtc.util.Utilities;
026:
027: /**
028: * A pretty printer for the simply typed lambda calculus.
029: *
030: * @author Robert Grimm
031: * @version $Revision: 1.1 $
032: */
033: public class TypedLambdaPrinter extends Visitor {
034:
035: /** The printer. */
036: protected final Printer printer;
037:
038: /**
039: * Create a new printer for the simply typed lambda calculus.
040: *
041: * @param printer The printer.
042: */
043: public TypedLambdaPrinter(Printer printer) {
044: this .printer = printer;
045: printer.register(this );
046: }
047:
048: /** Visit the specified application node. */
049: public void visitApplication(GNode n) {
050: boolean paren = n.getGeneric(0).hasName("Abstraction");
051: if (paren)
052: printer.p('(');
053: printer.p(n.getNode(0));
054: if (paren)
055: printer.p(')');
056:
057: printer.p(' ');
058:
059: paren = n.getGeneric(1).hasName("Application");
060: if (paren)
061: printer.p('(');
062: printer.p(n.getNode(1));
063: if (paren)
064: printer.p(')');
065: }
066:
067: /** Visit the specified abstraction node. */
068: public void visitAbstraction(GNode n) {
069: printer.p("\\ ").p(n.getNode(0)).p(" : ").p(n.getNode(1)).p(
070: " . ").p(n.getNode(2));
071: }
072:
073: /** Visit the specified identifier node. */
074: public void visitIdentifier(GNode n) {
075: printer.p(n.getString(0));
076: }
077:
078: /** Visit the specified integer constant node. */
079: public void visitIntegerConstant(GNode n) {
080: printer.p(n.getString(0));
081: }
082:
083: /** Visit the specified string constant node. */
084: public void visitStringConstant(GNode n) {
085: String s = n.getString(0);
086: s = s.substring(1, s.length() - 1);
087: printer.p('"').escape(s, Utilities.C_ESCAPES).p('"');
088: }
089:
090: /** Visit the specified function type node. */
091: public void visitFunctionType(GNode n) {
092: boolean paren = n.getGeneric(0).hasName("FunctionType");
093: if (paren)
094: printer.p('(');
095: printer.p(n.getNode(0));
096: if (paren)
097: printer.p(')');
098:
099: printer.p(" -> ");
100:
101: printer.p(n.getNode(1));
102: }
103:
104: /** Visit the specified integer type node. */
105: public void visitIntegerType(GNode n) {
106: printer.p("int");
107: }
108:
109: /** Visit the specified string type node. */
110: public void visitStringType(GNode n) {
111: printer.p("string");
112: }
113:
114: }
|