1 /********************* */
4 ** Original author: Morgan Deters
5 ** Major contributors: none
6 ** Minor contributors (to current version): Clark Barrett, Andrew Reynolds
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2013 New York University and The University of Iowa
9 ** See the file COPYING in the top-level source directory for licensing
10 ** information.\endverbatim
12 ** \brief Base of the pretty-printer interface
14 ** Base of the pretty-printer interface.
17 #include "cvc4_private.h"
19 #ifndef __CVC4__PRINTER__PRINTER_H
20 #define __CVC4__PRINTER__PRINTER_H
22 #include "util/language.h"
23 #include "util/sexpr.h"
24 #include "util/model.h"
25 #include "expr/node.h"
26 #include "expr/command.h"
31 /** Printers for each OutputLanguage */
32 static Printer
* d_printers
[language::output::LANG_MAX
];
34 /** Make a Printer for a given OutputLanguage */
35 static Printer
* makePrinter(OutputLanguage lang
) throw();
37 // disallow copy, assignment
38 Printer(const Printer
&) CVC4_UNUSED
;
39 Printer
& operator=(const Printer
&) CVC4_UNUSED
;
42 // derived classes can construct, but no one else.
45 /** write model response to command */
46 virtual void toStream(std::ostream
& out
, const Model
& m
, const Command
* c
) const throw() = 0;
48 /** write model response to command using another language printer */
49 void toStreamUsing(OutputLanguage lang
, std::ostream
& out
, const Model
& m
, const Command
* c
) const throw() {
50 getPrinter(lang
)->toStream(out
, m
, c
);
54 /** Get the Printer for a given OutputLanguage */
55 static Printer
* getPrinter(OutputLanguage lang
) throw() {
56 if(lang
== language::output::LANG_AUTO
) {
57 lang
= language::output::LANG_CVC4
; // default
59 if(d_printers
[lang
] == NULL
) {
60 d_printers
[lang
] = makePrinter(lang
);
62 return d_printers
[lang
];
65 /** Write a Node out to a stream with this Printer. */
66 virtual void toStream(std::ostream
& out
, TNode n
,
67 int toDepth
, bool types
, size_t dag
) const throw() = 0;
69 /** Write a Command out to a stream with this Printer. */
70 virtual void toStream(std::ostream
& out
, const Command
* c
,
71 int toDepth
, bool types
, size_t dag
) const throw() = 0;
73 /** Write a CommandStatus out to a stream with this Printer. */
74 virtual void toStream(std::ostream
& out
, const CommandStatus
* s
) const throw() = 0;
76 /** Write an SExpr out to a stream with this Printer. */
77 virtual void toStream(std::ostream
& out
, const SExpr
& sexpr
) const throw();
80 * Write a Result out to a stream with this Printer.
82 * The default implementation writes a reasonable string in lowercase
83 * for sat, unsat, valid, invalid, or unknown results. This behavior
84 * is overridable by each Printer, since sometimes an output language
85 * has a particular preference for how results should appear.
87 virtual void toStream(std::ostream
& out
, const Result
& r
) const throw();
89 /** Write a Model out to a stream with this Printer. */
90 virtual void toStream(std::ostream
& out
, const Model
& m
) const throw();
95 * IOStream manipulator to pretty-print SExprs.
99 * The allocated index in ios_base for our setting.
101 static const int s_iosIndex
;
104 * When this manipulator is used, the setting is stored here.
110 * Construct a PrettySExprs with the given setting.
112 PrettySExprs(bool prettySExprs
) : d_prettySExprs(prettySExprs
) {}
114 inline void applyPrettySExprs(std::ostream
& out
) {
115 out
.iword(s_iosIndex
) = d_prettySExprs
;
118 static inline bool getPrettySExprs(std::ostream
& out
) {
119 return out
.iword(s_iosIndex
);
122 static inline void setPrettySExprs(std::ostream
& out
, bool prettySExprs
) {
123 out
.iword(s_iosIndex
) = prettySExprs
;
127 * Set the pretty-sexprs state on the output stream for the current
128 * stack scope. This makes sure the old state is reset on the
129 * stream after normal OR exceptional exit from the scope, using the
134 bool d_oldPrettySExprs
;
138 inline Scope(std::ostream
& out
, bool prettySExprs
) :
140 d_oldPrettySExprs(PrettySExprs::getPrettySExprs(out
)) {
141 PrettySExprs::setPrettySExprs(out
, prettySExprs
);
145 PrettySExprs::setPrettySExprs(d_out
, d_oldPrettySExprs
);
148 };/* class PrettySExprs::Scope */
150 };/* class PrettySExprs */
153 * Sets the default pretty-sexprs setting for an ostream. Use like this:
155 * // let out be an ostream, s an SExpr
156 * out << PrettySExprs(true) << s << endl;
158 * The setting stays permanently (until set again) with the stream.
160 inline std::ostream
& operator<<(std::ostream
& out
, PrettySExprs ps
) {
161 ps
.applyPrettySExprs(out
);
165 }/* CVC4 namespace */
167 #endif /* __CVC4__PRINTER__PRINTER_H */