Skip to content

Commit cbfcd32

Browse files
author
Daniel Kroening
committed
smt2_format can now format symbols and constants
1 parent 449a3f9 commit cbfcd32

File tree

2 files changed

+71
-12
lines changed

2 files changed

+71
-12
lines changed

src/solvers/smt2/smt2_format.cpp

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,65 @@ Author: Daniel Kroening, [email protected]
88

99
#include "smt2_format.h"
1010

11+
#include <util/arith_tools.h>
12+
#include <util/std_expr.h>
1113
#include <util/std_types.h>
1214

13-
std::ostream &operator<<(std::ostream &out, const smt2_format &f)
15+
std::ostream &smt2_format_rec(std::ostream &out, const typet &type)
1416
{
15-
if(f.type.id() == ID_unsignedbv)
16-
out << "(_ BitVec " << to_unsignedbv_type(f.type).get_width() << ')';
17-
else if(f.type.id() == ID_bool)
17+
if(type.id() == ID_unsignedbv)
18+
out << "(_ BitVec " << to_unsignedbv_type(type).get_width() << ')';
19+
else if(type.id() == ID_bool)
1820
out << "Bool";
19-
else if(f.type.id() == ID_integer)
21+
else if(type.id() == ID_integer)
2022
out << "Int";
21-
else if(f.type.id() == ID_real)
23+
else if(type.id() == ID_real)
2224
out << "Real";
2325
else
24-
out << "? " << f.type.id();
26+
out << "? " << type.id();
27+
28+
return out;
29+
}
30+
31+
std::ostream &smt2_format_rec(std::ostream &out, const exprt &expr)
32+
{
33+
if(expr.id()==ID_constant)
34+
{
35+
const auto &value = to_constant_expr(expr).get_value();
36+
37+
const typet &expr_type=expr.type();
38+
39+
if(expr_type.id()==ID_unsignedbv)
40+
{
41+
const std::size_t width=to_unsignedbv_type(expr_type).get_width();
42+
43+
const auto value=numeric_cast_v<mp_integer>(expr);
44+
45+
out << "(_ bv" << value
46+
<< " " << width << ")";
47+
}
48+
else if(expr_type.id()==ID_bool)
49+
{
50+
if(expr.is_true())
51+
out << "true";
52+
else if(expr.is_false())
53+
out << "false";
54+
else
55+
DATA_INVARIANT(false, "unknown Boolean constant");
56+
}
57+
else if(expr_type.id()==ID_integer)
58+
{
59+
out << value;
60+
}
61+
else
62+
DATA_INVARIANT(false, "unhandled constant: "+expr_type.id_string());
63+
}
64+
else if(expr.id()==ID_symbol)
65+
{
66+
out << to_symbol_expr(expr).get_identifier();
67+
}
68+
else
69+
out << "? " << expr.id();
2570

2671
return out;
2772
}

src/solvers/smt2/smt2_format.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,31 @@ Author: Daniel Kroening, [email protected]
99
#ifndef CPROVER_SOLVERS_SMT2_SMT2_FORMAT_H
1010
#define CPROVER_SOLVERS_SMT2_SMT2_FORMAT_H
1111

12-
#include <util/type.h>
12+
#include <util/expr.h>
1313

14-
struct smt2_format
14+
template<typename T>
15+
struct smt2_format_containert
1516
{
16-
explicit smt2_format(const typet &_type) : type(_type)
17+
explicit smt2_format_containert(const T &_o) : o(_o)
1718
{
1819
}
1920

20-
const typet &type;
21+
const T &o;
2122
};
2223

23-
std::ostream &operator<<(std::ostream &, const smt2_format &);
24+
template<typename T>
25+
static inline smt2_format_containert<T> smt2_format(const T &_o)
26+
{
27+
return smt2_format_containert<T>(_o);
28+
}
29+
30+
template<typename T>
31+
static inline std::ostream &operator<<(std::ostream &out, const smt2_format_containert<T> &c)
32+
{
33+
return smt2_format_rec(out, c.o);
34+
}
35+
36+
std::ostream &smt2_format_rec(std::ostream &, const exprt &);
37+
std::ostream &smt2_format_rec(std::ostream &, const typet &);
2438

2539
#endif // CPROVER_SOLVERS_SMT2_SMT2_FORMAT_H

0 commit comments

Comments
 (0)