-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmtlib2abstractparser.c
670 lines (564 loc) · 23 KB
/
smtlib2abstractparser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/* -*- C -*-
*
* An abstract parser for the SMT-LIB v2 language
*
* Author: Alberto Griggio <[email protected]>
*
* Copyright (C) 2010 Alberto Griggio
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "smtlib2abstractparser.h"
#include "smtlib2abstractparser_private.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
void smtlib2_abstract_parser_init(smtlib2_abstract_parser *p,
smtlib2_context ctx)
{
smtlib2_parser_interface *pi;
p->termparser_ = smtlib2_term_parser_new(ctx);
p->outstream_ = stdout;
p->errstream_ = stderr;
p->response_ = SMTLIB2_RESPONSE_SUCCESS;
p->print_success_ = true;
p->pvt_errmsg_ = NULL;
p->response_data_ = smtlib2_vector_new();
p->status_ = SMTLIB2_STATUS_UNKNOWN;
p->set_logic_ok_ = true;
p->exiting_ = false;
p->info_ = smtlib2_hashtable_new(smtlib2_hashfun_str, smtlib2_eqfun_str);
p->internal_parsed_terms_ = smtlib2_vector_new();
p->internal_parsed_terms_enabled_ = false;
p->scanner_ = NULL;
/* set the default interface */
pi = SMTLIB2_PARSER_INTERFACE(p);
pi->set_logic = smtlib2_abstract_parser_set_logic;
pi->declare_sort = smtlib2_abstract_parser_declare_sort;
pi->define_sort = smtlib2_abstract_parser_define_sort;
pi->declare_function = smtlib2_abstract_parser_declare_function;
pi->declare_variable = smtlib2_abstract_parser_declare_variable;
pi->define_function = smtlib2_abstract_parser_define_function;
pi->push = smtlib2_abstract_parser_push;
pi->pop = smtlib2_abstract_parser_pop;
pi->assert_formula = smtlib2_abstract_parser_assert_formula;
pi->check_sat = smtlib2_abstract_parser_check_sat;
pi->get_assignment = smtlib2_abstract_parser_get_assignment;
pi->get_assertions = smtlib2_abstract_parser_get_assertions;
pi->get_unsat_core = smtlib2_abstract_parser_get_unsat_core;
pi->get_proof = smtlib2_abstract_parser_get_proof;
pi->set_str_option = smtlib2_abstract_parser_set_str_option;
pi->set_int_option = smtlib2_abstract_parser_set_int_option;
pi->set_rat_option = smtlib2_abstract_parser_set_rat_option;
pi->get_info = smtlib2_abstract_parser_get_info;
pi->set_info = smtlib2_abstract_parser_set_info;
pi->get_value = smtlib2_abstract_parser_get_value;
pi->get_model = smtlib2_abstract_parser_get_model;
pi->exit = smtlib2_abstract_parser_exit;
pi->handle_error = smtlib2_abstract_parser_handle_error;
pi->set_internal_parsed_terms = smtlib2_abstract_parser_set_internal_parsed_terms;
pi->push_let_scope = smtlib2_abstract_parser_push_let_scope;
pi->pop_let_scope = smtlib2_abstract_parser_pop_let_scope;
pi->push_quantifier_scope = smtlib2_abstract_parser_push_quantifier_scope;
pi->pop_quantifier_scope = smtlib2_abstract_parser_pop_quantifier_scope;
pi->push_sort_param_scope = smtlib2_abstract_parser_push_sort_param_scope;
pi->pop_sort_param_scope = smtlib2_abstract_parser_pop_sort_param_scope;
pi->make_term = smtlib2_abstract_parser_make_term;
pi->make_number_term = smtlib2_abstract_parser_make_number_term;
pi->make_forall_term = smtlib2_abstract_parser_make_forall_term;
pi->make_exists_term = smtlib2_abstract_parser_make_exists_term;
pi->annotate_term = smtlib2_abstract_parser_annotate_term;
pi->define_let_binding = smtlib2_abstract_parser_define_let_binding;
pi->make_sort = smtlib2_abstract_parser_make_sort;
pi->make_parametric_sort = smtlib2_abstract_parser_make_parametric_sort;
pi->make_function_sort = smtlib2_abstract_parser_make_function_sort;
}
void smtlib2_abstract_parser_deinit(smtlib2_abstract_parser *p)
{
smtlib2_vector_delete(p->internal_parsed_terms_);
smtlib2_hashtable_delete(p->info_,
(smtlib2_freefun)free, (smtlib2_freefun)free);
smtlib2_vector_delete(p->response_data_);
smtlib2_term_parser_delete(p->termparser_);
}
void smtlib2_abstract_parser_parse(smtlib2_abstract_parser *p, FILE *src)
{
smtlib2_fstream *stream;
smtlib2_scanner *scanner;
stream = smtlib2_fstream_new(src);
scanner = smtlib2_scanner_new((smtlib2_stream *)stream);
smtlib2_abstract_parser_reset_response(p);
while (!smtlib2_stream_eof((smtlib2_stream *)stream)) {
smtlib2_parse(scanner, SMTLIB2_PARSER_INTERFACE(p));
if (p->exiting_) {
break;
}
if (!smtlib2_stream_eof((smtlib2_stream *)stream)) {
smtlib2_abstract_parser_print_response(p);
smtlib2_abstract_parser_reset_response(p);
}
}
smtlib2_scanner_delete(scanner);
smtlib2_fstream_delete(stream);
}
void smtlib2_abstract_parser_set_logic(smtlib2_parser_interface *p,
const char *logic)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
if (pp->set_logic_ok_) {
pp->set_logic_ok_ = false;
} else {
smtlib2_abstract_parser_set_error(pp, "set-logic can't be called at this point");
}
}
void smtlib2_abstract_parser_declare_sort(smtlib2_parser_interface *p,
const char *sortname,
int arity)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_abstract_parser_set_error(pp, "unimplemented (declare-sort)");
}
void smtlib2_abstract_parser_define_sort(smtlib2_parser_interface *p,
const char *sortname,
smtlib2_vector *params,
smtlib2_sort sort)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_abstract_parser_set_error(pp, "unimplemented (define-sort)");
}
void smtlib2_abstract_parser_push_sort_param_scope(smtlib2_parser_interface *p)
{
/* nothing to do */
}
void smtlib2_abstract_parser_pop_sort_param_scope(smtlib2_parser_interface *p)
{
/* nothing to do */
}
void smtlib2_abstract_parser_declare_function(smtlib2_parser_interface *p,
const char *name,
smtlib2_sort sort)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_abstract_parser_set_error(pp, "unimplemented (declare-fun)");
}
void smtlib2_abstract_parser_declare_variable(smtlib2_parser_interface *p,
const char *name,
smtlib2_sort sort)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_abstract_parser_set_error(pp, "unimplemented (declare_variable)");
}
void smtlib2_abstract_parser_define_function(smtlib2_parser_interface *p,
const char *name,
smtlib2_vector *params,
smtlib2_sort sort,
smtlib2_term term)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
if (pp->response_ != SMTLIB2_RESPONSE_ERROR) {
smtlib2_term_parser_define_binding(pp->termparser_, name, params, term);
if (smtlib2_term_parser_error(pp->termparser_)) {
smtlib2_abstract_parser_set_error(pp, "%s", smtlib2_term_parser_get_error_msg(pp->termparser_));
} else {
pp->response_ = SMTLIB2_RESPONSE_SUCCESS;
}
}
}
void smtlib2_abstract_parser_push(smtlib2_parser_interface *p, int n)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_abstract_parser_set_error(pp, "unimplemented (push)");
}
void smtlib2_abstract_parser_pop(smtlib2_parser_interface *p, int n)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_abstract_parser_set_error(pp, "unimplemented (pop)");
}
void smtlib2_abstract_parser_assert_formula(smtlib2_parser_interface *p,
smtlib2_term term)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_abstract_parser_set_error(pp, "unimplemented (assert)");
}
void smtlib2_abstract_parser_check_sat(smtlib2_parser_interface *p)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_abstract_parser_set_error(pp, "unimplemented (check-sat)");
}
void smtlib2_abstract_parser_get_unsat_core(smtlib2_parser_interface *p)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_abstract_parser_set_error(pp, "unimplemented (get-unsat-core)");
}
void smtlib2_abstract_parser_get_proof(smtlib2_parser_interface *p)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_abstract_parser_set_error(pp, "unimplemented (get-proof)");
}
void smtlib2_abstract_parser_set_str_option(smtlib2_parser_interface *p,
const char *keyword,
const char *value)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
if (pp->response_ == SMTLIB2_RESPONSE_SUCCESS) {
FILE **which = NULL;
if (strcmp(keyword, ":regular-output-channel") == 0) {
which = &(pp->outstream_);
} else if (strcmp(keyword, ":diagnostic-output-channel") == 0) {
which = &(pp->errstream_);
} else {
pp->response_ = SMTLIB2_RESPONSE_UNSUPPORTED;
}
if (which != NULL) {
if (*which != stdout && *which != stderr) {
fclose(*which);
}
if (strcmp(value, "stdout") == 0) {
*which = stdout;
} else if (strcmp(value, "stderr") == 0) {
*which = stderr;
} else {
*which = fopen(value, "w");
if (!*which) {
smtlib2_abstract_parser_set_error(pp, "Impossible to open `%s' as `%s'", value, keyword);
if (strcmp(keyword, ":regular-output-channel") == 0) {
*which = stdout;
} else {
*which = stderr;
}
}
}
}
}
}
void smtlib2_abstract_parser_set_int_option(smtlib2_parser_interface *p,
const char *keyword,
int value)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
if (pp->response_ == SMTLIB2_RESPONSE_SUCCESS) {
if (strcmp(keyword, ":print-success") == 0) {
pp->print_success_ = value;
} else {
pp->response_ = SMTLIB2_RESPONSE_UNSUPPORTED;
}
}
}
void smtlib2_abstract_parser_set_rat_option(smtlib2_parser_interface *p,
const char *keyword,
double value)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
pp->response_ = SMTLIB2_RESPONSE_UNSUPPORTED;
}
void smtlib2_abstract_parser_get_info(smtlib2_parser_interface *p,
const char *keyword)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
if (pp->response_ != SMTLIB2_RESPONSE_ERROR) {
intptr_t v;
if (smtlib2_hashtable_find(pp->info_, (intptr_t)keyword, &v)) {
smtlib2_vector_push(pp->response_data_, (intptr_t)keyword);
smtlib2_vector_push(pp->response_data_, v);
pp->response_ = SMTLIB2_RESPONSE_INFO;
} else {
pp->response_ = SMTLIB2_RESPONSE_UNSUPPORTED;
}
}
}
void smtlib2_abstract_parser_set_info(smtlib2_parser_interface *p,
const char *keyword,
const char *value)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
if (pp->response_ != SMTLIB2_RESPONSE_ERROR) {
smtlib2_hashtable_set(pp->info_, (intptr_t)smtlib2_strdup(keyword),
(intptr_t)smtlib2_strdup(value));
pp->response_ = SMTLIB2_RESPONSE_SUCCESS;
}
}
void smtlib2_abstract_parser_get_assignment(smtlib2_parser_interface *p)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_abstract_parser_set_error(pp, "unimplemented (get-assignment)");
}
void smtlib2_abstract_parser_get_assertions(smtlib2_parser_interface *p)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_abstract_parser_set_error(pp, "unimplemented (get-assertions)");
}
void smtlib2_abstract_parser_get_value(smtlib2_parser_interface *p,
smtlib2_vector *terms)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_abstract_parser_set_error(pp, "unimplemented (get-value)");
}
void smtlib2_abstract_parser_get_model(smtlib2_parser_interface *p)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_abstract_parser_set_error(pp, "unimplemented (get-model)");
}
void smtlib2_abstract_parser_exit(smtlib2_parser_interface *p)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
pp->exiting_ = true;
}
void smtlib2_abstract_parser_set_internal_parsed_terms(
smtlib2_parser_interface *p,
smtlib2_vector *terms)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
if (pp->response_ != SMTLIB2_RESPONSE_ERROR) {
if (pp->internal_parsed_terms_enabled_) {
smtlib2_vector_swap(pp->internal_parsed_terms_, terms);
} else {
smtlib2_abstract_parser_set_error(pp, "unknown command `.internal-parse-terms'");
}
}
}
void smtlib2_abstract_parser_handle_error(smtlib2_parser_interface *p,
const char *msg)
{
// TODO: refactor this to include the location of the error?
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_abstract_parser_set_error(pp, "%s", msg);
printf("handle_error(%s)\n", msg);
}
void smtlib2_abstract_parser_push_let_scope(smtlib2_parser_interface *p)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
if (pp->response_ != SMTLIB2_RESPONSE_ERROR) {
smtlib2_term_parser_push_let_scope(pp->termparser_);
if (smtlib2_term_parser_error(pp->termparser_)) {
smtlib2_abstract_parser_set_error(pp, "%s", smtlib2_term_parser_get_error_msg(pp->termparser_));
}
}
}
smtlib2_term smtlib2_abstract_parser_pop_let_scope(smtlib2_parser_interface *p)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
if (pp->response_ != SMTLIB2_RESPONSE_ERROR) {
smtlib2_term_parser_pop_let_scope(pp->termparser_);
if (smtlib2_term_parser_error(pp->termparser_)) {
smtlib2_abstract_parser_set_error(pp, "%s", smtlib2_term_parser_get_error_msg(pp->termparser_));
}
}
return NULL;
}
smtlib2_term smtlib2_abstract_parser_push_quantifier_scope(smtlib2_parser_interface *p)
{
/* nothing to do */
return NULL;
}
smtlib2_term smtlib2_abstract_parser_pop_quantifier_scope(smtlib2_parser_interface *p)
{
/* nothing to do */
return NULL;
}
smtlib2_term smtlib2_abstract_parser_make_term(smtlib2_parser_interface *p,
const char *symbol,
smtlib2_sort sort,
smtlib2_vector *index,
smtlib2_vector *args)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_term ret = NULL;
if (pp->response_ != SMTLIB2_RESPONSE_ERROR) {
ret = smtlib2_term_parser_make_term(pp->termparser_, symbol, sort,
index, args);
if (smtlib2_term_parser_error(pp->termparser_)) {
smtlib2_abstract_parser_set_error(pp, "%s", smtlib2_term_parser_get_error_msg(pp->termparser_));
}
}
return ret;
}
smtlib2_term smtlib2_abstract_parser_make_number_term(
smtlib2_parser_interface *p,
const char *numval,
int width,
int base)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_term ret = NULL;
if (pp->response_ != SMTLIB2_RESPONSE_ERROR) {
ret = smtlib2_term_parser_make_number_term(pp->termparser_,
numval, width, base);
if (smtlib2_term_parser_error(pp->termparser_)) {
smtlib2_abstract_parser_set_error(pp, "%s", smtlib2_term_parser_get_error_msg(pp->termparser_));
}
}
return ret;
}
smtlib2_term smtlib2_abstract_parser_make_forall_term(
smtlib2_parser_interface *p,
smtlib2_term term
)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_abstract_parser_set_error(pp, "unimplemented (make_forall_term)");
return NULL;
}
smtlib2_term smtlib2_abstract_parser_make_exists_term(
smtlib2_parser_interface *p,
smtlib2_term term
)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_abstract_parser_set_error(pp, "unimplemented (make_exists_term)");
return NULL;
}
void smtlib2_abstract_parser_annotate_term(smtlib2_parser_interface *p,
smtlib2_term term,
smtlib2_vector *annotations)
{
/* do nothing for now */
}
void smtlib2_abstract_parser_define_let_binding(smtlib2_parser_interface *p,
const char *symbol,
smtlib2_term term)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
if (pp->response_ != SMTLIB2_RESPONSE_ERROR) {
smtlib2_term_parser_define_let_binding(pp->termparser_, symbol, term);
if (smtlib2_term_parser_error(pp->termparser_)) {
smtlib2_abstract_parser_set_error(pp, "%s", smtlib2_term_parser_get_error_msg(pp->termparser_));
}
}
}
smtlib2_sort smtlib2_abstract_parser_make_sort(smtlib2_parser_interface *p,
const char *sortname,
smtlib2_vector *index)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_abstract_parser_set_error(pp, "unimplemented (make_sort)");
return NULL;
}
smtlib2_sort smtlib2_abstract_parser_make_parametric_sort(
smtlib2_parser_interface *p,
const char *name,
smtlib2_vector *tps)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_abstract_parser_set_error(pp, "unimplemented (make_parametric_sort)");
return NULL;
}
smtlib2_sort smtlib2_abstract_parser_make_function_sort(
smtlib2_parser_interface *p,
smtlib2_vector *tps)
{
smtlib2_abstract_parser *pp = (smtlib2_abstract_parser *)p;
smtlib2_abstract_parser_set_error(pp, "unimplemented (make_function_sort)");
return NULL;
}
void smtlib2_abstract_parser_print_response(smtlib2_abstract_parser *p)
{
switch (p->response_) {
case SMTLIB2_RESPONSE_SUCCESS:
if (p->print_success_) {
fprintf(p->outstream_, "success\n");
}
break;
case SMTLIB2_RESPONSE_ERROR:
fprintf(p->errstream_, "(error \"%s\")\n", p->pvt_errmsg_);
break;
case SMTLIB2_RESPONSE_UNSUPPORTED:
fprintf(p->outstream_, "unsupported\n");
break;
case SMTLIB2_RESPONSE_STATUS:
switch (p->status_) {
case SMTLIB2_STATUS_UNSAT: fprintf(p->outstream_, "unsat\n"); break;
case SMTLIB2_STATUS_SAT: fprintf(p->outstream_, "sat\n"); break;
default: fprintf(p->outstream_, "unknown\n");
}
break;
case SMTLIB2_RESPONSE_INFO:
case SMTLIB2_RESPONSE_MODEL: {
bool first = true;
size_t i;
bool parens = (p->response_ == SMTLIB2_RESPONSE_MODEL);
fprintf(p->outstream_, "( ");
for (i = 0; i < smtlib2_vector_size(p->response_data_); i += 2) {
if (!first) {
fprintf(p->outstream_, "\n ");
} else {
first = false;
}
if (parens) fputc('(', p->outstream_);
fprintf(p->outstream_, "%s %s",
(const char *)smtlib2_vector_at(p->response_data_, i),
(const char *)smtlib2_vector_at(p->response_data_, i+1));
if (parens) fputc(')', p->outstream_);
}
fprintf(p->outstream_, " )\n");
}
break;
case SMTLIB2_RESPONSE_UNSATCORE: {
bool first = true;
size_t i;
fprintf(p->outstream_, "( ");
for (i = 0; i < smtlib2_vector_size(p->response_data_); ++i) {
if (!first) {
fprintf(p->outstream_, "\n ");
} else {
first = false;
}
fprintf(p->outstream_, "%s",
(const char *)smtlib2_vector_at(p->response_data_, i));
}
fprintf(p->outstream_, " )\n");
}
break;
}
fflush(p->outstream_);
fflush(p->errstream_);
}
void smtlib2_abstract_parser_reset_response(smtlib2_abstract_parser *p)
{
p->response_ = SMTLIB2_RESPONSE_SUCCESS;
if (p->pvt_errmsg_) {
free(p->pvt_errmsg_);
p->pvt_errmsg_ = NULL;
}
smtlib2_vector_clear(p->response_data_);
}
void smtlib2_abstract_parser_set_errorv(smtlib2_abstract_parser *p, const char *fmt, va_list args)
{
char *ret = smtlib2_vsprintf(fmt, args);
// Append the error if one already exists
p->response_ = SMTLIB2_RESPONSE_ERROR;
if(p->pvt_errmsg_ != NULL) {
int len1 = strlen(p->pvt_errmsg_);
int len2 = strlen(ret);
char* tmp = (char*)malloc(len1 + len2 + 2);
memcpy(tmp, p->pvt_errmsg_, len1);
tmp[len1] = ';';
memcpy(tmp + len1 + 1, ret, len2 + 1);
free(p->pvt_errmsg_);
p->pvt_errmsg_ = tmp;
} else {
p->pvt_errmsg_ = ret;
}
}
void smtlib2_abstract_parser_set_error(smtlib2_abstract_parser *p, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
smtlib2_abstract_parser_set_errorv(p, fmt, args);
va_end(args);
}