summaryrefslogtreecommitdiff
path: root/src/main/main.cpp
blob: f5e53f34ace5955fc04bd9f909fb90efa54724ba (plain)
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
/*********************                                                        */
/** main.cpp
 ** Original author: mdeters
 ** Major contributors: barrett, dejan
 ** Minor contributors (to current version): cconway
 ** This file is part of the CVC4 prototype.
 ** Copyright (c) 2009, 2010  The Analysis of Computer Systems Group (ACSys)
 ** Courant Institute of Mathematical Sciences
 ** New York University
 ** See the file COPYING in the top-level source directory for licensing
 ** information.
 **
 ** Main driver for CVC4 executable.
 **/

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <new>

#include "config.h"
#include "main.h"
#include "usage.h"
#include "parser/parser.h"
#include "expr/expr_manager.h"
#include "smt/smt_engine.h"
#include "expr/command.h"
#include "util/result.h"
#include "util/Assert.h"
#include "util/output.h"
#include "util/options.h"

using namespace std;
using namespace CVC4;
using namespace CVC4::parser;
using namespace CVC4::main;

static Result lastResult;
static struct Options options;

int runCvc4(int argc, char* argv[]);
void doCommand(SmtEngine&, Command*);

/**
 * CVC4's main() routine is just an exception-safe wrapper around CVC4.
 * Please don't construct anything here.  Even if the constructor is
 * inside the try { }, an exception during destruction can cause
 * problems.  That's why main() wraps runCvc4() in the first place.
 * Put everything in runCvc4().
 */
int main(int argc, char* argv[]) {
  try {
    return runCvc4(argc, argv);
  } catch(OptionException& e) {
    if(options.smtcomp_mode) {
      cout << "unknown" << endl;
    }
    cerr << "CVC4 Error:" << endl << e << endl;
    printf(usage, options.binary_name.c_str());
    abort();
  } catch(Exception& e) {
    if(options.smtcomp_mode) {
      cout << "unknown" << endl;
    }
    cerr << "CVC4 Error:" << endl << e << endl;
    abort();
  } catch(bad_alloc) {
    if(options.smtcomp_mode) {
      cout << "unknown" << endl;
    }
    cerr << "CVC4 ran out of memory." << endl;
    abort();
  } catch(...) {
    if(options.smtcomp_mode) {
      cout << "unknown" << endl;
    }
    cerr << "CVC4 threw an exception of unknown type." << endl;
    abort();
  }
}

int runCvc4(int argc, char* argv[]) {

  // Initialize the signal handlers
  cvc4_init();

  // Parse the options
  int firstArgIndex = parseOptions(argc, argv, &options);

  // If in competition mode, set output stream option to flush immediately
  if(options.smtcomp_mode) {
    cout << unitbuf;
  }

  // We only accept one input file
  if(argc > firstArgIndex + 1) {
    throw Exception("Too many input files specified.");
  }

  // Create the expression manager
  ExprManager exprMgr;

  // Create the SmtEngine
  SmtEngine smt(&exprMgr, &options);

  // If no file supplied we read from standard input
  bool inputFromStdin = firstArgIndex >= argc || !strcmp("-", argv[firstArgIndex]);

  // Auto-detect input language by filename extension
  if(!inputFromStdin && options.lang == Parser::LANG_AUTO) {
    const char* filename = argv[firstArgIndex];
    unsigned len = strlen(filename);
    if(len >= 4 && !strcmp(".smt", filename + len - 4)) {
      options.lang = Parser::LANG_SMTLIB;
    } else if(( len >= 4 && !strcmp(".cvc", filename + len - 4) )
              || ( len >= 5 && !strcmp(".cvc4", filename + len - 5) )) {
      options.lang = Parser::LANG_CVC4;
    }
  }

  // Determine which messages to show based on smtcomp_mode and verbosity
  if(options.smtcomp_mode) {
    Debug.setStream(CVC4::null_os);
    Trace.setStream(CVC4::null_os);
    Notice.setStream(CVC4::null_os);
    Chat.setStream(CVC4::null_os);
    Message.setStream(CVC4::null_os);
    Warning.setStream(CVC4::null_os);
  } else {
    if(options.verbosity < 2) {
      Chat.setStream(CVC4::null_os);
    }
    if(options.verbosity < 1) {
      Notice.setStream(CVC4::null_os);
    }
    if(options.verbosity < 0) {
      Message.setStream(CVC4::null_os);
      Warning.setStream(CVC4::null_os);
    }
  }

  // Create the parser
  Parser* parser;
  istream* input = NULL;

  if(inputFromStdin) {
    parser = Parser::getNewParser(&exprMgr, options.lang, cin, "<stdin>");
  } else if( options.memoryMap ) {
    parser = Parser::getMemoryMappedParser(&exprMgr, options.lang, argv[firstArgIndex]);
  } else {
    string filename = argv[firstArgIndex];
    input = new ifstream(filename.c_str());
    if(!*input) {
      throw Exception("file does not exist or is unreadable: " + filename);
    }
    parser = Parser::getNewParser(&exprMgr, options.lang, *input, filename);
  }

  if(!options.semanticChecks) {
    parser->disableChecks();
  }

  // Parse and execute commands until we are done
  Command* cmd;
  while((cmd = parser->parseNextCommand())) {
    if( !options.parseOnly ) {
      doCommand(smt, cmd);
    }
    delete cmd;
  }

  // Remove the parser
  delete parser;
  delete input;

  switch(lastResult.asSatisfiabilityResult().isSAT()) {

  case Result::SAT:
    return 10;

  case Result::UNSAT:
    return 20;

  default:
    return 0;

  }
}

void doCommand(SmtEngine& smt, Command* cmd) {
  CommandSequence *seq = dynamic_cast<CommandSequence*>(cmd);
  if(seq != NULL) {
    for(CommandSequence::iterator subcmd = seq->begin();
        subcmd != seq->end();
        ++subcmd) {
      doCommand(smt, *subcmd);
    }
  } else {
    if(options.verbosity > 0) {
      cout << "Invoking: " << *cmd << endl;
    }

    cmd->invoke(&smt);

    QueryCommand *qc = dynamic_cast<QueryCommand*>(cmd);
    if(qc != NULL) {
      lastResult = qc->getResult();
      if(options.verbosity >= 0) {
        cout << lastResult << endl;
      }
    } else {
      CheckSatCommand *csc = dynamic_cast<CheckSatCommand*>(cmd);
      if(csc != NULL) {
        lastResult = csc->getResult();
        if(options.verbosity >= 0) {
          cout << lastResult << endl;
        }
      }
    }
  }
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback