Skip to content

Commit 2b22106

Browse files
committed
SMT-LIB2 parser: extract repeated code for operand type checking
This extracts repeated code for checking that the types of an SMT-LIB2 expression are the same into a method.
1 parent 1abed03 commit 2b22106

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

src/solvers/smt2/smt2_parser.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -345,11 +345,9 @@ exprt smt2_parsert::cast_bv_to_unsigned(const exprt &expr)
345345
return typecast_exprt(expr, unsignedbv_typet(width));
346346
}
347347

348-
exprt smt2_parsert::multi_ary(irep_idt id, const exprt::operandst &op)
348+
void smt2_parsert::check_matching_operand_types(
349+
const exprt::operandst &op) const
349350
{
350-
if(op.empty())
351-
throw error("expression must have at least one operand");
352-
353351
for(std::size_t i = 1; i < op.size(); i++)
354352
{
355353
if(op[i].type() != op[0].type())
@@ -360,6 +358,14 @@ exprt smt2_parsert::multi_ary(irep_idt id, const exprt::operandst &op)
360358
<< smt2_format(op[i].type()) << '\'';
361359
}
362360
}
361+
}
362+
363+
exprt smt2_parsert::multi_ary(irep_idt id, const exprt::operandst &op)
364+
{
365+
if(op.empty())
366+
throw error("expression must have at least one operand");
367+
368+
check_matching_operand_types(op);
363369

364370
exprt result(id, op[0].type());
365371
result.operands() = op;
@@ -371,13 +377,7 @@ exprt smt2_parsert::binary_predicate(irep_idt id, const exprt::operandst &op)
371377
if(op.size()!=2)
372378
throw error("expression must have two operands");
373379

374-
if(op[0].type() != op[1].type())
375-
{
376-
throw error() << "expression must have operands with matching types,"
377-
" but got '"
378-
<< smt2_format(op[0].type()) << "' and '"
379-
<< smt2_format(op[1].type()) << '\'';
380-
}
380+
check_matching_operand_types(op);
381381

382382
return binary_predicate_exprt(op[0], id, op[1]);
383383
}
@@ -395,8 +395,7 @@ exprt smt2_parsert::binary(irep_idt id, const exprt::operandst &op)
395395
if(op.size()!=2)
396396
throw error("expression must have two operands");
397397

398-
if(op[0].type() != op[1].type())
399-
throw error("expression must have operands with matching types");
398+
check_matching_operand_types(op);
400399

401400
return binary_exprt(op[0], id, op[1], op[0].type());
402401
}

src/solvers/smt2/smt2_parser.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ class smt2_parsert
7575

7676
bool exit;
7777

78-
smt2_tokenizert::smt2_errort error(const std::string &message)
78+
smt2_tokenizert::smt2_errort error(const std::string &message) const
7979
{
8080
return smt2_tokenizer.error(message);
8181
}
8282

83-
smt2_tokenizert::smt2_errort error()
83+
smt2_tokenizert::smt2_errort error() const
8484
{
8585
return smt2_tokenizer.error();
8686
}
@@ -158,6 +158,7 @@ class smt2_parsert
158158
exprt::operandst operands();
159159
typet function_signature_declaration();
160160
signature_with_parameter_idst function_signature_definition();
161+
void check_matching_operand_types(const exprt::operandst &) const;
161162
exprt multi_ary(irep_idt, const exprt::operandst &);
162163
exprt binary_predicate(irep_idt, const exprt::operandst &);
163164
exprt binary(irep_idt, const exprt::operandst &);

src/solvers/smt2/smt2_tokenizer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ class smt2_tokenizert
9292
}
9393

9494
/// generate an error exception, pre-filled with a message
95-
smt2_errort error(const std::string &message)
95+
smt2_errort error(const std::string &message) const
9696
{
9797
return smt2_errort(message, line_no);
9898
}
9999

100100
/// generate an error exception
101-
smt2_errort error()
101+
smt2_errort error() const
102102
{
103103
return smt2_errort(line_no);
104104
}

0 commit comments

Comments
 (0)