summaryrefslogtreecommitdiff
path: root/src/util/sexpr.cpp
blob: fbc8802d672c0aad3958b3970e875dcc666e13c8 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*********************                                                        */
/*! \file sexpr.cpp
 ** \verbatim
 ** Top contributors (to current version):
 **   Tim King, Andrew Reynolds
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2017 by the authors listed in the file AUTHORS
 ** in the top-level source directory) and their institutional affiliations.
 ** All rights reserved.  See the file COPYING in the top-level source
 ** directory for licensing information.\endverbatim
 **
 ** \brief Simple representation of S-expressions
 **
 ** Simple representation of S-expressions.
 **
 ** SExprs have their own language specific printing procedures. The reason for
 ** this being implemented on SExpr and not on the Printer class is that the
 ** Printer class lives in libcvc4. It has to currently as it prints fairly
 ** complicated objects, like Model, which in turn uses SmtEngine pointers.
 ** However, SExprs need to be printed by Statistics. To get the output
 ** consistent with the previous version, the printing of SExprs in different
 ** languages is handled in the SExpr class and the libexpr library.
 **/

#include "util/sexpr.h"

#include <iostream>
#include <sstream>
#include <vector>

#include "base/cvc4_assert.h"
#include "options/set_language.h"
#include "util/ostream_util.h"
#include "util/smt2_quote_string.h"

