summaryrefslogtreecommitdiff
path: root/src/printer/ast/ast_printer.cpp
blob: cd9b0cad5b70ce9cd6ce5650c47908bcaaeaf22e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*********************                                                        */
/*! \file ast_printer.cpp
 ** \verbatim
 ** Original author: mdeters
 ** Major contributors: none
 ** Minor contributors (to current version): none
 ** This file is part of the CVC4 prototype.
 ** Copyright (c) 2009, 2010  The Analysis of Computer Systems Group (ACSys)
 ** Courant Institute of Mathematical Sciences
 ** New York University
 ** See the file COPYING in the top-level source directory for licensing
 ** information.\endverbatim
 **
 ** \brief The pretty-printer interface for the AST output language
 **
 ** The pretty-printer interface for the AST output language.
 **/

#include "printer/ast/ast_printer.h"
#include "util/language.h" // for LANG_AST
#include "expr/node_manager.h" // for VarNameAttr

#include <iostream>

using namespace std;

namespace CVC4 {
namespace printer {
namespace ast {

std::ostream& AstPrinter::toStream(std::ostream& out, TNode n,
                                   int toDepth, bool types) const {
  // null
  if(n.getKind() == kind::NULL_EXPR) {
    out << "null";
    return out;
  }

  // variable
  if(n.getMetaKind() == kind::metakind::VARIABLE) {
    if(n.getKind() != kind::VARIABLE &&
       n.getKind() != kind::SORT_TYPE) {
      out << n.getKind() << ':';
    }

    string s;
    if(n.getAttribute(expr::VarNameAttr(), s)) {
      out << s;
    } else {
      out << "var_" << n.getId();
    }
    if(types) {
      // print the whole type, but not *its* type
      out << ":";
      n.getType().toStream(out, -1, false, language::output::LANG_AST);
    }

    return out;
  }

  out << '(' << n.getKind();
  if(n.getMetaKind() == kind::metakind::CONSTANT) {
    // constant
    out << ' ';
    kind::metakind::NodeValueConstPrinter::toStream(out, n);
  } else {
    // operator
    if(n.getMetaKind() == kind::metakind::PARAMETERIZED) {
      out << ' ';
      if(toDepth != 0) {
        n.getOperator().toStream(out, toDepth < 0 ? toDepth : toDepth - 1,
                                 types, language::output::LANG_AST);
      } else {
        out << "(...)";
      }
    }
    for(TNode::iterator i = n.begin(),
          iend = n.end();
        i != iend;
        ++i) {
      if(i != iend) {
        out << ' ';
      }
      if(toDepth != 0) {
        (*i).toStream(out, toDepth < 0 ? toDepth : toDepth - 1,
                      types, language::output::LANG_AST);
      } else {
        out << "(...)";
      }
    }
  }
  out << ')';

  return out;
}/* AstPrinter::toStream() */

}/* CVC4::printer::ast namespace */
}/* CVC4::printer namespace */
}/* CVC4 namespace */

generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback