| 1 |
//---------------------------------------------------------------------------- |
| 2 |
// qcrun.cpp |
| 3 |
// Interpreter for ntermediate code |
| 4 |
// $Date: 2003/02/20 00:24:16 $ |
| 5 |
// $Revision: 1.8 $ |
| 6 |
//---------------------------------------------------------------------------- |
| 7 |
#include <iostream> |
| 8 |
#include <iomanip> |
| 9 |
#include <fstream> |
| 10 |
#include <cstdlib> |
| 11 |
#include <vector> |
| 12 |
#include <string> |
| 13 |
|
| 14 |
#include "QBits.h" |
| 15 |
#include "QCompilerCntl.h" |
| 16 |
#include "QCalcUnit.h" |
| 17 |
|
| 18 |
typedef unsigned int Appoptions; |
| 19 |
typedef std::vector<QCalcUnit *> QCalcUnits; |
| 20 |
|
| 21 |
const char * const app_name = "qcrun"; |
| 22 |
const char * const app_version = "1.0"; |
| 23 |
|
| 24 |
const Appoptions opt_version = 1; |
| 25 |
const Appoptions opt_help = 1 << 1; |
| 26 |
const Appoptions opt_verbose = 1 << 2; |
| 27 |
const Appoptions opt_output = 1 << 3; |
| 28 |
const Appoptions opt_input = 1 << 4; |
| 29 |
|
| 30 |
//---------------------------------------------------------------------------- |
| 31 |
// Function prototypes |
| 32 |
//---------------------------------------------------------------------------- |
| 33 |
inline bool isSet(const Appoptions &o1, const Appoptions &o2) { |
| 34 |
return (o2 == (o1 & o2)); |
| 35 |
} |
| 36 |
|
| 37 |
Appoptions checkOption(const char * inOptions); |
| 38 |
void compile(QCompilerCntl * const , QCalcUnits * const); |
| 39 |
void forceQuit(void); |
| 40 |
void printUsage(void); |
| 41 |
void printVersion(void); |
| 42 |
void errorAndQuit(const char * const inMessage); |
| 43 |
std::string setOutputFilename(const std::string &inString); |
| 44 |
|
| 45 |
//---------------------------------------------------------------------------- |
| 46 |
/** |
| 47 |
* Main |
| 48 |
*/ |
| 49 |
int main(int argc, char **argv) { |
| 50 |
Appoptions parameters_ready = 0; |
| 51 |
QBits *qBits = NULL; |
| 52 |
QCompilerCntl *qCCntl = NULL; |
| 53 |
QCalcUnits *qCalcUnits = NULL; |
| 54 |
|
| 55 |
std::string ofilename = ""; |
| 56 |
std::string ifilename = ""; |
| 57 |
std::vector<std::string> args; |
| 58 |
|
| 59 |
if (argc <= 1) { |
| 60 |
printVersion(); |
| 61 |
printUsage(); |
| 62 |
std::exit(0); |
| 63 |
} |
| 64 |
|
| 65 |
for (int i = 0; i < argc; i++) { |
| 66 |
std::string tmpstr = argv[i]; |
| 67 |
args.push_back(tmpstr); |
| 68 |
} |
| 69 |
|
| 70 |
int vec_size = args.size(); |
| 71 |
int arg_at = 1; |
| 72 |
|
| 73 |
while (arg_at < vec_size) { |
| 74 |
if ('-' == args[arg_at][0] && args[arg_at].length() > 1) { |
| 75 |
Appoptions tmpoptions = checkOption(args[arg_at].c_str()); |
| 76 |
|
| 77 |
//_____ HELP _____ |
| 78 |
if (isSet(tmpoptions, opt_help)) { |
| 79 |
if (isSet(parameters_ready, opt_help)) { |
| 80 |
errorAndQuit("-h option can be used only once."); |
| 81 |
} else { |
| 82 |
parameters_ready |= opt_help; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
//_____ VERBOSE _____ |
| 87 |
if (isSet(tmpoptions, opt_verbose)) { |
| 88 |
if (isSet(parameters_ready, opt_verbose)) { |
| 89 |
errorAndQuit("-v option can be used only once."); |
| 90 |
} else { |
| 91 |
parameters_ready |= opt_verbose; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
//_____ VERSION _____ |
| 96 |
if (isSet(tmpoptions, opt_version)) { |
| 97 |
if (isSet(parameters_ready, opt_version)) { |
| 98 |
errorAndQuit("-V option can be used only once."); |
| 99 |
} else { |
| 100 |
parameters_ready |= opt_version; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
//_____ OUTPUT _____ |
| 105 |
if (isSet(tmpoptions, opt_output)) { |
| 106 |
if (isSet(parameters_ready, opt_output)) { |
| 107 |
errorAndQuit("-o option can be used only once."); |
| 108 |
} |
| 109 |
|
| 110 |
if (arg_at < vec_size - 1) { |
| 111 |
ofilename = args[++arg_at]; |
| 112 |
parameters_ready |= opt_output; |
| 113 |
} else { |
| 114 |
errorAndQuit("too few parameters."); |
| 115 |
} |
| 116 |
} |
| 117 |
arg_at++; |
| 118 |
continue; |
| 119 |
} |
| 120 |
|
| 121 |
if (isSet(parameters_ready, opt_input)) { |
| 122 |
errorAndQuit("too many input file."); |
| 123 |
} else { |
| 124 |
ifilename = args[arg_at]; |
| 125 |
parameters_ready |= opt_input; |
| 126 |
} |
| 127 |
arg_at++; |
| 128 |
} |
| 129 |
|
| 130 |
//_____ VERSION _____ |
| 131 |
if (isSet(parameters_ready, opt_version)) printVersion(); |
| 132 |
|
| 133 |
//_____ HELP ________ |
| 134 |
if (isSet(parameters_ready, opt_help)) printUsage(); |
| 135 |
|
| 136 |
//_____ OUTPUT ______ |
| 137 |
if (isSet(parameters_ready, opt_input)) { |
| 138 |
if (false == isSet(parameters_ready, opt_output)) { |
| 139 |
ofilename = setOutputFilename(ifilename); |
| 140 |
} |
| 141 |
|
| 142 |
std::ifstream ifs(ifilename.c_str(), std::ios::in); |
| 143 |
if (!ifs) { |
| 144 |
std::cerr << "Cannot open the input file.\n"; |
| 145 |
forceQuit(); |
| 146 |
} |
| 147 |
|
| 148 |
if (isSet(parameters_ready, opt_verbose)) { |
| 149 |
std::cerr << "Input file: " << ifilename << "\n" |
| 150 |
<< "Output file: " << ofilename << "\n"; |
| 151 |
} |
| 152 |
|
| 153 |
try { |
| 154 |
qCCntl = new QCompilerCntl(ifs); |
| 155 |
qCalcUnits = new QCalcUnits(0); |
| 156 |
} catch (const std::bad_alloc ex) { |
| 157 |
std::cerr << "Failed in memory allocation.\n"; |
| 158 |
forceQuit(); |
| 159 |
} |
| 160 |
|
| 161 |
compile(qCCntl, qCalcUnits); |
| 162 |
|
| 163 |
try { |
| 164 |
qBits = new QBits(qCCntl->GetNumberOfQBits()); |
| 165 |
} catch (const std::bad_alloc &ex) { |
| 166 |
std::cerr << "Failed in memory allocation.\n" |
| 167 |
<< "This .mcd code needs at least " |
| 168 |
<< (1 << (qCCntl->GetNumberOfQBits() + 1 + 3)) |
| 169 |
<< " bytes to allocate the qubits.\n"; |
| 170 |
forceQuit(); |
| 171 |
} |
| 172 |
|
| 173 |
for (unsigned int i = 0; i < qCalcUnits->size(); i++) { |
| 174 |
if (isSet(parameters_ready, opt_verbose)) { |
| 175 |
std::cerr << std::setw(15) << std::left |
| 176 |
<< (*qCalcUnits)[i]->GetName() |
| 177 |
<< " ... "; |
| 178 |
} |
| 179 |
(*qCalcUnits)[i]->Calc(qBits); |
| 180 |
if (isSet(parameters_ready, opt_verbose)) { |
| 181 |
std::cerr << "OK\n"; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
qBits->SaveToFile(ofilename.c_str()); |
| 186 |
delete qBits; |
| 187 |
delete qCCntl; |
| 188 |
|
| 189 |
for (unsigned int i = 0; i < qCalcUnits->size(); i++) { |
| 190 |
delete (*qCalcUnits)[i]; |
| 191 |
} |
| 192 |
delete qCalcUnits; |
| 193 |
ifs.close(); |
| 194 |
|
| 195 |
if (isSet(parameters_ready, opt_verbose)) { |
| 196 |
std::cerr << "done." << std::endl; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
//---------------------------------------------------------------------------- |
| 204 |
/** |
| 205 |
* Check options |
| 206 |
*/ |
| 207 |
Appoptions checkOption(const char * inOptions) { |
| 208 |
Appoptions retVal = 0; |
| 209 |
inOptions++; |
| 210 |
while ('\0' != *inOptions) { |
| 211 |
switch (*inOptions) { |
| 212 |
case 'h': |
| 213 |
retVal |= opt_help; |
| 214 |
break; |
| 215 |
case 'o': |
| 216 |
retVal |= opt_output; |
| 217 |
break; |
| 218 |
case 'v': |
| 219 |
retVal |= opt_verbose; |
| 220 |
break; |
| 221 |
case 'V': |
| 222 |
retVal |= opt_version; |
| 223 |
break; |
| 224 |
default: |
| 225 |
errorAndQuit("there are some invalid options."); |
| 226 |
break; |
| 227 |
} |
| 228 |
inOptions++; |
| 229 |
} |
| 230 |
return retVal; |
| 231 |
} |
| 232 |
|
| 233 |
//---------------------------------------------------------------------------- |
| 234 |
/** |
| 235 |
* |
| 236 |
*/ |
| 237 |
void compile(QCompilerCntl * const ioCompiler, QCalcUnits * const ioUnits) { |
| 238 |
QCalcUnit *cu = NULL; |
| 239 |
ioUnits->clear(); |
| 240 |
|
| 241 |
if (true == ioCompiler->Compile()) { |
| 242 |
unsigned int cntl_size = ioCompiler->GetNumberOfControl(); |
| 243 |
for (unsigned int i = 0; i < cntl_size; i++) { |
| 244 |
cu = ioCompiler->AllocateControl(i); |
| 245 |
if (NULL != cu) ioUnits->push_back(cu); |
| 246 |
} |
| 247 |
} else { |
| 248 |
errorAndQuit("Parse error occurred.\n"); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
//---------------------------------------------------------------------------- |
| 253 |
/** |
| 254 |
* Force quit |
| 255 |
*/ |
| 256 |
void forceQuit(void) { |
| 257 |
std::cerr << "Quit forced.\n"; |
| 258 |
std::exit(1); |
| 259 |
} |
| 260 |
|
| 261 |
//---------------------------------------------------------------------------- |
| 262 |
/** |
| 263 |
* Print usages |
| 264 |
*/ |
| 265 |
void printUsage(void) { |
| 266 |
std::cout << "Usage: " << app_name |
| 267 |
<< " [-hvV] [-o output_file] input_file.mcd\n\n" |
| 268 |
<< " -h show this help\n" |
| 269 |
<< " -v verbose mode\n" |
| 270 |
<< " -V show version\n\n" |
| 271 |
<< std::flush; |
| 272 |
} |
| 273 |
|
| 274 |
//---------------------------------------------------------------------------- |
| 275 |
/** |
| 276 |
* Print version |
| 277 |
*/ |
| 278 |
void printVersion(void) { |
| 279 |
std::cout << "\nCopyright (C) 2002-2003 QCAD project\n" |
| 280 |
<< app_name << " version " << app_version << "\n\n" |
| 281 |
<< std::flush; |
| 282 |
} |
| 283 |
|
| 284 |
//---------------------------------------------------------------------------- |
| 285 |
/** |
| 286 |
* Show message and quit |
| 287 |
*/ |
| 288 |
void errorAndQuit(const char * const inMessage) { |
| 289 |
std::cerr << "Error: " << inMessage << "\n\n"; |
| 290 |
printUsage(); |
| 291 |
forceQuit(); |
| 292 |
} |
| 293 |
|
| 294 |
//---------------------------------------------------------------------------- |
| 295 |
/** |
| 296 |
* Set output filename |
| 297 |
*/ |
| 298 |
std::string setOutputFilename(const std::string &inString) { |
| 299 |
std::string retString = ""; |
| 300 |
|
| 301 |
unsigned int posdot = inString.rfind(".", inString.length() - 1); |
| 302 |
|
| 303 |
if (posdot == std::string::npos) { |
| 304 |
retString = inString; |
| 305 |
} else { |
| 306 |
retString.assign(inString, 0, posdot); |
| 307 |
} |
| 308 |
|
| 309 |
retString += ".qdt"; |
| 310 |
|
| 311 |
return retString; |
| 312 |
} |
| 313 |
|