@@ -29,6 +29,8 @@ Author: Daniel Kroening, kroening@kroening.com
29
29
#include < util/pointer_predicates.h>
30
30
#include < util/prefix.h>
31
31
#include < util/range.h>
32
+ #include < util/rational.h>
33
+ #include < util/rational_tools.h>
32
34
#include < util/simplify_expr.h>
33
35
#include < util/std_expr.h>
34
36
#include < util/string2int.h>
@@ -430,6 +432,26 @@ constant_exprt smt2_convt::parse_literal(
430
432
}
431
433
else
432
434
{
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
+ }
433
455
// Numeral
434
456
value=string2integer (s);
435
457
}
@@ -527,6 +549,11 @@ constant_exprt smt2_convt::parse_literal(
527
549
{
528
550
return from_integer (value + to_range_type (type).get_from (), type);
529
551
}
552
+ else if (type.id () == ID_rational)
553
+ {
554
+ // TODO parse this literal back correctly.
555
+ return from_integer (value, type);
556
+ }
530
557
else
531
558
UNREACHABLE_BECAUSE (
532
559
" smt2_convt::parse_literal should not be of unsupported type " +
@@ -3167,6 +3194,19 @@ void smt2_convt::convert_typecast(const typecast_exprt &expr)
3167
3194
convert_typecast (tmp);
3168
3195
}
3169
3196
}
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
+ }
3170
3210
else
3171
3211
UNEXPECTEDCASE (
3172
3212
" TODO typecast8 " +src_type.id_string ()+" -> " +dest_type.id_string ());
@@ -3588,7 +3628,10 @@ void smt2_convt::convert_constant(const constant_exprt &expr)
3588
3628
const bool negative = has_prefix (value, " -" );
3589
3629
3590
3630
if (negative)
3631
+ {
3591
3632
out << " (- " ;
3633
+ value = value.substr (1 );
3634
+ }
3592
3635
3593
3636
size_t pos=value.find (" /" );
3594
3637
@@ -4190,6 +4233,16 @@ void smt2_convt::convert_div(const div_exprt &expr)
4190
4233
// the rounding mode. See smt2_convt::convert_floatbv_div.
4191
4234
UNREACHABLE;
4192
4235
}
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
+ }
4193
4246
else
4194
4247
UNEXPECTEDCASE (" unsupported type for /: " +expr.type ().id_string ());
4195
4248
}
0 commit comments