engine: add query for all names
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sat, 25 Dec 2010 18:45:06 +0000 (18:45 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sat, 25 Dec 2010 18:45:06 +0000 (18:45 +0000)
add demo

git-svn-id: https://silmor.de/svn/softmagic/elam/trunk@682 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

demo/demo.cpp [new file with mode: 0644]
demo/demo.h [new file with mode: 0644]
demo/demo.pro [new file with mode: 0644]
src/elamengine.cpp
src/elamengine.h

diff --git a/demo/demo.cpp b/demo/demo.cpp
new file mode 100644 (file)
index 0000000..44fe86a
--- /dev/null
@@ -0,0 +1,167 @@
+#include "elam.h"
+#include "demo.h"
+
+#include <QApplication>
+#include <QTabWidget>
+#include <QBoxLayout>
+#include <QLineEdit>
+#include <QPushButton>
+#include <QLabel>
+#include <QTreeView>
+#include <QStandardItemModel>
+#include <QHeaderView>
+
+//central engine instance
+ELAM::Engine engine;
+
+ElamDemo::ElamDemo(QWidget* parent, Qt::WindowFlags f): QDialog(parent, f)
+{
+       //basics
+       setWindowTitle("ELAM Demo");
+       setSizeGripEnabled(true);
+       resize(600,400);
+       QVBoxLayout*vl;
+       QHBoxLayout*hl;
+       setLayout(vl=new QVBoxLayout);
+       vl->addWidget(mtabs=new QTabWidget);
+       QWidget *w;
+       QPushButton*p;
+       mtabs->addTab(w=new QWidget,"Expression");
+       //expression line
+       w->setLayout(vl=new QVBoxLayout);
+       vl->addLayout(hl=new QHBoxLayout);
+       hl->addWidget(mexpression=new QLineEdit);
+       hl->addWidget(p=new QPushButton("exec"));
+       connect(p,SIGNAL(clicked()),this,SLOT(execute()));
+       //result label
+       vl->addWidget(mresult=new QLabel);
+       
+       //variables/constants table
+       mtabs->addTab(mvalues=new QTreeView,"Values");
+       mvalues->setModel(mvalmodel=new QStandardItemModel(this));
+       mvalues->setEditTriggers(QAbstractItemView::NoEditTriggers);
+       
+       //functions/operators table
+       mtabs->addTab(msyntax=new QTreeView,"Syntax");
+       msyntax->setModel(msynmodel=new QStandardItemModel(this));
+       msyntax->setEditTriggers(QAbstractItemView::NoEditTriggers);
+       
+       //initialize
+       updateViews();
+}
+
+void ElamDemo::execute()
+{
+       //evaluate expression
+       QVariant res=engine.evaluate(mexpression->text());
+       //get result as printable string
+       QString str;
+       int type=res.userType();
+       //handle Exception specially
+       if(type==ELAM::Exception::metaTypeId()){
+               ELAM::Exception ex=res.value<ELAM::Exception>();
+               str=QString("Exception at %1:%2 - %3")
+                       .arg(ex.errorLine())
+                       .arg(ex.errorColumn())
+                       .arg(ex.errorText());
+       }else{
+               str=QString("Type %1, Value: %2")
+                       .arg(res.typeName())
+                       .arg(res.toString());
+       }
+       mresult->setText(str);
+       //update tables (in case of assignments)
+       updateViews();
+}
+
+void ElamDemo::updateViews()
+{
+       mvalmodel->clear();
+       mvalmodel->insertRows(0,2);
+       if(mvalmodel->columnCount()<3)
+               mvalmodel->insertColumns(0,3);
+       mvalmodel->setHorizontalHeaderLabels(QStringList()<<"Name"<<"Type"<<"Value");
+       //constants
+       QModelIndex pidx=mvalmodel->index(0,0);
+       mvalmodel->setData(pidx,"Constants");
+       QStringList k=engine.constantNames();
+       mvalmodel->insertRows(0,k.size(),pidx);
+       mvalmodel->insertColumns(0,3,pidx);
+       for(int i=0;i<k.size();i++){
+               mvalmodel->setData(mvalmodel->index(i,0,pidx),k[i]);
+               mvalmodel->setData(mvalmodel->index(i,1,pidx),engine.getConstant(k[i]).typeName());
+               mvalmodel->setData(mvalmodel->index(i,2,pidx),engine.getConstant(k[i]).toString());
+       }
+       //variables
+       k=engine.variableNames();
+       pidx=mvalmodel->index(1,0);
+       mvalmodel->setData(pidx,"Variables");
+       mvalmodel->insertRows(0,k.size(),pidx);
+       mvalmodel->insertColumns(0,3,pidx);
+       for(int i=0;i<k.size();i++){
+               mvalmodel->setData(mvalmodel->index(i,0,pidx),k[i]);
+               mvalmodel->setData(mvalmodel->index(i,1,pidx),engine.getVariable(k[i]).typeName());
+               mvalmodel->setData(mvalmodel->index(i,2,pidx),engine.getVariable(k[i]).toString());
+       }
+       mvalues->expandAll();
+       mvalues->resizeColumnToContents(0);
+       mvalues->resizeColumnToContents(1);
+       mvalues->resizeColumnToContents(2);
+       //syntactic elements
+       msynmodel->clear();
+       msynmodel->insertRows(0,3);
+       if(msynmodel->columnCount()<2)
+               msynmodel->insertColumns(0,2);
+       msynmodel->setHorizontalHeaderLabels(QStringList()<<"Name"<<"Instances");
+       //functions
+       pidx=msynmodel->index(0,0);
+       k=engine.functionNames();
+       msynmodel->setData(pidx,"Functions");
+       msynmodel->insertRows(0,k.size(),pidx);
+       msynmodel->insertColumns(0,1,pidx);
+       for(int i=0;i<k.size();i++)
+               msynmodel->setData(msynmodel->index(i,0,pidx),k[i]);
+       //unary
+       pidx=msynmodel->index(1,0);
+       k=engine.unaryOperatorNames();
+       msynmodel->setData(pidx,"Unary Operators");
+       msynmodel->insertRows(0,k.size(),pidx);
+       msynmodel->insertColumns(0,2,pidx);
+       for(int i=0;i<k.size();i++){
+               msynmodel->setData(msynmodel->index(i,0,pidx),k[i]);
+               msynmodel->setData(msynmodel->index(i,1,pidx), engine.unaryOperator(k[i]).getTypeIds().size());
+       }
+       //binary
+       pidx=msynmodel->index(2,0);
+       msynmodel->setData(pidx,"Binary Operators");
+       k=engine.binaryOperatorNames();
+       msynmodel->insertRows(0,k.size(),pidx);
+       msynmodel->insertColumns(0,2,pidx);
+       for(int i=0;i<k.size();i++){
+               msynmodel->setData(msynmodel->index(i,0,pidx),k[i]);
+               msynmodel->setData(msynmodel->index(i,1,pidx), engine.binaryOperator(k[i]).getTypeIds().size());
+       }
+       //adjust
+       msyntax->expandAll();
+       msyntax->resizeColumnToContents(0);
+       msyntax->resizeColumnToContents(1);
+}
+
+
+int main(int argc,char**argv)
+{
+       QApplication app(argc,argv);
+       
+       //init engine
+       ELAM::IntEngine::configureIntEngine(engine);
+       ELAM::FloatEngine::configureFloatEngine(engine);
+       ELAM::BoolEngine::configureBoolEngine(engine);
+       ELAM::BoolEngine::configureLogicEngine(engine);
+       ELAM::StringEngine::configureStringEngine(engine);
+       
+       //init and show window
+       ElamDemo d;
+       d.show();
+       
+       return app.exec();
+}
\ No newline at end of file
diff --git a/demo/demo.h b/demo/demo.h
new file mode 100644 (file)
index 0000000..df030fd
--- /dev/null
@@ -0,0 +1,31 @@
+#ifndef ELAM_DEMO_H
+#define ELAM_DEMO_H
+
+#include <QDialog>
+
+class QStandardItemModel;
+class QTreeView;
+class QTabWidget;
+class QLabel;
+class QLineEdit;
+///Demo Window
+class ElamDemo:public QDialog
+{
+       Q_OBJECT
+       public:
+               ///instantiate the demo window
+               ElamDemo(QWidget* parent = 0, Qt::WindowFlags f = 0);
+       public slots:
+               ///executes the user's expression
+               void execute();
+               ///update tables
+               void updateViews();
+       private:
+               QTabWidget*mtabs;
+               QLineEdit*mexpression;
+               QLabel*mresult;
+               QTreeView*mvalues,*msyntax;
+               QStandardItemModel*mvalmodel,*msynmodel;
+};
+
+#endif
diff --git a/demo/demo.pro b/demo/demo.pro
new file mode 100644 (file)
index 0000000..d30e797
--- /dev/null
@@ -0,0 +1,9 @@
+TEMPLATE = app
+TARGET = elamdemo
+CONFIG += link_prl
+LIBS += -L.. -lelam
+
+INCLUDEPATH += ../src
+
+SOURCES += demo.cpp
+HEADERS += demo.h
\ No newline at end of file
index d53a432..501a4a1 100644 (file)
@@ -409,6 +409,30 @@ QVariant Engine::autoCast(const QVariant& v)const
        else return d->casts[pos].cfunc(v);
 }
 
