Beispiel #1
0
int main ()
{
  QLinkedList<int> myints;
  cout << "0. size: " << (int) myints.size() << endl;
  assert(myints.size() == 0);
  for (int i=0; i<10; i++) myints.push_back(i);
  cout << "1. size: " << (int) myints.size() << endl;
  assert(myints.size() == 10);
  myints.insert (myints.begin(),10);
  cout << "2. size: " << (int) myints.size() << endl;
  assert(myints.size() == 11);
  myints.pop_back();
  cout << "3. size: " << (int) myints.size() << endl;
  assert(myints.size() == 10);
  return 0;
}
Beispiel #2
0
int main ()
{
  QLinkedList<int> myQLinkedList;
  int sum (0);
  myQLinkedList.push_back (100);
  myQLinkedList.push_back (200);
  myQLinkedList.push_back (300);
  assert(myQLinkedList.back() == 300);
  int n = 3;
  while (!myQLinkedList.empty())
  {
    assert(myQLinkedList.back() == n*100);
    sum+=myQLinkedList.back();
    myQLinkedList.pop_back();
    n--;
  }

  cout << "The elements of myQLinkedList summed " << sum << endl;

  return 0;
}
Beispiel #3
0
void compile(QString path,QStringList& import,QFile &out){
    QFile fp(path);
    if(fp.open(QFile::ReadOnly)){
        QDir dir(QFileInfo(fp).absoluteDir());
        QString modName = QFileInfo(fp).baseName();
        if(readLine(fp).trimmed()!=FILE_HEAD){
            qDebug("Indicated file is not a algorithm file of Uranus2");
            exit(0);
        }
        int pIndent,cIndent=0,functionIndent=0;
        Function* function=0;
        QRegExp keyWord("(\\s*)(\\w+)\\s",Qt::CaseInsensitive);
        QRegExp argHook("(<.+>)?(.+)?");
        QLinkedList<StackItem*> stack;
        while(!fp.atEnd()){
            QString line=QString::fromUtf8(fp.readLine());
            keyWord.indexIn(line);
            QString key=keyWord.cap(2).toLower();
            line=line.mid(keyWord.cap().length()).trimmed();
            pIndent = cIndent;
            cIndent=keyWord.cap(1).length();
            if(cIndent<functionIndent){
                if(function!=0){
                    function->write(out);
                    function=0;
                }
            }else if(cIndent<=pIndent){
                while(pIndent>=cIndent){
                    pIndent--;
                    if(!stack.isEmpty()){
                        stack.pop_back();
                    }
                }
            }
            StackItem* parent = (stack.isEmpty())?0:stack.back(),
                    *self=new StackItem(key,line,cIndent);
            stack.push_back(self);

            if(key=="import"){
                import.append(dir.absoluteFilePath(line));
            }else if(key=="function"){
                function=new Function(modName,line);
                functionIndent=cIndent+1;
            }else if(key=="hint"){
                if(function!=0){
                    if(parent->key=="function")
                        self->indent++;
                    self->outBegin="#"+line;
                    function->append(self);
                }
            }else if(key=="arg"){
                if(parent->key=="function"&&function){
                    function->args()<<line;
                }
            }else if(key=="assign"){
                self->outBegin=line+"=";
                function->append(self);
            }else if(key=="value"){
                if(parent->key=="assign"){
                    argHook.indexIn(line);
                    if(argHook.cap(1)=="<list>"){
                    }else if(argHook.cap(1)=="<function>"){
                    }else if(argHook.cap(1)=="<variable>"){
                        self->outBegin=argHook.cap(2).trimmed();
                    }else{
                        self->outBegin=line.trimmed();
                    }
                    parent->addChild(self);
                }
            }else if(key=="branch"){
                self->outBegin="if True:";
                function->append(self);
            }else if(key=="call"){
                QString fname;
                if(line.left(3)=="://"){
                    fname=Function::conv(modName,line.mid(3));
                }else{
                    QStringList f = line.split("/");
                    if(f.count()==2){
                        fname=Function::conv(f.at(0),f.at(1));
                    }
                }
                self->outBegin=fname+"(";
                self->outSep=",";
                self->outEnd=")";
                if(parent->key=="do"){
                    function->append(self);
                }else{
                    parent->addChild(self);
                }
            }else if(key=="conditional"){
                self->outBegin="if True:";
                function->append(self);
            }else if(key=="do"){
            }else if(key=="list"){
                self->outBegin="[";
                self->outSep=",";
                self->outEnd="]";
                parent->addChild(self);
            }else if(key=="item"){
                argHook.indexIn(line);
                if(argHook.cap(1)=="<list>"){
                }else if(argHook.cap(1)=="<function>"){
                }else if(argHook.cap(1)=="<variable>"){
                    self->outBegin=argHook.cap(2).trimmed();
                }else{
                    self->outBegin=line.trimmed();
                }
                parent->addChild(self);
            }else if(key=="loop"){
                self->outBegin="if True:";
                function->append(self);
            }else if(key=="traverseloop"){
                self->outBegin="if True:";
                function->append(self);
            }else if(key=="traverse"){
                self->outBegin="for traverse in ";
                self->outEnd=":";
                function->append(self);
            }else if(key=="condition"){
                self->outEnd=":";
                if(parent->key=="branch"){
                    if(parent->data==0){
                        self->outBegin="if ";
                    }else{
                        self->outBegin="elif ";
                    }
                    parent->data=1;
                }else if(parent->key=="loop"){
                    self->outBegin="while ";
                }
                argHook.indexIn(line);
                if(argHook.cap(1)=="<list>"){
                }else if(argHook.cap(1)=="<function>"){
                }else if(argHook.cap(1)=="<variable>"){
                    self->outBegin+=argHook.cap(2).trimmed();
                }else{
                    self->outBegin+=line.trimmed();
                }
                function->append(self);
            }else if(key=="return"){
                self->outBegin="return ";
                argHook.indexIn(line);
                if(argHook.cap(1)=="<list>"){
                }else if(argHook.cap(1)=="<function>"){
                }else if(argHook.cap(1)=="<variable>"){
                    self->outBegin+=argHook.cap(2).trimmed();
                }else{
                    self->outBegin+=line.trimmed();
                }
                function->append(self);
            }else if(key=="execute"){
                self->outBegin="exec(";
                self->outEnd=")";
                argHook.indexIn(line);
                if(argHook.cap(1)=="<list>"){
                }else if(argHook.cap(1)=="<function>"){
                }else if(argHook.cap(1)=="<variable>"){
                    self->outBegin+=argHook.cap(2).trimmed();
                }else{
                    self->outBegin+=line.trimmed();
                }
                function->append(self);
            }else if(key=="break"){
                self->outBegin = "break";
                function->append(self);
            }else if(key=="continue"){
                self->outBegin = "continue";
                function->append(self);
            }else{
                if(parent->key=="call"){
                    argHook.indexIn(line);
                    if(argHook.cap(1)=="<list>"){
                    }else if(argHook.cap(1)=="<function>"){
                    }else if(argHook.cap(1)=="<variable>"){
                        self->outBegin+=argHook.cap(2).trimmed();
                    }else{
                        self->outBegin+=line.trimmed();
                    }
                    parent->addChild(self);
                }
            }
        }
        if(function!=0)
            function->write(out);
    }else{
        qDebug()<<"Can not open file "+path;
    }
}