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:
020: package xtc.lang.p2;
021:
022: import xtc.tree.GNode;
023: import xtc.tree.Node;
024: import xtc.tree.Visitor;
025:
026: /**
027: * A pretty printer for the Overlog, adapted from
028: * the simply typed lambda calculus pretty printer.
029: *
030: * @author Robert Soule
031: * @version $Revision: 1.4 $
032: */
033: public class Printer extends Visitor {
034:
035: /** The printer. */
036: protected final xtc.tree.Printer printer;
037:
038: /**
039: * Create a new printer for Overlog
040: *
041: * @param printer The printer.
042: */
043: public Printer(xtc.tree.Printer printer) {
044: this .printer = printer;
045: printer.register(this );
046: }
047:
048: /**
049: * Generic catch-all visit method
050: */
051: public void visit(final GNode n) {
052: for (Object o : n) {
053: if (o instanceof Node) {
054: dispatch((Node) o);
055: } else if (Node.isList(o)) {
056: iterate(Node.toList(o));
057: }
058: }
059: }
060:
061: public void visitRule(GNode n) {
062: int index = 0;
063: if (0 == n.getNode(0).getName().compareTo("RuleIdentifier")) {
064: index++;
065: printer.p(n.getNode(0).getString(0) + " ");
066: }
067: // dispatch to the "event" node
068: dispatch(n.getNode(index));
069: printer.p(" :- ");
070: index++;
071: // dispatch to the "actions"
072: int numActions = n.getList(index).size();
073: for (Node child : n.<Node> getList(index)) {
074: dispatch(child);
075: numActions--;
076: if (numActions > 0) {
077: printer.p(", ");
078: }
079: }
080: printer.pln(".");
081: }
082:
083: public void visitMaterialization(GNode n) {
084: int index = 0;
085: printer.p("materialize(" + n.getNode(index).getString(0));
086: index++;
087: for (; index < 4; index++) {
088: printer.p(", ");
089: dispatch(n.getNode(index));
090: }
091: printer.pln(").");
092: }
093:
094: public void visitPrimaryKeys(GNode n) {
095: printer.p("keys(");
096: int numKeys = n.getList(0).size();
097: for (Node key : n.<Node> getList(0)) {
098: dispatch(key);
099: numKeys--;
100: if (numKeys > 0) {
101: printer.p(", ");
102: }
103: }
104: printer.p(")");
105: }
106:
107: public void visitTupleObservation(GNode n) {
108: printer.pln("watch(" + n.getNode(0).getString(0) + ").");
109: }
110:
111: public void visitFlowObservation(GNode n) {
112: printer.pln("watchmod(" + n.getNode(0).getString(0) + ").");
113: }
114:
115: public void visitExternalization(GNode n) {
116: printer.pln("stage(" + ").");
117: }
118:
119: public void visitGenericFact(GNode n) {
120: dispatch(n.getNode(0));
121: printer.pln(".");
122: }
123:
124: public void visitExpression(GNode n) {
125: dispatch(n.getNode(0));
126: printer.p(" " + n.getString(1) + " ");
127: dispatch(n.getNode(2));
128: }
129:
130: public void visitLogicalOrExpression(GNode n) {
131: dispatch(n.getNode(0));
132: printer.p(" || ");
133: dispatch(n.getNode(2));
134: }
135:
136: public void visitLogicalAndExpression(GNode n) {
137: dispatch(n.getNode(0));
138: printer.p(" && ");
139: dispatch(n.getNode(2));
140: }
141:
142: public void visitEqualityExpression(GNode n) {
143: dispatch(n.getNode(0));
144: printer.p(" " + n.getString(1) + " ");
145: dispatch(n.getNode(2));
146: }
147:
148: public void visitRelationalExpression(GNode n) {
149: dispatch(n.getNode(0));
150: printer.p(" " + n.getString(1) + " ");
151: dispatch(n.getNode(2));
152: }
153:
154: public void visitShiftExpression(GNode n) {
155: dispatch(n.getNode(0));
156: printer.p(" " + n.getString(1) + " ");
157: dispatch(n.getNode(2));
158: }
159:
160: public void visitAdditiveExpression(GNode n) {
161: dispatch(n.getNode(0));
162: printer.p(" " + n.getString(1) + " ");
163: dispatch(n.getNode(2));
164: }
165:
166: public void visitMultiplicativeExpression(GNode n) {
167: dispatch(n.getNode(0));
168: printer.p(" " + n.getString(1) + " ");
169: dispatch(n.getNode(2));
170: }
171:
172: public void visitLogicalNegationExpression(GNode n) {
173: printer.p("!");
174: dispatch(n.getNode(0));
175: }
176:
177: public void visitInclusiveExpression(GNode n) {
178: dispatch(n.getNode(0));
179: printer.p(" in ");
180: dispatch(n.getNode(2));
181: }
182:
183: public void visitRangeExpression(GNode n) {
184: printer.p(n.getString(0) + " ");
185: dispatch(n.getNode(1));
186: printer.p(", ");
187: dispatch(n.getNode(2));
188: printer.p(" " + n.getString(3));
189: }
190:
191: public void visitPostfixExpression(GNode n) {
192: dispatch(n.getNode(0));
193: if (n.size() > 0) {
194: dispatch(n.getNode(1));
195: }
196: }
197:
198: public void visitArguments(GNode n) {
199: printer.p("(");
200: if (n.size() == 1) {
201: int numArgs = n.getList(0).size();
202: for (Node arg : n.<Node> getList(0)) {
203: dispatch(arg);
204: numArgs--;
205: if (numArgs > 0) {
206: printer.p(", ");
207: }
208: }
209: }
210: printer.p(")");
211: }
212:
213: public void visitVectorExpression(GNode n) {
214: printer.p("[");
215: printer.p("]");
216: }
217:
218: public void visitMatrixExpression(GNode n) {
219: printer.p("{");
220: printer.p("}");
221: }
222:
223: public void visitMatrixEntry(GNode n) {
224: printer.p("{");
225: printer.p("}");
226: }
227:
228: public void visitParenthesizedExpression(GNode n) {
229: printer.p("(");
230: dispatch(n.getNode(0));
231: printer.p(")");
232: }
233:
234: public void visitTuple(GNode n) {
235: printer.p(n.getNode(0).getString(0) + "(");
236: int numTerms = n.getList(1).size();
237: for (Node term : n.<Node> getList(1)) {
238: dispatch(term);
239: numTerms--;
240: if (numTerms > 0) {
241: printer.p(",");
242: }
243: }
244: printer.p(")");
245: }
246:
247: public void visitAggregate(GNode n) {
248: dispatch(n.getNode(0));
249: printer.p("<");
250: if (n.get(1) instanceof String) {
251: printer.p("*");
252: } else {
253: dispatch(n.getNode(1));
254: }
255: printer.p(">");
256: }
257:
258: public void visitLocationIdentifier(final GNode n) {
259: printer.p(n.getString(0));
260: }
261:
262: public void visitFunctionIdentifier(final GNode n) {
263: printer.p(n.getString(0));
264: }
265:
266: public void visitAggregateIdentifier(final GNode n) {
267: printer.p(n.getString(0));
268: }
269:
270: public void visitVariableIdentifier(final GNode n) {
271: printer.p(n.getString(0));
272: }
273:
274: public void visitUnnamedIdentifier(final GNode n) {
275: printer.p("_");
276: }
277:
278: public void visitFloatingPointConstant(final GNode n) {
279: printer.p(n.getString(0));
280: }
281:
282: public void visitIntegerConstant(final GNode n) {
283: printer.p(n.getString(0));
284: }
285:
286: public void visitStringConstant(final GNode n) {
287: printer.p("\"" + n.getString(0) + "\"");
288: }
289:
290: public void visitBooleanConstant(final GNode n) {
291: printer.p(n.getString(0));
292: }
293:
294: public void visitInfinityConstant(final GNode n) {
295: printer.p("infinity");
296: }
297:
298: public void visitNullConstant(final GNode n) {
299: printer.p("null");
300: }
301: }
|