+QStringList Engine::binaryOperatorNames() const
+{
+       return d->binary.keys();
+}
+
+QStringList Engine::constantNames() const
+{
+       return d->consts.keys();
+}
+
+QStringList Engine::functionNames() const
+{
+       return d->funcs.keys();
+}
+
+QStringList Engine::unaryOperatorNames() const
+{
+       return d->unary.keys();
+}
+
+QStringList Engine::variableNames() const
+{
+       return d->vars.keys();
+}
 
 //end of namespace
 };
\ No newline at end of file
index b7f08ca..e5774b9 100644 (file)
@@ -115,6 +115,17 @@ class Engine:public QObject
                /**performs automatic casting
                \returns the casted value or the original value if it is of a primary type or if there is no known cast for it*/
                Q_INVOKABLE QVariant autoCast(const QVariant&)const;
+               
+               ///returns the names of all currently existing variables
+               Q_INVOKABLE QStringList variableNames()const;
+               ///returns the names of all currently existing constants
+               Q_INVOKABLE QStringList constantNames()const;
+               ///returns the names of all currently existing functions
+               Q_INVOKABLE QStringList functionNames()const;
+               ///returns the names of all currently existing binary operators
+               Q_INVOKABLE QStringList binaryOperatorNames()const;
+               ///returns the names of all currently existing unary operators
+               Q_INVOKABLE QStringList unaryOperatorNames()const;
 
        public slots:
                ///returns the value of the named variable or constant