|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: Expression Pretty Printing |
| 4 | +
|
| 5 | +Author: Daniel Kroening, [email protected] |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +/// \file |
| 10 | +/// Expression Pretty Printing |
| 11 | + |
| 12 | +#include "arith_tools.h" |
| 13 | +#include "expr.h" |
| 14 | +#include "expr_iterator.h" |
| 15 | +#include "fixedbv.h" |
| 16 | +#include "format_expr.h" |
| 17 | +#include "format_type.h" |
| 18 | +#include "ieee_float.h" |
| 19 | +#include "invariant.h" |
| 20 | +#include "mp_arith.h" |
| 21 | +#include "rational.h" |
| 22 | +#include "rational_tools.h" |
| 23 | +#include "std_expr.h" |
| 24 | +#include "string2int.h" |
| 25 | + |
| 26 | +#include <stack> |
| 27 | +#include <ostream> |
| 28 | + |
| 29 | +/// We use the precendences that most readers expect |
| 30 | +/// (i.e., the ones you learn in primary school), |
| 31 | +/// and stay clear of the surprising ones that C has. |
| 32 | +static bool bracket_subexpression( |
| 33 | + const exprt &sub_expr, |
| 34 | + const exprt &expr) |
| 35 | +{ |
| 36 | + // no need for parentheses whenever the subexpression |
| 37 | + // doesn't have operands |
| 38 | + if(!sub_expr.has_operands()) |
| 39 | + return false; |
| 40 | + |
| 41 | + // * and / bind stronger than + and - |
| 42 | + if( |
| 43 | + (sub_expr.id() == ID_mult || sub_expr.id() == ID_div) && |
| 44 | + (expr.id() == ID_plus || expr.id() == ID_minus)) |
| 45 | + return false; |
| 46 | + |
| 47 | + // ==, !=, <, <=, >, >= bind stronger than && and || |
| 48 | + if( |
| 49 | + (sub_expr.id() == ID_equal || sub_expr.id() == ID_notequal || |
| 50 | + sub_expr.id() == ID_lt || sub_expr.id() == ID_gt || |
| 51 | + sub_expr.id() == ID_le || sub_expr.id() == ID_ge) && |
| 52 | + (expr.id() == ID_and || expr.id() == ID_or)) |
| 53 | + return false; |
| 54 | + |
| 55 | + return true; |
| 56 | +} |
| 57 | + |
| 58 | +/// This formats a multi-ary expression, |
| 59 | +/// adding parentheses where indicated by \ref bracket_subexpression |
| 60 | +static std::ostream &format_rec( |
| 61 | + std::ostream &os, |
| 62 | + const multi_ary_exprt &src) |
| 63 | +{ |
| 64 | + bool first = true; |
| 65 | + |
| 66 | + for(const auto &op : src.operands()) |
| 67 | + { |
| 68 | + if(first) |
| 69 | + first = false; |
| 70 | + else |
| 71 | + os << ' ' << src.id() << ' '; |
| 72 | + |
| 73 | + const bool need_parentheses = bracket_subexpression(op, src); |
| 74 | + |
| 75 | + if(need_parentheses) |
| 76 | + os << '('; |
| 77 | + |
| 78 | + os << format(op); |
| 79 | + |
| 80 | + if(need_parentheses) |
| 81 | + os << ')'; |
| 82 | + } |
| 83 | + |
| 84 | + return os; |
| 85 | +} |
| 86 | + |
| 87 | +/// This formats a binary expression, |
| 88 | +/// which we do as for multi-ary expressions |
| 89 | +static std::ostream &format_rec( |
| 90 | + std::ostream &os, |
| 91 | + const binary_exprt &src) |
| 92 | +{ |
| 93 | + return format_rec(os, to_multi_ary_expr(src)); |
| 94 | +} |
| 95 | + |
| 96 | +/// This formats a unary expression, |
| 97 | +/// adding parentheses very aggressively. |
| 98 | +static std::ostream &format_rec( |
| 99 | + std::ostream &os, |
| 100 | + const unary_exprt &src) |
| 101 | +{ |
| 102 | + if(src.id() == ID_not) |
| 103 | + os << '!'; |
| 104 | + else if(src.id() == ID_unary_minus) |
| 105 | + os << '-'; |
| 106 | + else |
| 107 | + return os << src.pretty(); |
| 108 | + |
| 109 | + if(src.op0().has_operands()) |
| 110 | + return os << '(' << format(src.op0()) << ')'; |
| 111 | + else |
| 112 | + return os << format(src.op0()); |
| 113 | +} |
| 114 | + |
| 115 | +/// This formats a constant |
| 116 | +static std::ostream &format_rec( |
| 117 | + std::ostream &os, |
| 118 | + const constant_exprt &src) |
| 119 | +{ |
| 120 | + auto type = src.type().id(); |
| 121 | + |
| 122 | + if(type == ID_bool) |
| 123 | + { |
| 124 | + if(src.is_true()) |
| 125 | + return os << "true"; |
| 126 | + else if(src.is_false()) |
| 127 | + return os << "false"; |
| 128 | + else |
| 129 | + return os << src.pretty(); |
| 130 | + } |
| 131 | + else if(type == ID_unsignedbv || type == ID_signedbv) |
| 132 | + return os << *numeric_cast<mp_integer>(src); |
| 133 | + else if(type == ID_integer) |
| 134 | + return os << src.get_value(); |
| 135 | + else if(type == ID_floatbv) |
| 136 | + return os << ieee_floatt(src); |
| 137 | + else |
| 138 | + return os << src.pretty(); |
| 139 | +} |
| 140 | + |
| 141 | +// The below generates a string in a generic syntax |
| 142 | +// that is inspired by C/C++/Java, and is meant for debugging |
| 143 | +// purposes. |
| 144 | +std::ostream &format_rec( |
| 145 | + std::ostream &os, |
| 146 | + const exprt &expr) |
| 147 | +{ |
| 148 | + const auto &id = expr.id(); |
| 149 | + |
| 150 | + if(id == ID_plus || id == ID_mult || id == ID_and || id == ID_or) |
| 151 | + return format_rec(os, to_multi_ary_expr(expr)); |
| 152 | + else if( |
| 153 | + id == ID_lt || id == ID_gt || id == ID_ge || id == ID_le || |
| 154 | + id == ID_minus || id == ID_implies || id == ID_equal || id == ID_notequal) |
| 155 | + return format_rec(os, to_binary_expr(expr)); |
| 156 | + else if(id == ID_not || id == ID_unary_minus) |
| 157 | + return format_rec(os, to_unary_expr(expr)); |
| 158 | + else if(id == ID_constant) |
| 159 | + return format_rec(os, to_constant_expr(expr)); |
| 160 | + else if(id == ID_typecast) |
| 161 | + return os << "cast(" << format(to_typecast_expr(expr).op()) << ", " |
| 162 | + << format(expr.type()) << ')'; |
| 163 | + else if(id == ID_member) |
| 164 | + return os << format(to_member_expr(expr).op()) << '.' |
| 165 | + << to_member_expr(expr).get_component_name(); |
| 166 | + else if(id == ID_symbol) |
| 167 | + return os << to_symbol_expr(expr).get_identifier(); |
| 168 | + else if(id == ID_forall || id == ID_exists) |
| 169 | + return os << id << ' ' << format(to_quantifier_expr(expr).symbol()) << " : " |
| 170 | + << format(to_quantifier_expr(expr).symbol().type()) << " . " |
| 171 | + << format(to_quantifier_expr(expr).where()); |
| 172 | + else if(id == ID_let) |
| 173 | + return os << "LET " << format(to_let_expr(expr).symbol()) << " = " |
| 174 | + << format(to_let_expr(expr).value()) << " IN " |
| 175 | + << format(to_let_expr(expr).where()); |
| 176 | + else if(id == ID_array || id == ID_struct) |
| 177 | + { |
| 178 | + os << "{ "; |
| 179 | + |
| 180 | + bool first = true; |
| 181 | + |
| 182 | + for(const auto &op : expr.operands()) |
| 183 | + { |
| 184 | + if(first) |
| 185 | + first = false; |
| 186 | + else |
| 187 | + os << ", "; |
| 188 | + |
| 189 | + os << format(op); |
| 190 | + } |
| 191 | + |
| 192 | + return os << '}'; |
| 193 | + } |
| 194 | + else if(id == ID_if) |
| 195 | + { |
| 196 | + const auto &if_expr = to_if_expr(expr); |
| 197 | + return os << format(if_expr.cond()) << '?' << format(if_expr.true_case()) |
| 198 | + << ':' << format(if_expr.false_case()); |
| 199 | + } |
| 200 | + else |
| 201 | + { |
| 202 | + if(!expr.has_operands()) |
| 203 | + return os << id; |
| 204 | + |
| 205 | + os << id << '('; |
| 206 | + bool first = true; |
| 207 | + |
| 208 | + for(const auto &op : expr.operands()) |
| 209 | + { |
| 210 | + if(first) |
| 211 | + first = false; |
| 212 | + else |
| 213 | + os << ", "; |
| 214 | + |
| 215 | + os << format(op); |
| 216 | + } |
| 217 | + |
| 218 | + return os << ')'; |
| 219 | + } |
| 220 | +} |
0 commit comments