Why not load a file specified on the commandline...

Also, some placeholder functions for the great and powerful script
manager!


git-svn-id: file:///srv/svn/ircclient/trunk@2 a9804ffe-773b-11dd-bd7c-89c3ef1d2733
This commit is contained in:
Correl Roush 2008-08-31 09:50:57 +00:00
parent 0381859219
commit 7315012830
6 changed files with 55 additions and 75 deletions

View file

@ -4,15 +4,26 @@
#include <QObject> #include <QObject>
#include <QString> #include <QString>
#include <QMap> #include <QMap>
#include <QVector>
#include "script.h" #include "script.h"
class MIRCScript;
class MIRCScriptManager : public QObject { class MIRCScriptManager : public QObject {
Q_OBJECT Q_OBJECT
private:
QObject *parent;
QVector<MIRCScript> scripts;
QMap<QString, QString> _variables;
public: public:
//QMap<QString, QString> variables; MIRCScriptManager(QObject *parent = 0);
/*
bool load(QString filename);
bool unload(QString filename);
MIRCScriptManager(QObject *parent); QString call_alias(QString alias, QStringList arguments);
QString variable(QString variable);
*/
}; };
#endif #endif

View file

@ -17,6 +17,8 @@
#include <QStringList> #include <QStringList>
#include <QMap> #include <QMap>
#include <QStack> #include <QStack>
//#include "mirc.h"
class MIRCScriptManager;
using namespace std; using namespace std;
using namespace boost; using namespace boost;
@ -42,6 +44,7 @@ enum mirc_engine_stage {
class mirc_script_engine : public QObject { class mirc_script_engine : public QObject {
Q_OBJECT Q_OBJECT
private: private:
MIRCScriptManager *manager;
mirc_aliases::iterator current_alias; mirc_aliases::iterator current_alias;
mirc_variables::iterator current_variable; mirc_variables::iterator current_variable;
QStack<QStringList> stack; QStack<QStringList> stack;
@ -51,7 +54,8 @@ public:
mirc_aliases aliases; mirc_aliases aliases;
mirc_variables vars; mirc_variables vars;
mirc_script_engine() : script() { mirc_script_engine(MIRCScriptManager *m) : script() {
manager = m;
stage = PARSE; stage = PARSE;
current_alias = aliases.end(); current_alias = aliases.end();
current_variable = vars.end(); current_variable = vars.end();

View file

@ -10,12 +10,13 @@
class MIRCScript : public QObject { class MIRCScript : public QObject {
Q_OBJECT Q_OBJECT
private: private:
MIRCScriptManager *manager;
QString script; QString script;
QMap<QString, mirc_alias> _aliases; QMap<QString, mirc_alias> _aliases;
QMap<QString, QString> _variables; QMap<QString, QString> _variables;
bool loaded; bool loaded;
public: public:
MIRCScript(); MIRCScript(MIRCScriptManager *m);
bool load(QString filename); bool load(QString filename);
bool parse(QString script); bool parse(QString script);
bool run(); bool run();

View file

@ -1,4 +1,5 @@
#include "mirc.h" #include "mirc.h"
MIRCScriptManager::MIRCScriptManager(QObject *parent = 0) { MIRCScriptManager::MIRCScriptManager(QObject *parent) {
this->parent = parent;
} }

View file

@ -1,76 +1,38 @@
#include <QTextStream> #include <QTextStream>
#include "script.h" #include "script.h"
int main() { int main(int argc, char* argv[]) {
QTextStream output(stdout); QTextStream output(stdout);
QString foo; MIRCScriptManager *mirc = new MIRCScriptManager;
foo.append("; Hey, here's some test code\n"); MIRCScript *ms = new MIRCScript(mirc);
foo.append("set name Correl\n"); if (argc < 2) {
foo.append("%name = Correl $&\n\tRoush\n"); output << "No mIRC script file was specified!\n";
foo.append("echo Hello, %name!\n"); return(1);
foo.append("alias dostuff {\n"); }
foo.append(" ; Not very useful, but good for testing the parser!\n"); output << "Attempting to load " << argv[1] << "\n";
foo.append(" var %b = 42\n"); if (ms->load(argv[1])) {
foo.append(" %b = $calc(%b * 3)\n"); output << "MRC LOADED\n";
foo.append(" return %b;\n"); output << "Code:\n" << ms->code();
foo.append("}\n"); output << "Aliases:\n";
foo.append("alias -l dosomethingelse {\n"); QMapIterator<QString, mirc_alias> alias(ms->aliases());
foo.append(" ; Useless local alias!\n"); while(alias.hasNext()) {
foo.append(" echo -s Busy doing nothing\n"); alias.next();
foo.append("}\n"); output << (alias.value().global ? "global" : "local");
foo.append("alias -l dosomethingelse {\n"); output << " " << alias.key() << "\n";
foo.append(" ; Useless local alias!\n"); }
foo.append(" echo -s Busy doing nothing\n"); if (ms->run()) {
foo.append("}\n"); output << "MRC RAN SUCCESSFULLY\n";
QMapIterator<QString, QString> i(ms->variables());
output << "Code:\n" output << "Variables:\n";
<< "================================================================================\n" while(i.hasNext()) {
<< foo i.next();
<< "================================================================================\n" output << i.key() << " = " << i.value() << "\n";
<< "\n";
mirc_script_engine *interpreter = new mirc_script_engine();
mirc_script parser(interpreter);
parse_info<> info = parse((const char*)foo.toLatin1(), parser);
if (info.full)
{
output << "-------------------------\n";
output << "Parsing succeeded\n";
output << "-------------------------\n";
MIRCScript *ms = new MIRCScript();
output << "Creating new MIRCScript object\n";
//if (ms->parse(foo)) {
if (ms->load("test.mrc")) {
output << "MRC LOADED\n";
output << "Code:\n" << ms->code();
output << "Aliases:\n";
QMapIterator<QString, mirc_alias> alias(ms->aliases());
while(alias.hasNext()) {
alias.next();
output << (alias.value().global ? "global" : "local");
output << " " << alias.key() << "\n";
}
if (ms->run()) {
output << "MRC RAN SUCCESSFULLY\n";
QMapIterator<QString, QString> i(ms->variables());
output << "Variables:\n";
while(i.hasNext()) {
i.next();
output << i.key() << " = " << i.value() << "\n";
}
} }
} }
} } else {
else output << "Failed to load " << argv[1] << "\n";
{ return(1);
output << "-------------------------\n";
output << "Parsing failed\n";
output << "stopped at: \": " << info.stop << "\"\n";
output << "-------------------------\n";
} }
return 0; return 0;

View file

@ -1,7 +1,8 @@
#include "script.h" #include "script.h"
#include <QFile> #include <QFile>
MIRCScript::MIRCScript() { MIRCScript::MIRCScript(MIRCScriptManager *m) {
manager = m;
} }
bool MIRCScript::load(QString filename) { bool MIRCScript::load(QString filename) {
@ -16,7 +17,7 @@ bool MIRCScript::load(QString filename) {
} }
bool MIRCScript::parse(QString code) { bool MIRCScript::parse(QString code) {
mirc_script_engine *interpreter = new mirc_script_engine(); mirc_script_engine *interpreter = new mirc_script_engine(manager);
mirc_script parser(interpreter); mirc_script parser(interpreter);
parse_info<> info = boost::spirit::parse((const char*)code.toLatin1(), parser); parse_info<> info = boost::spirit::parse((const char*)code.toLatin1(), parser);
loaded = info.full; loaded = info.full;
@ -29,7 +30,7 @@ bool MIRCScript::parse(QString code) {
bool MIRCScript::run() { bool MIRCScript::run() {
if (!loaded) return false; if (!loaded) return false;
mirc_script_engine *interpreter = new mirc_script_engine(); mirc_script_engine *interpreter = new mirc_script_engine(manager);
interpreter->stage = EXECUTE; interpreter->stage = EXECUTE;
mirc_script parser(interpreter); mirc_script parser(interpreter);
parse_info<> info = boost::spirit::parse((const char*)script.toLatin1(), parser); parse_info<> info = boost::spirit::parse((const char*)script.toLatin1(), parser);