| 1 |
//---------------------------------------------------------------------------- |
| 2 |
// QCompiler.h |
| 3 |
// Compile intermediate codes |
| 4 |
// $Date: 2003/01/17 19:37:43 $ |
| 5 |
// $Revision: 1.3 $ |
| 6 |
//---------------------------------------------------------------------------- |
| 7 |
#ifndef QCOMPILER_H |
| 8 |
#define QCOMPILER_H |
| 9 |
|
| 10 |
#include <iostream> |
| 11 |
#include <string> |
| 12 |
#include <vector> |
| 13 |
#include "QParseInfo.h" |
| 14 |
|
| 15 |
class QCompiler { |
| 16 |
|
| 17 |
public: |
| 18 |
QCompiler(void); |
| 19 |
QCompiler(std::istream &is); |
| 20 |
virtual ~QCompiler(); |
| 21 |
bool Compile(void); |
| 22 |
void ReadFromStream(std::istream &is); |
| 23 |
bool HasError(void) { |
| 24 |
return mError; |
| 25 |
} |
| 26 |
bool GetState(void) { |
| 27 |
return mState; |
| 28 |
} |
| 29 |
|
| 30 |
typedef enum _opnumber { |
| 31 |
opn_arg_max = 3, |
| 32 |
opn_length = 10 |
| 33 |
} OpNumber; |
| 34 |
|
| 35 |
static const int opn_max; |
| 36 |
|
| 37 |
protected: |
| 38 |
virtual bool CompileOneLine(const QParseInfo &pinfo) = 0; |
| 39 |
virtual void CatchError(const QParseInfo &pinfo, const int at) = 0; |
| 40 |
|
| 41 |
QParseInfo ParseOneLine(const std::string &strline) const; |
| 42 |
bool ExtractComArg(const std::string &strline, const char bra, const char ket, |
| 43 |
std::string &Com, std::string &Arg) const; |
| 44 |
bool ExtractField(const std::string &strline, const char delim, |
| 45 |
std::vector<std::string> &Args) const ; |
| 46 |
int GetComint(const std::string &_str) const; |
| 47 |
|
| 48 |
// Member valuables |
| 49 |
std::vector<std::string> mLines; |
| 50 |
bool mError; |
| 51 |
bool mState; // true == finished |
| 52 |
|
| 53 |
//------------------------------------------------------------------------ |
| 54 |
// Syntax of intermediate code (see also `QCompiler.cpp') |
| 55 |
//------------------------------------------------------------------------ |
| 56 |
// Argument types |
| 57 |
typedef enum _argtype { |
| 58 |
at_null = 0, |
| 59 |
at_qbit = 1, |
| 60 |
at_real = 2, |
| 61 |
at_oprt = 3 |
| 62 |
} ArgType; |
| 63 |
|
| 64 |
// Command numbers, each of which corresponds to the index num of OPSTR |
| 65 |
|
| 66 |
typedef enum _commandnumber { |
| 67 |
cn_cnot = 0, |
| 68 |
cn_rot , |
| 69 |
cn_crot , |
| 70 |
cn_h , |
| 71 |
cn_m , |
| 72 |
cn_swap , |
| 73 |
cn_x , |
| 74 |
cn_y , |
| 75 |
cn_z , |
| 76 |
cn_not , |
| 77 |
cn_ccnot, |
| 78 |
cn_init |
| 79 |
} CommandNumber; |
| 80 |
|
| 81 |
// Size of array |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
struct QGATES { |
| 86 |
char opstr[opn_length]; |
| 87 |
int arg_num; |
| 88 |
int arg_types[opn_arg_max]; |
| 89 |
}; |
| 90 |
|
| 91 |
static const QGATES qgates[]; |
| 92 |
|
| 93 |
static const char OP_BRA; |
| 94 |
static const char OP_KET; |
| 95 |
static const char QB_BRA; |
| 96 |
static const char QB_KET; |
| 97 |
static const char DELIM; |
| 98 |
}; |
| 99 |
|
| 100 |
#endif //QCOMPILER_H |