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
|
while (!operator_stack.empty()){
output_queue.push(operator_stack.top());//estrai l'ultimo elemento dello stack e mettilo in output
operator_stack.pop();
}
//a questo punto ho output queue in ordine: costruisci albero
//creo stack: metto component, quando trovo un operatore faccio pop degli ultimi due
while(!output_queue.empty()){
if(output_queue.size()==1){
address_stack=tree(checkOperator(output_queue.front()), address_stack);
head=address_stack.top();
output_queue.pop();
break;
}
if (checkOperator(output_queue.front())>=0){ //trovato un operatore in _gate vector (checkoperator da -1 se non trova gate, indirizzo nel vettore se lo trova
address_stack=tree(checkOperator(output_queue.front()), address_stack);//costruito alberp, attenzione a non perdere le teste:
output_queue.pop();
}
address = checkInput(output_queue.front());
if (address>=0){
cpPtr=_input_vector[address].data;
address_stack.push(cpPtr);//puntatore a elemento di input vector
output_queue.pop();
}
address=checkFF(output_queue.front());
if (address>=0){
cpPtr=_FF_vector[address].data;
address_stack.push(cpPtr);//puntatore a elemento di input vector
output_queue.pop();
}
}
return head;
//ho la testa del circuito, la attacco a _output_vector:
}
bool Circuit :: doOp(int& i){//{"0clk","1input","2output","3assign","4instance","5endmodule", "6ffx", "7and","8or", "9xor","10not","11nand","12nor","13xnor"};
string word;
int c=0;
Component* head;
head= nullptr;
bool flag= false;
switch (i){
case 0: _sequenziale=true;
break;
case 1: getWord(word);
if (word==":"){
makeVectors(i);
}
break;//input
case 2: getWord(word);
if (word==":"){
makeVectors(i);
}
break;//output
case 3: getWord(word);
//leggi elemento di output vector, controlla se c'è
c=0;
do{
if (word.compare(_output_vector[c].name.c_str())==0) {
flag = true;
break;
}
c++;
}while(c<_output_vector.size());
if (!flag) {//uscito dal ciclo senza trovare output definito
cout << "errore: output '" << word << "' non definito";
return false;
}
//RICOMINCIARE DA QUI: CHECK DELL'ELEMENTO NEL VETTORE USCITE, DOPODICHè IMPLEMENTAZIONE SHUNTING-YARD
getWord(word);//leggi '='
if (!(word=="=")){
cout<<"errore: inserire '=' dopo "<<_output_vector[c].name;
return false;
}
head=assign();
_output_vector[c].data->setInputs(*head);
//assign()
break; //assigncase 4: break; //istance
case 4:_my_file.close();
break;//endmodule
default: break;
}
}bool myispunct(char ch) { return std::ispunct(ch) && ch != '_'; }
bool Circuit :: getWord(string& word) { //estrae una parola e rende tutti i caratteri minuscoli
word.clear();
char ch;
while (_my_file.get(ch) && isspace(ch)); //scorre il file finché elimina gli spazi
if (isalpha(ch)) ch = tolower(ch);
if (_my_file) word += ch;
if (!myispunct(ch)) {
while (_my_file.get(ch)) {
if (isalpha(ch)) ch = tolower(ch);
// for ( ; _my_file.get(ch); word += ch)
if (isspace(ch) || myispunct(ch)) { // or myispunct
if (ispunct(ch)) _my_file.putback(ch);
break;
}
word += ch;
}
}
return word.size() !=0;
}
void Circuit :: makeVectors(int& i){
char *pch;
char buffer[200];
string line;
int j=0;
getline(_my_file,line);
strcpy(buffer,line.c_str());
pch = strtok(buffer, ", ");
while(pch != nullptr){
if(i==1) {
j = _input_vector.size();
_input_vector.push_back( { pch, new ConstantComponent } );
}else{
j= _output_vector.size();
_output_vector.push_back( { pch, new ConstantComponent } );
}
pch = strtok(nullptr, ",");
}
}
void Circuit :: setFileName(){
string nomefile;
cout<<"Insert file's name to open: ";
cin>>nomefile;
_file_name=nomefile;
}
void Circuit :: openFile() {
_my_file.open(_file_name.c_str(), ios:: in);
if (!_my_file.is_open()) cout<<"unable to open file";
}
void Circuit :: closeFile() {
_my_file.close();
}
int Circuit :: checkOperator(string& operatore){
// if
for (int i=0;i<dict_lenght;i++){
if (_gate[i]==operatore) {
return i;
}
}
return -1;
}
int Circuit :: checkInput(string &word){
for (int i=0; i<_input_vector.size(); i++){
if (_input_vector[i].name==word){
return i;
}
}
return -1;
}
int Circuit :: checkFF(string &word){
for (int i=0; i<_FF_vector.size(); i++){
if (_FF_vector[i].name==word){
return i;
}
}
return -1;
}
stack<Component*> Circuit :: tree(int address, stack<Component*> stack) { //and","or", "xor","nand","nor","xnor"};
Component* temp= nullptr;
switch (address) {
case 0:{
Not *pNot = new Not;
pNot->setInputs(*stack.top());
stack.pop();
stack.push(pNot);
//deleteP(pAnd,pOr,pXor,pNand,pNor,pXnor);
return stack;
break;
}
case 1: {
And *pAnd = new And;
temp = stack.top();
stack.pop();
pAnd->setInputs(*stack.top(), *temp);
stack.pop();
stack.push(pAnd);
//deleteP(pNot,pOr,pXor,pNand,pNor,pXnor);
return stack;
break;
}
case 2: {
Or *pOr = new Or ;
temp = stack.top();
stack.pop();
pOr->setInputs(*stack.top(), *temp);
stack.pop();
stack.push(pOr);
//deleteP(pAnd,pNot,pXor,pNand,pNor,pXnor);
return stack;
break;
}
case 3: {
Xor *pXor = new Xor;
temp = stack.top();
stack.pop();
pXor->setInputs(*stack.top(), *temp);
stack.pop();
stack.push(pXor);
//deleteP(pAnd,pOr,pNot,pNand,pNor,pXnor);
return stack;
break;
}
case 4: {
Nand *pNand = new Nand;
temp = stack.top();
stack.pop();
pNand->setInputs(*stack.top(), *temp);
stack.pop();
stack.push(pNand);
//deleteP(pAnd,pOr,pXor,pNot,pNor,pXnor);
return stack;
break;
}
case 5: {
Nor *pNor = new Nor;
temp = stack.top();
stack.pop();
pNor->setInputs(*stack.top(), *temp);
stack.pop();
stack.push(pNor);
//deleteP(pAnd,pOr,pXor,pNand,pNot,pXnor);
return stack;
break;
}
case 6: {
Xnor *pXnor = new Xnor;
temp = stack.top();
stack.pop();
pXnor->setInputs(*stack.top(), *temp);
stack.pop();
stack.push(pXnor);
//deleteP(pAnd,pOr,pXor,pNand,pNor,pNot);
return stack;
break;
}
default:break;
}
}
/*implementare shunting-yard che fa diventare la linea una visita reverse polish postfix di un albero e da quella costruire l'albero*/
void Circuit :: postOrder(Component* node) {
if(node== nullptr) return;
postOrder(node->leftChild());
postOrder(node->rightChild());
node->logicFunction();
}
|