namespace CVC4 {

const int PrettySExprs::s_iosIndex = std::ios_base::xalloc();

std::ostream& operator<<(std::ostream& out, PrettySExprs ps) {
  ps.applyPrettySExprs(out);
  return out;
}

SExpr::~SExpr() {
  if (d_children != NULL) {
    delete d_children;
    d_children = NULL;
  }
  Assert(d_children == NULL);
}

SExpr& SExpr::operator=(const SExpr& other) {
  d_sexprType = other.d_sexprType;
  d_integerValue = other.d_integerValue;
  d_rationalValue = other.d_rationalValue;
  d_stringValue = other.d_stringValue;

  if (d_children == NULL && other.d_children == NULL) {
    // Do nothing.
  } else if (d_children == NULL) {
    d_children = new SExprVector(*other.d_children);
  } else if (other.d_children == NULL) {
    delete d_children;
    d_children = NULL;
  } else {
    (*d_children) = other.getChildren();
  }
  Assert(isAtom() == other.isAtom());
  Assert((d_children == NULL) == isAtom());
  return *this;
}

SExpr::SExpr()
    : d_sexprType(SEXPR_STRING),
      d_integerValue(0),
      d_rationalValue(0),
      d_stringValue(""),
      d_children(NULL) {}

SExpr::SExpr(const SExpr& other)
    : d_sexprType(other.d_sexprType),
      d_integerValue(other.d_integerValue),
      d_rationalValue(other.d_rationalValue),
      d_stringValue(other.d_stringValue),
      d_children(NULL) {
  d_children =
      (other.d_children == NULL) ? NULL : new SExprVector(*other.d_children);
  // d_children being NULL is equivalent to the node being an atom.
  Assert((d_children == NULL) == isAtom());
}

SExpr::SExpr(const CVC4::Integer& value)
    : d_sexprType(SEXPR_INTEGER),
      d_integerValue(value),
      d_rationalValue(0),
      d_stringValue(""),
      d_children(NULL) {}

SExpr::SExpr(int value)
    : d_sexprType(SEXPR_INTEGER),
      d_integerValue(value),
      d_rationalValue(0),
      d_stringValue(""),
      d_children(NULL) {}

SExpr::SExpr(long int value)
    : d_sexprType(SEXPR_INTEGER),
      d_integerValue(value),
      d_rationalValue(0),
      d_stringValue(""),
      d_children(NULL) {}

SExpr::SExpr(unsigned int value)
    : d_sexprType(SEXPR_INTEGER),
      d_integerValue(value),
      d_rationalValue(0),
      d_stringValue(""),
      d_children(NULL) {}

SExpr::SExpr(unsigned long int value)
    : d_sexprType(SEXPR_INTEGER),
      d_integerValue(value),
      d_rationalValue(0),
      d_stringValue(""),
      d_children(NULL) {}

SExpr::SExpr(const CVC4::Rational& value)
    : d_sexprType(SEXPR_RATIONAL),
      d_integerValue(0),
      d_rationalValue(value),
      d_stringValue(""),
      d_children(NULL) {}

SExpr::SExpr(const std::string& value)
    : d_sexprType(SEXPR_STRING),
      d_integerValue(0),
      d_rationalValue(0),
      d_stringValue(value),
      d_children(NULL) {}

/**
 * This constructs a string expression from a const char* value.
 * This cannot be removed in order to support SExpr("foo").
 * Given the other constructors this SExpr("foo") converts to bool.
 * instead of SExpr(string("foo")).
 */
SExpr::SExpr(const char* value)
    : d_sexprType(SEXPR_STRING),
      d_integerValue(0),
      d_rationalValue(0),
      d_stringValue(value),
      d_children(NULL) {}

SExpr::SExpr(bool value)
    : d_sexprType(SEXPR_KEYWORD),
      d_integerValue(0),
      d_rationalValue(0),
      d_stringValue(value ? "true" : "false"),
      d_children(NULL) {}

SExpr::SExpr(const Keyword& value)
    : d_sexprType(SEXPR_KEYWORD),
      d_integerValue(0),
      d_rationalValue(0),
      d_stringValue(value.getString()),
      d_children(NULL) {}

SExpr::SExpr(const std::vector<SExpr>& children)
    : d_sexprType(SEXPR_NOT_ATOM),
      d_integerValue(0),
      d_rationalValue(0),
      d_stringValue(""),
      d_children(new SExprVector(children)) {}

std::string SExpr::toString() const {
  std::stringstream ss;
  ss << (*this);
  return ss.str();
}

/** Is this S-expression an atom? */
bool SExpr::isAtom() const { return d_sexprType != SEXPR_NOT_ATOM; }

/** Is this S-expression an integer? */
bool SExpr::isInteger() const { return d_sexprType == SEXPR_INTEGER; }

/** Is this S-expression a rational? */
bool SExpr::isRational() const { return d_sexprType == SEXPR_RATIONAL; }

/** Is this S-expression a string? */
bool SExpr::isString() const { return d_sexprType == SEXPR_STRING; }

/** Is this S-expression a keyword? */
bool SExpr::isKeyword() const { return d_sexprType == SEXPR_KEYWORD; }

std::ostream& operator<<(std::ostream& out, const SExpr& sexpr) {
  SExpr::toStream(out, sexpr);
  return out;
}

void SExpr::toStream(std::ostream& out, const SExpr& sexpr) {
  toStream(out, sexpr, language::SetLanguage::getLanguage(out));
}

void SExpr::toStream(std::ostream& out, const SExpr& sexpr,
                     OutputLanguage language) {
  const int indent = PrettySExprs::getPrettySExprs(out) ? 2 : 0;
  toStream(out, sexpr, language, indent);
}

void SExpr::toStream(std::ostream& out, const SExpr& sexpr,
                     OutputLanguage language, int indent) {
  if (sexpr.isKeyword() && languageQuotesKeywords(language)) {
    out << quoteSymbol(sexpr.getValue());
  } else {
    toStreamRec(out, sexpr, language, indent);
  }
}

void SExpr::toStreamRec(std::ostream& out, const SExpr& sexpr,
                        OutputLanguage language, int indent) {
  StreamFormatScope scope(out);

  if (sexpr.isInteger()) {
    out << sexpr.getIntegerValue();
  } else if (sexpr.isRational()) {
    const double approximation = sexpr.getRationalValue().getDouble();
    out << std::fixed << approximation;
  } else if (sexpr.isKeyword()) {
    out << sexpr.getValue();
  } else if (sexpr.isString()) {
    std::string s = sexpr.getValue();
    // escape backslash and quote
    for (size_t i = 0; i < s.length(); ++i) {
      if (s[i] == '"') {
        s.replace(i, 1, "\\\"");
        ++i;
      } else if (s[i] == '\\') {
        s.replace(i, 1, "\\\\");
        ++i;
      }
    }
    out << "\"" << s << "\"";
  } else {
    const std::vector<SExpr>& kids = sexpr.getChildren();
    out << (indent > 0 && kids.size() > 1 ? "( " : "(");
    bool first = true;
    for (std::vector<SExpr>::const_iterator i = kids.begin(); i != kids.end();
         ++i) {
      if (first) {
        first = false;
      } else {
        if (indent > 0) {
          out << "\n" << std::string(indent, ' ');
        } else {
          out << ' ';
        }
      }
      toStreamRec(out, *i, language,
                  indent <= 0 || indent > 2 ? 0 : indent + 2);
    }
    if (indent > 0 && kids.size() > 1) {
      out << '\n';
      if (indent > 2) {
        out << std::string(indent - 2, ' ');
      }
    }
    out << ')';
  }
} /* toStreamRec() */

bool SExpr::languageQuotesKeywords(OutputLanguage language) {
  switch (language) {
    case language::output::LANG_SMTLIB_V1:
    case language::output::LANG_SYGUS:
    case language::output::LANG_TPTP:
      return true;
    case language::output::LANG_AST:
    case language::output::LANG_CVC3:
    case language::output::LANG_CVC4:
    default: return language::isOutputLang_smt2(language);
  };
}

std::string SExpr::getValue() const {
  PrettyCheckArgument(isAtom(), this);
  switch (d_sexprType) {
    case SEXPR_INTEGER:
      return d_integerValue.toString();
    case SEXPR_RATIONAL: {
      // We choose to represent rationals as decimal strings rather than
      // "numerator/denominator."  Perhaps an additional SEXPR_DECIMAL
      // could be added if we need both styles, even if it's backed by
      // the same Rational object.
      std::stringstream ss;
      ss << std::fixed << d_rationalValue.getDouble();
      return ss.str();
    }
    case SEXPR_STRING:
    case SEXPR_KEYWORD:
      return d_stringValue;
    case SEXPR_NOT_ATOM:
      return std::string();
  }
  return std::string();
}

const CVC4::Integer& SExpr::getIntegerValue() const {
  PrettyCheckArgument(isInteger(), this);
  return d_integerValue;
}

const CVC4::Rational& SExpr::getRationalValue() const {
  PrettyCheckArgument(isRational(), this);
  return d_rationalValue;
}

const std::vector<SExpr>& SExpr::getChildren() const {
  PrettyCheckArgument(!isAtom(), this);
  Assert(d_children != NULL);
  return *d_children;
}

bool SExpr::operator==(const SExpr& s) const {
  if (d_sexprType == s.d_sexprType && d_integerValue == s.d_integerValue &&
      d_rationalValue == s.d_rationalValue &&
      d_stringValue == s.d_stringValue) {
    if (d_children == NULL && s.d_children == NULL) {
      return true;
    } else if (d_children != NULL && s.d_children != NULL) {
      return getChildren() == s.getChildren();
    }
  }
  return false;
}

bool SExpr::operator!=(const SExpr& s) const { return !(*this == s); }

SExpr SExpr::parseAtom(const std::string& atom) {
  if (atom == "true") {
    return SExpr(true);
  } else if (atom == "false") {
    return SExpr(false);
  } else {
    try {
      Integer z(atom);
      return SExpr(z);
    } catch (std::invalid_argument&) {
      // Fall through to the next case
    }
    try {
      Rational q(atom);
      return SExpr(q);
    } catch (std::invalid_argument&) {
      // Fall through to the next case
    }
    return SExpr(atom);
  }
}

SExpr SExpr::parseListOfAtoms(const std::vector<std::string>& atoms) {
  std::vector<SExpr> parsedAtoms;
  typedef std::vector<std::string>::const_iterator const_iterator;
  for (const_iterator i = atoms.begin(), i_end = atoms.end(); i != i_end; ++i) {
    parsedAtoms.push_back(parseAtom(*i));
  }
  return SExpr(parsedAtoms);
}

SExpr SExpr::parseListOfListOfAtoms(
    const std::vector<std::vector<std::string> >& atoms_lists) {
  std::vector<SExpr> parsedListsOfAtoms;
  typedef std::vector<std::vector<std::string> >::const_iterator const_iterator;
  for (const_iterator i = atoms_lists.begin(), i_end = atoms_lists.end();
       i != i_end; ++i) {
    parsedListsOfAtoms.push_back(parseListOfAtoms(*i));
  }
  return SExpr(parsedListsOfAtoms);
}

} /* CVC4 namespace */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback