3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-28 03:15:50 +00:00

Optimize calls to Z3_eval_smtlib2_string (#6422)

* Allow reseting the stream of smt2::scanner

* Put the parser of parse_smt2_commands in the cmd_context

* Move parser streams to cmd_context

* Move parser fields from cmd_context to api::context

* Move forward declarations from cmd_context.h to api_context.h

* Change parse_smt2_commands_with_parser to use *& instead of **

* Add tests for Z3_eval_smtlib2_string

* Don't reuse the streams in Z3_eval_smtlib2_string

* Fix indentation

* Add back unnecessary deleted line

Co-authored-by: Nuno Lopes <nuno.lopes@tecnico.ulisboa.pt>
This commit is contained in:
Facundo Domínguez 2022-10-28 17:57:22 -03:00 committed by GitHub
parent a409a4a677
commit 91cdc082c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 154 additions and 9 deletions

View file

@ -3105,6 +3105,10 @@ namespace smt2 {
}
void reset_input(std::istream & is, bool interactive) {
m_scanner.reset_input(is, interactive);
}
sexpr_ref parse_sexpr_ref() {
m_num_bindings = 0;
m_num_open_paren = 0;
@ -3204,6 +3208,8 @@ namespace smt2 {
}
}
};
void free_parser(parser * p) { dealloc(p); }
};
bool parse_smt2_commands(cmd_context & ctx, std::istream & is, bool interactive, params_ref const & ps, char const * filename) {
@ -3211,6 +3217,14 @@ bool parse_smt2_commands(cmd_context & ctx, std::istream & is, bool interactive,
return p();
}
bool parse_smt2_commands_with_parser(class smt2::parser *& p, cmd_context & ctx, std::istream & is, bool interactive, params_ref const & ps, char const * filename) {
if (p)
p->reset_input(is, interactive);
else
p = alloc(smt2::parser, ctx, is, interactive, ps, filename);
return (*p)();
}
sort_ref parse_smt2_sort(cmd_context & ctx, std::istream & is, bool interactive, params_ref const & ps, char const * filename) {
smt2::parser p(ctx, is, interactive, ps, filename);
return p.parse_sort_ref(filename);

View file

@ -20,7 +20,14 @@ Revision History:
#include "cmd_context/cmd_context.h"
bool parse_smt2_commands(cmd_context & ctx, std::istream & is, bool interactive = false, params_ref const & p = params_ref(), char const * filename = nullptr);
namespace smt2 {
class parser;
void free_parser(parser * p);
}
bool parse_smt2_commands(cmd_context & ctx, std::istream & is, bool interactive = false, params_ref const & ps = params_ref(), char const * filename = nullptr);
bool parse_smt2_commands_with_parser(class smt2::parser *& p, cmd_context & ctx, std::istream & is, bool interactive = false, params_ref const & ps = params_ref(), char const * filename = nullptr);
sexpr_ref parse_sexpr(cmd_context& ctx, std::istream& is, params_ref const& ps, char const* filename);

View file

@ -27,8 +27,8 @@ namespace smt2 {
if (m_at_eof)
throw scanner_exception("unexpected end of file");
if (m_interactive) {
m_curr = m_stream.get();
if (m_stream.eof())
m_curr = m_stream->get();
if (m_stream->eof())
m_at_eof = true;
}
else if (m_bpos < m_bend) {
@ -36,8 +36,8 @@ namespace smt2 {
m_bpos++;
}
else {
m_stream.read(m_buffer, SCANNER_BUFFER_SIZE);
m_bend = static_cast<unsigned>(m_stream.gcount());
m_stream->read(m_buffer, SCANNER_BUFFER_SIZE);
m_bend = static_cast<unsigned>(m_stream->gcount());
m_bpos = 0;
if (m_bpos == m_bend) {
m_at_eof = true;
@ -281,7 +281,7 @@ namespace smt2 {
m_bv_size(UINT_MAX),
m_bpos(0),
m_bend(0),
m_stream(stream),
m_stream(&stream),
m_cache_input(false) {
@ -390,5 +390,13 @@ namespace smt2 {
return m_cache_result.begin();
}
void scanner::reset_input(std::istream & stream, bool interactive) {
m_stream = &stream;
m_interactive = interactive;
m_at_eof = false;
m_bpos = 0;
m_bend = 0;
next();
}
};

View file

@ -49,7 +49,7 @@ namespace smt2 {
unsigned m_bpos;
unsigned m_bend;
svector<char> m_string;
std::istream& m_stream;
std::istream* m_stream;
bool m_cache_input;
svector<char> m_cache;
@ -99,6 +99,7 @@ namespace smt2 {
void stop_caching() { m_cache_input = false; }
unsigned cache_size() const { return m_cache.size(); }
void reset_cache() { m_cache.reset(); }
void reset_input(std::istream & stream, bool interactive = false);
char const * cached_str(unsigned begin, unsigned end);
};