@@ -29,6 +29,8 @@ Author: Daniel Kroening, kroening@kroening.com
2929#include < util/pointer_predicates.h>
3030#include < util/prefix.h>
3131#include < util/range.h>
32+ #include < util/rational.h>
33+ #include < util/rational_tools.h>
3234#include < util/simplify_expr.h>
3335#include < util/std_expr.h>
3436#include < util/string2int.h>
@@ -430,6 +432,26 @@ constant_exprt smt2_convt::parse_literal(
430432 }
431433 else
432434 {
435+ std::size_t pos = s.find (" ." );
436+ if (pos != std::string::npos)
437+ {
438+ // Decimal, return as rational
439+ if (type.id () == ID_rational)
440+ {
441+ rationalt rational_value;
442+ bool failed = to_rational (
443+ constant_exprt{src.id (), rational_typet{}}, rational_value);
444+ CHECK_RETURN (!failed);
445+ return from_rational (rational_value);
446+ }
447+ else
448+ {
449+ UNREACHABLE_BECAUSE (
450+ " smt2_convt::parse_literal parsed a number with a decimal point "
451+ " as type " +
452+ type.id_string ());
453+ }
454+ }
433455 // Numeral
434456 value=string2integer (s);
435457 }
@@ -527,6 +549,11 @@ constant_exprt smt2_convt::parse_literal(
527549 {
528550 return from_integer (value + to_range_type (type).get_from (), type);
529551 }
552+ else if (type.id () == ID_rational)
553+ {
554+ // TODO parse this literal back correctly.
555+ return from_integer (value, type);
556+ }
530557 else
531558 UNREACHABLE_BECAUSE (
532559 " smt2_convt::parse_literal should not be of unsupported type " +
@@ -3167,6 +3194,19 @@ void smt2_convt::convert_typecast(const typecast_exprt &expr)
31673194 convert_typecast (tmp);
31683195 }
31693196 }
3197+ else if (dest_type.id () == ID_rational)
3198+ {
3199+ if (src_type.id () == ID_signedbv)
3200+ {
3201+ // TODO: negative numbers
3202+ out << " (/ " ;
3203+ convert_expr (src);
3204+ out << " 1)" ;
3205+ }
3206+ else
3207+ UNEXPECTEDCASE (
3208+ " Unknown typecast " + src_type.id_string () + " -> rational" );
3209+ }
31703210 else
31713211 UNEXPECTEDCASE (
31723212 " TODO typecast8 " +src_type.id_string ()+" -> " +dest_type.id_string ());
@@ -3588,7 +3628,10 @@ void smt2_convt::convert_constant(const constant_exprt &expr)
35883628 const bool negative = has_prefix (value, " -" );
35893629
35903630 if (negative)
3631+ {
35913632 out << " (- " ;
3633+ value = value.substr (1 );
3634+ }
35923635
35933636 size_t pos=value.find (" /" );
35943637
@@ -4190,6 +4233,16 @@ void smt2_convt::convert_div(const div_exprt &expr)
41904233 // the rounding mode. See smt2_convt::convert_floatbv_div.
41914234 UNREACHABLE;
41924235 }
4236+ else if (
4237+ expr.type ().id () == ID_rational || expr.type ().id () == ID_integer ||
4238+ expr.type ().id () == ID_real)
4239+ {
4240+ out << " (/ " ;
4241+ convert_expr (expr.op0 ());
4242+ out << " " ;
4243+ convert_expr (expr.op1 ());
4244+ out << " )" ;
4245+ }
41934246 else
41944247 UNEXPECTEDCASE (" unsupported type for /: " +expr.type ().id_string ());
41954248}
0 commit comments